Passed
Push — feature/eco-3656/eco-3658-fix-... ( cebc8b...d44644 )
by
unknown
05:10
created

PayPalMapper::calculateItemTotal()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 5
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 Generated\Shared\Transfer\TotalsTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\TotalsTransfer 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...
15
use Spryker\Yves\Router\Router\Router;
16
use SprykerEco\Shared\Computop\ComputopConfig as ComputopSharedConfig;
17
use SprykerEco\Shared\Computop\Config\ComputopApiConfig;
18
use SprykerEco\Yves\Computop\Mapper\Init\AbstractMapper;
19
use SprykerEco\Yves\Computop\Plugin\Router\ComputopRouteProviderPlugin;
20
21
class PayPalMapper extends AbstractMapper
22
{
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 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)
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
        foreach ($orderDescriptions as $key => $orderDesc) {
154
            if ($key < 2)
155
            $dataSubArray[$this->getOrderItemDescriptionKey($key)] = $orderDesc;
156
        }
157
158
        $dataSubArray[ComputopApiConfig::TAX_TOTAL] = $computopPayPalPaymentTransfer->getTaxTotal();
159
        $dataSubArray[ComputopApiConfig::ITEM_TOTAL] = $computopPayPalPaymentTransfer->getItemTotal();
160
        $dataSubArray[ComputopApiConfig::SHIPPING_AMOUNT] = $computopPayPalPaymentTransfer->getShAmount();
161
162
        $dataSubArray[ComputopApiConfig::MAC] = $computopPayPalPaymentTransfer->getMac();
163
        $dataSubArray[ComputopApiConfig::URL_SUCCESS] = $computopPayPalPaymentTransfer->getUrlSuccess();
164
        $dataSubArray[ComputopApiConfig::URL_FAILURE] = $computopPayPalPaymentTransfer->getUrlFailure();
165
        $dataSubArray[ComputopApiConfig::RESPONSE] = $computopPayPalPaymentTransfer->getResponse();
166
        $dataSubArray[ComputopApiConfig::URL_NOTIFY] = $computopPayPalPaymentTransfer->getUrlNotify();
167
        $dataSubArray[ComputopApiConfig::REQ_ID] = $computopPayPalPaymentTransfer->getReqId();
168
169
        $dataSubArray[ComputopApiConfig::ETI_ID] = $this->config->getEtiId();
170
        $dataSubArray[ComputopApiConfig::IP_ADDRESS] = $computopPayPalPaymentTransfer->getClientIp();
171
        $dataSubArray[ComputopApiConfig::SHIPPING_ZIP] = $computopPayPalPaymentTransfer->getShippingZip();
172
173
        if (!$computopPayPalPaymentTransfer->getAddressStreet()) {
174
            $dataSubArray[ComputopApiConfig::NO_SHIPPING] = ComputopSharedConfig::PAY_PAL_NO_SHIPPING;
175
176
            return $dataSubArray;
177
        }
178
179
        $dataSubArray[ComputopApiConfig::FIRST_NAME] = $computopPayPalPaymentTransfer->getFirstName();
180
        $dataSubArray[ComputopApiConfig::LAST_NAME] = $computopPayPalPaymentTransfer->getLastName();
181
        $dataSubArray[ComputopApiConfig::ADDRESS_STREET] = $computopPayPalPaymentTransfer->getAddressStreet();
182
        $dataSubArray[ComputopApiConfig::ADDRESS_STREET2] = $computopPayPalPaymentTransfer->getAddressStreet2();
183
        $dataSubArray[ComputopApiConfig::ADDRESS_CITY] = $computopPayPalPaymentTransfer->getAddressCity();
184
185
        if ($computopPayPalPaymentTransfer->getAddressState()) {
186
            $dataSubArray[ComputopApiConfig::ADDRESS_STATE] = $computopPayPalPaymentTransfer->getAddressState();
187
        }
188
189
        $dataSubArray[ComputopApiConfig::ADDRESS_ZIP] = $computopPayPalPaymentTransfer->getAddressZip();
190
        $dataSubArray[ComputopApiConfig::ADDRESS_COUNTRY_CODE] = $computopPayPalPaymentTransfer->getAddressCountryCode();
191
192
        if ($computopPayPalPaymentTransfer->getPhone()) {
193
            $dataSubArray[ComputopApiConfig::PHONE] = $computopPayPalPaymentTransfer->getPhone();
194
        }
195
196
        return $dataSubArray;
197
    }
198
199
    /**
200
     * @param \Generated\Shared\Transfer\ItemTransfer $itemTransfer
201
     *
202
     * @return string
203
     */
204
    protected function getItemOrderDescription(ItemTransfer $itemTransfer): string
205
    {
206
        return sprintf(
207
            '%s,%f,%s,%d,,%f',
208
            $itemTransfer->getName(),
209
            $itemTransfer->getSumPrice(),
210
            $itemTransfer->getSku(),
211
            $itemTransfer->getQuantity(),
212
            $itemTransfer->getUnitTaxAmount()
213
        );
214
    }
215
216
    /**
217
     * Order item description for items should start from OrderDesc2 to OrderDesc99
218
     *
219
     * @param int $key
220
     *
221
     * @return string
222
     */
223
    protected function getOrderItemDescriptionKey(int $key): string
224
    {
225
        return ComputopApiConfig::ORDER_DESC . $key;
226
    }
227
228
    /**
229
     * @param \Generated\Shared\Transfer\ComputopPayPalPaymentTransfer $computopPaymentTransfer
230
     * @param \ArrayObject|\Generated\Shared\Transfer\ItemTransfer[] $itemTransfers
231
     * @param \Generated\Shared\Transfer\TotalsTransfer $totals
232
     *
233
     * @return 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