Passed
Push — feature/eco-3656/eco-3658-fix-... ( 54b10f...cebc8b )
by
unknown
05:12
created

PayPalMapper::addOrderDescriptions()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 20
rs 9.9332
cc 3
nc 3
nop 3
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
            $quoteTransfer->getTotals()
42
        );
43
44
        $decryptedValues = $this->computopApiService->getEncryptedArray(
45
            $this->getDataSubArray($computopPaymentTransfer),
46
            $this->config->getBlowfishPassword()
47
        );
48
49
        $computopPaymentTransfer->setData($decryptedValues[ComputopApiConfig::DATA]);
50
        $computopPaymentTransfer->setLen($decryptedValues[ComputopApiConfig::LENGTH]);
51
        $computopPaymentTransfer->setUrl(
52
            $this->getActionUrl(
53
                $this->config->getPayPalInitActionUrl(),
54
                $this->getQueryParameters(
55
                    $computopPaymentTransfer->getMerchantId(),
56
                    $decryptedValues[ComputopApiConfig::DATA],
57
                    $decryptedValues[ComputopApiConfig::LENGTH]
58
                )
59
            )
60
        );
61
62
        return $computopPaymentTransfer;
63
    }
64
65
    /**
66
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
67
     *
68
     * @return \Generated\Shared\Transfer\ComputopPayPalPaymentTransfer
69
     */
70
    protected function createTransferWithUnencryptedValues(QuoteTransfer $quoteTransfer)
71
    {
72
        $computopPaymentTransfer = new ComputopPayPalPaymentTransfer();
73
74
        $computopPaymentTransfer->setCapture(
75
            $this->getCaptureType(ComputopSharedConfig::PAYMENT_METHOD_PAY_PAL)
76
        );
77
        $computopPaymentTransfer->setTransId($this->generateTransId($quoteTransfer));
78
        $computopPaymentTransfer->setTxType($this->config->getPayPalTxType());
79
        $computopPaymentTransfer->setUrlSuccess(
80
            $this->router->generate(ComputopRouteProviderPlugin::PAY_PAL_SUCCESS, [], Router::ABSOLUTE_URL)
81
        );
82
        $computopPaymentTransfer->setOrderDesc(
83
            $this->computopApiService->getDescriptionValue($quoteTransfer->getItems()->getArrayCopy())
84
        );
85
86
        $this->mapAddressFromQuoteToComputopPayPalPayment($quoteTransfer, $computopPaymentTransfer);
87
88
        return $computopPaymentTransfer;
89
    }
90
91
    /**
92
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
93
     * @param \Generated\Shared\Transfer\ComputopPayPalPaymentTransfer $computopPayPalPaymentTransfer
94
     *
95
     * @return \Generated\Shared\Transfer\ComputopPayPalPaymentTransfer
96
     */
97
    protected function mapAddressFromQuoteToComputopPayPalPayment(
98
        QuoteTransfer $quoteTransfer,
99
        ComputopPayPalPaymentTransfer $computopPayPalPaymentTransfer
100
    ): ComputopPayPalPaymentTransfer {
101
        $addressTransfer = $quoteTransfer->getBillingSameAsShipping() ? $quoteTransfer->getBillingAddress() : $quoteTransfer->getShippingAddress();
102
103
        if (!$addressTransfer) {
104
            return $computopPayPalPaymentTransfer;
105
        }
106
107
        $computopPayPalPaymentTransfer
108
            ->setFirstName($addressTransfer->getFirstName())
109
            ->setLastName($addressTransfer->getLastName())
110
            ->setAddressStreet($addressTransfer->getAddress1())
111
            ->setAddressStreet2($addressTransfer->getAddress2())
112
            ->setAddressCity($addressTransfer->getCity())
113
            ->setAddressState($addressTransfer->getState())
114
            ->setAddressZip($addressTransfer->getZipCode())
115
            ->setAddressCountryCode($addressTransfer->getIso2Code())
116
            ->setPhone($addressTransfer->getPhone());
117
118
        return $computopPayPalPaymentTransfer;
119
    }
120
121
    /**
122
     * @param \Generated\Shared\Transfer\ComputopPayPalPaymentTransfer $computopPayPalPaymentTransfer
123
     *
124
     * @return array
125
     */
126
    protected function getDataSubArray(ComputopPayPalPaymentTransfer $computopPayPalPaymentTransfer)
