Passed
Push — feature/eco-3656/eco-3658-fix-... ( 317645...c3c640 )
by
unknown
06:16
created

PayPalMapper::getOrderItemDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 9
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
    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)
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)
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
155
        foreach ($orderDescriptions as $key => $orderDescription) {
156
            $orderDescriptionIndex = static::PAYPAL_ITEM_DESCRIPTION_OFFSET + $key;
157
            $orderDescriptionKey = $this->getOrderItemDescriptionKey($orderDescriptionIndex);
158
            $dataSubArray[$orderDescriptionKey] = $orderDescription;
159
        }
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