Passed
Push — bugfix/ECO-3539-braintree ( 89f06b...c96e81 )
by Artem
11:41
created

checkCondition()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 13
nc 3
nop 2
dl 0
loc 24
rs 9.8333
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\Communication\Plugin\Checkout;
9
10
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...
11
use Generated\Shared\Transfer\CheckoutErrorTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\CheckoutErrorTransfer 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 Generated\Shared\Transfer\CheckoutResponseTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\CheckoutResponseTransfer 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\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...
14
use Spryker\Zed\CheckoutExtension\Dependency\Plugin\CheckoutPreConditionPluginInterface;
15
use Spryker\Zed\Kernel\Communication\AbstractPlugin;
16
use SprykerEco\Shared\Braintree\BraintreeConfig;
17
18
/**
19
 * @method \SprykerEco\Zed\Braintree\Business\BraintreeFacadeInterface getFacade()
20
 * @method \SprykerEco\Zed\Braintree\BraintreeConfig getConfig()
21
 * @method \SprykerEco\Zed\Braintree\Persistence\BraintreeQueryContainerInterface getQueryContainer()
22
 * @method \SprykerEco\Zed\Braintree\Communication\BraintreeCommunicationFactory getFactory()
23
 */
24
class BraintreePlaceOrderPreCheckPlugin extends AbstractPlugin implements CheckoutPreConditionPluginInterface
25
{
26
    /**
27
     * {@inheritDoc}
28
     * Specification:
29
     * - Checks a condition before the order is placed. If the condition fails, an error is added to the response transfer and 'false' is returned.
30
     * - Check could be passed (returns 'true') along with errors added to the checkout response.
31
     * - Don't use this plugin to write to a DB.
32
     *
33
     * @api
34
     *
35
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
36
     * @param \Generated\Shared\Transfer\CheckoutResponseTransfer $checkoutResponseTransfer
37
     *
38
     * @return bool
39
     */
40
    public function checkCondition(
41
        QuoteTransfer $quoteTransfer,
42
        CheckoutResponseTransfer $checkoutResponseTransfer
43
    ): bool {
44
        $quoteTransfer->requirePayment();
45
        $paymentTransfer = $quoteTransfer->getPayment();
46
        if ($paymentTransfer->getPaymentProvider() !== BraintreeConfig::PROVIDER_NAME) {
47
            return true;
48
        }
49
50
        $braintreeTransactionResponseTransfer = $this->getFacade()->preCheckPayment($quoteTransfer);
51
        $isPassed = $this->checkForErrors($braintreeTransactionResponseTransfer, $checkoutResponseTransfer);
52
53
        if (!$braintreeTransactionResponseTransfer->getIsSuccess()) {
54
            return false;
55
        }
56
57
        $paymentTransfer->getBraintree()
58
            ->setTransactionId($braintreeTransactionResponseTransfer->getTransactionId());
59
60
        $paymentTransfer->setBraintreeTransactionResponse($braintreeTransactionResponseTransfer);
61
        $quoteTransfer->setPayment($paymentTransfer);
62
63
        return $isPassed;
64
    }
65
66
    /**
67
     * @param \Generated\Shared\Transfer\BraintreeTransactionResponseTransfer $braintreeTransactionResponseTransfer
68
     * @param \Generated\Shared\Transfer\CheckoutResponseTransfer $checkoutResponseTransfer
69
     *
70
     * @return bool
71
     */
72
    protected function checkForErrors(
73
        BraintreeTransactionResponseTransfer $braintreeTransactionResponseTransfer,
74
        CheckoutResponseTransfer $checkoutResponseTransfer
75
    ): bool {
76
        if ($braintreeTransactionResponseTransfer->getIsSuccess()) {
77
            return true;
78
        }
79
80
        $errorCode = $braintreeTransactionResponseTransfer->getCode() ?: 500;
81
        $checkoutErrorTransfer = new CheckoutErrorTransfer();
82
83
        $checkoutErrorTransfer->setErrorCode($errorCode)
84
            ->setMessage($braintreeTransactionResponseTransfer->getMessage());
85
        $checkoutResponseTransfer->addError($checkoutErrorTransfer);
86
87
        return false;
88
    }
89
}
90