Passed
Push — feature/eco-3308_psd2_module_u... ( 6dfa85...b14128 )
by Dmitri
10:54
created

PaymentTransaction::getAmount()   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
nc 1
nop 0
dl 0
loc 5
rs 10
c 1
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\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 PaymentTransaction 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
        return BraintreeTransaction::sale($this->getRequestData());
61
    }
62
63
    /**
64
     * @return array
65
     */
66
    protected function getRequestData()
67
    {
68
        return [
69
            'amount' => $this->getAmount(),
70
            'paymentMethodNonce' => $this->getNonce(),
71
            'options' => $this->getRequestOptions(),
72
            'customer' => $this->getCustomerData(),
73
            'billing' => $this->getCustomerAddressData($this->getBillingAddress()),
74
            'shipping' => $this->getCustomerAddressData($this->getShippingAddress()),
75
            'channel' => $this->config->getChannel(),
76
        ];
77
    }
78
79
    /**
80
     * @return array
81
     */
82
    protected function getRequestOptions()
83
    {
84
        return [
85
            'threeDSecure' => [
86
                'required' => $this->config->getIs3DSecure(),
87
            ],
88
            'storeInVault' => $this->config->getIsVaulted(),
89
        ];
90
    }
91
92
    /**
93
     * @return array
94
     */
95
    protected function getCustomerData()
96
    {
97
        $customerTransfer = $this->getCustomer();
98
        $billingAddressTransfer = $this->getBillingAddress();
99
100
        return [
101
            'firstName' => $customerTransfer->getFirstName(),
102
            'lastName' => $customerTransfer->getLastName(),
103
            'email' => $customerTransfer->getEmail(),
104
            'company' => $billingAddressTransfer->getCompany(),
105
            'phone' => $billingAddressTransfer->getPhone(),
106
        ];
107
    }
108
109
    /**
110
     * @param \Generated\Shared\Transfer\AddressTransfer $addressTransfer
111
     *
112
     * @return array
113
     */
114
    protected function getCustomerAddressData(AddressTransfer $addressTransfer)
115
    {
116
        return [
117
            'firstName' => $addressTransfer->getFirstName(),
118
            'lastName' => $addressTransfer->getLastName(),
119
            'company' => $addressTransfer->getCompany(),
120
            'streetAddress' => trim(sprintf('%s %s', $addressTransfer->getAddress1(), $addressTransfer->getAddress2())),
121
            'extendedAddress' => $addressTransfer->getAddress3(),
122
            'locality' => $addressTransfer->getCity(),
123
            'region' => $addressTransfer->getRegion(),
124
            'postalCode' => $addressTransfer->getZipCode(),
125
            'countryCodeAlpha2' => $addressTransfer->getIso2Code(),
126
        ];
127
    }
128
129
    /**
130
     * @return float
131
     */
132
    protected function getAmount()
133
    {
134
        $grandTotal = $this->getQuote()->requireTotals()->getTotals()->getGrandTotal();
135
136
        return $this->moneyFacade->convertIntegerToDecimal($grandTotal);
137
    }
138
139
    /**
140
     * @return string
141
     */
142
    protected function getNonce()
143
    {
144
        return $this->getBraintreePayment()->requireNonce()->getNonce();
145
    }
146
147
    /**
148
     * @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...
149
     */
150
    protected function getBraintreePayment()
151
    {
152
        return $this->getPayment()->requireBraintree()->getBraintree();
153
    }
154
155
    /**
156
     * @return string
157
     */
158
    protected function getPaymentSelection()
159
    {
160
        return $this->getPayment()->requirePaymentSelection()->getPaymentSelection();
161
    }
162
163
    /**
164
     * Customer is not required for guest checkout, so no `requireCustomer()`
165
     *
166
     * @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...
167
     */
168
    protected function getCustomer()
169
    {
170
        return $this->getQuote()->getCustomer();
171
    }
172
173
    /**
174
     * @return \Generated\Shared\Transfer\PaymentTransfer
175
     */
176
    protected function getPayment()
177
    {
178
        return $this->getQuote()->requirePayment()->getPayment();
179
    }
180
181
    /**
182
     * @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...
183
     */
184
    protected function getQuote()
185
    {
186
        return $this->transactionMetaTransfer->requireQuote()->getQuote();
187
    }
188
189
    /**
190
     * @return \Generated\Shared\Transfer\AddressTransfer
191
     */
192
    protected function getBillingAddress()
193
    {
194
        return $this->getQuote()->requireBillingAddress()->getBillingAddress();
195
    }
196
197
    /**
198
     * @return \Generated\Shared\Transfer\AddressTransfer
199
     */
200
    protected function getShippingAddress()
201
    {
202
        return $this->getQuote()->requireShippingAddress()->getShippingAddress();
203
    }
204
205
    /**
206
     * Prevent logging
207
     *
208
     * @return void
209
     */
210
    protected function beforeTransaction()
211
    {
212
    }
213
214
    /**
215
     * @param \Braintree\Result\Successful|\Braintree\Result\Error|\Braintree\Transaction $response
216
     *
217
     * @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...
218
     */
219
    protected function afterTransaction($response)
