Passed
Pull Request — master (#38)
by Ruslan
04:45
created

CashOnDelivery::mapPaymentToAuthorization()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 6
rs 10
c 0
b 0
f 0
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\Zed\Payone\Business\Payment\MethodMapper;
9
10
use Generated\Shared\Transfer\LocaleTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\LocaleTransfer 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\OrderTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\OrderTransfer 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\PayoneAuthorizationTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...neAuthorizationTransfer 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\PayoneBankAccountCheckTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...ankAccountCheckTransfer 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 Orm\Zed\Payone\Persistence\SpyPaymentPayone;
0 ignored issues
show
Bug introduced by
The type Orm\Zed\Payone\Persistence\SpyPaymentPayone 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\Shared\Kernel\Store;
16
use SprykerEco\Shared\Payone\PayoneApiConstants;
17
use SprykerEco\Zed\Payone\Business\Api\Request\Container\Authorization\AbstractAuthorizationContainer;
18
use SprykerEco\Zed\Payone\Business\Api\Request\Container\Authorization\PaymentMethod\CashOnDeliveryContainer;
19
use SprykerEco\Zed\Payone\Business\Api\Request\Container\Authorization\PersonalContainer;
20
use SprykerEco\Zed\Payone\Business\Api\Request\Container\AuthorizationContainer;
21
use SprykerEco\Zed\Payone\Business\Api\Request\Container\BankAccountCheckContainer;
22
use SprykerEco\Zed\Payone\Business\Api\Request\Container\CaptureContainer;
23
use SprykerEco\Zed\Payone\Business\Api\Request\Container\DebitContainer;
24
use SprykerEco\Zed\Payone\Business\Api\Request\Container\PreAuthorizationContainer;
25
use SprykerEco\Zed\Payone\Business\Api\Request\Container\RefundContainer;
26
use SprykerEco\Zed\Payone\Dependency\Facade\PayoneToGlossaryInterface;
27
28
class CashOnDelivery extends AbstractMapper
29
{
30
    /**
31
     * @var \SprykerEco\Zed\Payone\Dependency\Facade\PayoneToGlossaryInterface
32
     */
33
    protected $glossary;
34
35
    /**
36
     * @param \Spryker\Shared\Kernel\Store $storeConfig
37
     * @param \SprykerEco\Zed\Payone\Dependency\Facade\PayoneToGlossaryInterface $glossary
38
     */
39
    public function __construct(Store $storeConfig, PayoneToGlossaryInterface $glossary)
40
    {
41
        parent::__construct($storeConfig);
42
        $this->glossary = $glossary;
43
    }
44
45
    /**
46
     * @return string
47
     */
48
    public function getName(): string
49
    {
50
        return PayoneApiConstants::PAYMENT_METHOD_CASH_ON_DELIVERY;
51
    }
52
53
    /**
54
     * @param \Orm\Zed\Payone\Persistence\SpyPaymentPayone $paymentEntity
55
     * @param \Generated\Shared\Transfer\OrderTransfer $orderTransfer
56
     *
57
     * @return \SprykerEco\Zed\Payone\Business\Api\Request\Container\Authorization\AbstractAuthorizationContainer|\SprykerEco\Zed\Payone\Business\Api\Request\Container\AuthorizationContainer
58
     */
59
    public function mapPaymentToAuthorization(SpyPaymentPayone $paymentEntity, OrderTransfer $orderTransfer): AbstractAuthorizationContainer
60
    {
61
        $authorizationContainer = new AuthorizationContainer();
62
        $authorizationContainer = $this->mapPaymentToAbstractAuthorization($paymentEntity, $authorizationContainer);
63
64
        return $authorizationContainer;
65
    }
66
67
    /**
68
     * @param \Orm\Zed\Payone\Persistence\SpyPaymentPayone $paymentEntity
69
     *
70
     * @return \SprykerEco\Zed\Payone\Business\Api\Request\Container\CaptureContainer
71
     */
72
    public function mapPaymentToCapture(SpyPaymentPayone $paymentEntity): CaptureContainer
73
    {
74
        $paymentDetailEntity = $paymentEntity->getSpyPaymentPayoneDetail();
75
76
        $captureContainer = new CaptureContainer();
77
        $captureContainer->setAmount($paymentDetailEntity->getAmount());
78
        $captureContainer->setCurrency($this->getStandardParameter()->getCurrency());
79
        $captureContainer->setTxid($paymentEntity->getTransactionId());
80
        $captureContainer->setSequenceNumber($this->getNextSequenceNumber($paymentEntity->getTransactionId()));
81
82
        return $captureContainer;
83
    }
84
85
    /**
86
     * @param \Orm\Zed\Payone\Persistence\SpyPaymentPayone $paymentEntity
87
     *
88
     * @return \SprykerEco\Zed\Payone\Business\Api\Request\Container\Authorization\AbstractAuthorizationContainer|\SprykerEco\Zed\Payone\Business\Api\Request\Container\PreAuthorizationContainer
89
     */
90
    public function mapPaymentToPreAuthorization(SpyPaymentPayone $paymentEntity): AbstractAuthorizationContainer
91
    {
92
        $preAuthorizationContainer = new PreAuthorizationContainer();
93
        $preAuthorizationContainer = $this->mapPaymentToAbstractAuthorization($paymentEntity, $preAuthorizationContainer);
94
95
        return $preAuthorizationContainer;
96
    }
97
98
    /**
99
     * @param \Orm\Zed\Payone\Persistence\SpyPaymentPayone $paymentEntity
100
     * @param \SprykerEco\Zed\Payone\Business\Api\Request\Container\Authorization\AbstractAuthorizationContainer $authorizationContainer
101
     *
102
     * @return \SprykerEco\Zed\Payone\Business\Api\Request\Container\Authorization\AbstractAuthorizationContainer
103
     */
104
    protected function mapPaymentToAbstractAuthorization(
105
        SpyPaymentPayone $paymentEntity,
106
        AbstractAuthorizationContainer $authorizationContainer
107
    ): AbstractAuthorizationContainer {
108
        $paymentDetailEntity = $paymentEntity->getSpyPaymentPayoneDetail();
109
110
        $authorizationContainer->setAid($this->getStandardParameter()->getAid());
111
        $authorizationContainer->setClearingType(PayoneApiConstants::CLEARING_TYPE_CASH_ON_DELIVERY);
112
        $authorizationContainer->setReference($paymentEntity->getReference());
113
        $authorizationContainer->setAmount($paymentDetailEntity->getAmount());
114
        $authorizationContainer->setCurrency($this->getStandardParameter()->getCurrency());
115
        $authorizationContainer->setPaymentMethod($this->createPaymentMethodContainerFromPayment($paymentEntity));
116
117
        $personalContainer = new PersonalContainer();
118
        $this->mapBillingAddressToPersonalContainer($personalContainer, $paymentEntity);
119
        $personalContainer->setLanguage($this->getStandardParameter()->getLanguage());
120
121
        $authorizationContainer->setPersonalData($personalContainer);
122
123
        return $authorizationContainer;
124
    }
125
126
    /**
127
     * @param \Orm\Zed\Payone\Persistence\SpyPaymentPayone $paymentEntity
128
     *
129
     * @return \SprykerEco\Zed\Payone\Business\Api\Request\Container\DebitContainer
130
     */
131
    public function mapPaymentToDebit(SpyPaymentPayone $paymentEntity): DebitContainer
132
    {
133
        $debitContainer = new DebitContainer();
134
135
        $debitContainer->setTxid($paymentEntity->getTransactionId());
136
        $debitContainer->setSequenceNumber($this->getNextSequenceNumber($paymentEntity->getTransactionId()));
137
        $debitContainer->setCurrency($this->getStandardParameter()->getCurrency());
138
        $debitContainer->setAmount($paymentEntity->getSpyPaymentPayoneDetail()->getAmount());
139
140
        return $debitContainer;
141
    }
142
143
    /**
144
     * @param \Orm\Zed\Payone\Persistence\SpyPaymentPayone $paymentEntity
145
     *
146
     * @return \SprykerEco\Zed\Payone\Business\Api\Request\Container\RefundContainer
147
     */
148
    public function mapPaymentToRefund(SpyPaymentPayone $paymentEntity): RefundContainer
149
    {
150
        $refundContainer = new RefundContainer();
151
152
        $refundContainer->setTxid($paymentEntity->getTransactionId());
153
        $refundContainer->setSequenceNumber($this->getNextSequenceNumber($paymentEntity->getTransactionId()));
154
        $refundContainer->setCurrency($this->getStandardParameter()->getCurrency());
155
156
        $refundContainer->setBankcountry($paymentEntity->getSpyPaymentPayoneDetail()->getBankCountry());
157
        $refundContainer->setBankaccount($paymentEntity->getSpyPaymentPayoneDetail()->getBankAccount());
158
        $refundContainer->setBankcode($paymentEntity->getSpyPaymentPayoneDetail()->getBankCode());
159
        $refundContainer->setBankbranchcode($paymentEntity->getSpyPaymentPayoneDetail()->getBankBranchCode());
160
        $refundContainer->setBankcheckdigit($paymentEntity->getSpyPaymentPayoneDetail()->getBankCheckDigit());
161
        $refundContainer->setIban($paymentEntity->getSpyPaymentPayoneDetail()->getIban());
162
        $refundContainer->setBic($paymentEntity->getSpyPaymentPayoneDetail()->getBic());
163
164
        return $refundContainer;
165
    }
166
167
    /**
168
     * @param \Orm\Zed\Payone\Persistence\SpyPaymentPayone $paymentEntity
169
     *
170
     * @return \SprykerEco\Zed\Payone\Business\Api\Request\Container\Authorization\PaymentMethod\CashOnDeliveryContainer
171
     */
172
    protected function createPaymentMethodContainerFromPayment(SpyPaymentPayone $paymentEntity): CashOnDeliveryContainer
173
    {
174
        $shippingProviderName = $paymentEntity->getSpyPaymentPayoneDetail()->getShippingProvider();
175
176
        $translatedShippingProviderName = $shippingProviderName ? $this->translate($shippingProviderName) : $shippingProviderName;
177
178
        $paymentMethodContainer = new CashOnDeliveryContainer();
179
        $paymentMethodContainer->setShippingProvider($translatedShippingProviderName);
180
181
        return $paymentMethodContainer;
182
    }
183
184
    /**
185
     * @param \Generated\Shared\Transfer\PayoneAuthorizationTransfer $authorizationData
186
     *
187
     * @return \SprykerEco\Zed\Payone\Business\Api\Request\Container\Authorization\PersonalContainer
188
     */
189
    protected function createAuthorizationPersonalData(PayoneAuthorizationTransfer $authorizationData): PersonalContainer
190
    {
191
        $personalContainer = new PersonalContainer();
192
193
        $personalContainer->setFirstName($authorizationData->getOrder()->getFirstName());
194
        $personalContainer->setLastName($authorizationData->getOrder()->getLastName());
195
        $personalContainer->setCountry($this->storeConfig->getCurrentCountry());
196
197
        return $personalContainer;
198
    }
199
200
    /**
201
     * @param \Generated\Shared\Transfer\PayoneBankAccountCheckTransfer $bankAccountCheckTransfer
202
     *
203
     * @return \SprykerEco\Zed\Payone\Business\Api\Request\Container\BankAccountCheckContainer
204
     */
205
    public function mapBankAccountCheck(PayoneBankAccountCheckTransfer $bankAccountCheckTransfer): BankAccountCheckContainer
206
    {
207
        $bankAccountCheckContainer = new BankAccountCheckContainer();
208
209
        $bankAccountCheckContainer->setAid($this->getStandardParameter()->getAid());
210
        $bankAccountCheckContainer->setBankCountry($bankAccountCheckTransfer->getBankCountry());
211
        $bankAccountCheckContainer->setBankAccount($bankAccountCheckTransfer->getBankAccount());
212
        $bankAccountCheckContainer->setBankCode($bankAccountCheckTransfer->getBankCode());
213
        $bankAccountCheckContainer->setIban($bankAccountCheckTransfer->getIban());
214
        $bankAccountCheckContainer->setBic($bankAccountCheckTransfer->getBic());
215
        $bankAccountCheckContainer->setLanguage($this->getStandardParameter()->getLanguage());
216
217
        return $bankAccountCheckContainer;
218
    }
219
220
    /**
221
     * @param string $shippingProviderName
222
     *
223
     * @return string
224
     */
225
    protected function translate(string $glossaryKey): string
226
    {
227
        return $this->glossary->translate($glossaryKey, []);
228
    }
229
}
230