Passed
Pull Request — master (#84)
by Olha
06:07
created

KlarnaPaymentMapper::getCurrentCountry()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 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\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...
11
use Generated\Shared\Transfer\PayoneKlarnaStartSessionRequestTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...tSessionRequestTransfer 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 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...
14
use SprykerEco\Shared\Payone\PayoneApiConstants;
15
use SprykerEco\Zed\Payone\Business\Api\Request\Container\Authorization\AbstractAuthorizationContainer;
16
use SprykerEco\Zed\Payone\Business\Api\Request\Container\Authorization\PersonalContainer;
17
use SprykerEco\Zed\Payone\Business\Api\Request\Container\Authorization\ShippingContainer;
18
use SprykerEco\Zed\Payone\Business\Api\Request\Container\AuthorizationContainer;
19
use SprykerEco\Zed\Payone\Business\Api\Request\Container\CaptureContainer;
20
use SprykerEco\Zed\Payone\Business\Api\Request\Container\CaptureContainerInterface;
21
use SprykerEco\Zed\Payone\Business\Api\Request\Container\ContainerInterface;
22
use SprykerEco\Zed\Payone\Business\Api\Request\Container\DebitContainer;
23
use SprykerEco\Zed\Payone\Business\Api\Request\Container\DebitContainerInterface;
24
use SprykerEco\Zed\Payone\Business\Api\Request\Container\GenericPayment\PaydataContainer;
25
use SprykerEco\Zed\Payone\Business\Api\Request\Container\KlarnaGenericPaymentContainer;
26
use SprykerEco\Zed\Payone\Business\Api\Request\Container\PreAuthorizationContainer;
27
use SprykerEco\Zed\Payone\Business\Api\Request\Container\PreAuthorizationContainerInterface;
28
use SprykerEco\Zed\Payone\Business\Api\Request\Container\RefundContainer;
29
use SprykerEco\Zed\Payone\Business\Api\Request\Container\RefundContainerInterface;
30
use SprykerEco\Zed\Payone\Dependency\Facade\PayoneToStoreFacadeInterface;
31
use Symfony\Component\HttpFoundation\RequestStack;
32
33
class KlarnaPaymentMapper extends AbstractMapper implements KlarnaPaymentMapperInterface
34
{
35
    protected const STREET_ADDRESS_SEPARATOR = ' ';
36
37
    /**
38
     * @var \Symfony\Component\HttpFoundation\RequestStack
39
     */
40
    protected $requestStack;
41
42
    /**
43
     * @var \SprykerEco\Zed\Payone\Dependency\Facade\PayoneToStoreFacadeInterface
44
     */
45
    protected $storeFacade;
46
47
    /**
48
     * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
49
     * @param \SprykerEco\Zed\Payone\Dependency\Facade\PayoneToStoreFacadeInterface $storeFacade
50
     */
51
    public function __construct(
52
        PayoneToStoreFacadeInterface $storeFacade,
53
        RequestStack $requestStack
54
    ) {
55
        $this->storeFacade = $storeFacade;
56
        $this->requestStack = $requestStack;
57
    }
58
59
    /**
60
     * @return string
61
     */
62
    public function getName(): string
63
    {
64
        return PayoneApiConstants::PAYMENT_METHOD_KLARNA;
65
    }
66
67
    /**
68
     * @param \Orm\Zed\Payone\Persistence\SpyPaymentPayone $paymentEntity
69
     *
70
     * @return \SprykerEco\Zed\Payone\Business\Api\Request\Container\PreAuthorizationContainerInterface
71
     */
72
    public function mapPaymentToPreAuthorization(SpyPaymentPayone $paymentEntity): PreAuthorizationContainerInterface
73
    {
74
        $preAuthorizationContainer = new PreAuthorizationContainer();
75
76
        return $this->mapPaymentToAbstractAuthorization($paymentEntity, $preAuthorizationContainer);
77
    }
78
79
    /**
80
     * @param \Orm\Zed\Payone\Persistence\SpyPaymentPayone $paymentEntity
81
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
82
     *
83
     * @return \SprykerEco\Zed\Payone\Business\Api\Request\Container\PreAuthorizationContainerInterface
84
     */
85
    public function mapKlarnaPaymentToPreAuthorization(SpyPaymentPayone $paymentEntity, QuoteTransfer $quoteTransfer): PreAuthorizationContainerInterface
86
    {
87
        $preAuthorizationContainer = new PreAuthorizationContainer();
88
        $this->mapKlarnaPaymentToAbstractAuthorization($paymentEntity, $quoteTransfer, $preAuthorizationContainer);
89
90
        return $preAuthorizationContainer;
91
    }
92
93
    /**
94
     * @param \Orm\Zed\Payone\Persistence\SpyPaymentPayone $paymentEntity
95
     * @param \Generated\Shared\Transfer\OrderTransfer $orderTransfer
96
     *
97
     * @return \SprykerEco\Zed\Payone\Business\Api\Request\Container\AuthorizationContainer
98
     */
99
    public function mapPaymentToAuthorization(SpyPaymentPayone $paymentEntity, OrderTransfer $orderTransfer): AbstractAuthorizationContainer
100
    {
101
        return new AuthorizationContainer();
102
    }
103
104
    /**
105
     * @param \Orm\Zed\Payone\Persistence\SpyPaymentPayone $paymentEntity
106
     *
107
     * @return \SprykerEco\Zed\Payone\Business\Api\Request\Container\CaptureContainerInterface
108
     */
109
    public function mapPaymentToCapture(SpyPaymentPayone $paymentEntity): CaptureContainerInterface
110
    {
111
        $paymentDetailEntity = $paymentEntity->getSpyPaymentPayoneDetail();
112
        $captureContainer = new CaptureContainer();
113
114
        $captureContainer->setAmount($paymentDetailEntity->getAmount());
115
        $captureContainer->setCurrency($this->getStandardParameter()->getCurrency());
116
        $captureContainer->setTxid($paymentEntity->getTransactionId());
117
        $captureContainer->setCaptureMode(PayoneApiConstants::CAPTURE_MODE_NOTCOMPLETED);
118
        $sequenceNumber = $this->getNextSequenceNumber($paymentEntity->getTransactionId());
119
        $captureContainer->setSequenceNumber($sequenceNumber);
120
121
        return $captureContainer;
122
    }
123
124
    /**
125
     * @param \Orm\Zed\Payone\Persistence\SpyPaymentPayone $paymentEntity
126
     *
127
     * @return \SprykerEco\Zed\Payone\Business\Api\Request\Container\DebitContainerInterface
128
     */
129
    public function mapPaymentToDebit(SpyPaymentPayone $paymentEntity): DebitContainerInterface
130
    {
131
        $debitContainer = new DebitContainer();
132
133
        $debitContainer->setTxid($paymentEntity->getTransactionId());
134
        $debitContainer->setSequenceNumber($this->getNextSequenceNumber($paymentEntity->getTransactionId()));
135
        $debitContainer->setCurrency($this->getStandardParameter()->getCurrency());
136
        $debitContainer->setAmount($paymentEntity->getSpyPaymentPayoneDetail()->getAmount());
137
138
        return $debitContainer;
139
    }
140
141
    /**
142
     * @param \Orm\Zed\Payone\Persistence\SpyPaymentPayone $paymentEntity
143
     *
144
     * @return \SprykerEco\Zed\Payone\Business\Api\Request\Container\RefundContainerInterface
145
     */
146
    public function mapPaymentToRefund(SpyPaymentPayone $paymentEntity): RefundContainerInterface
147
    {
148
        $refundContainer = new RefundContainer();
149
150
        $refundContainer->setTxid($paymentEntity->getTransactionId());
151
        $refundContainer->setSequenceNumber($this->getNextSequenceNumber($paymentEntity->getTransactionId()));
152
        $refundContainer->setCurrency($this->getStandardParameter()->getCurrency());
153
154
        return $refundContainer;
155
    }
156
157
    /**
158
     * @param \Generated\Shared\Transfer\PayoneKlarnaStartSessionRequestTransfer $payoneKlarnaStartSessionRequestTransfer
159
     *
160
     * @return \SprykerEco\Zed\Payone\Business\Api\Request\Container\KlarnaGenericPaymentContainer
161
     */
162
    public function mapPaymentToKlarnaGenericPaymentContainer(
163
        PayoneKlarnaStartSessionRequestTransfer $payoneKlarnaStartSessionRequestTransfer
164
    ): ContainerInterface {
165
        $quoteTransfer = $payoneKlarnaStartSessionRequestTransfer->getQuote();
166
167
        $klarnaGenericPaymentContainer = new KlarnaGenericPaymentContainer();
168
169
        $klarnaGenericPaymentContainer->setAid($this->getStandardParameter()->getAid());
170
        $klarnaGenericPaymentContainer->setAmount($quoteTransfer->getTotals()->getGrandTotal());
171
        $klarnaGenericPaymentContainer->setCurrency($quoteTransfer->getCurrency()->getCode());
172
        $klarnaGenericPaymentContainer->setClearingType(PayoneApiConstants::CLEARING_TYPE_FINANCING);
173
        $klarnaGenericPaymentContainer->setFinancingType($payoneKlarnaStartSessionRequestTransfer->getPayMethod());
174
175
        $paydataContainer = new PaydataContainer();
176
        $paydataContainer->setAction(PayoneApiConstants::PAYMENT_KLARNA_START_SESSION_ACTION);
177
        $klarnaGenericPaymentContainer->setPaydata($paydataContainer);
178
179
        $personalContainer = $this->createPersonalContainerFromQuoteTransfer($quoteTransfer);
180
        $klarnaGenericPaymentContainer->setPersonalData($personalContainer);
181
182
        return $klarnaGenericPaymentContainer;
183
    }
184
185
    /**
186
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
187
     *
188
     * @return \SprykerEco\Zed\Payone\Business\Api\Request\Container\Authorization\PersonalContainer
189
     */
190
    protected function createPersonalContainerFromQuoteTransfer(QuoteTransfer $quoteTransfer): ContainerInterface
191
    {
192
        $personalContainer = new PersonalContainer();
193
        $billingAddress = $quoteTransfer->getBillingAddress();
194
        $personalContainer->setEmail($billingAddress->getEmail());
195
        $personalContainer->setCity($billingAddress->getCity());
196
197
        $personalContainer->setCountry($this->getCurrentCountry());
198
        $personalContainer->setFirstName($billingAddress->getFirstName());
199
        $personalContainer->setLastName($billingAddress->getLastName());
200
        $personalContainer->setSalutation($billingAddress->getSalutation());
201
202
        $personalContainer->setTelephoneNumber($billingAddress->getPhone());
203
        $personalContainer->setLanguage($this->getStandardParameter()->getLanguage());
204
205
        return $personalContainer;
206
    }
207
208
    /**
209
     * @param \Orm\Zed\Payone\Persistence\SpyPaymentPayone $paymentEntity
210
     * @param \SprykerEco\Zed\Payone\Business\Api\Request\Container\PreAuthorizationContainer $authorizationContainer
211
     *
212
     * @return \SprykerEco\Zed\Payone\Business\Api\Request\Container\PreAuthorizationContainer
213
     */
214
    protected function mapPaymentToAbstractAuthorization(
215
        SpyPaymentPayone $paymentEntity,
216
        AbstractAuthorizationContainer $authorizationContainer
217
    ): AbstractAuthorizationContainer {
218
        $authorizationContainer->setAid($this->getStandardParameter()->getAid());
219
220
        $paymentDetailEntity = $paymentEntity->getSpyPaymentPayoneDetail();
221
222
        $authorizationContainer->setAmount($paymentDetailEntity->getAmount());
223
        $authorizationContainer->setCurrency($this->getStandardParameter()->getCurrency());
224
        $authorizationContainer->setClearingType(PayoneApiConstants::CLEARING_TYPE_FINANCING);
225
226
        $authorizationContainer->setReference($paymentEntity->getReference());
227
228
        $paydataContainer = new PaydataContainer();
229
        $authorizationContainer->setPaydata($paydataContainer);
0 ignored issues
show
Bug introduced by
The method setPaydata() does not exist on SprykerEco\Zed\Payone\Bu...tAuthorizationContainer. It seems like you code against a sub-type of SprykerEco\Zed\Payone\Bu...tAuthorizationContainer such as SprykerEco\Zed\Payone\Bu...eAuthorizationContainer. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

229
        $authorizationContainer->/** @scrutinizer ignore-call */ 
230
                                 setPaydata($paydataContainer);
Loading history...
230
231
        $personalContainer = $this->buildPersonalContainer($paymentEntity);
232
        $authorizationContainer->setPersonalData($personalContainer);
233
        $orderReference = $paymentEntity->getSpySalesOrder()->getOrderReference();
234
        $authorizationContainer->setRedirect($this->createRedirectContainer($orderReference));
0 ignored issues
show
Bug introduced by
The method setRedirect() does not exist on SprykerEco\Zed\Payone\Bu...tAuthorizationContainer. It seems like you code against a sub-type of SprykerEco\Zed\Payone\Bu...tAuthorizationContainer such as SprykerEco\Zed\Payone\Bu...eAuthorizationContainer. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

234
        $authorizationContainer->/** @scrutinizer ignore-call */ 
235
                                 setRedirect($this->createRedirectContainer($orderReference));
Loading history...
235
236
        $shippingAddressEntity = $paymentEntity->getSpySalesOrder()->getShippingAddress();
237
238
        if ($shippingAddressEntity) {
239
            $shippingContainer = new ShippingContainer();
240
            $this->mapShippingAddressToShippingContainer($shippingContainer, $shippingAddressEntity);
241
        }
242
243
        return $authorizationContainer;
244
    }
245
246
    /**
247
     * @param \Orm\Zed\Payone\Persistence\SpyPaymentPayone $paymentEntity
248
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
249
     * @param \SprykerEco\Zed\Payone\Business\Api\Request\Container\PreAuthorizationContainerInterface $authorizationContainer
250
     *
251
     * @return \SprykerEco\Zed\Payone\Business\Api\Request\Container\PreAuthorizationContainerInterface
252
     */
253
    protected function mapKlarnaPaymentToAbstractAuthorization(
254
        SpyPaymentPayone $paymentEntity,
255
        QuoteTransfer $quoteTransfer,
256
        PreAuthorizationContainerInterface $authorizationContainer
257
    ): PreAuthorizationContainerInterface {
258
        $authorizationContainer->setAid($this->getStandardParameter()->getAid());
259
260
        $paymentDetailEntity = $paymentEntity->getSpyPaymentPayoneDetail();
261
        $payoneKlarnaTransfer = $quoteTransfer->getPayment()->getPayoneKlarna();
262
263
        $authorizationContainer->setAmount($paymentDetailEntity->getAmount());
264
        $authorizationContainer->setCurrency($this->getStandardParameter()->getCurrency());
265
        $authorizationContainer->setClearingType(PayoneApiConstants::CLEARING_TYPE_FINANCING);
266
        $authorizationContainer->setFinancingType($payoneKlarnaTransfer->getPayMethod());
267
268
        $authorizationContainer->setReference($paymentEntity->getReference());
269
270
        $paydataContainer = new PaydataContainer();
271
        $paydataContainer->setAuthorizationToken($payoneKlarnaTransfer->getPayMethodToken());
272
        $authorizationContainer->setPaydata($paydataContainer);
273
274
        $personalContainer = $this->buildPersonalContainer($paymentEntity);
275
        $authorizationContainer->setPersonalData($personalContainer);
276
        $orderReference = $paymentEntity->getSpySalesOrder()->getOrderReference();
277
        $authorizationContainer->setRedirect($this->createRedirectContainer($orderReference));
278
279
        $shippingAddressEntity = $paymentEntity->getSpySalesOrder()->getShippingAddress();
280
281
        if ($shippingAddressEntity) {
282
            $shippingContainer = new ShippingContainer();
283
            $this->mapShippingAddressToShippingContainer($shippingContainer, $shippingAddressEntity);
284
        }
285
286
        return $authorizationContainer;
287
    }
288
289
    /**
290
     * @param \Orm\Zed\Payone\Persistence\SpyPaymentPayone $paymentEntity
291
     *
292
     * @return \SprykerEco\Zed\Payone\Business\Api\Request\Container\Authorization\PersonalContainer
293
     */
294
    protected function buildPersonalContainer(SpyPaymentPayone $paymentEntity): ContainerInterface
295
    {
296
        $personalContainer = new PersonalContainer();
297
298
        $this->mapBillingAddressToPersonalContainer($personalContainer, $paymentEntity);
299
        $personalContainer->setCompany(null);
300
        $personalContainer->setEmail($paymentEntity->getSpySalesOrder()->getEmail());
301
302
        $currentRequest = $this->requestStack->getCurrentRequest();
303
        $personalContainer->setIp($currentRequest->getClientIp());
304
305
        return $personalContainer;
306
    }
307
308
    /**
309
     * @return string
310
     */
311
    protected function getCurrentCountry(): string
312
    {
313
        return current($this->storeFacade->getCurrentStore()->getCountries());
314
    }
315
}
316