Passed
Push — feature/eco-3656/eco-3658-fix-... ( 4b5a6f...ae3841 )
by
unknown
06:08
created

PayPalMapper::addOrderItemDescriptions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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