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