1 | <?php |
||
2 | /** |
||
3 | * Notification request item |
||
4 | * |
||
5 | * @author Pronamic <[email protected]> |
||
6 | * @copyright 2005-2019 Pronamic |
||
7 | * @license GPL-3.0-or-later |
||
8 | * @package Pronamic\WordPress\Pay\Gateways\Adyen |
||
9 | */ |
||
10 | |||
11 | namespace Pronamic\WordPress\Pay\Gateways\Adyen; |
||
12 | |||
13 | use DateTime; |
||
14 | use JsonSchema\Constraints\Constraint; |
||
15 | use JsonSchema\Exception\ValidationException; |
||
16 | use JsonSchema\Validator; |
||
17 | use Pronamic\WordPress\Pay\Core\Util; |
||
18 | |||
19 | /** |
||
20 | * Notification request item |
||
21 | * |
||
22 | * @link https://docs.adyen.com/developers/api-reference/notifications-api#notificationrequestitem |
||
23 | * @author Remco Tolsma |
||
24 | * @version 1.0.0 |
||
25 | * @since 1.0.0 |
||
26 | */ |
||
27 | class NotificationRequestItem { |
||
28 | /** |
||
29 | * Amount. |
||
30 | * |
||
31 | * @var Amount |
||
32 | */ |
||
33 | private $amount; |
||
34 | |||
35 | /** |
||
36 | * Adyen's 16-character unique reference associated with the transaction/the request. This value is globally unique; quote it when communicating with us about this request. |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | private $psp_reference; |
||
41 | |||
42 | /** |
||
43 | * The type of event the notification item refers to. |
||
44 | * |
||
45 | * @var string |
||
46 | */ |
||
47 | private $event_code; |
||
48 | |||
49 | /** |
||
50 | * The time when the event was generated. |
||
51 | * |
||
52 | * @var DateTime |
||
53 | */ |
||
54 | private $event_date; |
||
55 | |||
56 | /** |
||
57 | * The merchant account identifier used in the transaction the notification item refers to. |
||
58 | * |
||
59 | * @var string |
||
60 | */ |
||
61 | private $merchant_account_code; |
||
62 | |||
63 | /** |
||
64 | * This field holds a list of the modification operations supported by the transaction the notification item refers to. |
||
65 | * |
||
66 | * @var array |
||
67 | */ |
||
68 | private $operations; |
||
69 | |||
70 | /** |
||
71 | * A reference to uniquely identify the payment. |
||
72 | * |
||
73 | * @var string |
||
74 | */ |
||
75 | private $merchant_reference; |
||
76 | |||
77 | /** |
||
78 | * The payment method used in the transaction the notification item refers to. |
||
79 | * |
||
80 | * @var string |
||
81 | */ |
||
82 | private $payment_method; |
||
83 | |||
84 | /** |
||
85 | * Informs about the outcome of the event (`eventCode`) the notification refers to: |
||
86 | * |
||
87 | * - `true`: the event (`eventCode`) the notification refers to was executed successfully. |
||
88 | * - `false`: the event was not executed successfully. |
||
89 | * |
||
90 | * @var boolean |
||
91 | */ |
||
92 | private $success; |
||
93 | |||
94 | /** |
||
95 | * Get amount. |
||
96 | * |
||
97 | * @return Amount |
||
98 | */ |
||
99 | 1 | public function get_amount() { |
|
100 | 1 | return $this->amount; |
|
101 | } |
||
102 | |||
103 | /** |
||
104 | * Set amount. |
||
105 | * |
||
106 | * @param Amount $amount Amount. |
||
107 | */ |
||
108 | 4 | public function set_amount( Amount $amount ) { |
|
109 | 4 | $this->amount = $amount; |
|
110 | 4 | } |
|
111 | |||
112 | /** |
||
113 | * Get PSP reference. |
||
114 | * |
||
115 | * @return string |
||
116 | */ |
||
117 | 2 | public function get_psp_reference() { |
|
118 | 2 | return $this->psp_reference; |
|
119 | } |
||
120 | |||
121 | /** |
||
122 | * Set PSP reference. |
||
123 | * |
||
124 | * @param string $psp_reference PSP reference. |
||
125 | */ |
||
126 | 4 | public function set_psp_reference( $psp_reference ) { |
|
127 | 4 | $this->psp_reference = $psp_reference; |
|
128 | 4 | } |
|
129 | |||
130 | /** |
||
131 | * Get event code. |
||
132 | * |
||
133 | * @return string |
||
134 | */ |
||
135 | 2 | public function get_event_code() { |
|
136 | 2 | return $this->event_code; |
|
137 | } |
||
138 | |||
139 | /** |
||
140 | * Set event code. |
||
141 | * |
||
142 | * @param string $event_code Event code. |
||
143 | */ |
||
144 | 4 | public function set_event_code( $event_code ) { |
|
145 | 4 | $this->event_code = $event_code; |
|
146 | 4 | } |
|
147 | |||
148 | /** |
||
149 | * Get event date. |
||
150 | * |
||
151 | * @return DateTime |
||
152 | */ |
||
153 | 2 | public function get_event_date() { |
|
154 | 2 | return $this->event_date; |
|
155 | } |
||
156 | |||
157 | /** |
||
158 | * Set event date. |
||
159 | * |
||
160 | * @param DateTime $event_date Event date. |
||
161 | */ |
||
162 | 4 | public function set_event_date( DateTime $event_date ) { |
|
163 | 4 | $this->event_date = $event_date; |
|
164 | 4 | } |
|
165 | |||
166 | /** |
||
167 | * Get merchant account code. |
||
168 | * |
||
169 | * @return string |
||
170 | */ |
||
171 | 2 | public function get_merchant_account_code() { |
|
172 | 2 | return $this->merchant_account_code; |
|
173 | } |
||
174 | |||
175 | /** |
||
176 | * Set merchant account code. |
||
177 | * |
||
178 | * @param string $merchant_account_code Merchant account code. |
||
179 | */ |
||
180 | 4 | public function set_merchant_account_code( $merchant_account_code ) { |
|
181 | 4 | $this->merchant_account_code = $merchant_account_code; |
|
182 | 4 | } |
|
183 | |||
184 | /** |
||
185 | * Get operations. |
||
186 | * |
||
187 | * @return array |
||
188 | */ |
||
189 | 2 | public function get_operations() { |
|
190 | 2 | return $this->operations; |
|
191 | } |
||
192 | |||
193 | /** |
||
194 | * Set operations. |
||
195 | * |
||
196 | * @param array $operations Operations. |
||
197 | */ |
||
198 | 4 | public function set_operations( $operations ) { |
|
199 | 4 | $this->operations = $operations; |
|
200 | 4 | } |
|
201 | |||
202 | /** |
||
203 | * Get operations. |
||
204 | * |
||
205 | * @return string |
||
206 | */ |
||
207 | 4 | public function get_merchant_reference() { |
|
208 | 4 | return $this->merchant_reference; |
|
209 | } |
||
210 | |||
211 | /** |
||
212 | * Set merchant reference. |
||
213 | * |
||
214 | * @param string $merchant_reference Merchant reference. |
||
215 | */ |
||
216 | 4 | public function set_merchant_reference( $merchant_reference ) { |
|
217 | 4 | $this->merchant_reference = $merchant_reference; |
|
218 | 4 | } |
|
219 | |||
220 | /** |
||
221 | * Get payment method. |
||
222 | * |
||
223 | * @return string |
||
224 | */ |
||
225 | 2 | public function get_payment_method() { |
|
226 | 2 | return $this->payment_method; |
|
227 | } |
||
228 | |||
229 | /** |
||
230 | * Set payment method. |
||
231 | * |
||
232 | * @param string $payment_method Payment method. |
||
233 | */ |
||
234 | 4 | public function set_payment_method( $payment_method ) { |
|
235 | 4 | $this->payment_method = $payment_method; |
|
236 | 4 | } |
|
237 | |||
238 | /** |
||
239 | * Is success. |
||
240 | * |
||
241 | * @return boolean |
||
242 | */ |
||
243 | 2 | public function is_success() { |
|
244 | 2 | return $this->success; |
|
245 | } |
||
246 | |||
247 | /** |
||
248 | * Set success. |
||
249 | * |
||
250 | * @param boolean $success Success. |
||
251 | */ |
||
252 | 4 | public function set_success( $success ) { |
|
253 | 4 | $this->success = $success; |
|
254 | 4 | } |
|
255 | |||
256 | /** |
||
257 | * Get JSON. |
||
258 | * |
||
259 | * @return object|null |
||
260 | */ |
||
261 | 1 | public function get_json() { |
|
262 | $data = array( |
||
263 | 1 | 'amount' => $this->get_amount(), |
|
264 | 1 | 'pspReference' => $this->get_psp_reference(), |
|
265 | 1 | 'eventCode' => $this->get_event_code(), |
|
266 | 1 | 'eventDate' => $this->get_event_date(), |
|
267 | 1 | 'merchantAccountCode' => $this->get_merchant_account_code(), |
|
268 | 1 | 'operations' => $this->get_operations(), |
|
269 | 1 | 'merchantReference' => $this->get_merchant_reference(), |
|
270 | 1 | 'paymentMethod' => $this->get_payment_method(), |
|
271 | 1 | 'success' => Util::boolean_to_string( $this->is_success() ), |
|
272 | ); |
||
273 | |||
274 | 1 | $data = array_filter( $data ); |
|
275 | |||
276 | 1 | if ( empty( $data ) ) { |
|
277 | return null; |
||
278 | } |
||
279 | |||
280 | 1 | return (object) $data; |
|
281 | } |
||
282 | |||
283 | /** |
||
284 | * Create notification request item from object. |
||
285 | * |
||
286 | * @param object $object Object. |
||
287 | * @return NotificationRequestItem |
||
288 | * @throws ValidationException Throws JSON schema validation exception when JSON is invalid. |
||
289 | */ |
||
290 | 5 | public static function from_object( $object ) { |
|
291 | 5 | $validator = new Validator(); |
|
292 | |||
293 | 5 | $validator->validate( |
|
294 | 5 | $object, |
|
295 | (object) array( |
||
296 | 5 | '$ref' => 'file://' . realpath( __DIR__ . '/../json-schemas/notification-request-item.json' ), |
|
297 | ), |
||
298 | 5 | Constraint::CHECK_MODE_EXCEPTIONS |
|
299 | ); |
||
300 | |||
301 | 4 | if ( ! isset( $object->amount ) ) { |
|
302 | throw new InvalidArgumentException( 'Object must contain `amount` property.' ); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
303 | } |
||
304 | |||
305 | 4 | if ( ! isset( $object->pspReference ) ) { |
|
306 | throw new InvalidArgumentException( 'Object must contain `pspReference` property.' ); |
||
307 | } |
||
308 | |||
309 | 4 | if ( ! isset( $object->eventCode ) ) { |
|
310 | throw new InvalidArgumentException( 'Object must contain `eventCode` property.' ); |
||
311 | } |
||
312 | |||
313 | 4 | if ( ! isset( $object->eventDate ) ) { |
|
314 | throw new InvalidArgumentException( 'Object must contain `eventDate` property.' ); |
||
315 | } |
||
316 | |||
317 | 4 | if ( ! isset( $object->merchantAccountCode ) ) { |
|
318 | throw new InvalidArgumentException( 'Object must contain `merchantAccountCode` property.' ); |
||
319 | } |
||
320 | |||
321 | 4 | if ( ! isset( $object->operations ) ) { |
|
322 | throw new InvalidArgumentException( 'Object must contain `operations` property.' ); |
||
323 | } |
||
324 | |||
325 | 4 | if ( ! isset( $object->merchantReference ) ) { |
|
326 | throw new InvalidArgumentException( 'Object must contain `merchantReference` property.' ); |
||
327 | } |
||
328 | |||
329 | 4 | if ( ! isset( $object->paymentMethod ) ) { |
|
330 | throw new InvalidArgumentException( 'Object must contain `paymentMethod` property.' ); |
||
331 | } |
||
332 | |||
333 | 4 | if ( ! isset( $object->success ) ) { |
|
334 | throw new InvalidArgumentException( 'Object must contain `success` property.' ); |
||
335 | } |
||
336 | |||
337 | 4 | if ( ! is_array( $object->operations ) ) { |
|
338 | throw new InvalidArgumentException( 'Object property `operations` must be an array.' ); |
||
339 | } |
||
340 | |||
341 | 4 | $item = new self(); |
|
342 | |||
343 | 4 | $item->set_amount( Amount::from_object( $object->amount ) ); |
|
344 | 4 | $item->set_psp_reference( $object->pspReference ); |
|
345 | 4 | $item->set_event_code( $object->eventCode ); |
|
346 | 4 | $item->set_event_date( new DateTime( $object->eventDate ) ); |
|
347 | 4 | $item->set_merchant_account_code( $object->merchantAccountCode ); |
|
348 | 4 | $item->set_operations( $object->operations ); |
|
349 | 4 | $item->set_merchant_reference( $object->merchantReference ); |
|
350 | 4 | $item->set_payment_method( $object->paymentMethod ); |
|
351 | 4 | $item->set_success( filter_var( $object->success, FILTER_VALIDATE_BOOLEAN ) ); |
|
352 | |||
353 | 4 | return $item; |
|
354 | } |
||
355 | } |
||
356 |