127
    {
128
        $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...
129
        $dataSubArray[ComputopApiConfig::AMOUNT] = $computopPayPalPaymentTransfer->getAmount();
130
        $dataSubArray[ComputopApiConfig::CURRENCY] = $computopPayPalPaymentTransfer->getCurrency();
131
        $dataSubArray[ComputopApiConfig::CAPTURE] = $computopPayPalPaymentTransfer->getCapture();
132
        $dataSubArray[ComputopApiConfig::TX_TYPE] = $computopPayPalPaymentTransfer->getTxType();
133
        $dataSubArray[ComputopApiConfig::ORDER_DESC] = $computopPayPalPaymentTransfer->getOrderDesc();
134
135
        $orderDescriptions = $computopPayPalPaymentTransfer->getOrderDescriptions();
136
        foreach ($orderDescriptions as $key => $orderDesc) {
137
            if ($key < 2)
138
            $dataSubArray[$this->getOrderItemDescriptionKey($key)] = $orderDesc;
139
        }
140
141
        $dataSubArray[ComputopApiConfig::TAX_TOTAL] = $computopPayPalPaymentTransfer->getTaxTotal();
142
        $dataSubArray[ComputopApiConfig::ITEM_TOTAL] = $computopPayPalPaymentTransfer->getItemTotal();
143
        $dataSubArray[ComputopApiConfig::SHIPPING_AMOUNT] = $computopPayPalPaymentTransfer->getShAmount();
144
145
        $dataSubArray[ComputopApiConfig::MAC] = $computopPayPalPaymentTransfer->getMac();
146
        $dataSubArray[ComputopApiConfig::URL_SUCCESS] = $computopPayPalPaymentTransfer->getUrlSuccess();
147
        $dataSubArray[ComputopApiConfig::URL_FAILURE] = $computopPayPalPaymentTransfer->getUrlFailure();
148
        $dataSubArray[ComputopApiConfig::RESPONSE] = $computopPayPalPaymentTransfer->getResponse();
149
        $dataSubArray[ComputopApiConfig::URL_NOTIFY] = $computopPayPalPaymentTransfer->getUrlNotify();
150
        $dataSubArray[ComputopApiConfig::REQ_ID] = $computopPayPalPaymentTransfer->getReqId();
151
152
        $dataSubArray[ComputopApiConfig::ETI_ID] = $this->config->getEtiId();
153
        $dataSubArray[ComputopApiConfig::IP_ADDRESS] = $computopPayPalPaymentTransfer->getClientIp();
154
        $dataSubArray[ComputopApiConfig::SHIPPING_ZIP] = $computopPayPalPaymentTransfer->getShippingZip();
155
156
        if (!$computopPayPalPaymentTransfer->getAddressStreet()) {
157
            $dataSubArray[ComputopApiConfig::NO_SHIPPING] = ComputopSharedConfig::PAY_PAL_NO_SHIPPING;
158
159
            return $dataSubArray;
160
        }
161
162
        $dataSubArray[ComputopApiConfig::FIRST_NAME] = $computopPayPalPaymentTransfer->getFirstName();
163
        $dataSubArray[ComputopApiConfig::LAST_NAME] = $computopPayPalPaymentTransfer->getLastName();
164
        $dataSubArray[ComputopApiConfig::ADDRESS_STREET] = $computopPayPalPaymentTransfer->getAddressStreet();
165
        $dataSubArray[ComputopApiConfig::ADDRESS_STREET2] = $computopPayPalPaymentTransfer->getAddressStreet2();
166
        $dataSubArray[ComputopApiConfig::ADDRESS_CITY] = $computopPayPalPaymentTransfer->getAddressCity();
167
168
        if ($computopPayPalPaymentTransfer->getAddressState()) {
169
            $dataSubArray[ComputopApiConfig::ADDRESS_STATE] = $computopPayPalPaymentTransfer->getAddressState();
170
        }
171
172
        $dataSubArray[ComputopApiConfig::ADDRESS_ZIP] = $computopPayPalPaymentTransfer->getAddressZip();
173
        $dataSubArray[ComputopApiConfig::ADDRESS_COUNTRY_CODE] = $computopPayPalPaymentTransfer->getAddressCountryCode();
174
175
        if ($computopPayPalPaymentTransfer->getPhone()) {
176
            $dataSubArray[ComputopApiConfig::PHONE] = $computopPayPalPaymentTransfer->getPhone();
177
        }
178
179
        return $dataSubArray;
180
    }
181
182
    /**
183
     * @param \Generated\Shared\Transfer\ItemTransfer $itemTransfer
184
     *
185
     * @return string
186
     */
187
    protected function getItemOrderDescription(ItemTransfer $itemTransfer): string
188
    {
189
        return sprintf(
190
            '%s,%f,%s,%d,,%f',
191
            $itemTransfer->getName(),
192
            $itemTransfer->getSumPrice(),
193
            $itemTransfer->getSku(),
194
            $itemTransfer->getQuantity(),
195
            $itemTransfer->getUnitTaxAmount()
196
        );
197
    }
198
199
    /**
200
     * Order item description for items should start from OrderDesc2 to OrderDesc99
201
     *
202
     * @param int $key
203
     *
204
     * @return string
205
     */
206
    protected function getOrderItemDescriptionKey(int $key): string
207
    {
208
        return ComputopApiConfig::ORDER_DESC . $key;
209
    }
210
211
    /**
212
     * @param \Generated\Shared\Transfer\ComputopPayPalPaymentTransfer $computopPaymentTransfer
213
     * @param \ArrayObject|\Generated\Shared\Transfer\ItemTransfer[] $itemTransfers
214
     * @param \Generated\Shared\Transfer\TotalsTransfer $totals
215
     *
216
     * @return ComputopPayPalPaymentTransfer
217
     */
218
    protected function addOrderDescriptions(
219
        ComputopPayPalPaymentTransfer $computopPaymentTransfer,
220
        ArrayObject $itemTransfers,
221
        TotalsTransfer $totals
222
    ): ComputopPayPalPaymentTransfer {
223
        if ($itemTransfers->count() > $this->config->getMaxOrderDescriptionItemsForPayPalPaymentPage()) {
224
            return $computopPaymentTransfer;
225
        }
226
227
        $taxTotal = $totals->getTaxTotal()->getAmount();
228
        foreach ($itemTransfers as $item) {
229
            $computopPaymentTransfer->addOrderDescriptions($this->getItemOrderDescription($item));
230
            $taxTotal += $item->getQuantity() * $item->getUnitTaxAmount();
231
        }
232
233
        $computopPaymentTransfer->setTaxTotal($taxTotal);
234
        $computopPaymentTransfer->setShAmount($totals->getShipmentTotal());
235
        $computopPaymentTransfer->setItemTotal($totals->getGrandTotal() - $totals->getShipmentTotal() - $taxTotal);
236
237
        return $computopPaymentTransfer;
238
    }
239
}
240