Passed
Push — feature/eco-3656/eco-3658-fix-... ( b1d0d7...d62755 )
by Roman
05:56
created

PayPalMapper::addOrderItemsDetails()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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