-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathCardApi.php
More file actions
178 lines (165 loc) · 6.53 KB
/
CardApi.php
File metadata and controls
178 lines (165 loc) · 6.53 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
namespace Tpay\OriginApi;
use Tpay\OriginApi\Dictionaries\CardDictionary;
use Tpay\OriginApi\PaymentOptions\CardOptions;
use Tpay\OriginApi\Utilities\TException;
use Tpay\OriginApi\Utilities\Util;
use Tpay\OriginApi\Validators\PaymentTypes\PaymentTypeCard;
class CardApi extends CardOptions
{
/**
* Prepare for register sale @see $this->registerSale
*
* @param string $clientName client name
* @param string $clientEmail client email
* @param string $saleDescription sale description
*
* @return array
*/
public function registerSaleMethod(
$clientName,
$clientEmail,
$saleDescription
) {
$params = [CardDictionary::METHOD => $this->method];
if (!is_null($this->cardData)) {
$params['card'] = $this->cardData;
}
$params = array_merge($params, [
CardDictionary::NAME => $clientName,
CardDictionary::EMAIL => $clientEmail,
CardDictionary::DESC => $saleDescription,
CardDictionary::AMOUNT => $this->amount,
]);
$params[CardDictionary::CURRENCY] = $this->currency;
$optionalParams = [
'order_id' => !empty($this->orderID) ? $this->orderID : '',
'onetimer' => $this->oneTimer ?: '',
CardDictionary::LANGUAGE => $this->lang,
'enable_pow_url' => $this->enablePowUrl ? 1 : '',
];
if ('register_sale' === $this->method) {
unset($optionalParams['enable_pow_url']);
}
$params = array_merge($params, $optionalParams);
$params[CardDictionary::SIGN] = hash($this->cardHashAlg, implode('&', $params).'&'.$this->cardVerificationCode);
foreach ($optionalParams as $optionalParamKey => $optionalParamValue) {
if ('' === $optionalParamValue) {
unset($params[$optionalParamKey]);
}
}
$params[CardDictionary::APIPASS] = $this->cardApiPass;
$params = array_merge($params, $this->checkReturnUrls());
if (!is_null($this->moduleName)) {
$params['module'] = $this->moduleName;
}
$this->validateConfig(new PaymentTypeCard(), $params);
Util::log('Card request', print_r($params, true));
return $this->requests($this->cardsApiURL.$this->cardApiKey, $params);
}
/**
* Method used to create new sale for payment on demand.
* It can be called after receiving notification with cli_auth (see communication schema in register_sale method).
* It cannot be used if oneTimer option was sent in register_sale or client has unregistered
* (by link in email or by API).
*
* @param string $saleDescription sale description
*
* @throws TException
*
* @return bool|mixed
*/
public function presaleMethod($saleDescription)
{
$params = [
CardDictionary::AMOUNT => $this->amount,
CardDictionary::METHOD => CardDictionary::PRESALE,
CardDictionary::CLIAUTH => $this->clientAuthCode,
CardDictionary::DESC => $saleDescription,
CardDictionary::CURRENCY => $this->currency,
CardDictionary::LANGUAGE => $this->lang,
];
if (!empty($this->orderID)) {
$params[CardDictionary::ORDERID] = $this->orderID;
}
$hashParams = [
CardDictionary::PRESALE,
$this->clientAuthCode,
$saleDescription,
$this->amount,
$this->currency,
$this->orderID,
$this->lang,
$this->cardVerificationCode,
];
$params[CardDictionary::SIGN] = hash($this->cardHashAlg, implode('&', $hashParams));
$params[CardDictionary::APIPASS] = $this->cardApiPass;
Util::log(
'Pre sale params with hash ',
print_r($params, true).'req url '.$this->cardsApiURL.$this->cardApiKey
);
return $this->requests($this->cardsApiURL.$this->cardApiKey, $params);
}
/**
* Method used to execute created sale with presale method. Sale defined with sale_auth can be executed only once.
* If the method is called second time with the same parameters, system returns sale actual status - in parameter
* status - done for correct payment and declined for rejected payment.
* In that case, client card is not charged the second time.
*
* @param string $saleAuthCode sale auth code
*
* @throws TException
*
* @return bool|mixed
*/
public function saleMethod($saleAuthCode)
{
if (40 !== strlen($saleAuthCode)) {
throw new TException('invalid sale_auth code');
}
$params = [
CardDictionary::METHOD => CardDictionary::SALE,
CardDictionary::CLIAUTH => $this->clientAuthCode,
CardDictionary::SALEAUTH => $saleAuthCode,
];
$hashParams = [
CardDictionary::SALE,
$this->clientAuthCode,
$saleAuthCode,
$this->cardVerificationCode,
];
$params[CardDictionary::SIGN] = hash($this->cardHashAlg, implode('&', $hashParams));
$params[CardDictionary::APIPASS] = $this->cardApiPass;
Util::log('Sale request params', print_r($params, true));
return $this->requests($this->cardsApiURL.$this->cardApiKey, $params);
}
/**
* Method used to deregister client card data from system.
* Client can also do it himself from link in email after payment - if oneTimer was not set - in that case system
* will send notification. After successful deregistration Merchant can no more charge client's card
*
* @return array
*/
public function deregisterClient()
{
$params = [];
$params[CardDictionary::METHOD] = CardDictionary::DEREGISTER;
$params[CardDictionary::CLIAUTH] = $this->clientAuthCode;
$params[CardDictionary::LANGUAGE] = $this->lang;
$params[CardDictionary::SIGN] = hash($this->cardHashAlg, implode('&', $params).'&'.$this->cardVerificationCode);
$params[CardDictionary::APIPASS] = $this->cardApiPass;
return $this->requests($this->cardsApiURL.$this->cardApiKey, $params);
}
/** @return array<string, string> */
private function checkReturnUrls()
{
$params = [];
if (filter_var($this->powUrl, FILTER_VALIDATE_URL)) {
$params['pow_url'] = $this->powUrl;
}
if (filter_var($this->powUrlBlad, FILTER_VALIDATE_URL)) {
$params['pow_url_blad'] = $this->powUrlBlad;
}
return $params;
}
}