Passed
Pull Request — master (#42)
by Roman
08:34 queued 03:30
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
    /**
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()
123
            ? $quoteTransfer->getBillingAddress()
124
            : $this->getShippingAddressFromQuote($quoteTransfer);
125
126
        if (!$addressTransfer) {
127
            return $computopPayPalPaymentTransfer;
128
        }
129
130
        $computopPayPalPaymentTransfer
131
            ->setFirstName($addressTransfer->getFirstName())
132
            ->setLastName($addressTransfer->getLastName())
133
            ->setAddressStreet($addressTransfer->getAddress1())
134
            ->setAddressStreet2($addressTransfer->getAddress2())
135
            ->setAddressCity($addressTransfer->getCity())
136
            ->setAddressState($addressTransfer->getState())
137
            ->setAddressZip($addressTransfer->getZipCode())
138
            ->setAddressCountryCode($addressTransfer->getIso2Code())
139
            ->setPhone($addressTransfer->getPhone());
140
141
        return $computopPayPalPaymentTransfer;
142
    }
143
144
    /**
145
     * @param \Generated\Shared\Transfer\ComputopPayPalPaymentTransfer $computopPayPalPaymentTransfer
146
     *
147
     * @return array
148
     */
149
    protected function getDataSubArray(ComputopPayPalPaymentTransfer $computopPayPalPaymentTransfer): array
150
    {
151
        $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...
152
        $dataSubArray[ComputopApiConfig::AMOUNT] = $computopPayPalPaymentTransfer->getAmount();
153
        $dataSubArray[ComputopApiConfig::CURRENCY] = $computopPayPalPaymentTransfer->getCurrency();
154
        $dataSubArray[ComputopApiConfig::CAPTURE] = $computopPayPalPaymentTransfer->getCapture();
155
        $dataSubArray[ComputopApiConfig::TX_TYPE] = $computopPayPalPaymentTransfer->getTxType();
156
        $dataSubArray[ComputopApiConfig::ORDER_DESC] = $computopPayPalPaymentTransfer->getOrderDesc();
157
158
        $orderDescriptions = $computopPayPalPaymentTransfer->getOrderDescriptions();
159
        $dataSubArray = $this->addOrderItemDescriptions($dataSubArray, $orderDescriptions);
160
161
        $dataSubArray[ComputopApiConfig::TAX_TOTAL] = $computopPayPalPaymentTransfer->getTaxTotal();
162
        $dataSubArray[ComputopApiConfig::ITEM_TOTAL] = $computopPayPalPaymentTransfer->getItemTotal();
163
        $dataSubArray[ComputopApiConfig::SHIPPING_AMOUNT] = $computopPayPalPaymentTransfer->getShAmount();
164
165
        $dataSubArray[ComputopApiConfig::MAC] = $computopPayPalPaymentTransfer->getMac();
166
        $dataSubArray[ComputopApiConfig::URL_SUCCESS] = $computopPayPalPaymentTransfer->getUrlSuccess();
167
        $dataSubArray[ComputopApiConfig::URL_FAILURE] = $computopPayPalPaymentTransfer->getUrlFailure();
168
        $dataSubArray[ComputopApiConfig::RESPONSE] = $computopPayPalPaymentTransfer->getResponse();
169
        $dataSubArray[ComputopApiConfig::URL_NOTIFY] = $computopPayPalPaymentTransfer->getUrlNotify();
170
        $dataSubArray[ComputopApiConfig::REQ_ID] = $computopPayPalPaymentTransfer->getReqId();
171
172
        $dataSubArray[ComputopApiConfig::ETI_ID] = $this->config->getEtiId();
173
        $dataSubArray[ComputopApiConfig::IP_ADDRESS] = $computopPayPalPaymentTransfer->getClientIp();
174
        $dataSubArray[ComputopApiConfig::SHIPPING_ZIP] = $computopPayPalPaymentTransfer->getShippingZip();
175
176
        if (!$computopPayPalPaymentTransfer->getAddressStreet()) {
177
            $dataSubArray[ComputopApiConfig::NO_SHIPPING] = ComputopSharedConfig::PAY_PAL_NO_SHIPPING;
178
179
            return $dataSubArray;
180
        }
181
182
        $dataSubArray[ComputopApiConfig::FIRST_NAME] = $computopPayPalPaymentTransfer->getFirstName();
183
        $dataSubArray[ComputopApiConfig::LAST_NAME] = $computopPayPalPaymentTransfer->getLastName();
184
        $dataSubArray[ComputopApiConfig::ADDRESS_STREET] = $computopPayPalPaymentTransfer->getAddressStreet();
185
        $dataSubArray[ComputopApiConfig::ADDRESS_STREET2] = $computopPayPalPaymentTransfer->getAddressStreet2();
186
        $dataSubArray[ComputopApiConfig::ADDRESS_CITY] = $computopPayPalPaymentTransfer->getAddressCity();
187
188
        if ($computopPayPalPaymentTransfer->getAddressState()) {
189
            $dataSubArray[ComputopApiConfig::ADDRESS_STATE] = $computopPayPalPaymentTransfer->getAddressState();
190
        }
191
192
        $dataSubArray[ComputopApiConfig::ADDRESS_ZIP] = $computopPayPalPaymentTransfer->getAddressZip();
193
        $dataSubArray[ComputopApiConfig::ADDRESS_COUNTRY_CODE] = $computopPayPalPaymentTransfer->getAddressCountryCode();
194
195
        if ($computopPayPalPaymentTransfer->getPhone()) {
196
            $dataSubArray[ComputopApiConfig::PHONE] = $computopPayPalPaymentTransfer->getPhone();
197
        }
198
199
        return $dataSubArray;
200
    }
201
202
    /**
203
     * @param \Generated\Shared\Transfer\ItemTransfer $itemTransfer
204
     *
205
     * @return string
206
     */
207
    protected function getOrderItemDescription(ItemTransfer $itemTransfer): string
208
    {
209
        return sprintf(
210
            '%s,%f,%s,%d,,%f',
211
            $itemTransfer->getName(),
212
            $itemTransfer->getSumPrice(),
213
            $itemTransfer->getSku(),
214
            $itemTransfer->getQuantity(),
215
            $itemTransfer->getUnitTaxAmount()
216
        );
217
    }
218
219
    /**
220
     * Order item description for items should start from OrderDesc2 to OrderDesc99.
221
     *
222
     * @param int $key
223
     *
224
     * @return string
225
     */
226
    protected function getOrderItemDescriptionKey(int $key): string
227
    {
228
        return ComputopApiConfig::ORDER_DESC . $key;
229
    }
230
231
    /**
232
     * @param \Generated\Shared\Transfer\ComputopPayPalPaymentTransfer $computopPaymentTransfer
233
     * @param \ArrayObject|\Generated\Shared\Transfer\ItemTransfer[] $itemTransfers
234
     *
235
     * @return \Generated\Shared\Transfer\ComputopPayPalPaymentTransfer
236
     */
237
    protected function addOrderDescriptions(
238
        ComputopPayPalPaymentTransfer $computopPaymentTransfer,
239
        ArrayObject $itemTransfers
240
    ): ComputopPayPalPaymentTransfer {
241
        if ($itemTransfers->count() > $this->config->getMaxOrderDescriptionItemsForPayPalPaymentPage()) {
242
            return $computopPaymentTransfer;
243
        }
244
245
        foreach ($itemTransfers as $itemTransfer) {
246
            $orderDescription = $this->getOrderItemDescription($itemTransfer);
247
            $computopPaymentTransfer->addOrderDescriptions($orderDescription);
248
        }
249
250
        return $computopPaymentTransfer;
251
    }
252
253
    /**
254
     * @param array $dataSubArray
255
     * @param array $orderDescriptions
256
     *
257
     * @return array
258
     */
259
    protected function addOrderItemDescriptions(array $dataSubArray, array $orderDescriptions): array
260
    {
261
        foreach ($orderDescriptions as $key => $orderDescription) {
262
            $orderDescriptionIndex = static::PAYPAL_ITEM_DESCRIPTION_OFFSET + $key;
263
            $orderDescriptionKey = $this->getOrderItemDescriptionKey($orderDescriptionIndex);
264
            $dataSubArray[$orderDescriptionKey] = $orderDescription;
265
        }
266
267
        return $dataSubArray;
268
    }
269
}
270