Completed
Push — feature/paypal-express ( f680dd...2b3442 )
by Volodymyr
04:40 queued 04:37
created

CaptureItemsTransaction::addOrderIdToAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
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\ShipmentConstants;
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
     * @var \SprykerEco\Zed\Braintree\Dependency\Facade\BraintreeToMoneyFacadeInterface
27
     */
28
    protected $moneyFacade;
29
30
    /**
31
     * @var \SprykerEco\Zed\Braintree\Persistence\BraintreeRepositoryInterface
32
     */
33
    protected $braintreeRepository;
34
35
    /**
36
     * @var \SprykerEco\Zed\Braintree\Persistence\BraintreeEntityManagerInterface
37
     */
38
    protected $braintreeEntityManager;
39
40
    /**
41
     * @var \SprykerEco\Zed\Braintree\Dependency\Facade\BraintreeToSalesFacadeInterface
42
     */
43
    protected $salesFacade;
44
45
    /**
46
     * @var \SprykerEco\Zed\Braintree\Business\Payment\Transaction\Handler\ShipmentTransactionHandlerInterface
47
     */
48
    protected $shipmentTransactionHandler;
49
50
    /**
51
     * @param \SprykerEco\Zed\Braintree\BraintreeConfig $config
52
     * @param \SprykerEco\Zed\Braintree\Dependency\Facade\BraintreeToMoneyFacadeInterface $moneyFacade
53
     * @param \SprykerEco\Zed\Braintree\Persistence\BraintreeRepositoryInterface $braintreeRepository
54
     * @param \SprykerEco\Zed\Braintree\Persistence\BraintreeEntityManagerInterface $braintreeEntityManager
55
     * @param \SprykerEco\Zed\Braintree\Dependency\Facade\BraintreeToSalesFacadeInterface $salesFacade
56
     * @param \SprykerEco\Zed\Braintree\Business\Payment\Transaction\Handler\ShipmentTransactionHandlerInterface $shipmentTransactionHandler
57
     */
58
    public function __construct(
59
        BraintreeConfig $config,
60
        BraintreeToMoneyFacadeInterface $moneyFacade,
61
        BraintreeRepositoryInterface $braintreeRepository,
62
        BraintreeEntityManagerInterface $braintreeEntityManager,
63
        BraintreeToSalesFacadeInterface $salesFacade,
64
        ShipmentTransactionHandlerInterface $shipmentTransactionHandler
65
    ) {
66
        parent::__construct($config);
67
        $this->moneyFacade = $moneyFacade;
68
        $this->braintreeRepository = $braintreeRepository;
69
        $this->braintreeEntityManager = $braintreeEntityManager;
70
        $this->salesFacade = $salesFacade;
71
        $this->shipmentTransactionHandler = $shipmentTransactionHandler;
72
    }
73
74
    /**
75
     * @return string
76
     */
77
    protected function getTransactionType(): string
78
    {
79
        return ApiConstants::SALE;
80
    }
81
82
    /**
83
     * @return string
84
     */
85
    protected function getTransactionCode(): string
86
    {
87
        return ApiConstants::TRANSACTION_CODE_CAPTURE;
88
    }
89
90
    /**
91
     * @return \Braintree\Result\Error|\Braintree\Result\Successful
92
     */
93
    protected function doTransaction()
94
    {
95
        return $this->capture();
96
    }
97
98
    /**
99
     * @param \Braintree\Result\Successful|\Braintree\Result\Error $response
100
     *
101
     * @return \Generated\Shared\Transfer\BraintreeTransactionResponseTransfer
102
     */
103
    protected function afterTransaction($response): BraintreeTransactionResponseTransfer
104
    {
105
        if ($this->isTransactionSuccessful($response)) {
106
            $braintreeTransactionResponseTransfer = $this->getSuccessResponseTransfer($response);
107
            $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

107
            $this->logApiResponse($braintreeTransactionResponseTransfer, /** @scrutinizer ignore-type */ $this->getIdPayment(), $response->transaction->statusHistory);
Loading history...
108
109
            $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

109
            $this->braintreeEntityManager->updateIsShipmentPaidValue(/** @scrutinizer ignore-type */ $this->getIdPayment(), true);
Loading history...
110
            $this->braintreeEntityManager->addOrderItemsToTransactionLog(
111
                $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

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