Passed
Push — feature/eco-3656/eco-3658-fix-... ( 04a066...b8eacf )
by
unknown
06:41
created

PayPalMapper::getItemOrderDescription()   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
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
25
     *
26
     * @return \Generated\Shared\Transfer\ComputopPayPalPaymentTransfer
27
     */
28
    public function createComputopPaymentTransfer(QuoteTransfer $quoteTransfer)
29
    {
30
        /** @var \Generated\Shared\Transfer\ComputopPayPalPaymentTransfer $computopPaymentTransfer */
31
        $computopPaymentTransfer = parent::createComputopPaymentTransfer($quoteTransfer);
32
        $computopPaymentTransfer->setMac(
33
            $this->computopApiService->generateEncryptedMac(
34
                $this->createRequestTransfer($computopPaymentTransfer)
35
            )
36
        );
37
38
        $computopPaymentTransfer = $this->addOrderDescriptions(
39
            $computopPaymentTransfer,
40
            $quoteTransfer->getItems()
41
        );
42
43
        $computopPaymentTransfer->setTaxTotal($quoteTransfer->getTotals()->getTaxTotal()->getAmount());
44
        $computopPaymentTransfer->setShAmount($quoteTransfer->getTotals()->getShipmentTotal());
45
46
        $itemTotal = $this->calculateItemTotal($quoteTransfer);
47
        $computopPaymentTransfer->setItemTotal($itemTotal);
48
49
        $decryptedValues = $this->computopApiService->getEncryptedArray(
50
            $this->getDataSubArray($computopPaymentTransfer),
51
            $this->config->getBlowfishPassword()
52
        );
53
54
        $computopPaymentTransfer->setData($decryptedValues[ComputopApiConfig::DATA]);
55
        $computopPaymentTransfer->setLen($decryptedValues[ComputopApiConfig::LENGTH]);
56
        $computopPaymentTransfer->setUrl(
57
            $this->getActionUrl(
58
                $this->config->getPayPalInitActionUrl(),
59
                $this->getQueryParameters(
60
                    $computopPaymentTransfer->getMerchantId(),
61
                    $decryptedValues[ComputopApiConfig::DATA],
62
                    $decryptedValues[ComputopApiConfig::LENGTH]
63
                )
64
            )
65
        );
66
67
        return $computopPaymentTransfer;
68
    }
69
70
    /**
71
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
72
     *
73
     * @return int
74
     */
75
    protected function calculateItemTotal(QuoteTransfer $quoteTransfer): int
76
    {
77
        return $quoteTransfer->getTotals()->getGrandTotal() -
78
            $quoteTransfer->getTotals()->getShipmentTotal() -
79
            $quoteTransfer->getTotals()->getTaxTotal()->getAmount();
80
    }
81
82
    /**
83
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
84
     *
85
     * @return \Generated\Shared\Transfer\ComputopPayPalPaymentTransfer
86
     */
87
    protected function createTransferWithUnencryptedValues(QuoteTransfer $quoteTransfer)
88
    {
89
        $computopPaymentTransfer = new ComputopPayPalPaymentTransfer();
90
91
        $computopPaymentTransfer->setCapture(
92
            $this->getCaptureType(ComputopSharedConfig::PAYMENT_METHOD_PAY_PAL)
93
        );
94
        $computopPaymentTransfer->setTransId($this->generateTransId($quoteTransfer));
95
        $computopPaymentTransfer->setTxType($this->config->getPayPalTxType());
96
        $computopPaymentTransfer->setUrlSuccess(
97
            $this->router->generate(ComputopRouteProviderPlugin::PAY_PAL_SUCCESS, [], Router::ABSOLUTE_URL)
98
        );
99
        $computopPaymentTransfer->setOrderDesc(
100
            $this->computopApiService->getDescriptionValue($quoteTransfer->getItems()->getArrayCopy())
101
        );
102
103
        $this->mapAddressFromQuoteToComputopPayPalPayment($quoteTransfer, $computopPaymentTransfer);
104
105
        return $computopPaymentTransfer;
106
    }
107
108
    /**
109
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
110
     * @param \Generated\Shared\Transfer\ComputopPayPalPaymentTransfer $computopPayPalPaymentTransfer
111
     *
112
     * @return \Generated\Shared\Transfer\ComputopPayPalPaymentTransfer
113
     */
114
    protected function mapAddressFromQuoteToComputopPayPalPayment(
115
        QuoteTransfer $quoteTransfer,
116
        ComputopPayPalPaymentTransfer $computopPayPalPaymentTransfer
117
    ): ComputopPayPalPaymentTransfer {
118
        $addressTransfer = $quoteTransfer->getBillingSameAsShipping() ? $quoteTransfer->getBillingAddress() : $quoteTransfer->getShippingAddress();
119
120
        if (!$addressTransfer) {
121
            return $computopPayPalPaymentTransfer;
122
        }
123
124
        $computopPayPalPaymentTransfer
125
            ->setFirstName($addressTransfer->getFirstName())
126
            ->setLastName($addressTransfer->getLastName())
127
            ->setAddressStreet($addressTransfer->getAddress1())
128
            ->setAddressStreet2($addressTransfer->getAddress2())
129
            ->setAddressCity($addressTransfer->getCity())
130
            ->setAddressState($addressTransfer->getState())
131
            ->setAddressZip($addressTransfer->getZipCode())
132
            ->setAddressCountryCode($addressTransfer->getIso2Code())
133
            ->setPhone($addressTransfer->getPhone());
134
135
        return $computopPayPalPaymentTransfer;
136
    }
137
138
    /**
139
     * @param \Generated\Shared\Transfer\ComputopPayPalPaymentTransfer $computopPayPalPaymentTransfer
140
     *
141
     * @return array
142
     */
143
    protected function getDataSubArray(ComputopPayPalPaymentTransfer $computopPayPalPaymentTransfer): array
144
    {
145
        $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...
146
        $dataSubArray[ComputopApiConfig::AMOUNT] = $computopPayPalPaymentTransfer->getAmount();
147
        $dataSubArray[ComputopApiConfig::CURRENCY] = $computopPayPalPaymentTransfer->getCurrency();
148
        $dataSubArray[ComputopApiConfig::CAPTURE] = $computopPayPalPaymentTransfer->getCapture();
149
        $dataSubArray[ComputopApiConfig::TX_TYPE] = $computopPayPalPaymentTransfer->getTxType();
150
        $dataSubArray[ComputopApiConfig::ORDER_DESC] = $computopPayPalPaymentTransfer->getOrderDesc();
151
152
        $orderDescriptions = $computopPayPalPaymentTransfer->getOrderDescriptions();
153
154
        $orderDescriptionsCount = count($orderDescriptions);
155
        for ($key = static::PAYPAL_ITEM_DESCRIPTION_OFFSET; $key < $orderDescriptionsCount; $key++){
156
            $dataSubArray[$this->getOrderItemDescriptionKey($key)] = $orderDescriptions[$key];
157
        }
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 getItemOrderDescription(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 $item) {
244
            $computopPaymentTransfer->addOrderDescriptions($this->getItemOrderDescription($item));
245
        }
246
247
        return $computopPaymentTransfer;
248
    }
249
}
250