Completed
Push — master ( 25da63...b177ca )
by Oleksandr
16s queued 11s
created

PayolutionPreCheckPlugin::isResponseInvalid()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 3
nc 3
nop 1
1
<?php
2
3
/**
4
 * MIT License
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerEco\Zed\Payolution\Communication\Plugin\Checkout;
9
10
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...
11
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...
12
use Generated\Shared\Transfer\PayolutionTransactionResponseTransfer;
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...
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\Kernel\Communication\AbstractPlugin as BaseAbstractPlugin;
15
use Spryker\Zed\Payment\Dependency\Plugin\Checkout\CheckoutPreCheckPluginInterface;
16
use SprykerEco\Shared\Payolution\PayolutionConfig;
17
18
/**
19
 * @method \SprykerEco\Zed\Payolution\Business\PayolutionFacade getFacade()
20
 */
21
class PayolutionPreCheckPlugin extends BaseAbstractPlugin implements CheckoutPreCheckPluginInterface
22
{
23
    /**
24
     * @api
25
     *
26
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
27
     * @param \Generated\Shared\Transfer\CheckoutResponseTransfer $checkoutResponseTransfer
28
     *
29
     * @return void
30
     */
31
    public function execute(
32
        QuoteTransfer $quoteTransfer,
33
        CheckoutResponseTransfer $checkoutResponseTransfer
34
    ) {
35
        $payolutionTransactionResponseTransfer = $this->getFacade()->preCheckPayment($quoteTransfer);
36
        $this->checkForErrors($payolutionTransactionResponseTransfer, $checkoutResponseTransfer);
37
        $quoteTransfer->getPayment()->getPayolution()
38
            ->setPreCheckId($payolutionTransactionResponseTransfer->getIdentificationUniqueid());
39
    }
40
41
    /**
42
     * @param \Generated\Shared\Transfer\PayolutionTransactionResponseTransfer $payolutionTransactionResponseTransfer
43
     * @param \Generated\Shared\Transfer\CheckoutResponseTransfer $checkoutResponseTransfer
44
     *
45
     * @return void
46
     */
47
    protected function checkForErrors(
48
        PayolutionTransactionResponseTransfer $payolutionTransactionResponseTransfer,
49
        CheckoutResponseTransfer $checkoutResponseTransfer
50
    ) {
51
        if ($this->isResponseInvalid($payolutionTransactionResponseTransfer)) {
52
            $errorCode = (int)preg_replace('/[^\d]+/', '', $payolutionTransactionResponseTransfer->getProcessingCode());
53
            $error = new CheckoutErrorTransfer();
54
            $error
55
                ->setErrorCode($errorCode)
56
                ->setMessage($payolutionTransactionResponseTransfer->getProcessingReturn());
57
            $checkoutResponseTransfer->addError($error);
58
        }
59
    }
60
61
    /**
62
     * @param \Generated\Shared\Transfer\PayolutionTransactionResponseTransfer $payolutionTransactionResponseTransfer
63
     *
64
     * @return bool
65
     */
66
    protected function isResponseInvalid(PayolutionTransactionResponseTransfer $payolutionTransactionResponseTransfer): bool
67
    {
68
        return $payolutionTransactionResponseTransfer->getProcessingReasonCode() !== PayolutionConfig::REASON_CODE_SUCCESS
69
            || $payolutionTransactionResponseTransfer->getProcessingStatusCode() !== PayolutionConfig::STATUS_CODE_SUCCESS
70
            || $payolutionTransactionResponseTransfer->getPaymentCode() !== PayolutionConfig::PAYMENT_CODE_PRE_CHECK;
71
    }
72
}
73