FirstDataResponseValidator   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 25
c 1
b 0
f 0
dl 0
loc 74
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A validateResponseHash() 0 18 4
A __construct() 0 6 1
A validateFirstDataResponse() 0 15 5
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\FirstData\Business\Checker;
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\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...
13
use SprykerEco\Client\FirstData\FirstDataClientInterface;
14
use SprykerEco\Zed\FirstData\FirstDataConfig;
15
16
class FirstDataResponseValidator implements FirstDataResponseValidatorInterface
17
{
18
    /**
19
     * @var \SprykerEco\Client\FirstData\FirstDataClientInterface
20
     */
21
    protected $firstDataClient;
22
23
    /**
24
     * @var \SprykerEco\Zed\FirstData\FirstDataConfig
25
     */
26
    protected $firstDataConfig;
27
28
    protected const GLOSSARY_KEY_FIRST_DATA_TRANSACTION_DATA_MISSING = 'first_data.transaction_data_is_missing';
29
    protected const GLOSSARY_KEY_FIRST_DATA_RESPONSE_HASH_INVALID = 'first_data.response_hash_invalid';
30
31
    /**
32
     * @param \SprykerEco\Client\FirstData\FirstDataClientInterface $firstDataClient
33
     * @param \SprykerEco\Zed\FirstData\FirstDataConfig $firstDataConfig
34
     */
35
    public function __construct(
36
        FirstDataClientInterface $firstDataClient,
37
        FirstDataConfig $firstDataConfig
38
    ) {
39
        $this->firstDataClient = $firstDataClient;
40
        $this->firstDataConfig = $firstDataConfig;
41
    }
42
43
    /**
44
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
45
     * @param \Generated\Shared\Transfer\CheckoutResponseTransfer $checkoutResponseTransfer
46
     *
47
     * @return bool
48
     */
49
    public function validateFirstDataResponse(QuoteTransfer $quoteTransfer, CheckoutResponseTransfer $checkoutResponseTransfer): bool
50
    {
51
        if ($quoteTransfer->getPayments()->count()) {
52
            foreach ($quoteTransfer->getPayments() as $selectedPaymentMethod) {
53
                if ($selectedPaymentMethod->getPaymentMethod() === $this->firstDataConfig::PAYMENT_METHOD_KEY_CREDIT_CARD) {
54
                    return $this->validateResponseHash($quoteTransfer, $checkoutResponseTransfer);
55
                }
56
            }
57
        }
58
59
        if ($quoteTransfer->getPaymentOrFail()->getPaymentMethod() === $this->firstDataConfig::PAYMENT_METHOD_KEY_CREDIT_CARD) {
60
            return $this->validateResponseHash($quoteTransfer, $checkoutResponseTransfer);
61
        }
62
63
        return true;
64
    }
65
66
    /**
67
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
68
     * @param \Generated\Shared\Transfer\CheckoutResponseTransfer $checkoutResponseTransfer
69
     *
70
     * @return bool
71
     */
72
    protected function validateResponseHash(QuoteTransfer $quoteTransfer, CheckoutResponseTransfer $checkoutResponseTransfer): bool
73
    {
74
        $firstDataCreditCard = $quoteTransfer->getPayment() ?
75
            $quoteTransfer->getPaymentOrFail()->getFirstDataCreditCard() :
76
            null;
77
        $firstDataTransactionData = $firstDataCreditCard ? $firstDataCreditCard->getFirstDataTransactionData() : null;
78
79
        if (!$firstDataTransactionData) {
80
            $checkoutErrorTransfer = (new CheckoutErrorTransfer())
81
                ->setMessage(static::GLOSSARY_KEY_FIRST_DATA_TRANSACTION_DATA_MISSING);
82
83
            $checkoutResponseTransfer->addError($checkoutErrorTransfer);
84
            $checkoutResponseTransfer->setIsSuccess(false);
85
86
            return false;
87
        }
88
89
        return true;
90
    }
91
}
92