-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.php
152 lines (131 loc) · 4.6 KB
/
Client.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
namespace Wearesho\Bobra\Platon;
use Wearesho\Bobra\Payments;
class Client implements Payments\ClientInterface
{
use ValidateLanguage;
protected ConfigInterface $config;
public function __construct(ConfigInterface $config)
{
$this->config = $config;
}
public function createPayment(
Payments\UrlPairInterface $pair,
Payments\TransactionInterface $transaction,
Payments\PayerDetailsInterface $payerDetails
): Payments\PaymentInterface {
$payment = $this->config->getPayment();
$ext = $this->transformInfoIntoExt($transaction->getInfo());
$language = $this->fetchLanguage($transaction);
$formId = $transaction instanceof TransactionInterface ? $transaction->getFormId() : $transaction->getType();
$cardToken = $transaction instanceof Transaction\CardToken ? $transaction->getCardToken() : null;
switch ($payment) {
case Payment\CC::TYPE:
$data = $this->getDataParam($transaction);
return new Payment\CC(
$transaction->getService(),
$language,
$pair,
$payerDetails,
$this->getSign(
$cardToken
? Payment\CC::TYPE_CARD_TOKEN
: Payment\CC::TYPE,
$data,
$pair->getGood(),
$cardToken
),
$this->config->getKey(),
$this->getUrl(),
$data,
$ext,
$formId,
$cardToken
);
case Payment\C2A::TYPE:
$amount = $this->fetchAmount($transaction);
return new Payment\C2A(
$transaction->getService(),
$language,
$pair,
$payerDetails,
$this->getSign(
$cardToken
? Payment\C2A::TYPE_CARD_TOKEN
: Payment\C2A::TYPE,
$amount,
$transaction->getCurrency(),
$transaction->getDescription(),
$pair->getGood()
),
$this->config->getKey(),
$this->getUrl(),
$amount,
$transaction->getDescription(),
$transaction->getCurrency(),
$ext,
$formId,
$cardToken
);
}
throw new \InvalidArgumentException("Payment type $payment is not supported");
}
protected function getUrl(): string
{
return \rtrim($this->config->getBaseUrl(), '/') . '/payment/auth';
}
protected function fetchLanguage(Payments\TransactionInterface $transaction): string
{
if (!$transaction instanceof Payments\HasLanguage) {
return $this->config->getLanguage();
}
$language = $transaction->getLanguage();
$this->validateLanguage($language);
return $language;
}
protected function fetchAmount(Payments\TransactionInterface $transaction): string
{
return \number_format(
(float)($transaction->getAmount() / 100),
2,
'.',
''
);
}
protected function transformInfoIntoExt(array $info): array
{
$maxExt = null;
$ext = [];
foreach ($info as $key => $value) {
if (\is_int($key)) {
$maxExt = \max($key, $maxExt);
} else {
$key = \is_null($maxExt) ? $maxExt = 1 : ++$maxExt;
}
$ext["ext{$key}"] = $value;
}
return $ext;
}
protected function getDataParam(Payments\TransactionInterface $transaction): string
{
return \base64_encode(\json_encode([
'amount' => $this->fetchAmount($transaction),
'name' => $transaction->getDescription(),
'currency' => $transaction->getCurrency(),
'recurring'
]));
}
/**
* Set 'sign' item for POST request
* @param array $params
* @return string
*/
protected function getSign(...$params): string
{
$middleHash = \implode(\array_map('strrev', $params));
$resultHash = \strrev($this->config->getKey())
. $middleHash
. \strrev($this->config->getPass());
return \md5(\strtoupper($resultHash));
}
}