Passed
Push — feature/eco-3656/eco-3658-fix-... ( ef4348 )
by Roman
06:28
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 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...
11
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...
12
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...
13
use Spryker\Yves\Router\Router\Router;
14
use SprykerEco\Shared\Computop\ComputopConfig as ComputopSharedConfig;
15
use SprykerEco\Shared\Computop\Config\ComputopApiConfig;
16
use SprykerEco\Yves\Computop\Mapper\Init\AbstractMapper;
17
use SprykerEco\Yves\Computop\Plugin\Router\ComputopRouteProviderPlugin;
18
19
class PayPalMapper extends AbstractMapper
20
{
21
    /**
22
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
23
     *
24
     * @return \Generated\Shared\Transfer\ComputopPayPalPaymentTransfer
25
     */
26
    public function createComputopPaymentTransfer(QuoteTransfer $quoteTransfer)
27
    {
28
        /** @var \Generated\Shared\Transfer\ComputopPayPalPaymentTransfer $computopPaymentTransfer */
29
        $computopPaymentTransfer = parent::createComputopPaymentTransfer($quoteTransfer);
30
        $computopPaymentTransfer->setMac(
31
            $this->computopApiService->generateEncryptedMac(
32
                $this->createRequestTransfer($computopPaymentTransfer)
33
            )
34
        );
35
36
        $taxTotal = 0;
37
        foreach ($quoteTransfer->getItems() as $item) {
38
            $computopPaymentTransfer->addOrderDescriptions($this->getItemOrderDescription($item));
39
            $taxTotal += $item->getQuantity() * $item->getUnitTaxAmount();
40
        }
41
42
        $totals = $quoteTransfer->getTotals();
43
        $computopPaymentTransfer->setTaxTotal($taxTotal);
44
        $computopPaymentTransfer->setShAmount($totals->getShipmentTotal());
45
        $computopPaymentTransfer->setItemTotal($totals->getGrandTotal() - $totals->getShipmentTotal() - $taxTotal);
46
47
        $decryptedValues = $this->computopApiService->getEncryptedArray(
48
            $this->getDataSubArray($computopPaymentTransfer),
49
            $this->config->getBlowfishPassword()
50
        );
51
52
        $computopPaymentTransfer->setData($decryptedValues[ComputopApiConfig::DATA]);
53
        $computopPaymentTransfer->setLen($decryptedValues[ComputopApiConfig::LENGTH]);
54
        $computopPaymentTransfer->setUrl(
55
            $this->getActionUrl(
56
                $this->config->getPayPalInitActionUrl(),
57
                $this->getQueryParameters(
58
                    $computopPaymentTransfer->getMerchantId(),
59
                    $decryptedValues[ComputopApiConfig::DATA],
60
                    $decryptedValues[ComputopApiConfig::LENGTH]
61
                )
62
            )
63
        );
64
65
        return $computopPaymentTransfer;
66
    }
67
68
    /**
69
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
70
     *
71
     * @return \Generated\Shared\Transfer\ComputopPayPalPaymentTransfer
72
     */
73
    protected function createTransferWithUnencryptedValues(QuoteTransfer $quoteTransfer)
74
    {
75
        $computopPaymentTransfer = new ComputopPayPalPaymentTransfer();
76
77
        $computopPaymentTransfer->setCapture(
78
            $this->getCaptureType(ComputopSharedConfig::PAYMENT_METHOD_PAY_PAL)
79
        );
80
        $computopPaymentTransfer->setTransId($this->generateTransId($quoteTransfer));
81
        $computopPaymentTransfer->setTxType($this->config->getPayPalTxType());
82
        $computopPaymentTransfer->setUrlSuccess(
83
            $this->router->generate(ComputopRouteProviderPlugin::PAY_PAL_SUCCESS, [], Router::ABSOLUTE_URL)
84
        );
85
        $computopPaymentTransfer->setOrderDesc(
86
            $this->computopApiService->getDescriptionValue($quoteTransfer->getItems()->getArrayCopy())
87
        );
88
89
        $this->mapAddressFromQuoteToComputopPayPalPayment($quoteTransfer, $computopPaymentTransfer);
90
91
        return $computopPaymentTransfer;
92
    }
93
94
    /**
95
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
96
     * @param \Generated\Shared\Transfer\ComputopPayPalPaymentTransfer $computopPayPalPaymentTransfer
97
     *
98
     * @return \Generated\Shared\Transfer\ComputopPayPalPaymentTransfer
99
     */
100
    protected function mapAddressFromQuoteToComputopPayPalPayment(
101
        QuoteTransfer $quoteTransfer,
102
        ComputopPayPalPaymentTransfer $computopPayPalPaymentTransfer
103
    ): ComputopPayPalPaymentTransfer {
104
        $addressTransfer = $quoteTransfer->getBillingSameAsShipping() ? $quoteTransfer->getBillingAddress() : $quoteTransfer->getShippingAddress();
105
106
        if (!$addressTransfer) {
107
            return $computopPayPalPaymentTransfer;
108
        }
109
110
        $computopPayPalPaymentTransfer
111
            ->setFirstName($addressTransfer->getFirstName())
112
            ->setLastName($addressTransfer->getLastName())
113
            ->setAddressStreet($addressTransfer->getAddress1())
114
            ->setAddressStreet2($addressTransfer->getAddress2())
115
            ->setAddressCity($addressTransfer->getCity())
116
            ->setAddressState($addressTransfer->getState())
117
            ->setAddressZip($addressTransfer->getZipCode())
118
            ->setAddressCountryCode($addressTransfer->getIso2Code())
119
            ->setPhone($addressTransfer->getPhone());
120
121
        return $computopPayPalPaymentTransfer;
122
    }
123
124
    /**
125
     * @param \Generated\Shared\Transfer\ComputopPayPalPaymentTransfer $computopPayPalPaymentTransfer
126
     *
127
     * @return array
128
     */
129
    protected function getDataSubArray(ComputopPayPalPaymentTransfer $computopPayPalPaymentTransfer)
130
    {
131
        $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...
132
        $dataSubArray[ComputopApiConfig::AMOUNT] = $computopPayPalPaymentTransfer->getAmount();
133
        $dataSubArray[ComputopApiConfig::CURRENCY] = $computopPayPalPaymentTransfer->getCurrency();
134
        $dataSubArray[ComputopApiConfig::CAPTURE] = $computopPayPalPaymentTransfer->getCapture();
135
        $dataSubArray[ComputopApiConfig::TX_TYPE] = $computopPayPalPaymentTransfer->getTxType();
136
        $dataSubArray[ComputopApiConfig::ORDER_DESC] = $computopPayPalPaymentTransfer->getOrderDesc();
137
        foreach ($computopPayPalPaymentTransfer->getOrderDescriptions() as $key => $orderDesc) {
138
            $dataSubArray[ComputopApiConfig::ORDER_DESC . ($key + 2)] = $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 $item
184
     *
185
     * @return string
186
     */
187
    protected function getItemOrderDescription(ItemTransfer $item): string
188
    {
189
        return implode(',', [
190
            $item->getName(),
191
            $item->getUnitGrossPrice() - $item->getUnitTaxAmount(),
192
            $item->getSku(),
193
            $item->getQuantity(),
194
            '',
195
            $item->getUnitTaxAmount(),
196
        ]);
197
    }
198
}
199