forked from larabook/gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GatewayResolver.php
152 lines (127 loc) · 3.46 KB
/
GatewayResolver.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 Larabookir\Gateway;
use Larabookir\Gateway\Parsian\Parsian;
use Larabookir\Gateway\Sadad\Sadad;
use Larabookir\Gateway\Mellat\Mellat;
use Larabookir\Gateway\Payline\Payline;
use Larabookir\Gateway\Pasargad\Pasargad;
use Larabookir\Gateway\Zarinpal\Zarinpal;
use Larabookir\Gateway\JahanPay\JahanPay;
use Larabookir\Gateway\Exceptions\RetryException;
use Larabookir\Gateway\Exceptions\PortNotFoundException;
use Larabookir\Gateway\Exceptions\InvalidRequestException;
use Larabookir\Gateway\Exceptions\NotFoundTransactionException;
use Illuminate\Support\Facades\DB;
class GatewayResolver
{
protected $request;
/**
* @var Config
*/
public $config;
/**
* Keep current port driver
*
* @var Mellat|Sadad|Zarinpal|Payline|JahanPay|Parsian
*/
protected $port;
/**
* Gateway constructor.
* @param null $config
* @param null $port
*/
public function __construct($config = null, $port = null)
{
$this->config = app('config');
$this->request = app('request');
if ($this->config->has('gateway.timezone'))
date_default_timezone_set($this->config->get('gateway.timezone'));
if (!is_null($port)) $this->make($port);
}
/**
* Get supported ports
*
* @return array
*/
public function getSupportedPorts()
{
return [Enum::MELLAT, Enum::SADAD, Enum::ZARINPAL, Enum::PAYLINE, Enum::JAHANPAY, Enum::PARSIAN, Enum::PASARGAD];
}
/**
* Call methods of current driver
*
* @return mixed
*/
public function __call($name, $arguments)
{
// calling by this way ( Gateway::mellat()->.. , Gateway::parsian()->.. )
if(in_array(strtoupper($name),$this->getSupportedPorts())){
return $this->make($name);
}
return call_user_func_array([$this->port, $name], $arguments);
}
/**
* Gets query builder from you transactions table
* @return mixed
*/
function getTable()
{
return DB::table($this->config->get('gateway.table'));
}
/**
* Callback
*
* @return $this->port
*
* @throws InvalidRequestException
* @throws NotFoundTransactionException
* @throws PortNotFoundException
* @throws RetryException
*/
public function verify()
{
if (!$this->request->has('transaction_id'))
throw new InvalidRequestException;
$id = intval($this->request->get('transaction_id'));
$transaction = $this->getTable()->whereId($id)->first();
if (!$transaction)
throw new NotFoundTransactionException;
if (in_array($transaction->status, [Enum::TRANSACTION_SUCCEED, Enum::TRANSACTION_FAILED]))
throw new RetryException;
$this->make($transaction->port);
return $this->port->verify($transaction);
}
/**
* Create new object from port class
*
* @param int $port
* @throws PortNotFoundException
*/
function make($port)
{
if ($port InstanceOf Mellat) {
$name = Enum::MELLAT;
} elseif ($port InstanceOf Parsian) {
$name = Enum::PARSIAN;
} elseif ($port InstanceOf Payline) {
$name = Enum::PAYLINE;
} elseif ($port InstanceOf Zarinpal) {
$name = Enum::ZARINPAL;
} elseif ($port InstanceOf JahanPay) {
$name = Enum::JAHANPAY;
} elseif ($port InstanceOf SADAD) {
$name = Enum::SADAD;
} elseif(in_array(strtoupper($port),$this->getSupportedPorts())){
$port=ucfirst(strtolower($port));
$name=strtoupper($port);
$class=__NAMESPACE__.'\\'.$port.'\\'.$port;
$port=new $class;
} else
throw new PortNotFoundException;
$this->port = $port;
$this->port->setConfig($this->config); // injects config
$this->port->setPortName($name); // injects config
$this->port->boot();
return $this;
}
}