forked from PoolPort/PoolPort
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPoolPort.php
299 lines (231 loc) · 7.83 KB
/
PoolPort.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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<?php
namespace PoolPort;
use PoolPort\AP\AP;
use PoolPort\Pay\Pay;
use PoolPort\PNA\PNA;
use PoolPort\Saman\Saman;
use PoolPort\Sadad\Sadad;
use PoolPort\JiBit\JiBit;
use PoolPort\IDPay\IDPay;
use PoolPort\BitPay\BitPay;
use PoolPort\Mellat\Mellat;
use PoolPort\Vandar\Vandar;
use PoolPort\PayPing\PayPing;
use PoolPort\Saderat\Saderat;
use PoolPort\Payline\Payline;
use PoolPort\Parsian\Parsian;
use PoolPort\Pasargad\Pasargad;
use PoolPort\Zarinpal\Zarinpal;
use PoolPort\JahanPay\JahanPay;
use PoolPort\IranKish\IranKish;
use PoolPort\Exceptions\RetryException;
use PoolPort\PortSimulator\PortSimulator;
use PoolPort\Exceptions\PortNotFoundException;
use PoolPort\Exceptions\InvalidRequestException;
use PoolPort\Exceptions\NotFoundTransactionException;
class PoolPort
{
const P_MELLAT = 1;
const P_SADAD = 2;
const P_ZARINPAL = 3;
const P_PAYLINE = 4;
const P_JAHANPAY = 5;
const P_PARSIAN = 6;
const P_PASARGAD = 7;
const P_SADERAT = 8;
const P_IRANKISH = 9;
const P_SIMULATOR = 10;
const P_SAMAN = 11;
const P_PAY = 12;
const P_JIBIT = 13;
const P_AP = 14;
const P_BITPAY = 15;
const P_IDPAY = 16;
const P_PAYPING = 17;
const P_VANDAR = 18;
const P_PNA = 19;
/**
* @var Config
*/
public $config;
/**
* @var DataBaseManager
*/
protected $db;
/**
* Keep current port driver
*
* @var PortAbstract
*/
protected $portClass;
/**
* Path of config file
*
* @var null|string
*/
private $configFilePath = null;
/**
* @param null|string $port
* @param null|string $configFile
*/
public function __construct($port = null, $configFile = null)
{
$this->configFilePath = $configFile;
$this->config = new Config($this->configFilePath);
$this->db = new DataBaseManager($this->config);
if (!empty($this->config->get('timezone')))
date_default_timezone_set($this->config->get('timezone'));
if (!is_null($port)) $this->buildPort($port);
}
/**
* Get supported ports
*
* @return array
*/
public function getSupportedPorts()
{
return array(self::P_MELLAT, self::P_SADAD, self::P_ZARINPAL,
self::P_PAYLINE, self::P_JAHANPAY, self::P_PARSIAN, self::P_PASARGAD,
self::P_SADERAT, self::P_IRANKISH, self::P_SIMULATOR, self::P_SAMAN,
self::P_PAY, self::P_JIBIT, self::P_AP, self::P_BITPAY, self::P_IDPAY,
self::P_PAYPING, self::P_VANDAR, self::P_PNA);
}
/**
* Call methods of current driver
*
* @return mixed
*/
public function __call($name, $arguments)
{
return call_user_func_array([$this->portClass, $name], $arguments);
}
/**
* Verify transaction, use verifyLock for security reason
*
* @return $this->portClass
*
* @throws InvalidRequestException
* @throws NotFoundTransactionException
* @throws PortNotFoundException
* @throws RetryException
*/
public function verify()
{
if (!isset($_GET['u']))
throw new InvalidRequestException;
$uniqeId = $_GET['u'];
$transaction = $this->db->find($uniqeId);
if (!$transaction)
throw new NotFoundTransactionException;
if (!PortAbstract::checkVerifyKey($transaction))
throw new InvalidRequestException;
if ($transaction->status == PortAbstract::TRANSACTION_SUCCEED || $transaction->status == PortAbstract::TRANSACTION_FAILED)
throw new RetryException;
$this->buildPort($transaction->port_id);
return $this->portClass->verify($transaction);
}
/**
* Verify transaction, preventing duplicate request at the same time
*
* @return $this->portClass
*
* @throws InvalidRequestException
* @throws NotFoundTransactionException
* @throws PortNotFoundException
* @throws RetryException
*/
public function verifyLock()
{
if (!isset($_GET['u']))
throw new InvalidRequestException;
$uniqeId = $_GET['u'];
try {
$this->db->beginTransaction();
$transaction = $this->db->find($uniqeId, true);
if (!$transaction)
throw new NotFoundTransactionException;
if (!PortAbstract::checkVerifyKey($transaction))
throw new InvalidRequestException;
if ($transaction->status != PortAbstract::TRANSACTION_INIT)
throw new RetryException;
$this->buildPort($transaction->port_id);
$this->portClass->transactionPending($transaction->id);
$this->db->commit();
} catch(\Exception $e) {
$this->db->rollBack();
throw $e;
}
return $this->portClass->verify($transaction);
}
/**
* Create new object from port class
*
* @param int $port
* @throws PortNotFoundException
*/
protected function buildPort($port)
{
switch ($port) {
case self::P_MELLAT:
$this->portClass = new Mellat($this->config, $this->db, self::P_MELLAT);
break;
case self::P_SADAD:
$this->portClass = new Sadad($this->config, $this->db, self::P_SADAD);
break;
case self::P_ZARINPAL:
$this->portClass = new Zarinpal($this->config, $this->db, self::P_ZARINPAL);
break;
case self::P_PAYLINE:
$this->portClass = new Payline($this->config, $this->db, self::P_PAYLINE);
break;
case self::P_JAHANPAY:
$this->portClass = new JahanPay($this->config, $this->db, self::P_JAHANPAY);
break;
case self::P_PARSIAN:
$this->portClass = new Parsian($this->config, $this->db, self::P_PARSIAN);
break;
case self::P_PASARGAD:
$this->portClass = new Pasargad($this->config, $this->db, self::P_PASARGAD);
break;
case self::P_SADERAT:
$this->portClass = new Saderat($this->config, $this->db, self::P_SADERAT);
break;
case self::P_IRANKISH;
$this->portClass = new IranKish($this->config, $this->db, self::P_IRANKISH);
break;
case self::P_SIMULATOR;
$this->portClass = new PortSimulator($this->config, $this->db, self::P_SIMULATOR);
break;
case self::P_SAMAN;
$this->portClass = new Saman($this->config, $this->db, self::P_SAMAN);
break;
case self::P_PAY:
$this->portClass = new Pay($this->config, $this->db, self::P_PAY);
break;
case self::P_JIBIT:
$this->portClass = new JiBit($this->config, $this->db, self::P_JIBIT);
break;
case self::P_AP:
$this->portClass = new AP($this->config, $this->db, self::P_AP);
break;
case self::P_BITPAY:
$this->portClass = new BitPay($this->config, $this->db, self::P_BITPAY);
break;
case self::P_IDPAY:
$this->portClass = new IDPAY($this->config, $this->db, self::P_IDPAY);
break;
case self::P_PAYPING:
$this->portClass = new PayPing($this->config, $this->db, self::P_PAYPING);
break;
case self::P_VANDAR:
$this->portClass = new Vandar($this->config, $this->db, self::P_VANDAR);
break;
case self::P_PNA:
$this->portClass = new PNA($this->config, $this->db, self::P_PNA);
break;
default:
throw new PortNotFoundException;
break;
}
}
}