220
    {
221
        if ($this->isTransactionSuccessful($response)) {
222
            $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

222
            $this->updatePaymentForSuccessfulResponse(/** @scrutinizer ignore-type */ $response);
Loading history...
223
            $transaction = $response->transaction;
224
            $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

224
            $braintreeTransactionResponseTransfer = $this->getSuccessResponseTransfer(/** @scrutinizer ignore-type */ $response);
Loading history...
225
            $braintreeTransactionResponseTransfer->setCode($transaction->processorSettlementResponseCode);
226
            $braintreeTransactionResponseTransfer->setCreditCardType($transaction->creditCardDetails->cardType);
227
            $braintreeTransactionResponseTransfer->setPaymentType($transaction->paymentInstrumentType);
228
229
            return $braintreeTransactionResponseTransfer;
230
        }
231
232
        $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

232
        $this->updatePaymentForErrorResponse(/** @scrutinizer ignore-type */ $response);
Loading history...
233
234
        $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

234
        $braintreeTransactionResponseTransfer = $this->getErrorResponseTransfer(/** @scrutinizer ignore-type */ $response);
Loading history...
235
236
        return $braintreeTransactionResponseTransfer;
237
    }
238
239
    /**
240
     * @param \Braintree\Result\Successful|\Braintree\Result\Error|\Braintree\Transaction $response
241
     *
242
     * @return bool
243
     */
244
    protected function isTransactionSuccessful($response)
245
    {
246
        return $response->success;
247
    }
248
249
    /**
250
     * @param \Braintree\Result\Successful $response
251
     *
252
     * @return void
253
     */
254
    protected function updatePaymentForSuccessfulResponse($response)
255
    {
256
        $braintreePaymentTransfer = $this->getBraintreePayment();
257
        $braintreePaymentTransfer->setPaymentType($response->transaction->paymentInstrumentType);
258
259
        if ($braintreePaymentTransfer->getPaymentType() === PaymentInstrumentType::PAYPAL_ACCOUNT) {
260
            $this->setPaypalPaymentMethod($this->getPayment());
261
        } elseif ($braintreePaymentTransfer->getPaymentType() === PaymentInstrumentType::CREDIT_CARD) {
262
            $this->setCreditCardMethod($this->getPayment());
263
        }
264
    }
265
266
    /**
267
     * When error occurs unset nonce, so this cannot be used anymore
268
     *
269
     * @param \Braintree\Result\Error $response
270
     *
271
     * @return void
272
     */
273
    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

273
    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...
274
    {
275
        $this->getBraintreePayment()->setNonce('');
276
    }
277
278
    /**
279
     * @param \Braintree\Result\Successful $response
280
     *
281
     * @return bool
282
     */
283
    protected function isValidPaymentType($response)
284
    {
285
        $returnedType = $response->transaction->paymentInstrumentType;
286
287
        $matching = [
288
            SharedBraintreeConfig::PAYMENT_METHOD_PAY_PAL => PaymentInstrumentType::PAYPAL_ACCOUNT,
289
            SharedBraintreeConfig::PAYMENT_METHOD_CREDIT_CARD => PaymentInstrumentType::CREDIT_CARD,
290
            SharedBraintreeConfig::PAYMENT_METHOD_PAY_PAL_EXPRESS => PaymentInstrumentType::PAYPAL_ACCOUNT,
291
        ];
292
293
        return ($matching[$this->getPaymentSelection()] === $returnedType);
294
    }
295
296
    /**
297
     * @param \Generated\Shared\Transfer\PaymentTransfer $paymentTransfer
298
     *
299
     * @return \Generated\Shared\Transfer\PaymentTransfer
300
     */
301
    protected function setPaypalPaymentMethod(PaymentTransfer $paymentTransfer): PaymentTransfer
302
    {
303
        $paymentTransfer->setPaymentProvider(SharedBraintreeConfig::PROVIDER_NAME);
304
        $paymentTransfer->setPaymentMethod(PaymentTransfer::BRAINTREE_PAY_PAL);
305
306
        if ($this->transactionMetaTransfer->getQuote()->getPayment()->getPaymentSelection() === PaymentTransfer::BRAINTREE_PAY_PAL_EXPRESS) {
307
            $paymentTransfer->setPaymentMethod(PaymentTransfer::BRAINTREE_PAY_PAL_EXPRESS);
308
            $paymentTransfer->setPaymentSelection(PaymentTransfer::BRAINTREE_PAY_PAL_EXPRESS);
309
        }
310
311
        return $paymentTransfer;
312
    }
313
314
    /**
315
     * @param \Generated\Shared\Transfer\PaymentTransfer $paymentTransfer
316
     *
317
     * @return \Generated\Shared\Transfer\PaymentTransfer
318
     */
319
    protected function setCreditCardMethod(PaymentTransfer $paymentTransfer): PaymentTransfer
320
    {
321
        $paymentTransfer->setPaymentMethod(PaymentTransfer::BRAINTREE_CREDIT_CARD);
322
        $paymentTransfer->setPaymentProvider(SharedBraintreeConfig::PROVIDER_NAME);
323
        $paymentTransfer->setPaymentSelection(PaymentTransfer::BRAINTREE_CREDIT_CARD);
324
325
        return $paymentTransfer;
326
    }
327
}
328