Passed
Pull Request — master (#42)
by
unknown
07:32 queued 03:40
created

PayPalMapper::getOrderItemDescriptionKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
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 ArrayObject;
11
use Generated\Shared\Transfer\ComputopPayPalPaymentTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...opPayPalPaymentTransfer 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\ItemTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\ItemTransfer 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\Yves\Router\Router\Router;
15
use SprykerEco\Shared\Computop\ComputopConfig as ComputopSharedConfig;
16
use SprykerEco\Shared\Computop\Config\ComputopApiConfig;
17
use SprykerEco\Yves\Computop\Mapper\Init\AbstractMapper;
18
use SprykerEco\Yves\Computop\Plugin\Router\ComputopRouteProviderPlugin;
19
20
class PayPalMapper extends AbstractMapper
21
{
22
    /**
23
     * @var int
24
     */
25
    protected const PAYPAL_ITEM_DESCRIPTION_OFFSET = 2;
26
27
    /**
28
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
29
     *
30
     * @return \Generated\Shared\Transfer\ComputopPayPalPaymentTransfer
31
     */
32
    public function createComputopPaymentTransfer(QuoteTransfer $quoteTransfer): ComputopPayPalPaymentTransfer
33
    {
34
        /** @var \Generated\Shared\Transfer\ComputopPayPalPaymentTransfer $computopPaymentTransfer */
35
        $computopPaymentTransfer = parent::createComputopPaymentTransfer($quoteTransfer);
36
        $computopPaymentTransfer->setMac(
37
            $this->computopApiService->generateEncryptedMac(
38
                $this->createRequestTransfer($computopPaymentTransfer)
39
            )
40
        );
41
42
        $computopPaymentTransfer = $this->addOrderDescriptions(
43
            $computopPaymentTransfer,
44
            $quoteTransfer->getItems()
45
        );
46
47
        $computopPaymentTransfer->setTaxTotal($quoteTransfer->getTotals()->getTaxTotal()->getAmount());
48
        $computopPaymentTransfer->setShAmount($quoteTransfer->getTotals()->getShipmentTotal());
49
50
        $itemTotal = $this->calculateItemTotal($quoteTransfer);
51
        $computopPaymentTransfer->setItemTotal($itemTotal);
52
53
        $decryptedValues = $this->computopApiService->getEncryptedArray(
54
            $this->getDataSubArray($computopPaymentTransfer),
55
            $this->config->getBlowfishPassword()
56
        );
57
58
        $computopPaymentTransfer->setData($decryptedValues[ComputopApiConfig::DATA]);
59
        $computopPaymentTransfer->setLen($decryptedValues[ComputopApiConfig::LENGTH]);
60
        $computopPaymentTransfer->setUrl(
61
            $this->getActionUrl(
62
                $this->config->getPayPalInitActionUrl(),
63
                $this->getQueryParameters(
64
                    $computopPaymentTransfer->getMerchantId(),
65
                    $decryptedValues[ComputopApiConfig::DATA],
66
                    $decryptedValues[ComputopApiConfig::LENGTH]
67
                )
68
            )
69
        );
70
71
        return $computopPaymentTransfer;
72
    }
73
74
    /**
75
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
76
     *
77
     * @return int
78
     */
79
    protected function calculateItemTotal(QuoteTransfer $quoteTransfer): int
80
    {
81
        return $quoteTransfer->getTotals()->getGrandTotal() -
82
            $quoteTransfer->getTotals()->getShipmentTotal() -
83
            $quoteTransfer->getTotals()->getTaxTotal()->getAmount();
84
    }
85
86
    /**
87
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
88
     *
89
     * @return \Generated\Shared\Transfer\ComputopPayPalPaymentTransfer
90
     */
91
    protected function createTransferWithUnencryptedValues(QuoteTransfer $quoteTransfer): ComputopPayPalPaymentTransfer
92
    {
93
        $computopPaymentTransfer = new ComputopPayPalPaymentTransfer();
94
95
        $computopPaymentTransfer->setCapture(
96
            $this->getCaptureType(ComputopSharedConfig::PAYMENT_METHOD_PAY_PAL)
97
        );
98
        $computopPaymentTransfer->setTransId($this->generateTransId($quoteTransfer));
99
        $computopPaymentTransfer->setTxType($this->config->getPayPalTxType());
100
        $computopPaymentTransfer->setUrlSuccess(
101
            $this->router->generate(ComputopRouteProviderPlugin::PAY_PAL_SUCCESS, [], Router::ABSOLUTE_URL)
102
        );
103
        $computopPaymentTransfer->setOrderDesc(
104
            $this->computopApiService->getDescriptionValue($quoteTransfer->getItems()->getArrayCopy())
105
        );
106
107
        $this->mapAddressFromQuoteToComputopPayPalPayment($quoteTransfer, $computopPaymentTransfer);
108
109
        return $computopPaymentTransfer;
110
    }
111
112
    /**
113
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
114
     * @param \Generated\Shared\Transfer\ComputopPayPalPaymentTransfer $computopPayPalPaymentTransfer
115
     *
116
     * @return \Generated\Shared\Transfer\ComputopPayPalPaymentTransfer
117
     */
118
    protected function mapAddressFromQuoteToComputopPayPalPayment(
119
        QuoteTransfer $quoteTransfer,
120
        ComputopPayPalPaymentTransfer $computopPayPalPaymentTransfer
121
    ): ComputopPayPalPaymentTransfer {
122
        $addressTransfer = $quoteTransfer->getBillingSameAsShipping() ? $quoteTransfer->getBillingAddress() : $quoteTransfer->getShippingAddress();
123
124
        if (!$addressTransfer) {
125
            return $computopPayPalPaymentTransfer;
126
        }
127
128
        $computopPayPalPaymentTransfer
129
            ->setFirstName($addressTransfer->getFirstName())
130
            ->setLastName($addressTransfer->getLastName())
131
            ->setAddressStreet($addressTransfer->getAddress1())
132
            ->setAddressStreet2($addressTransfer->getAddress2())
133
            ->setAddressCity($addressTransfer->getCity())
134
            ->setAddressState($addressTransfer->getState())
135
            ->setAddressZip($addressTransfer->getZipCode())
136
            ->setAddressCountryCode($addressTransfer->getIso2Code())
137
            ->setPhone($addressTransfer->getPhone());
138
139
        return $computopPayPalPaymentTransfer;
140
    }
141
142
    /**
143
     * @param \Generated\Shared\Transfer\ComputopPayPalPaymentTransfer $computopPayPalPaymentTransfer
144
     *
145
     * @return array
146
     */
147
    protected function getDataSubArray(ComputopPayPalPaymentTransfer $computopPayPalPaymentTransfer): array
148
    {
149
        $dataSubArray[ComputopApiConfig::TRANS_ID] = $computopPayPalPaymentTransfer->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...
150
        $dataSubArray[ComputopApiConfig::AMOUNT] = $computopPayPalPaymentTransfer->getAmount();
151
        $dataSubArray[ComputopApiConfig::CURRENCY] = $computopPayPalPaymentTransfer->getCurrency();
152
        $dataSubArray[ComputopApiConfig::CAPTURE] = $computopPayPalPaymentTransfer->getCapture();
153
        $dataSubArray[ComputopApiConfig::TX_TYPE] = $computopPayPalPaymentTransfer->getTxType();
154
        $dataSubArray[ComputopApiConfig::ORDER_DESC] = $computopPayPalPaymentTransfer->getOrderDesc();
155
156
        $orderDescriptions = $computopPayPalPaymentTransfer->getOrderDescriptions();
157
        $dataSubArray = $this->addOrderItemDescriptions($dataSubArray, $orderDescriptions);
158
159
        $dataSubArray[ComputopApiConfig::TAX_TOTAL] = $computopPayPalPaymentTransfer->getTaxTotal();
160
        $dataSubArray[ComputopApiConfig::ITEM_TOTAL] = $computopPayPalPaymentTransfer->getItemTotal();
161
        $dataSubArray[ComputopApiConfig::SHIPPING_AMOUNT] = $computopPayPalPaymentTransfer->getShAmount();
162
163
        $dataSubArray[ComputopApiConfig::MAC] = $computopPayPalPaymentTransfer->getMac();
164
        $dataSubArray[ComputopApiConfig::URL_SUCCESS] = $computopPayPalPaymentTransfer->getUrlSuccess();
165
        $dataSubArray[ComputopApiConfig::URL_FAILURE] = $computopPayPalPaymentTransfer->getUrlFailure();
166
        $dataSubArray[ComputopApiConfig::RESPONSE] = $computopPayPalPaymentTransfer->getResponse();
167
        $dataSubArray[ComputopApiConfig::URL_NOTIFY] = $computopPayPalPaymentTransfer->getUrlNotify();
168
        $dataSubArray[ComputopApiConfig::REQ_ID] = $computopPayPalPaymentTransfer->getReqId();
169
170
        $dataSubArray[ComputopApiConfig::ETI_ID] = $this->config->getEtiId();
171
        $dataSubArray[ComputopApiConfig::IP_ADDRESS] = $computopPayPalPaymentTransfer->getClientIp();
172
        $dataSubArray[ComputopApiConfig::SHIPPING_ZIP] = $computopPayPalPaymentTransfer->getShippingZip();
173
174
        if (!$computopPayPalPaymentTransfer->getAddressStreet()) {
175
            $dataSubArray[ComputopApiConfig::NO_SHIPPING] = ComputopSharedConfig::PAY_PAL_NO_SHIPPING;
176
177
            return $dataSubArray;
178
        }
179
180
        $dataSubArray[ComputopApiConfig::FIRST_NAME] = $computopPayPalPaymentTransfer->getFirstName();
181
        $dataSubArray[ComputopApiConfig::LAST_NAME] = $computopPayPalPaymentTransfer->getLastName();
182
        $dataSubArray[ComputopApiConfig::ADDRESS_STREET] = $computopPayPalPaymentTransfer->getAddressStreet();
183
        $dataSubArray[ComputopApiConfig::ADDRESS_STREET2] = $computopPayPalPaymentTransfer->getAddressStreet2();
184
        $dataSubArray[ComputopApiConfig::ADDRESS_CITY] = $computopPayPalPaymentTransfer->getAddressCity();
185
186
        if ($computopPayPalPaymentTransfer->getAddressState()) {
187
            $dataSubArray[ComputopApiConfig::ADDRESS_STATE] = $computopPayPalPaymentTransfer->getAddressState();
188
        }
189
190
        $dataSubArray[ComputopApiConfig::ADDRESS_ZIP] = $computopPayPalPaymentTransfer->getAddressZip();
191
        $dataSubArray[ComputopApiConfig::ADDRESS_COUNTRY_CODE] = $computopPayPalPaymentTransfer->getAddressCountryCode();
192
193
        if ($computopPayPalPaymentTransfer->getPhone()) {
194
            $dataSubArray[ComputopApiConfig::PHONE] = $computopPayPalPaymentTransfer->getPhone();
195
        }
196
197
        return $dataSubArray;
198
    }
199
200
    /**
201
     * @param \Generated\Shared\Transfer\ItemTransfer $itemTransfer
202
     *
203
     * @return string
204
     */
205
    protected function getOrderItemDescription(ItemTransfer $itemTransfer): string
206
    {
207
        return sprintf(
208
            '%s,%f,%s,%d,,%f',
209
            $itemTransfer->getName(),
210
            $itemTransfer->getSumPrice(),
211
            $itemTransfer->getSku(),
212
            $itemTransfer->getQuantity(),
213
            $itemTransfer->getUnitTaxAmount()
214
        );
215
    }
216
217
    /**
218
     * Order item description for items should start from OrderDesc2 to OrderDesc99.
219
     *
220
     * @param int $key
221
     *
222
     * @return string
223
     */
224
    protected function getOrderItemDescriptionKey(int $key): string
225
    {
226
        return ComputopApiConfig::ORDER_DESC . $key;
227
    }
228
229
    /**
230
     * @param \Generated\Shared\Transfer\ComputopPayPalPaymentTransfer $computopPaymentTransfer
231
     * @param \ArrayObject|\Generated\Shared\Transfer\ItemTransfer[] $itemTransfers
232
     *
233
     * @return \Generated\Shared\Transfer\ComputopPayPalPaymentTransfer
234
     */
235
    protected function addOrderDescriptions(
236
        ComputopPayPalPaymentTransfer $computopPaymentTransfer,
237
        ArrayObject $itemTransfers
238
    ): ComputopPayPalPaymentTransfer {
239
        if ($itemTransfers->count() > $this->config->getMaxOrderDescriptionItemsForPayPalPaymentPage()) {
240
            return $computopPaymentTransfer;
241
        }
242
243
        foreach ($itemTransfers as $itemTransfer) {
244
            $orderDescription = $this->getOrderItemDescription($itemTransfer);
245
            $computopPaymentTransfer->addOrderDescriptions($orderDescription);
246
        }
247
248
        return $computopPaymentTransfer;
249
    }
250
251
    /**
252
     * @param array $dataSubArray
253
     * @param array $orderDescriptions
254
     *
255
     * @return array
256
     */
257
    protected function addOrderItemDescriptions(array $dataSubArray, array $orderDescriptions): array
258
    {
259
        foreach ($orderDescriptions as $key => $orderDescription) {
260
            $orderDescriptionIndex = static::PAYPAL_ITEM_DESCRIPTION_OFFSET + $key;
261
            $orderDescriptionKey = $this->getOrderItemDescriptionKey($orderDescriptionIndex);
262
            $dataSubArray[$orderDescriptionKey] = $orderDescription;
263
        }
264
265
        return $dataSubArray;
266
    }
267
}
268