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