PreCheckTransaction   A
last analyzed

Complexity

Total Complexity 31

Size/Duplication

Total Lines 319
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 84
c 1
b 1
f 0
dl 0
loc 319
rs 9.92
wmc 31

26 Methods

Rating   Name   Duplication   Size   Complexity  
A getShippingAddress() 0 3 1
A preCheck() 0 3 1
A getBraintreePayment() 0 3 1
A getRequestOptions() 0 7 1
A getBillingAddress() 0 3 1
A getAmount() 0 5 1
A beforeTransaction() 0 2 1
A getPayment() 0 3 1
A getCustomerAddressData() 0 12 1
A getRequestData() 0 10 1
A __construct() 0 5 1
A getPaymentSelection() 0 3 1
A getTransactionType() 0 3 1
A afterTransaction() 0 18 2
A getCustomer() 0 3 1
A doTransaction() 0 5 1
A getCustomerData() 0 11 1
A getTransactionCode() 0 3 1
A getQuote() 0 3 1
A isTransactionSuccessful() 0 3 2
A getNonce() 0 3 1
A updatePaymentForErrorResponse() 0 3 1
A isValidPaymentType() 0 11 1
A setPaypalPaymentMethod() 0 11 2
A setCreditCardMethod() 0 7 1
A updatePaymentForSuccessfulResponse() 0 9 3
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\PaymentInstrumentType;
11
use Braintree\Transaction as BraintreeTransaction;
12
use Generated\Shared\Transfer\AddressTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\AddressTransfer 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\PaymentTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\PaymentTransfer 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 SprykerEco\Shared\Braintree\BraintreeConfig as SharedBraintreeConfig;
15
use SprykerEco\Zed\Braintree\BraintreeConfig;
16
use SprykerEco\Zed\Braintree\Business\Payment\Method\ApiConstants;
17
use SprykerEco\Zed\Braintree\Dependency\Facade\BraintreeToMoneyFacadeInterface;
18
19
class PreCheckTransaction extends AbstractTransaction
20
{
21
    /**
22
     * @var \SprykerEco\Zed\Braintree\Dependency\Facade\BraintreeToMoneyFacadeInterface
23
     */
24
    protected $moneyFacade;
25
26
    /**
27
     * @param \SprykerEco\Zed\Braintree\BraintreeConfig $brainTreeConfig
28
     * @param \SprykerEco\Zed\Braintree\Dependency\Facade\BraintreeToMoneyFacadeInterface $moneyFacade
29
     */
30
    public function __construct(BraintreeConfig $brainTreeConfig, BraintreeToMoneyFacadeInterface $moneyFacade)
31
    {
32
        parent::__construct($brainTreeConfig);
33
34
        $this->moneyFacade = $moneyFacade;
35
    }
36
37
    /**
38
     * @return string
39
     */
40
    protected function getTransactionType()
41
    {
42
        return ApiConstants::SALE;
43
    }
44
45
    /**
46
     * PreCheck has no transaction code defined by braintree, added for logging purposes.
47
     *
48
     * @return string
49
     */
50
    protected function getTransactionCode()
51
    {
52
        return ApiConstants::STATUS_CODE_PRE_CHECK;
53
    }
54
55
    /**
56
     * @return \Braintree\Result\Error|\Braintree\Result\Successful|\Braintree\Transaction
57
     */
58
    protected function doTransaction()
59
    {
60
        $requestData = $this->getRequestData();
61
62
        return $this->preCheck($requestData);
63
    }
64
65
    /**
66
     * @param array $requestData
67
     *
68
     * @return \Braintree\Result\Error|\Braintree\Result\Successful|\Braintree\Transaction
69
     */
70
    protected function preCheck(array $requestData)
71
    {
72
        return BraintreeTransaction::sale($requestData);
73
    }
74
75
    /**
76
     * @return array
77
     */
78
    protected function getRequestData()
79
    {
80
        return [
81
            'amount' => $this->getAmount(),
82
            'paymentMethodNonce' => $this->getNonce(),
83
            'options' => $this->getRequestOptions(),
84
            'customer' => $this->getCustomerData(),
85
            'billing' => $this->getCustomerAddressData($this->getBillingAddress()),
86
            'shipping' => $this->getCustomerAddressData($this->getShippingAddress()),
87
            'channel' => $this->config->getChannel(),
88
        ];
89
    }
90
91
    /**
92
     * @return array
93
     */
94
    protected function getRequestOptions()
95
    {
96
        return [
97
            'threeDSecure' => [
98
                'required' => $this->config->getIs3DSecure(),
99
            ],
100
            'storeInVault' => $this->config->getIsVaulted(),
101
        ];
102
    }
103
104
    /**
105
     * @return array
106
     */
107
    protected function getCustomerData()
108
    {
109
        $customerTransfer = $this->getCustomer();
110
        $billingAddressTransfer = $this->getBillingAddress();
111
112
        return [
113
            'firstName' => $customerTransfer->getFirstName(),
114
            'lastName' => $customerTransfer->getLastName(),
115
            'email' => $customerTransfer->getEmail(),
116
            'company' => $billingAddressTransfer->getCompany(),
117
            'phone' => $billingAddressTransfer->getPhone(),
118
        ];
119
    }
120
121
    /**
122
     * @param \Generated\Shared\Transfer\AddressTransfer $addressTransfer
123
     *
124
     * @return array
125
     */
126
    protected function getCustomerAddressData(AddressTransfer $addressTransfer)
127
    {
128
        return [
129
            'firstName' => $addressTransfer->getFirstName(),
130
            'lastName' => $addressTransfer->getLastName(),
131
            'company' => $addressTransfer->getCompany(),
132
            'streetAddress' => trim(sprintf('%s %s', $addressTransfer->getAddress1(), $addressTransfer->getAddress2())),
133
            'extendedAddress' => $addressTransfer->getAddress3(),
134
            'locality' => $addressTransfer->getCity(),
135
            'region' => $addressTransfer->getRegion(),
136
            'postalCode' => $addressTransfer->getZipCode(),
137
            'countryCodeAlpha2' => $addressTransfer->getIso2Code(),
138
        ];
139
    }
140
141
    /**
142
     * @return float
143
     */
144
    protected function getAmount()
145
    {
146
        $grandTotal = $this->getQuote()->requireTotals()->getTotals()->getGrandTotal();
147
148
        return $this->moneyFacade->convertIntegerToDecimal($grandTotal);
149
    }
150
151
    /**
152
     * @return string
153
     */
154
    protected function getNonce()
155
    {
156
        return $this->getBraintreePayment()->requireNonce()->getNonce();
157
    }
158
159
    /**
160
     * @return \Generated\Shared\Transfer\BraintreePaymentTransfer
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\BraintreePaymentTransfer 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...
161
     */
162
    protected function getBraintreePayment()
163
    {
164
        return $this->getPayment()->requireBraintree()->getBraintree();
165
    }
166
167
    /**
168
     * @return string
169
     */
170
    protected function getPaymentSelection()
171
    {
172
        return $this->getPayment()->requirePaymentSelection()->getPaymentSelection();
173
    }
174
175
    /**
176
     * Customer is not required for guest checkout, so no `requireCustomer()`
177
     *
178
     * @return \Generated\Shared\Transfer\CustomerTransfer
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\CustomerTransfer 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...
179
     */
180
    protected function getCustomer()
181
    {
182
        return $this->getQuote()->getCustomer();
183
    }
184
185
    /**
186
     * @return \Generated\Shared\Transfer\PaymentTransfer
187
     */
188
    protected function getPayment()
189
    {
190
        return $this->getQuote()->requirePayment()->getPayment();
191
    }
192
193
    /**
194
     * @return \Generated\Shared\Transfer\QuoteTransfer
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\QuoteTransfer 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...
195
     */
196
    protected function getQuote()
197
    {
198
        return $this->transactionMetaTransfer->requireQuote()->getQuote();
199
    }
200
201
    /**
202
     * @return \Generated\Shared\Transfer\AddressTransfer
203
     */
204
    protected function getBillingAddress()
205
    {
206
        return $this->getQuote()->requireBillingAddress()->getBillingAddress();
207
    }
208
209
    /**
210
     * @return \Generated\Shared\Transfer\AddressTransfer
211
     */
212
    protected function getShippingAddress()
213
    {
214
        return $this->getQuote()->requireShippingAddress()->getShippingAddress();
215
    }
216
217
    /**
218
     * Prevent logging
219
     *
220
     * @return void
221
     */
222
    protected function beforeTransaction()
223
    {
224
    }
225
226
    /**
227
     * @param \Braintree\Result\Successful|\Braintree\Result\Error|\Braintree\Transaction $response
228
     *
229
     * @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...
230
     */
231
    protected function afterTransaction($response)
232
    {
233
        if ($this->isTransactionSuccessful($response)) {
234
            $this->updatePaymentForSuccessfulResponse($response);
0 ignored issues
show
Bug introduced by
It seems like $response can also be of type Braintree\Result\Error and Braintree\Transaction; however, parameter $response of SprykerEco\Zed\Braintree...ForSuccessfulResponse() does only seem to accept Braintree\Result\Successful, maybe add an additional type check? ( Ignorable by Annotation )

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

234
            $this->updatePaymentForSuccessfulResponse(/** @scrutinizer ignore-type */ $response);
Loading history...
235
            $transaction = $response->transaction;
236
            $braintreeTransactionResponseTransfer = $this->getSuccessResponseTransfer($response);
0 ignored issues
show
Bug introduced by
It seems like $response can also be of type Braintree\Transaction; however, parameter $response of SprykerEco\Zed\Braintree...ccessResponseTransfer() does only seem to accept Braintree\Result\Error|Braintree\Result\Successful, maybe add an additional type check? ( Ignorable by Annotation )

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

236
            $braintreeTransactionResponseTransfer = $this->getSuccessResponseTransfer(/** @scrutinizer ignore-type */ $response);
Loading history...
237
            $braintreeTransactionResponseTransfer->setCode($transaction->processorSettlementResponseCode);
238
            $braintreeTransactionResponseTransfer->setCreditCardType($transaction->creditCardDetails->cardType);
239
            $braintreeTransactionResponseTransfer->setPaymentType($transaction->paymentInstrumentType);
240
241
            return $braintreeTransactionResponseTransfer;
242
        }
243
244
        $this->updatePaymentForErrorResponse($response);
0 ignored issues
show
Bug introduced by
It seems like $response can also be of type Braintree\Result\Successful and Braintree\Transaction; however, parameter $response of SprykerEco\Zed\Braintree...ymentForErrorResponse() does only seem to accept Braintree\Result\Error, maybe add an additional type check? ( Ignorable by Annotation )

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

244
        $this->updatePaymentForErrorResponse(/** @scrutinizer ignore-type */ $response);
Loading history...
245
246
        $braintreeTransactionResponseTransfer = $this->getErrorResponseTransfer($response);
0 ignored issues
show
Bug introduced by
It seems like $response can also be of type Braintree\Transaction; however, parameter $response of SprykerEco\Zed\Braintree...ErrorResponseTransfer() does only seem to accept Braintree\Result\Error|Braintree\Result\Successful, maybe add an additional type check? ( Ignorable by Annotation )

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

246
        $braintreeTransactionResponseTransfer = $this->getErrorResponseTransfer(/** @scrutinizer ignore-type */ $response);
Loading history...
247
248
        return $braintreeTransactionResponseTransfer;
249
    }
250
251
    /**
252
     * @param \Braintree\Result\Successful|\Braintree\Result\Error|\Braintree\Transaction $response
253
     *
254
     * @return bool
255
     */
256
    protected function isTransactionSuccessful($response)
257
    {
258
        return ($response->success && $this->isValidPaymentType($response));
0 ignored issues
show
Bug introduced by
It seems like $response can also be of type Braintree\Result\Error and Braintree\Transaction; however, parameter $response of SprykerEco\Zed\Braintree...n::isValidPaymentType() does only seem to accept Braintree\Result\Successful, maybe add an additional type check? ( Ignorable by Annotation )

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

258
        return ($response->success && $this->isValidPaymentType(/** @scrutinizer ignore-type */ $response));
Loading history...
259
    }
260
261
    /**
262
     * @param \Braintree\Result\Successful $response
263
     *
264
     * @return void
265
     */
266
    protected function updatePaymentForSuccessfulResponse($response)
267
    {
268
        $braintreePaymentTransfer = $this->getBraintreePayment();
269
        $braintreePaymentTransfer->setPaymentType($response->transaction->paymentInstrumentType);
270
271
        if ($braintreePaymentTransfer->getPaymentType() === PaymentInstrumentType::PAYPAL_ACCOUNT) {
272
            $this->setPaypalPaymentMethod($this->getPayment());
273
        } elseif ($braintreePaymentTransfer->getPaymentType() === PaymentInstrumentType::CREDIT_CARD) {
274
            $this->setCreditCardMethod($this->getPayment());
275
        }
276
    }
277
278
    /**
279
     * When error occurs unset nonce, so this cannot be used anymore
280
     *
281
     * @param \Braintree\Result\Error $response
282
     *
283
     * @return void
284
     */
285
    protected function updatePaymentForErrorResponse($response)
0 ignored issues
show
Unused Code introduced by
The parameter $response is not used and could be removed. ( Ignorable by Annotation )

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

285
    protected function updatePaymentForErrorResponse(/** @scrutinizer ignore-unused */ $response)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
286
    {
287
        $this->getBraintreePayment()->setNonce('');
288
    }
289
290
    /**
291
     * @param \Braintree\Result\Successful $response
292
     *
293
     * @return bool
294
     */
295
    protected function isValidPaymentType($response)
296
    {
297
        $returnedType = $response->transaction->paymentInstrumentType;
298
299
        $matching = [
300
            SharedBraintreeConfig::PAYMENT_METHOD_PAY_PAL => PaymentInstrumentType::PAYPAL_ACCOUNT,
301
            SharedBraintreeConfig::PAYMENT_METHOD_CREDIT_CARD => PaymentInstrumentType::CREDIT_CARD,
302
            SharedBraintreeConfig::PAYMENT_METHOD_PAY_PAL_EXPRESS => PaymentInstrumentType::PAYPAL_ACCOUNT,
303
        ];
304
305
        return ($matching[$this->getPaymentSelection()] === $returnedType);
306
    }
307
308
    /**
309
     * @param \Generated\Shared\Transfer\PaymentTransfer $paymentTransfer
310
     *
311
     * @return \Generated\Shared\Transfer\PaymentTransfer
312
     */
313
    protected function setPaypalPaymentMethod(PaymentTransfer $paymentTransfer): PaymentTransfer
314
    {
315
        $paymentTransfer->setPaymentProvider(SharedBraintreeConfig::PROVIDER_NAME);
316
        $paymentTransfer->setPaymentMethod(PaymentTransfer::BRAINTREE_PAY_PAL);
317
318
        if ($this->transactionMetaTransfer->getQuote()->getPayment()->getPaymentSelection() === PaymentTransfer::BRAINTREE_PAY_PAL_EXPRESS) {
319
            $paymentTransfer->setPaymentMethod(PaymentTransfer::BRAINTREE_PAY_PAL_EXPRESS);
320
            $paymentTransfer->setPaymentSelection(PaymentTransfer::BRAINTREE_PAY_PAL_EXPRESS);
321
        }
322
323
        return $paymentTransfer;
324
    }
325
326
    /**
327
     * @param \Generated\Shared\Transfer\PaymentTransfer $paymentTransfer
328
     *
329
     * @return \Generated\Shared\Transfer\PaymentTransfer
330
     */
331
    protected function setCreditCardMethod(PaymentTransfer $paymentTransfer): PaymentTransfer
332
    {
333
        $paymentTransfer->setPaymentMethod(PaymentTransfer::BRAINTREE_CREDIT_CARD);
334
        $paymentTransfer->setPaymentProvider(SharedBraintreeConfig::PROVIDER_NAME);
335
        $paymentTransfer->setPaymentSelection(PaymentTransfer::BRAINTREE_CREDIT_CARD);
336
337
        return $paymentTransfer;
338
    }
339
}
340