Completed
Pull Request — master (#32)
by Artem
09:58 queued 05:49
created

CheckoutPaymentPluginExecutor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 4
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\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 SprykerEco\Shared\Braintree\BraintreeConfig;
15
use SprykerEco\Zed\Braintree\Business\Payment\Transaction\Handler\PreCheckTransactionHandlerInterface;
16
17
class CheckoutPaymentPluginExecutor implements CheckoutPaymentPluginExecutorInterface
18
{
19
    protected const HTTP_ERROR_SERVER = 500;
20
21
    /**
22
     * @var \SprykerEco\Zed\Braintree\Business\Payment\Transaction\Handler\PreCheckTransactionHandlerInterface
23
     */
24
    protected $preCheckTransactionHandler;
25
26
    /**
27
     * @param \SprykerEco\Zed\Braintree\Business\Payment\Transaction\Handler\PreCheckTransactionHandlerInterface $preCheckTransactionHandler
28
     */
29
    public function __construct(
30
        PreCheckTransactionHandlerInterface $preCheckTransactionHandler
31
    ) {
32
        $this->preCheckTransactionHandler = $preCheckTransactionHandler;
33
    }
34
35
    /**
36
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
37
     * @param \Generated\Shared\Transfer\CheckoutResponseTransfer $checkoutResponseTransfer
38
     *
39
     * @return bool
40
     */
41
    public function executePreCheckPlugin(
42
        QuoteTransfer $quoteTransfer,
43
        CheckoutResponseTransfer $checkoutResponseTransfer
44
    ): bool {
45
        $quoteTransfer->requirePayment();
46
        $paymentTransfer = $quoteTransfer->getPayment();
47
        if ($paymentTransfer->getPaymentProvider() !== BraintreeConfig::PROVIDER_NAME) {
48
            return true;
49
        }
50
51
        $braintreeTransactionResponseTransfer = $this->preCheckTransactionHandler->preCheck($quoteTransfer);
52
        $isPassed = $this->checkForErrors($braintreeTransactionResponseTransfer, $checkoutResponseTransfer);
53
54
        if (!$braintreeTransactionResponseTransfer->getIsSuccess()) {
55
            return false;
56
        }
57
58
        $paymentTransfer->getBraintree()
59
            ->setTransactionId($braintreeTransactionResponseTransfer->getTransactionId());
60
61
        $paymentTransfer->setBraintreeTransactionResponse($braintreeTransactionResponseTransfer);
62
        $quoteTransfer->setPayment($paymentTransfer);
63
64
        return $isPassed;
65
    }
66
67
    /**
68
     * @param \Generated\Shared\Transfer\BraintreeTransactionResponseTransfer $braintreeTransactionResponseTransfer
69
     * @param \Generated\Shared\Transfer\CheckoutResponseTransfer $checkoutResponseTransfer
70
     *
71
     * @return bool
72
     */
73
    protected function checkForErrors(
74
        BraintreeTransactionResponseTransfer $braintreeTransactionResponseTransfer,
75
        CheckoutResponseTransfer $checkoutResponseTransfer
76
    ): bool {
77
        if ($braintreeTransactionResponseTransfer->getIsSuccess()) {
78
            return true;
79
        }
80
81
        $errorCode = $braintreeTransactionResponseTransfer->getCode() ?: static::HTTP_ERROR_SERVER;
82
        $checkoutErrorTransfer = new CheckoutErrorTransfer();
83
84
        $checkoutErrorTransfer->setErrorCode($errorCode)
85
            ->setMessage($braintreeTransactionResponseTransfer->getMessage());
86
        $checkoutResponseTransfer->addError($checkoutErrorTransfer);
87
88
        return false;
89
    }
90
}
91