1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Apache OSL-2 |
5
|
|
|
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace SprykerEco\Zed\AmazonPay\Business\Api\Adapter; |
9
|
|
|
|
10
|
|
|
use AmazonPay\ClientInterface; |
11
|
|
|
use Generated\Shared\Transfer\AmazonpayCallTransfer; |
12
|
|
|
use Generated\Shared\Transfer\AmazonpayPaymentTransfer; |
13
|
|
|
use SprykerEco\Shared\AmazonPay\AmazonPayConfigInterface; |
14
|
|
|
use SprykerEco\Zed\AmazonPay\Business\Api\Converter\ResponseParserConverterInterface; |
15
|
|
|
use SprykerEco\Zed\AmazonPay\Dependency\Facade\AmazonPayToMoneyInterface; |
16
|
|
|
|
17
|
|
|
class AuthorizeAdapter extends AbstractAdapter |
18
|
|
|
{ |
19
|
|
|
public const AUTHORIZATION_AMOUNT = 'authorization_amount'; |
20
|
|
|
public const AUTHORIZATION_REFERENCE_ID = 'authorization_reference_id'; |
21
|
|
|
public const TRANSACTION_TIMEOUT = 'transaction_timeout'; |
22
|
|
|
public const CAPTURE_NOW = 'capture_now'; |
23
|
|
|
|
24
|
|
|
protected const REAUTHORIZING_ASYNC_TRANSACTION_TIMEOUT = 1440; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var bool |
28
|
|
|
*/ |
29
|
|
|
protected $captureNow; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var int |
33
|
|
|
*/ |
34
|
|
|
protected $transactionTimeout; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param \AmazonPay\ClientInterface $client |
38
|
|
|
* @param \SprykerEco\Zed\AmazonPay\Business\Api\Converter\ResponseParserConverterInterface $converter |
39
|
|
|
* @param \SprykerEco\Zed\AmazonPay\Dependency\Facade\AmazonPayToMoneyInterface $moneyFacade |
40
|
|
|
* @param \SprykerEco\Shared\AmazonPay\AmazonPayConfigInterface $config |
41
|
|
|
* @param bool|null $captureNow |
42
|
|
|
*/ |
43
|
|
|
public function __construct( |
44
|
|
|
ClientInterface $client, |
45
|
|
|
ResponseParserConverterInterface $converter, |
46
|
|
|
AmazonPayToMoneyInterface $moneyFacade, |
47
|
|
|
AmazonPayConfigInterface $config, |
48
|
|
|
$captureNow = null |
49
|
|
|
) { |
50
|
|
|
parent::__construct($client, $converter, $moneyFacade); |
51
|
|
|
|
52
|
|
|
$this->captureNow = $this->getCaptureNow($config, $captureNow); |
53
|
|
|
$this->transactionTimeout = $config->getAuthTransactionTimeout(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param \Generated\Shared\Transfer\AmazonpayCallTransfer $amazonpayCallTransfer |
58
|
|
|
* |
59
|
|
|
* @return \Generated\Shared\Transfer\AmazonpayResponseTransfer |
60
|
|
|
*/ |
61
|
|
|
public function call(AmazonpayCallTransfer $amazonpayCallTransfer) |
62
|
|
|
{ |
63
|
|
|
$result = $this->client->authorize( |
64
|
|
|
$this->getRequestArray($amazonpayCallTransfer->getAmazonpayPayment(), $this->getAmount($amazonpayCallTransfer)) |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
return $this->converter->convert($result); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param \SprykerEco\Shared\AmazonPay\AmazonPayConfigInterface $config |
72
|
|
|
* @param bool|null $captureNow |
73
|
|
|
* |
74
|
|
|
* @return bool |
75
|
|
|
*/ |
76
|
|
|
protected function getCaptureNow(AmazonPayConfigInterface $config, $captureNow = null) |
77
|
|
|
{ |
78
|
|
|
return $captureNow ?? $config->getCaptureNow(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param \Generated\Shared\Transfer\AmazonpayPaymentTransfer $amazonpayPaymentTransfer |
83
|
|
|
* @param float $amount |
84
|
|
|
* |
85
|
|
|
* @return array |
86
|
|
|
*/ |
87
|
|
|
protected function getRequestArray(AmazonpayPaymentTransfer $amazonpayPaymentTransfer, $amount): array |
88
|
|
|
{ |
89
|
|
|
$authorizationReferenceId = $amazonpayPaymentTransfer |
90
|
|
|
->getAuthorizationDetails() |
91
|
|
|
->getAuthorizationReferenceId(); |
92
|
|
|
$transactionTimeout = $amazonpayPaymentTransfer->getIsReauthorizingAsync() |
93
|
|
|
? static::REAUTHORIZING_ASYNC_TRANSACTION_TIMEOUT |
94
|
|
|
: $this->transactionTimeout; |
95
|
|
|
|
96
|
|
|
return [ |
97
|
|
|
static::AMAZON_ORDER_REFERENCE_ID => $amazonpayPaymentTransfer->getOrderReferenceId(), |
98
|
|
|
static::AUTHORIZATION_AMOUNT => $amount, |
99
|
|
|
static::AUTHORIZATION_REFERENCE_ID => $authorizationReferenceId, |
100
|
|
|
static::TRANSACTION_TIMEOUT => $transactionTimeout, |
101
|
|
|
static::CAPTURE_NOW => $this->captureNow, |
102
|
|
|
]; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|