Passed
Push — feature/paypal-express ( aa383f...32ec15 )
by Volodymyr
05:03
created

CaptureItemsTransaction   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 167
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 47
dl 0
loc 167
rs 10
c 0
b 0
f 0
wmc 15

9 Methods

Rating   Name   Duplication   Size   Complexity  
A capture() 0 10 1
A __construct() 0 14 1
A getTransactionType() 0 3 1
A doTransaction() 0 3 1
A getDecimalAmountValueFromInt() 0 3 1
A captureShipmentAmount() 0 15 3
A getShipmentExpenses() 0 9 3
A afterTransaction() 0 23 3
A getTransactionCode() 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 Spryker\Shared\Shipment\ShipmentConstants;
12
use SprykerEco\Zed\Braintree\BraintreeConfig;
13
use SprykerEco\Zed\Braintree\Business\Payment\Method\ApiConstants;
14
use SprykerEco\Zed\Braintree\Business\Payment\Transaction\Handler\ShipmentTransactionHandlerInterface;
15
use SprykerEco\Zed\Braintree\Dependency\Facade\BraintreeToMoneyFacadeInterface;
16
use SprykerEco\Zed\Braintree\Dependency\Facade\BraintreeToSalesFacadeInterface;
17
use SprykerEco\Zed\Braintree\Persistence\BraintreeEntityManagerInterface;
18
use SprykerEco\Zed\Braintree\Persistence\BraintreeRepositoryInterface;
19
20
class CaptureItemsTransaction extends AbstractTransaction
21
{
22
    /**
23
     * @var \SprykerEco\Zed\Braintree\Dependency\Facade\BraintreeToMoneyFacadeInterface
24
     */
25
    protected $moneyFacade;
26
27
    /**
28
     * @var \SprykerEco\Zed\Braintree\Persistence\BraintreeRepositoryInterface
29
     */
30
    protected $braintreeRepository;
31
32
    /**
33
     * @var \SprykerEco\Zed\Braintree\Persistence\BraintreeEntityManagerInterface
34
     */
35
    protected $braintreeEntityManager;
36
37
    /**
38
     * @var \SprykerEco\Zed\Braintree\Dependency\Facade\BraintreeToSalesFacadeInterface
39
     */
40
    protected $salesFacade;
41
42
    /**
43
     * @var \SprykerEco\Zed\Braintree\Business\Payment\Transaction\Handler\ShipmentTransactionHandlerInterface
44
     */
45
    protected $shipmentTransactionHandler;
46
47
    /**
48
     * @param \SprykerEco\Zed\Braintree\BraintreeConfig $config
49
     * @param \SprykerEco\Zed\Braintree\Dependency\Facade\BraintreeToMoneyFacadeInterface $moneyFacade
50
     * @param \SprykerEco\Zed\Braintree\Persistence\BraintreeRepositoryInterface $braintreeRepository
51
     * @param \SprykerEco\Zed\Braintree\Persistence\BraintreeEntityManagerInterface $braintreeEntityManager
52
     * @param \SprykerEco\Zed\Braintree\Dependency\Facade\BraintreeToSalesFacadeInterface $salesFacade
53
     * @param \SprykerEco\Zed\Braintree\Business\Payment\Transaction\Handler\ShipmentTransactionHandlerInterface $shipmentTransactionHandler
54
     */
55
    public function __construct(
56
        BraintreeConfig $config,
57
        BraintreeToMoneyFacadeInterface $moneyFacade,
58
        BraintreeRepositoryInterface $braintreeRepository,
59
        BraintreeEntityManagerInterface $braintreeEntityManager,
60
        BraintreeToSalesFacadeInterface $salesFacade,
61
        ShipmentTransactionHandlerInterface $shipmentTransactionHandler
62
    ) {
63
        parent::__construct($config);
64
        $this->moneyFacade = $moneyFacade;
65
        $this->braintreeRepository = $braintreeRepository;
66
        $this->braintreeEntityManager = $braintreeEntityManager;
67
        $this->salesFacade = $salesFacade;
68
        $this->shipmentTransactionHandler = $shipmentTransactionHandler;
69
    }
70
71
    /**
72
     * @return string
73
     */
74
    protected function getTransactionType()
75
    {
76
        return ApiConstants::SALE;
77
    }
78
79
    /**
80
     * @return string
81
     */
82
    protected function getTransactionCode()
83
    {
84
        return ApiConstants::TRANSACTION_CODE_CAPTURE;
85
    }
86
87
    /**
88
     * @return \Braintree\Result\Error|\Braintree\Result\Successful
89
     */
90
    protected function doTransaction()
91
    {
92
        return $this->capture();
93
    }
94
95
    /**
96
     * @param \Braintree\Result\Successful|\Braintree\Result\Error $response
97
     *
98
     * @return \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...
99
     */
100
    protected function afterTransaction($response)
101
    {
102
        if ($this->isTransactionSuccessful($response)) {
103
            $braintreeTransactionResponseTransfer = $this->getSuccessResponseTransfer($response);
104
            $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

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

106
            $this->braintreeEntityManager->updateIsShipmentPaidValue(/** @scrutinizer ignore-type */ $this->getIdPayment(), true);
Loading history...
107
108
            if (count($this->transactionMetaTransfer->getIdItems()) === 1) {
109
                $this->braintreeEntityManager->addOrderItemToSuccessLog(
110
                    $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...OrderItemToSuccessLog(). ( Ignorable by Annotation )

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

110
                    /** @scrutinizer ignore-type */ $this->getIdPayment(),
Loading history...
111
                    $this->transactionMetaTransfer->getIdItems()[0],
112
                    $braintreeTransactionResponseTransfer->getTransactionId()
113
                );
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->transactionMetaTransfer->getCaptureAmount();
133
        $amount = $this->getDecimalAmountValueFromInt($amount);
134
135
        return BraintreeTransaction::submitForPartialSettlement(
136
            $this->getTransactionIdentifier(),
137
            $amount
138
        );
139
    }
140
141
    /**
142
     * @param int $amount
143
     *
144
     * @return float
145
     */
146
    protected function getDecimalAmountValueFromInt(int $amount): float
147
    {
148
        return $this->moneyFacade->convertIntegerToDecimal($amount);
149
    }
150
151
    /**
152
     * @param \Generated\Shared\Transfer\TransactionMetaTransfer $transactionMetaTransfer
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\TransactionMetaTransfer 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...
153
     *
154
     * @return void
155
     */
156
    protected function captureShipmentAmount(): void
157
    {
158
        $orderTransfer = $this->salesFacade->getOrderByIdSalesOrder($this->transactionMetaTransfer->getIdSalesOrder());
159
        $braintreePayment = $this->braintreeRepository->findPaymentBraintreeBySalesOrderId($orderTransfer->getIdSalesOrder());
160
161
        if (!$braintreePayment || $braintreePayment->getIsShipmentPaid()) {
162
            return;
163
        }
164
165
        $amount = $this->getShipmentExpenses($orderTransfer->getExpenses());
166
167
        $shipmentTransactionMetaTransfer = clone $this->transactionMetaTransfer;
168
        $shipmentTransactionMetaTransfer->setCaptureAmount($this->getDecimalAmountValueFromInt($amount));
169
170
        $this->shipmentTransactionHandler->captureShipment($shipmentTransactionMetaTransfer);
171
    }
172
173
    /**
174
     * @param \ArrayObject|\Generated\Shared\Transfer\ExpenseTransfer[] $expenseTransfers
175
     *
176
     * @return int
177
     */
178
    protected function getShipmentExpenses($expenseTransfers): int
179
    {
180
        foreach ($expenseTransfers as $expenseTransfer) {
181
            if ($expenseTransfer->getType() === ShipmentConstants::SHIPMENT_EXPENSE_TYPE) {
182
                return $expenseTransfer->getUnitPriceToPayAggregation();
183
            }
184
        }
185
186
        return 0;
187
    }
188
}
189