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