1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GloBee\PaymentApi\Models; |
4
|
|
|
|
5
|
|
|
use GloBee\PaymentApi\Exceptions\Validation\ValidationException; |
6
|
|
|
|
7
|
|
|
class PaymentRequest extends Model |
8
|
|
|
{ |
9
|
|
|
protected $total; |
|
|
|
|
10
|
|
|
protected $currency; |
|
|
|
|
11
|
|
|
protected $customPaymentId; |
|
|
|
|
12
|
|
|
protected $callbackData; |
|
|
|
|
13
|
|
|
protected $customerName; |
|
|
|
|
14
|
|
|
protected $customerEmail; |
|
|
|
|
15
|
|
|
protected $successUrl; |
|
|
|
|
16
|
|
|
protected $cancelUrl; |
|
|
|
|
17
|
|
|
protected $ipnUrl; |
|
|
|
|
18
|
|
|
protected $notificationEmail; |
|
|
|
|
19
|
|
|
protected $confirmationSpeed = 'medium'; |
|
|
|
|
20
|
|
|
protected $id; |
|
|
|
|
21
|
|
|
protected $status; |
|
|
|
|
22
|
|
|
protected $redirectUrl; |
|
|
|
|
23
|
|
|
protected $expiresAt; |
|
|
|
|
24
|
|
|
protected $createdAt; |
|
|
|
|
25
|
|
|
|
26
|
22 |
|
public function __construct($customerEmail, $total, $currency = 'USD', $customerName = null) |
|
|
|
|
27
|
|
|
{ |
28
|
22 |
|
$this->setTotal($total); |
29
|
20 |
|
$this->setCurrency($currency); |
30
|
18 |
|
$this->setCustomer($customerEmail, $customerName); |
31
|
17 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param array $data |
|
|
|
|
35
|
|
|
* |
36
|
|
|
* @return PaymentRequest |
37
|
|
|
*/ |
38
|
6 |
|
public static function fromResponse(array $data) |
39
|
|
|
{ |
40
|
6 |
|
$self = new self($data['customer']['email'], $data['total']); |
41
|
|
|
|
42
|
6 |
|
$callbackData = json_decode($data['callback_data'], true); |
43
|
6 |
|
if (json_last_error() !== JSON_ERROR_NONE) { |
44
|
5 |
|
$callbackData = $data['callback_data']; |
45
|
|
|
} |
46
|
6 |
|
$self->id = $data['id']; |
47
|
6 |
|
$self->status = $data['status']; |
48
|
6 |
|
$self->currency = $data['currency']; |
49
|
6 |
|
$self->customPaymentId = $data['custom_payment_id']; |
50
|
6 |
|
$self->callbackData = $callbackData; |
51
|
6 |
|
$self->customerName = $data['customer']['name']; |
52
|
6 |
|
$self->redirectUrl = $data['redirect_url']; |
53
|
6 |
|
$self->successUrl = $data['success_url']; |
54
|
6 |
|
$self->cancelUrl = $data['cancel_url']; |
55
|
6 |
|
$self->ipnUrl = $data['ipn_url']; |
56
|
6 |
|
$self->notificationEmail = $data['notification_email']; |
57
|
6 |
|
$self->confirmationSpeed = $data['confirmation_speed']; |
58
|
6 |
|
$self->expiresAt = $data['expires_at']; |
59
|
6 |
|
$self->createdAt = $data['created_at']; |
60
|
|
|
|
61
|
6 |
|
return $self; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param float $total |
|
|
|
|
66
|
|
|
* |
67
|
|
|
* @throws \GloBee\PaymentApi\Exceptions\Validation\InvalidArgumentException |
|
|
|
|
68
|
|
|
* @throws \GloBee\PaymentApi\Exceptions\Validation\BelowMinimumException |
|
|
|
|
69
|
|
|
*/ |
|
|
|
|
70
|
22 |
|
protected function setTotal($total) |
|
|
|
|
71
|
|
|
{ |
72
|
22 |
|
Validator::validateNumberAboveMinimum('total', $total, 0); |
73
|
20 |
|
$this->total = $total; |
74
|
20 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param string $currency |
|
|
|
|
78
|
|
|
* |
79
|
|
|
* @throws \GloBee\PaymentApi\Exceptions\Validation\ValidationException |
|
|
|
|
80
|
|
|
* @throws \GloBee\PaymentApi\Exceptions\Validation\InvalidArgumentException |
|
|
|
|
81
|
|
|
*/ |
|
|
|
|
82
|
20 |
|
protected function setCurrency($currency) |
|
|
|
|
83
|
|
|
{ |
84
|
20 |
|
Validator::validateStringLength('currency', $currency, 3); |
85
|
18 |
|
$this->currency = strtoupper($currency); |
86
|
18 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param string $customerEmail |
|
|
|
|
90
|
|
|
* @param string|null $customerName |
|
|
|
|
91
|
|
|
* |
92
|
|
|
* @throws \GloBee\PaymentApi\Exceptions\Validation\InvalidArgumentException |
|
|
|
|
93
|
|
|
* @throws \GloBee\PaymentApi\Exceptions\Validation\InvalidEmailException |
|
|
|
|
94
|
|
|
*/ |
|
|
|
|
95
|
18 |
|
protected function setCustomer($customerEmail, $customerName = null) |
|
|
|
|
96
|
|
|
{ |
97
|
18 |
|
Validator::validateEmail('customer.email', $customerEmail); |
98
|
17 |
|
$this->customerEmail = $customerEmail; |
99
|
17 |
|
$this->customerName = $customerName; |
100
|
17 |
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string $successUrl |
|
|
|
|
104
|
|
|
* |
105
|
|
|
* @return self |
106
|
|
|
* @throws \GloBee\PaymentApi\Exceptions\Validation\InvalidArgumentException |
|
|
|
|
107
|
|
|
* @throws \GloBee\PaymentApi\Exceptions\Validation\InvalidUrlException |
|
|
|
|
108
|
|
|
*/ |
109
|
3 |
|
public function withSuccessUrl($successUrl) |
|
|
|
|
110
|
|
|
{ |
111
|
3 |
|
if ($successUrl !== null) { |
|
|
|
|
112
|
3 |
|
Validator::validateUrl('success_url', $successUrl); |
113
|
|
|
} |
114
|
2 |
|
$self = clone $this; |
115
|
2 |
|
$self->successUrl = $successUrl; |
116
|
|
|
|
117
|
2 |
|
return $self; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param string $cancelUrl |
|
|
|
|
122
|
|
|
* |
123
|
|
|
* @return self |
124
|
|
|
*/ |
125
|
3 |
|
public function withCancelUrl($cancelUrl) |
|
|
|
|
126
|
|
|
{ |
127
|
3 |
|
if ($cancelUrl !== null) { |
|
|
|
|
128
|
3 |
|
Validator::validateUrl('cancel_url', $cancelUrl); |
129
|
|
|
} |
130
|
2 |
|
$self = clone $this; |
131
|
2 |
|
$self->cancelUrl = $cancelUrl; |
132
|
|
|
|
133
|
2 |
|
return $self; |
134
|
|
|
} |
135
|
|
|
|
136
|
3 |
|
public function withCallbackData($callbackData) |
|
|
|
|
137
|
|
|
{ |
138
|
3 |
|
$callbackDataString = $callbackData; |
139
|
|
|
|
140
|
3 |
|
if (!is_string($callbackData)) { |
141
|
1 |
|
$callbackDataString = json_encode($callbackData); |
142
|
|
|
} |
143
|
|
|
|
144
|
3 |
|
if (strlen($callbackDataString) > 150) { |
145
|
|
|
throw new ValidationException([], 'Callback Data must be less than 150 characters long.'); |
146
|
|
|
} |
147
|
|
|
|
148
|
3 |
|
$self = clone $this; |
149
|
3 |
|
$self->callbackData = $callbackData; |
150
|
|
|
|
151
|
3 |
|
return $self; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
|
|
|
|
155
|
|
|
* @param $ipnUrl |
|
|
|
|
156
|
|
|
* |
157
|
|
|
* @return self |
158
|
|
|
*/ |
159
|
3 |
|
public function withIpnUrl($ipnUrl) |
|
|
|
|
160
|
|
|
{ |
161
|
3 |
|
if ($ipnUrl !== null) { |
162
|
3 |
|
Validator::validateUrl('ipn_url', $ipnUrl); |
163
|
|
|
} |
164
|
2 |
|
$self = clone $this; |
165
|
2 |
|
$self->ipnUrl = $ipnUrl; |
166
|
|
|
|
167
|
2 |
|
return $self; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param string $customPaymentId |
|
|
|
|
172
|
|
|
* |
173
|
|
|
* @return self |
174
|
|
|
*/ |
175
|
2 |
|
public function withCustomPaymentId($customPaymentId) |
|
|
|
|
176
|
|
|
{ |
177
|
2 |
|
$self = clone $this; |
178
|
2 |
|
$self->customPaymentId = $customPaymentId; |
179
|
|
|
|
180
|
2 |
|
return $self; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @param string $notificationEmail |
|
|
|
|
185
|
|
|
* |
186
|
|
|
* @return self |
187
|
|
|
*/ |
188
|
3 |
|
public function withNotificationEmail($notificationEmail) |
|
|
|
|
189
|
|
|
{ |
190
|
3 |
|
if ($notificationEmail !== null) { |
|
|
|
|
191
|
3 |
|
Validator::validateEmail('notification_email', $notificationEmail); |
192
|
|
|
} |
193
|
2 |
|
$self = clone $this; |
194
|
2 |
|
$self->notificationEmail = $notificationEmail; |
195
|
|
|
|
196
|
2 |
|
return $self; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @return self |
201
|
|
|
*/ |
202
|
1 |
|
public function lowRiskConfirmation() |
203
|
|
|
{ |
204
|
1 |
|
$self = clone $this; |
205
|
1 |
|
$self->confirmationSpeed = 'low'; |
206
|
|
|
|
207
|
1 |
|
return $self; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @return self |
212
|
|
|
*/ |
213
|
|
|
public function balancedConfirmation() |
214
|
|
|
{ |
215
|
|
|
$self = clone $this; |
216
|
|
|
$self->confirmationSpeed = 'medium'; |
217
|
|
|
|
218
|
|
|
return $self; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @return self |
223
|
|
|
*/ |
224
|
1 |
|
public function quickConfirmation() |
225
|
|
|
{ |
226
|
1 |
|
$self = clone $this; |
227
|
1 |
|
$self->confirmationSpeed = 'high'; |
228
|
|
|
|
229
|
1 |
|
return $self; |
230
|
|
|
} |
231
|
|
|
|
232
|
1 |
|
public function confirmationSpeed($confirmationSpeed) |
|
|
|
|
233
|
|
|
{ |
234
|
1 |
|
Validator::validateOptions('confirmation_speed', $confirmationSpeed, ['low', 'medium', 'high']); |
235
|
|
|
|
236
|
|
|
$self = clone $this; |
237
|
|
|
$self->confirmationSpeed = $confirmationSpeed; |
238
|
|
|
|
239
|
|
|
return $self; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @return array |
244
|
|
|
*/ |
245
|
3 |
|
public function toArray() |
246
|
|
|
{ |
247
|
3 |
|
$callbackData = $this->callbackData; |
248
|
3 |
|
if (is_array($callbackData)) { |
249
|
1 |
|
$callbackData = json_encode($callbackData); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
return [ |
253
|
3 |
|
'id' => $this->id, |
254
|
3 |
|
'status' => $this->status, |
255
|
3 |
|
'total' => $this->total, |
256
|
3 |
|
'currency' => $this->currency, |
257
|
3 |
|
'custom_payment_id' => $this->customPaymentId, |
258
|
3 |
|
'callback_data' => $callbackData, |
259
|
|
|
'customer' => [ |
260
|
3 |
|
'name' => $this->customerName, |
261
|
3 |
|
'email' => $this->customerEmail, |
262
|
|
|
], |
263
|
3 |
|
'redirect_url' => $this->redirectUrl, |
264
|
3 |
|
'success_url' => $this->successUrl, |
265
|
3 |
|
'cancel_url' => $this->cancelUrl, |
266
|
3 |
|
'ipn_url' => $this->ipnUrl, |
267
|
3 |
|
'notification_email' => $this->notificationEmail, |
268
|
3 |
|
'confirmation_speed' => $this->confirmationSpeed, |
269
|
3 |
|
'expires_at' => $this->expiresAt, |
270
|
3 |
|
'created_at' => $this->createdAt, |
271
|
|
|
]; |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
|