1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Shetabit\Multipay\Abstracts; |
4
|
|
|
|
5
|
|
|
use Shetabit\Multipay\Contracts\DriverInterface; |
6
|
|
|
use Shetabit\Multipay\Contracts\ReceiptInterface; |
7
|
|
|
use Shetabit\Multipay\Http\Client; |
8
|
|
|
use Shetabit\Multipay\Http\HttpAdapter; |
9
|
|
|
use Shetabit\Multipay\Invoice; |
10
|
|
|
use Shetabit\Multipay\RedirectionForm; |
11
|
|
|
|
12
|
|
|
abstract class Driver implements DriverInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Invoice |
16
|
|
|
* |
17
|
|
|
* @var Invoice |
18
|
|
|
*/ |
19
|
|
|
protected $invoice; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Driver's settings |
23
|
|
|
* |
24
|
|
|
* @var |
25
|
|
|
*/ |
26
|
|
|
protected $settings; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Http client |
30
|
|
|
* @var HttpAdapter |
31
|
|
|
*/ |
32
|
|
|
protected $client; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Driver constructor. |
36
|
|
|
* |
37
|
|
|
* @param Invoice $invoice |
38
|
|
|
* @param $settings |
39
|
|
|
*/ |
40
|
|
|
abstract public function __construct(Invoice $invoice, $settings); |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Set payment amount. |
44
|
|
|
* |
45
|
|
|
* @param $amount |
46
|
|
|
* |
47
|
|
|
* @return $this |
48
|
|
|
* |
49
|
|
|
* @throws \Exception |
50
|
|
|
*/ |
51
|
|
|
public function amount($amount) |
52
|
|
|
{ |
53
|
|
|
$this->invoice->amount($amount); |
54
|
|
|
|
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Set a piece of data to the details. |
60
|
|
|
* |
61
|
|
|
* @param $key |
62
|
|
|
* @param $value|null |
63
|
|
|
* |
64
|
|
|
* @return $this|DriverInterface |
65
|
|
|
*/ |
66
|
|
|
public function detail($key, $value = null) |
67
|
|
|
{ |
68
|
|
|
$key = is_array($key) ? $key : [$key => $value]; |
69
|
|
|
|
70
|
|
|
foreach ($key as $k => $v) { |
71
|
|
|
$this->invoice->detail($key, $value); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Set invoice. |
79
|
|
|
* |
80
|
|
|
* @param Invoice $invoice |
81
|
|
|
* |
82
|
|
|
* @return $this |
83
|
|
|
*/ |
84
|
|
|
public function invoice(Invoice $invoice) |
85
|
|
|
{ |
86
|
|
|
$this->invoice = $invoice; |
87
|
|
|
|
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Retrieve invoice. |
93
|
|
|
* |
94
|
|
|
* @return Invoice |
95
|
|
|
*/ |
96
|
|
|
public function getInvoice() |
97
|
|
|
{ |
98
|
|
|
return $this->invoice; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Create payment redirection form. |
103
|
|
|
* |
104
|
|
|
* @param $action |
105
|
|
|
* @param array $inputs |
106
|
|
|
* @param string $method |
107
|
|
|
* |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
|
|
public function redirectWithForm($action, array $inputs = [], $method = 'POST') : RedirectionForm |
111
|
|
|
{ |
112
|
|
|
return new RedirectionForm($action, $inputs, $method); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Set up Http client |
117
|
|
|
* @param string $baseUrl |
118
|
|
|
* @param HttpAdapter|null $httpAdapter |
119
|
|
|
*/ |
120
|
|
|
protected function setUpHttpClient(string $baseUrl, HttpAdapter $httpAdapter = null):void |
121
|
|
|
{ |
122
|
|
|
$adapter = ($httpAdapter ?? Client::class); |
123
|
|
|
$this->client = new $adapter($baseUrl, static::class); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Purchase the invoice |
128
|
|
|
* |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
|
|
abstract public function purchase(); |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Pay the invoice |
135
|
|
|
* |
136
|
|
|
* @return RedirectionForm |
137
|
|
|
*/ |
138
|
|
|
abstract public function pay() : RedirectionForm; |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Verify the payment |
142
|
|
|
* |
143
|
|
|
* @return ReceiptInterface |
144
|
|
|
*/ |
145
|
|
|
abstract public function verify() : ReceiptInterface; |
146
|
|
|
} |
147
|
|
|
|