Completed
Pull Request — master (#32)
by Alexey
21:40 queued 10:02
created

BraintreePlaceOrderPreCheckPlugin::check()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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