CaptureItemsTransaction::afterTransaction()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 2
nop 1
dl 0
loc 20
rs 9.8666
c 0
b 0
f 0
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 Spryker\Shared\Shipment\ShipmentConfig;
13
use SprykerEco\Zed\Braintree\BraintreeConfig;
14
use SprykerEco\Zed\Braintree\Business\Payment\Method\ApiConstants;
15
use SprykerEco\Zed\Braintree\Business\Payment\Transaction\Handler\ShipmentTransactionHandlerInterface;
16
use SprykerEco\Zed\Braintree\Dependency\Facade\BraintreeToMoneyFacadeInterface;
17
use SprykerEco\Zed\Braintree\Dependency\Facade\BraintreeToSalesFacadeInterface;
18
use SprykerEco\Zed\Braintree\Persistence\BraintreeEntityManagerInterface;
19
use SprykerEco\Zed\Braintree\Persistence\BraintreeRepositoryInterface;
20
21
class CaptureItemsTransaction extends AbstractTransaction
22
{
23
    protected const ATTRIBUTE_KEY_ORDER_ID = 'orderId';
24
25
    /**
26
     * @see \Spryker\Shared\Shipment\ShipmentConfig::SHIPMENT_EXPENSE_TYPE|\Spryker\Shared\Shipment\ShipmentConstants::SHIPMENT_EXPENSE_TYPE
27
     */
28
    protected const SHIPMENT_EXPENSE_TYPE = 'SHIPMENT_EXPENSE_TYPE';
29
30
    /**
31
     * @var \SprykerEco\Zed\Braintree\Dependency\Facade\BraintreeToMoneyFacadeInterface
32
     */
33
    protected $moneyFacade;
34
35
    /**
36
     * @var \SprykerEco\Zed\Braintree\Persistence\BraintreeRepositoryInterface
37
     */
38
    protected $braintreeRepository;
39
40
    /**
41
     * @var \SprykerEco\Zed\Braintree\Persistence\BraintreeEntityManagerInterface
42
     */
43
    protected $braintreeEntityManager;
44
45
    /**
46
     * @var \SprykerEco\Zed\Braintree\Dependency\Facade\BraintreeToSalesFacadeInterface
47
     */
48
    protected $salesFacade;
49
50
    /**
51
     * @var \SprykerEco\Zed\Braintree\Business\Payment\Transaction\Handler\ShipmentTransactionHandlerInterface
52
     */
53
    protected $shipmentTransactionHandler;
54
55
    /**
56
     * @param \SprykerEco\Zed\Braintree\BraintreeConfig $config
57
     * @param \SprykerEco\Zed\Braintree\Dependency\Facade\BraintreeToMoneyFacadeInterface $moneyFacade
58
     * @param \SprykerEco\Zed\Braintree\Persistence\BraintreeRepositoryInterface $braintreeRepository
59
     * @param \SprykerEco\Zed\Braintree\Persistence\BraintreeEntityManagerInterface $braintreeEntityManager
60
     * @param \SprykerEco\Zed\Braintree\Dependency\Facade\BraintreeToSalesFacadeInterface $salesFacade
61
     * @param \SprykerEco\Zed\Braintree\Business\Payment\Transaction\Handler\ShipmentTransactionHandlerInterface $shipmentTransactionHandler
62
     */
63
    public function __construct(
64
        BraintreeConfig $config,
65
        BraintreeToMoneyFacadeInterface $moneyFacade,
66
        BraintreeRepositoryInterface $braintreeRepository,
67
        BraintreeEntityManagerInterface $braintreeEntityManager,
68
        BraintreeToSalesFacadeInterface $salesFacade,
69
        ShipmentTransactionHandlerInterface $shipmentTransactionHandler
70
    ) {
71
        parent::__construct($config);
72
        $this->moneyFacade = $moneyFacade;
73
        $this->braintreeRepository = $braintreeRepository;
74
        $this->braintreeEntityManager = $braintreeEntityManager;
75
        $this->salesFacade = $salesFacade;
76
        $this->shipmentTransactionHandler = $shipmentTransactionHandler;
77
    }
78
79
    /**
80
     * @return string
81
     */
82
    protected function getTransactionType(): string
83
    {
84
        return ApiConstants::SALE;
85
    }
86
87
    /**
88
     * @return string
89
     */
90
    protected function getTransactionCode(): string
91
    {
92
        return ApiConstants::TRANSACTION_CODE_CAPTURE;
93
    }
94
95
    /**
96
     * @return \Braintree\Result\Error|\Braintree\Result\Successful
97
     */
98
    protected function doTransaction()
99
    {
100
        return $this->capture();
101
    }
102
103
    /**
104
     * @param \Braintree\Result\Successful|\Braintree\Result\Error $response
105
     *
106
     * @return \Generated\Shared\Transfer\BraintreeTransactionResponseTransfer
107
     */
108
    protected function afterTransaction($response): BraintreeTransactionResponseTransfer
109
    {
110
        if ($this->isTransactionSuccessful($response)) {
111
            $braintreeTransactionResponseTransfer = $this->getSuccessResponseTransfer($response);
112
            $this->logApiResponse($braintreeTransactionResponseTransfer, $this->getIdPayment(), $response->transaction->statusHistory);
0 ignored issues
show
Bug introduced by
$this->getIdPayment() of type string is incompatible with the type integer expected by parameter $idPayment of SprykerEco\Zed\Braintree...ction::logApiResponse(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

112
            $this->logApiResponse($braintreeTransactionResponseTransfer, /** @scrutinizer ignore-type */ $this->getIdPayment(), $response->transaction->statusHistory);
Loading history...
113
114
            $this->braintreeEntityManager->updateIsShipmentPaidValue($this->getIdPayment(), true);
0 ignored issues
show
Bug introduced by
$this->getIdPayment() of type string is incompatible with the type integer expected by parameter $idPaymentBraintree of SprykerEco\Zed\Braintree...teIsShipmentPaidValue(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

114
            $this->braintreeEntityManager->updateIsShipmentPaidValue(/** @scrutinizer ignore-type */ $this->getIdPayment(), true);
Loading history...
115
            $this->braintreeEntityManager->addOrderItemsToTransactionLog(
116
                $this->getIdPayment(),
0 ignored issues
show
Bug introduced by
$this->getIdPayment() of type string is incompatible with the type integer expected by parameter $idPaymentBraintree of SprykerEco\Zed\Braintree...ItemsToTransactionLog(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

116
                /** @scrutinizer ignore-type */ $this->getIdPayment(),
Loading history...
117
                $this->transactionMetaTransfer->getItems(),
118
                $braintreeTransactionResponseTransfer->getTransactionId()
119
            );
120
121
            return $braintreeTransactionResponseTransfer;
122
        }
123
124
        $braintreeTransactionResponseTransfer = $this->getErrorResponseTransfer($response);
125
        $this->logApiResponse($braintreeTransactionResponseTransfer, $this->getIdPayment());
126
127
        return $braintreeTransactionResponseTransfer;
128
    }
129
130
    /**
131
     * @return \Braintree\Result\Error|\Braintree\Result\Successful
132
     */
133
    protected function capture()
134
    {
135
        $this->captureShipmentAmount();
136
137
        $amount = $this->getCaptureAmount($this->transactionMetaTransfer->getItems());
138
        $amount = $this->getDecimalAmountValueFromInt($amount);
139
140
        $attributes = [];
141
        $attributes = $this->addOrderIdToAttributes($attributes);
142
143
        return BraintreeTransaction::submitForPartialSettlement(
144
            $this->getTransactionIdentifier(),
145
            $amount,
146
            $attributes
147
        );
148
    }
149
150
    /**
151
     * @param \Generated\Shared\Transfer\ItemTransfer[] $itemTransfers
152
     *
153
     * @return int
154
     */
155
    protected function getCaptureAmount(iterable $itemTransfers): int
156
    {
157
        $amount = 0;
158
159
        foreach ($itemTransfers as $itemTransfer) {
160
            $amount += $itemTransfer->getPriceToPayAggregation();
161
        }
162
163
        return $amount;
164
    }
165
166
    /**
167
     * @param int $amount
168
     *
169
     * @return float
170
     */
171
    protected function getDecimalAmountValueFromInt(int $amount): float
172
    {
173
        return $this->moneyFacade->convertIntegerToDecimal($amount);
174
    }
175
176
    /**
177
     * @return void
178
     */
179
    protected function captureShipmentAmount(): void
180
    {
181
        $orderTransfer = $this->salesFacade->getOrderByIdSalesOrder($this->transactionMetaTransfer->getIdSalesOrder());
182
        $braintreePayment = $this->braintreeRepository->findPaymentBraintreeBySalesOrderId($orderTransfer->getIdSalesOrder());
183
        $amount = $this->getShipmentExpenses($orderTransfer->getExpenses());
184
185
        if (!$braintreePayment || $braintreePayment->getIsShipmentPaid() || $amount === 0) {
186
            return;
187
        }
188
189
        $shipmentTransactionMetaTransfer = clone $this->transactionMetaTransfer;
190
        $shipmentTransactionMetaTransfer->setCaptureShipmentAmount($this->getDecimalAmountValueFromInt($amount));
191
192
        $this->shipmentTransactionHandler->captureShipment($shipmentTransactionMetaTransfer);
193
    }
194
195
    /**
196
     * @param array $attributes
197
     *
198
     * @return array
199
     */
200
    protected function addOrderIdToAttributes(array $attributes): array
201
    {
202
        $attributes[static::ATTRIBUTE_KEY_ORDER_ID] = $this->transactionMetaTransfer->getOrderReference();
203
204
        return $attributes;
205
    }
206
207
    /**
208
     * @param \ArrayObject|\Generated\Shared\Transfer\ExpenseTransfer[] $expenseTransfers
209
     *
210
     * @return int
211
     */
212
    protected function getShipmentExpenses(iterable $expenseTransfers): int
213
    {
214
        foreach ($expenseTransfers as $expenseTransfer) {
215
            if ($expenseTransfer->getType() === static::SHIPMENT_EXPENSE_TYPE) {
216
                return $expenseTransfer->getUnitPriceToPayAggregation();
217
            }
218
        }
219
220
        return 0;
221
    }
222
}
223