FirstDataCreditCardParametersMapper::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 1
nc 1
nop 3
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\Glue\FirstData\Processor\Mapper;
9
10
use DateTime;
11
use Generated\Shared\Transfer\FirstDataCreditCardParametersTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...tCardParametersTransfer 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\FirstDataHashRequestTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...DataHashRequestTransfer 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\RestCheckoutDataResponseAttributesTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...ponseAttributesTransfer 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 Generated\Shared\Transfer\RestCheckoutDataTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\RestCheckoutDataTransfer 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 SprykerEco\Client\FirstData\FirstDataClientInterface;
16
use SprykerEco\Glue\FirstData\Dependency\Service\FirstDataToUtilTextServiceInterface;
17
use SprykerEco\Glue\FirstData\FirstDataConfig;
18
19
class FirstDataCreditCardParametersMapper implements FirstDataCreditCardParametersMapperInterface
20
{
21
    protected const DEFAULT_OID_LENGTH = 32;
22
23
    /**
24
     * @var \SprykerEco\Glue\FirstData\FirstDataConfig
25
     */
26
    protected $firstDataConfig;
27
28
    /**
29
     * @var \SprykerEco\Client\FirstData\FirstDataClientInterface
30
     */
31
    protected $firstDataClient;
32
33
    /**
34
     * @var \SprykerEco\Glue\FirstData\Dependency\Service\FirstDataToUtilTextServiceInterface
35
     */
36
    protected $utilTextService;
37
38
    /**
39
     * @param \SprykerEco\Glue\FirstData\FirstDataConfig $firstDataConfig
40
     * @param \SprykerEco\Client\FirstData\FirstDataClientInterface $firstDataClient
41
     * @param \SprykerEco\Glue\FirstData\Dependency\Service\FirstDataToUtilTextServiceInterface $utilTextService
42
     */
43
    public function __construct(
44
        FirstDataConfig $firstDataConfig,
45
        FirstDataClientInterface $firstDataClient,
46
        FirstDataToUtilTextServiceInterface $utilTextService
47
    ) {
48
        $this->firstDataConfig = $firstDataConfig;
49
        $this->firstDataClient = $firstDataClient;
50
        $this->utilTextService = $utilTextService;
51
    }
52
53
    /**
54
     * @param \Generated\Shared\Transfer\RestCheckoutDataResponseAttributesTransfer $restCheckoutDataResponseAttributesTransfer
55
     * @param \Generated\Shared\Transfer\RestCheckoutDataTransfer $restCheckoutDataTransfer
56
     *
57
     * @return \Generated\Shared\Transfer\RestCheckoutDataResponseAttributesTransfer
58
     */
59
    public function mapFirstDataCreditCardParametersTransferToRestCheckoutDataResponseAttributesTransfer(
60
        RestCheckoutDataResponseAttributesTransfer $restCheckoutDataResponseAttributesTransfer,
61
        RestCheckoutDataTransfer $restCheckoutDataTransfer
62
    ): RestCheckoutDataResponseAttributesTransfer {
63
        if (!$this->isCreditCardPaymentMethodSelected($restCheckoutDataResponseAttributesTransfer)) {
64
            return $restCheckoutDataResponseAttributesTransfer;
65
        }
66
67
        $restCheckoutDataResponseAttributesTransfer->setFirstDataPaymentProcessingLink(
68
            $this->firstDataConfig->getFirstDataPaymentProcessingLink()
69
        );
70
71
        return $restCheckoutDataResponseAttributesTransfer->setFirstDataCreditCardParameters(
72
            $this->createFirstDataCreditCardParametersTransfer(
73
                $this->createFirstDataHashRequestTransfer($restCheckoutDataTransfer)
74
            )
75
        );
76
    }
77
78
    /**
79
     * @param \Generated\Shared\Transfer\RestCheckoutDataResponseAttributesTransfer $restCheckoutDataResponseAttributesTransfer
80
     *
81
     * @return bool
82
     */
83
    protected function isCreditCardPaymentMethodSelected(
84
        RestCheckoutDataResponseAttributesTransfer $restCheckoutDataResponseAttributesTransfer
85
    ): bool {
86
        foreach ($restCheckoutDataResponseAttributesTransfer->getSelectedPaymentMethods() as $selectedPaymentMethod) {
87
            if ($selectedPaymentMethod->getPaymentMethodName() === $this->firstDataConfig->getMethodNameCreditCard()) {
88
                return true;
89
            }
90
        }
91
92
        return false;
93
    }
94
95
    /**
96
     * @param \Generated\Shared\Transfer\RestCheckoutDataTransfer $restCheckoutDataTransfer
97
     *
98
     * @return float
99
     */
100
    protected function calculateTotal(RestCheckoutDataTransfer $restCheckoutDataTransfer): float
101
    {
102
        $price = $restCheckoutDataTransfer->getQuoteOrFail()->getTotalsOrFail()->getPriceToPay() * (1 + (int)$this->firstDataConfig->getAdditionAuthPercentageBuffer() / 100);
103
104
        return (float)($price / 100);
105
    }
106
107
    /**
108
     * @param \Generated\Shared\Transfer\RestCheckoutDataTransfer $restCheckoutDataTransfer
109
     *
110
     * @return \Generated\Shared\Transfer\FirstDataHashRequestTransfer
111
     */
112
    protected function createFirstDataHashRequestTransfer(RestCheckoutDataTransfer $restCheckoutDataTransfer): FirstDataHashRequestTransfer
113
    {
114
        $firstDataHashRequestTransfer = new FirstDataHashRequestTransfer();
115
        $firstDataHashRequestTransfer->setMobileMode($this->firstDataConfig->getMobileMode());
116
        $firstDataHashRequestTransfer->setCheckoutOption($this->firstDataConfig->getCheckoutOption());
117
        $firstDataHashRequestTransfer->setLanguage($this->firstDataConfig->getLanguage());
118
        $firstDataHashRequestTransfer->setAssignToken($this->firstDataConfig->getAssignToken());
119
        $firstDataHashRequestTransfer->setTokenType($this->firstDataConfig->getTokenType());
120
        $firstDataHashRequestTransfer->setDeclineHostedDataDuplicates($this->firstDataConfig->getDeclineHostedDataDuplicates());
121
        $firstDataHashRequestTransfer->setTxnType($this->firstDataConfig->getTransactionType());
122
        $firstDataHashRequestTransfer->setTimezone($this->firstDataConfig->getTimezone());
123
        $firstDataHashRequestTransfer->setTxnDateTime((new DateTime())->format($this->firstDataConfig->getDateTimeFormat()));
124
        $firstDataHashRequestTransfer->setHashAlgorithm($this->firstDataConfig->getHmacHashAlgo());
125
        $firstDataHashRequestTransfer->setStoreName($this->firstDataConfig->getStoreName());
126
        $firstDataHashRequestTransfer->setAuthenticateTransaction($this->firstDataConfig->getIs3dSecure());
127
        $firstDataHashRequestTransfer->setOid($this->utilTextService->generateRandomString(static::DEFAULT_OID_LENGTH));
128
        $firstDataHashRequestTransfer->setChargeTotal((string)$this->calculateTotal($restCheckoutDataTransfer));
129
        $firstDataHashRequestTransfer->setCurrency(
130
            $this->firstDataConfig->getIsoNumberByCode($restCheckoutDataTransfer->getQuoteOrFail()->getCurrencyOrFail()->getCodeOrFail())
131
        );
132
        $firstDataHashRequestTransfer->setCustomerId($restCheckoutDataTransfer->getQuoteOrFail()->getCustomerReference());
133
        $firstDataHashRequestTransfer->setEmail($restCheckoutDataTransfer->getQuoteOrFail()->getCustomerOrFail()->getEmail());
134
        $firstDataHashRequestTransfer->setResponseSuccessURL($this->firstDataConfig->getResponseSuccessUrl());
135
        $firstDataHashRequestTransfer->setResponseFailURL($this->firstDataConfig->getResponseFailUrl());
136
        $firstDataHashRequestTransfer->setTransactionNotificationURL($this->firstDataConfig->getTransactionNotificationUrl());
137
138
        if ($restCheckoutDataTransfer->getQuoteOrFail()->getBillingAddress()) {
139
            $fullName = sprintf(
140
                '%s %s',
141
                $restCheckoutDataTransfer->getQuoteOrFail()->getBillingAddressOrFail()->getFirstName() ?: '',
142
                $restCheckoutDataTransfer->getQuoteOrFail()->getBillingAddressOrFail()->getLastName() ?: ''
143
            );
144
145
            $firstDataHashRequestTransfer->setPhone($restCheckoutDataTransfer->getQuoteOrFail()->getCustomerOrFail()->getPhone());
146
            $firstDataHashRequestTransfer->setBname($fullName);
147
            $firstDataHashRequestTransfer->setSname($fullName);
148
            $firstDataHashRequestTransfer->setBaddr1($restCheckoutDataTransfer->getQuoteOrFail()->getBillingAddressOrFail()->getAddress1());
149
            $firstDataHashRequestTransfer->setBaddr2($restCheckoutDataTransfer->getQuoteOrFail()->getBillingAddressOrFail()->getAddress2());
150
            $firstDataHashRequestTransfer->setBcity($restCheckoutDataTransfer->getQuoteOrFail()->getBillingAddressOrFail()->getCity());
151
            $firstDataHashRequestTransfer->setBzip($restCheckoutDataTransfer->getQuoteOrFail()->getBillingAddressOrFail()->getZipCode());
152
            $firstDataHashRequestTransfer->setBcountry($restCheckoutDataTransfer->getQuoteOrFail()->getBillingAddressOrFail()->getIso2Code());
153
        }
154
155
        if ($restCheckoutDataTransfer->getQuoteOrFail()->getShippingAddress()) {
156
            $firstDataHashRequestTransfer->setSaddr1($restCheckoutDataTransfer->getQuoteOrFail()->getShippingAddressOrFail()->getAddress1());
157
            $firstDataHashRequestTransfer->setSaddr2($restCheckoutDataTransfer->getQuoteOrFail()->getShippingAddressOrFail()->getAddress2());
158
            $firstDataHashRequestTransfer->setSzip($restCheckoutDataTransfer->getQuoteOrFail()->getShippingAddressOrFail()->getZipCode());
159
            $firstDataHashRequestTransfer->setScity($restCheckoutDataTransfer->getQuoteOrFail()->getShippingAddressOrFail()->getCity());
160
            $firstDataHashRequestTransfer->setScountry($restCheckoutDataTransfer->getQuoteOrFail()->getShippingAddressOrFail()->getIso2Code());
161
        }
162
163
        return $firstDataHashRequestTransfer;
164
    }
165
166
    /**
167
     * @param \Generated\Shared\Transfer\FirstDataHashRequestTransfer $firstDataHashRequestTransfer
168
     *
169
     * @return \Generated\Shared\Transfer\FirstDataCreditCardParametersTransfer
170
     */
171
    protected function createFirstDataCreditCardParametersTransfer(
172
        FirstDataHashRequestTransfer $firstDataHashRequestTransfer
173
    ): FirstDataCreditCardParametersTransfer {
174
        $firstDataCreditCardParametersTransfer = (new FirstDataCreditCardParametersTransfer())
175
            ->fromArray($firstDataHashRequestTransfer->toArray(), true);
176
177
        return $firstDataCreditCardParametersTransfer->setHashExtended(
178
            $this->firstDataClient->generateHash($firstDataHashRequestTransfer)
179
        );
180
    }
181
}
182