RefundItemsTransaction   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 36
c 1
b 0
f 0
dl 0
loc 149
rs 10
wmc 16

11 Methods

Rating   Name   Duplication   Size   Complexity  
A findPaymentBraintreeTransactionStatusLogTransfer() 0 4 1
A doTransaction() 0 3 1
A getAmount() 0 3 1
A afterTransaction() 0 9 2
A refundShipmentExpense() 0 10 2
A findShipmentExpenseTransfer() 0 9 3
A refund() 0 11 2
A getTransactionCode() 0 3 1
A findTransaction() 0 3 1
A __construct() 0 10 1
A getTransactionType() 0 3 1
1
<?php
2
3
/**
4
 * MIT License
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
namespace SprykerEco\Zed\Braintree\Business\Payment\Transaction;
9
10
use Braintree\Transaction as BraintreeTransaction;
11
use Braintree\Transaction;
12
use Generated\Shared\Transfer\BraintreeTransactionResponseTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...sactionResponseTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Generated\Shared\Transfer\ExpenseTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\ExpenseTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Generated\Shared\Transfer\PaymentBraintreeTransactionStatusLogTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...actionStatusLogTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Spryker\Shared\Shipment\ShipmentConfig;
16
use SprykerEco\Zed\Braintree\BraintreeConfig;
17
use SprykerEco\Zed\Braintree\Business\Payment\Method\ApiConstants;
18
use SprykerEco\Zed\Braintree\Business\Payment\Transaction\Handler\ShipmentRefundTransactionHandlerInterface;
19
use SprykerEco\Zed\Braintree\Dependency\Facade\BraintreeToMoneyFacadeInterface;
20
use SprykerEco\Zed\Braintree\Persistence\BraintreeRepositoryInterface;
21
22
class RefundItemsTransaction extends AbstractTransaction
23
{
24
    /**
25
     * @see \Spryker\Shared\Shipment\ShipmentConfig::SHIPMENT_EXPENSE_TYPE|\Spryker\Shared\Shipment\ShipmentConstants::SHIPMENT_EXPENSE_TYPE
26
     */
27
    protected const SHIPMENT_EXPENSE_TYPE = 'SHIPMENT_EXPENSE_TYPE';
28
29
    /**
30
     * @var \SprykerEco\Zed\Braintree\Dependency\Facade\BraintreeToMoneyFacadeInterface
31
     */
32
    protected $moneyFacade;
33
34
    /**
35
     * @var \SprykerEco\Zed\Braintree\Business\Payment\Transaction\Handler\ShipmentRefundTransactionHandlerInterface
36
     */
37
    protected $shipmentRefundTransactionHandler;
38
39
    /**
40
     * @var \SprykerEco\Zed\Braintree\Persistence\BraintreeRepositoryInterface
41
     */
42
    protected $braintreeRepository;
43
44
    /**
45
     * @param \SprykerEco\Zed\Braintree\BraintreeConfig $brainTreeConfig
46
     * @param \SprykerEco\Zed\Braintree\Dependency\Facade\BraintreeToMoneyFacadeInterface $moneyFacade
47
     * @param \SprykerEco\Zed\Braintree\Business\Payment\Transaction\Handler\ShipmentRefundTransactionHandlerInterface $shipmentRefundTransactionHandler
48
     * @param \SprykerEco\Zed\Braintree\Persistence\BraintreeRepositoryInterface $braintreeRepository
49
     */
50
    public function __construct(
51
        BraintreeConfig $brainTreeConfig,
52
        BraintreeToMoneyFacadeInterface $moneyFacade,
53
        ShipmentRefundTransactionHandlerInterface $shipmentRefundTransactionHandler,
54
        BraintreeRepositoryInterface $braintreeRepository
55
    ) {
56
        parent::__construct($brainTreeConfig);
57
        $this->moneyFacade = $moneyFacade;
58
        $this->shipmentRefundTransactionHandler = $shipmentRefundTransactionHandler;
59
        $this->braintreeRepository = $braintreeRepository;
60
    }
