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