Completed
Push — master ( 9db63e...40913a )
by Oleksandr
24s queued 13s
created

KlarnaPaymentMapper::mapPaymentToCapture()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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

219
        $authorizationContainer->/** @scrutinizer ignore-call */ 
220
                                 setFinancingType($paymentDetailEntity->getPayMethod());
Loading history...
220
221
        $authorizationContainer->setReference($paymentEntity->getReference());
222
223
        $paydataContainer = new PaydataContainer();
224
        $paydataContainer->setAuthorizationToken($paymentDetailEntity->getToken());
225
        $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

225
        $authorizationContainer->/** @scrutinizer ignore-call */ 
226
                                 setPaydata($paydataContainer);
Loading history...
226
227
        $personalContainer = $this->buildPersonalContainer($paymentEntity);
228
        $authorizationContainer->setPersonalData($personalContainer);
229
        $orderReference = $paymentEntity->getSpySalesOrder()->getOrderReference();
230
        $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

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