Passed
Pull Request — feature/eco-3623-payone-pay-u-... (#41)
by Roman
30:51
created

mapAddressTransferToConsumerTransfer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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