61
62
    /**
63
     * @return string
64
     */
65
    protected function getTransactionType(): string
66
    {
67
        return ApiConstants::CREDIT;
68
    }
69
70
    /**
71
     * @return string
72
     */
73
    protected function getTransactionCode(): string
74
    {
75
        return ApiConstants::TRANSACTION_CODE_REFUND;
76
    }
77
78
    /**
79
     * @return \Braintree\Result\Error|\Braintree\Result\Successful
80
     */
81
    public function doTransaction()
82
    {
83
        return $this->refund();
84
    }
85
86
    /**
87
     * @param \Braintree\Result\Error|\Braintree\Result\Successful $response
88
     *
89
     * @return \Generated\Shared\Transfer\BraintreeTransactionResponseTransfer
90
     */
91
    public function afterTransaction($response): BraintreeTransactionResponseTransfer
92
    {
93
        $shipmentExpenseTransfer = $this->findShipmentExpenseTransfer();
94
95
        if ($shipmentExpenseTransfer) {
96
            $this->refundShipmentExpense();
97
        }
98
99
        return parent::afterTransaction($response);
100
    }
101
102
    /**
103
     * @return \Braintree\Result\Error|\Braintree\Result\Successful
104
     */
105
    protected function refund()
106
    {
107
        $transaction = $this->findTransaction();
108
109
        if ($transaction->status === ApiConstants::STATUS_CODE_CAPTURE_SUBMITTED) {
110
            return BraintreeTransaction::void($this->getTransactionIdentifier());
111
        }
112
113
        return BraintreeTransaction::refund(
114
            $this->getTransactionIdentifier(),
115
            $this->getAmount()
116
        );
117
    }
118
119
    /**
120
     * @return float
121
     */
122
    protected function getAmount(): float
123
    {
124
        return $this->moneyFacade->convertIntegerToDecimal($this->transactionMetaTransfer->getRefundAmount());
125
    }
126
127
    /**
128
     * @return \Braintree\Transaction
129
     */
130
    protected function findTransaction(): Transaction
131
    {
132
        return BraintreeTransaction::find($this->getTransactionIdentifier());
133
    }
134
135
    /**
136
     * @return \Generated\Shared\Transfer\ExpenseTransfer|null
137
     */
138
    protected function findShipmentExpenseTransfer(): ?ExpenseTransfer
139
    {
140
        foreach ($this->transactionMetaTransfer->getRefund()->getExpenses() as $expenseTransfer) {
141
            if ($expenseTransfer->getType() === static::SHIPMENT_EXPENSE_TYPE) {
142
                return $expenseTransfer;
143
            }
144
        }
145
146
        return null;
147
    }
148
149
    /**
150
     * @return \Generated\Shared\Transfer\PaymentBraintreeTransactionStatusLogTransfer|null
151
     */
152
    protected function findPaymentBraintreeTransactionStatusLogTransfer(): ?PaymentBraintreeTransactionStatusLogTransfer
153
    {
154
        return $this->braintreeRepository
155
            ->findTransactionRequestLogByIdSalesOrderForShipment($this->transactionMetaTransfer->getIdSalesOrder());
156
    }
157
158
    /**
159
     * @return void
160
     */
161
    protected function refundShipmentExpense(): void
162
    {
163
        $paymentBraintreeTransactionStatusLogTransfer = $this->findPaymentBraintreeTransactionStatusLogTransfer();
164
165
        if ($paymentBraintreeTransactionStatusLogTransfer) {
166
            $shipmentRefundTransitionMetaTransfer = clone $this->transactionMetaTransfer;
167
            $shipmentRefundTransitionMetaTransfer->setShipmentRefundTransactionId($paymentBraintreeTransactionStatusLogTransfer->getTransactionId());
168
            $shipmentRefundTransitionMetaTransfer->setRefundAmount($paymentBraintreeTransactionStatusLogTransfer->getTransactionAmount());
169
170
            $this->shipmentRefundTransactionHandler->refundShipment($shipmentRefundTransitionMetaTransfer);
171
        }
172
    }
173
}
174