CreditCardMapper::createComputopPaymentTransfer()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
c 0
b 0
f 0
dl 0
loc 20
rs 9.9
cc 1
nc 1
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\Yves\Computop\Mapper\Init\PostPlace;
9
10
use Generated\Shared\Transfer\ComputopAddressLineTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...utopAddressLineTransfer 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\ComputopAddressTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\ComputopAddressTransfer 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\ComputopConsumerTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\ComputopConsumerTransfer 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\ComputopCountryTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\ComputopCountryTransfer 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\ComputopCredentialOnFileTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...redentialOnFileTransfer 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 Generated\Shared\Transfer\ComputopCredentialOnFileTypeTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...ntialOnFileTypeTransfer 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...
16
use Generated\Shared\Transfer\ComputopCreditCardPaymentTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...editCardPaymentTransfer 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...
17
use Generated\Shared\Transfer\ComputopCustomerInfoTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...topCustomerInfoTransfer 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...
18
use Generated\Shared\Transfer\CountryCollectionTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...untryCollectionTransfer 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...
19
use Generated\Shared\Transfer\CountryTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\CountryTransfer 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...
20
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...
21
use Spryker\Yves\Router\Router\Router;
22
use SprykerEco\Shared\Computop\ComputopConfig as ComputopSharedConfig;
23
use SprykerEco\Shared\Computop\Config\ComputopApiConfig;
24
use SprykerEco\Yves\Computop\Mapper\Init\AbstractMapper;
25
use SprykerEco\Yves\Computop\Plugin\Router\ComputopRouteProviderPlugin;
26
27
class CreditCardMapper extends AbstractMapper
28
{
29
    /**
30
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
31
     *
32
     * @return \Generated\Shared\Transfer\ComputopCreditCardPaymentTransfer
33
     */
34
    public function createComputopPaymentTransfer(QuoteTransfer $quoteTransfer)
35
    {
36
        /** @var \Generated\Shared\Transfer\ComputopCreditCardPaymentTransfer $computopPaymentTransfer */
37
        $computopPaymentTransfer = parent::createComputopPaymentTransfer($quoteTransfer);
38
        $computopPaymentTransfer->setMac(
39
            $this->computopApiService->generateEncryptedMac(
40
                $this->createRequestTransfer($computopPaymentTransfer)
41
            )
42
        );
43
44
        $decryptedValues = $this->computopApiService->getEncryptedArray(
45
            $this->getDataSubArray($computopPaymentTransfer),
46
            $this->config->getBlowfishPassword()
47
        );
48
49
        $computopPaymentTransfer->setData($decryptedValues[ComputopApiConfig::DATA]);
50
        $computopPaymentTransfer->setLen($decryptedValues[ComputopApiConfig::LENGTH]);
51
        $computopPaymentTransfer->setUrl($this->config->getCreditCardInitActionUrl());
52
53
        return $computopPaymentTransfer;
54
    }
55
56
    /**
57
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
58
     *
59
     * @return \Generated\Shared\Transfer\ComputopCreditCardPaymentTransfer
60
     */
61
    protected function createTransferWithUnencryptedValues(QuoteTransfer $quoteTransfer)
62
    {
63
        $computopPaymentTransfer = new ComputopCreditCardPaymentTransfer();
64
65
        $computopPaymentTransfer->setCapture(
66
            $this->getCaptureType(ComputopSharedConfig::PAYMENT_METHOD_CREDIT_CARD)
67
        );
68
        $computopPaymentTransfer->setTransId($this->generateTransId($quoteTransfer));
69
        $computopPaymentTransfer->setTxType($this->config->getCreditCardTxType());
70
        $computopPaymentTransfer->setUrlSuccess(
71
            $this->router->generate(ComputopRouteProviderPlugin::CREDIT_CARD_SUCCESS, [], Router::ABSOLUTE_URL)
72
        );
73
        $computopPaymentTransfer->setUrlBack(
74
            $this->router->generate($this->config->getCallbackFailureRedirectPath(), [], Router::ABSOLUTE_URL)
75
        );
76
        $computopPaymentTransfer->setOrderDesc(
77
            $this->computopApiService->getDescriptionValue($quoteTransfer->getItems()->getArrayCopy())
78
        );
79
80
        $computopPaymentTransfer = $this->expandCreditCardPaymentWithShipToCustomer($computopPaymentTransfer, $quoteTransfer);
81
        $computopPaymentTransfer = $this->expandCreditCardPaymentWithBillToCustomer($computopPaymentTransfer, $quoteTransfer);
82
        $computopPaymentTransfer = $this->expandCreditCardPaymentWithShippingAddress($computopPaymentTransfer, $quoteTransfer);
83
        $computopPaymentTransfer = $this->expandCreditCardPaymentWithBillingAddress($computopPaymentTransfer, $quoteTransfer);
84
        $computopPaymentTransfer = $this->expandCreditCardPaymentWithCredentialOnFile($computopPaymentTransfer);
85
86
        return $computopPaymentTransfer;
87
    }
88
89
    /**
90
     * @param \Generated\Shared\Transfer\ComputopCreditCardPaymentTransfer $computopCreditCardPaymentTransfer
91
     *
92
     * @return array
93
     */
94
    protected function getDataSubArray(ComputopCreditCardPaymentTransfer $computopCreditCardPaymentTransfer)
95
    {
96
        $dataSubArray[ComputopApiConfig::TRANS_ID] = $computopCreditCardPaymentTransfer->getTransId();
0 ignored issues
show
Comprehensibility Best Practice introduced by
$dataSubArray was never initialized. Although not strictly required by PHP, it is generally a good practice to add $dataSubArray = array(); before regardless.
Loading history...
97
        $dataSubArray[ComputopApiConfig::AMOUNT] = $computopCreditCardPaymentTransfer->getAmount();
98
        $dataSubArray[ComputopApiConfig::CURRENCY] = $computopCreditCardPaymentTransfer->getCurrency();
99
        $dataSubArray[ComputopApiConfig::URL_SUCCESS] = $computopCreditCardPaymentTransfer->getUrlSuccess();
100
        $dataSubArray[ComputopApiConfig::URL_NOTIFY] = $computopCreditCardPaymentTransfer->getUrlNotify();
101
        $dataSubArray[ComputopApiConfig::URL_FAILURE] = $computopCreditCardPaymentTransfer->getUrlFailure();
102
        $dataSubArray[ComputopApiConfig::CAPTURE] = $computopCreditCardPaymentTransfer->getCapture();
103
        $dataSubArray[ComputopApiConfig::RESPONSE] = $computopCreditCardPaymentTransfer->getResponse();
104
        $dataSubArray[ComputopApiConfig::MAC] = $computopCreditCardPaymentTransfer->getMac();
105
        $dataSubArray[ComputopApiConfig::TX_TYPE] = $computopCreditCardPaymentTransfer->getTxType();
106
        $dataSubArray[ComputopApiConfig::ORDER_DESC] = $computopCreditCardPaymentTransfer->getOrderDesc();
107
        $dataSubArray[ComputopApiConfig::ETI_ID] = $this->config->getEtiId();
108
        $dataSubArray[ComputopApiConfig::IP_ADDRESS] = $computopCreditCardPaymentTransfer->getClientIp();
109
        $dataSubArray[ComputopApiConfig::SHIPPING_ZIP] = $computopCreditCardPaymentTransfer->getShippingZip();
110
111
        $dataSubArray[ComputopApiConfig::MSG_VER] = ComputopApiConfig::PSD2_MSG_VERSION;
112
        $dataSubArray[ComputopApiConfig::BILL_TO_CUSTOMER] = $this->encodeRequestParameterData(
113
            $computopCreditCardPaymentTransfer->getBillToCustomer()->toArray(true, true)
114
        );
115
        $dataSubArray[ComputopApiConfig::SHIP_TO_CUSTOMER] = $this->encodeRequestParameterData(
116
            $computopCreditCardPaymentTransfer->getShipToCustomer()->toArray(true, true)
117
        );
118
        $dataSubArray[ComputopApiConfig::BILLING_ADDRESS] = $this->encodeRequestParameterData(
119
            $computopCreditCardPaymentTransfer->getBillingAddress()->toArray(true, true)
120
        );
121
        $dataSubArray[ComputopApiConfig::SHIPPING_ADDRESS] = $this->encodeRequestParameterData(
122
            $computopCreditCardPaymentTransfer->getShippingAddress()->toArray(true, true)
123
        );
124
        $dataSubArray[ComputopApiConfig::CREDENTIAL_ON_FILE] = $this->encodeRequestParameterData(
125
            $computopCreditCardPaymentTransfer->getCredentialOnFile()->toArray(true, true)
126
        );
127
128
        return $dataSubArray;
129
    }
130
131
    /**
132
     * @param string $merchantId
133
     * @param string $data
134
     * @param int $length
135
     *
136
     * @return array
137
     */
138
    protected function getQueryParameters($merchantId, $data, $length)
139
    {
140
        $queryData = parent::getQueryParameters($merchantId, $data, $length);
141
142
        if ($this->config->getCreditCardTemplateEnabled()) {
143
            $queryData[ComputopApiConfig::TEMPLATE] = $merchantId;
144
        }
145
146
        return $queryData;
147
    }
148
149
    /**
150
     * @param \Generated\Shared\Transfer\ComputopCreditCardPaymentTransfer $computopCreditCardPaymentTransfer
151
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
152
     *
153
     * @return \Generated\Shared\Transfer\ComputopCreditCardPaymentTransfer
154
     */
155
    protected function expandCreditCardPaymentWithShipToCustomer(
156
        ComputopCreditCardPaymentTransfer $computopCreditCardPaymentTransfer,
157
        QuoteTransfer $quoteTransfer
158
    ): ComputopCreditCardPaymentTransfer {
159
        $computopCustomerInfoTransfer = (new ComputopCustomerInfoTransfer())
160
            ->setConsumer(
161
                (new ComputopConsumerTransfer())
162
                    ->setFirstName($quoteTransfer->getShippingAddress()->getFirstName())
163
                    ->setLastName($quoteTransfer->getShippingAddress()->getLastName())
164
                    ->setSalutation($quoteTransfer->getShippingAddress()->getSalutation())
165
            )
166
            ->setEmail($quoteTransfer->getCustomer()->getEmail());
167
168
        $computopCreditCardPaymentTransfer->setShipToCustomer($computopCustomerInfoTransfer);
169
170
        return $computopCreditCardPaymentTransfer;
171
    }
172
173
    /**
174
     * @param \Generated\Shared\Transfer\ComputopCreditCardPaymentTransfer $computopCreditCardPaymentTransfer
175
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
176
     *
177
     * @return \Generated\Shared\Transfer\ComputopCreditCardPaymentTransfer
178
     */
179
    protected function expandCreditCardPaymentWithBillToCustomer(
180
        ComputopCreditCardPaymentTransfer $computopCreditCardPaymentTransfer,
181
        QuoteTransfer $quoteTransfer
182
    ): ComputopCreditCardPaymentTransfer {
183
        if ($quoteTransfer->getBillingSameAsShipping()) {
184
            $computopCreditCardPaymentTransfer->setBillToCustomer(
185
                $computopCreditCardPaymentTransfer->getShipToCustomer()
186
            );
187
188
            return $computopCreditCardPaymentTransfer;
189
        }
190
191
        $computopCustomerInfoTransfer = (new ComputopCustomerInfoTransfer())
192
            ->setConsumer(
193
                (new ComputopConsumerTransfer())
194
                    ->setFirstName($quoteTransfer->getBillingAddress()->getFirstName())
195
                    ->setLastName($quoteTransfer->getBillingAddress()->getLastName())
196
                    ->setSalutation($quoteTransfer->getBillingAddress()->getSalutation())
197
            )
198
            ->setEmail($quoteTransfer->getCustomer()->getEmail());
199
200
        $computopCreditCardPaymentTransfer->setBillToCustomer($computopCustomerInfoTransfer);
201
202
        return $computopCreditCardPaymentTransfer;
203
    }
204
205
    /**
206
     * @param \Generated\Shared\Transfer\ComputopCreditCardPaymentTransfer $computopCreditCardPaymentTransfer
207
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
208
     *
209
     * @return \Generated\Shared\Transfer\ComputopCreditCardPaymentTransfer
210
     */
211
    protected function expandCreditCardPaymentWithShippingAddress(
212
        ComputopCreditCardPaymentTransfer $computopCreditCardPaymentTransfer,
213
        QuoteTransfer $quoteTransfer
214
    ): ComputopCreditCardPaymentTransfer {
215
        $countryCollectionTransfer = (new CountryCollectionTransfer())
216
            ->addCountries(
217
                (new CountryTransfer())->setIso2Code($quoteTransfer->getShippingAddress()->getIso2Code())
218
            );
219
        $countryCollectionTransfer = $this->countryClient->findCountriesByIso2Codes($countryCollectionTransfer);
220
221
        $computopAddressTransfer = (new ComputopAddressTransfer())
222
            ->setCity($quoteTransfer->getShippingAddress()->getCity())
223
            ->setCountry(
224
                (new ComputopCountryTransfer())
225
                    ->setCountryA3($countryCollectionTransfer->getCountries()->offsetGet(0)->getIso3Code())
226
            )
227
            ->setAddressLine1(
228
                (new ComputopAddressLineTransfer())
229
                    ->setStreet($quoteTransfer->getShippingAddress()->getAddress1())
230
                    ->setStreetNumber($quoteTransfer->getShippingAddress()->getAddress2())
231
            )
232
            ->setPostalCode($quoteTransfer->getShippingAddress()->getZipCode());
233
234
        $computopCreditCardPaymentTransfer->setShippingAddress($computopAddressTransfer);
235
236
        return $computopCreditCardPaymentTransfer;
237
    }
238
239
    /**
240
     * @param \Generated\Shared\Transfer\ComputopCreditCardPaymentTransfer $computopCreditCardPaymentTransfer
241
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
242
     *
243
     * @return \Generated\Shared\Transfer\ComputopCreditCardPaymentTransfer
244
     */
245
    protected function expandCreditCardPaymentWithBillingAddress(
246
        ComputopCreditCardPaymentTransfer $computopCreditCardPaymentTransfer,
247
        QuoteTransfer $quoteTransfer
248
    ): ComputopCreditCardPaymentTransfer {
249
        if ($quoteTransfer->getBillingSameAsShipping()) {
250
            $computopCreditCardPaymentTransfer->setBillingAddress(
251
                $computopCreditCardPaymentTransfer->getShippingAddress()
252
            );
253
254
            return $computopCreditCardPaymentTransfer;
255
        }
256
257
        $countryCollectionTransfer = (new CountryCollectionTransfer())
258
            ->addCountries(
259
                (new CountryTransfer())->setIso2Code($quoteTransfer->getBillingAddress()->getIso2Code())
260
            );
261
        $countryCollectionTransfer = $this->countryClient->findCountriesByIso2Codes($countryCollectionTransfer);
262
263
        $computopAddressTransfer = (new ComputopAddressTransfer())
264
            ->setCity($quoteTransfer->getBillingAddress()->getCity())
265
            ->setCountry(
266
                (new ComputopCountryTransfer())
267
                    ->setCountryA3($countryCollectionTransfer->getCountries()->offsetGet(0)->getIso3Code())
268
            )
269
            ->setAddressLine1(
270
                (new ComputopAddressLineTransfer())
271
                    ->setStreet($quoteTransfer->getBillingAddress()->getAddress1())
272
                    ->setStreetNumber($quoteTransfer->getBillingAddress()->getAddress2())
273
            )
274
            ->setPostalCode($quoteTransfer->getBillingAddress()->getZipCode());
275
276
        $computopCreditCardPaymentTransfer->setBillingAddress($computopAddressTransfer);
277
278
        return $computopCreditCardPaymentTransfer;
279
    }
280
281
    /**
282
     * @param \Generated\Shared\Transfer\ComputopCreditCardPaymentTransfer $computopCreditCardPaymentTransfer
283
     *
284
     * @return \Generated\Shared\Transfer\ComputopCreditCardPaymentTransfer
285
     */
286
    protected function expandCreditCardPaymentWithCredentialOnFile(
287
        ComputopCreditCardPaymentTransfer $computopCreditCardPaymentTransfer
288
    ): ComputopCreditCardPaymentTransfer {
289
        $computopCredentialOnFileTransfer = (new ComputopCredentialOnFileTransfer())
290
            ->setType(
291
                (new ComputopCredentialOnFileTypeTransfer())
292
                    ->setUnscheduled(ComputopApiConfig::UNSCHEDULED_CUSTOMER_INITIATED_TRANSACTION)
293
            )
294
            ->setInitialPayment(true);
295
296
        $computopCreditCardPaymentTransfer->setCredentialOnFile($computopCredentialOnFileTransfer);
297
298
        return $computopCreditCardPaymentTransfer;
299
    }
300
}
301