Passed
Push — feature/paypal-express ( 8eac93...3cde97 )
by Volodymyr
05:19
created

RefundItemsTransaction   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 162
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 41
dl 0
loc 162
rs 10
c 0
b 0
f 0
wmc 19

12 Methods

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