Passed
Pull Request — master (#13)
by Volodymyr
13:04
created

CaptureItemsTransaction::getTransactionType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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

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

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

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