Failed Conditions
Push — master ( cc3467...84f509 )
by Reüel
09:31 queued 01:25
created

PaymentResponse::from_object()   B

Complexity

Conditions 8
Paths 64

Size

Total Lines 48
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 8.0052

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 8
eloc 23
c 2
b 0
f 0
nc 64
nop 1
dl 0
loc 48
ccs 22
cts 23
cp 0.9565
crap 8.0052
rs 8.4444
1
<?php
2
/**
3
 * Payment response
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2020 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 JsonSchema\Constraints\Constraint;
14
use JsonSchema\Exception\ValidationException;
15
use JsonSchema\Validator;
16
17
/**
18
 * Payment response
19
 *
20
 * @link https://docs.adyen.com/api-explorer/#/PaymentSetupAndVerificationService/v41/payments
21
 *
22
 * @author  Remco Tolsma
23
 * @version 1.0.0
24
 * @since   1.0.0
25
 */
26
class PaymentResponse extends ResponseObject {
27
	/**
28
	 * Details.
29
	 *
30
	 * @var array<int, DetailsInformation>|null
31
	 */
32
	private $details;
33
34
	/**
35
	 * Adyen's 16-character string reference associated with the transaction/request. This value is globally unique; quote it when communicating with us about this request.
36
	 *
37
	 * `pspReference` is returned only for non-redirect payment methods.
38
	 *
39
	 * @var string|null
40
	 */
41
	private $psp_reference;
42
43
	/**
44
	 * When the payment flow requires a redirect, this object contains information about the redirect URL.
45
	 *
46
	 * @var RedirectInformation|null
47
	 */
48
	private $redirect;
49
50
	/**
51
	 * The action.
52
	 *
53
	 * @var ActionInformation|null
54
	 */
55
	private $action;
56
57
	/**
58
	 * Payment data.
59
	 *
60
	 * @var string|null
61
	 */
62
	private $payment_data;
63
64
	/**
65
	 * Refusal reason.
66
	 *
67
	 * @var string|null
68
	 */
69
	private $refusal_reason;
70
71
	/**
72
	 * The result of the payment.
73
	 *
74
	 * @var string
75
	 */
76
	private $result_code;
77
78
	/**
79
	 * Construct payment response object.
80
	 *
81
	 * @param string $result_code Result code.
82
	 */
83 4
	public function __construct( $result_code ) {
84 4
		$this->result_code = $result_code;
85 4
	}
86
87
	/**
88
	 * Get details.
89
	 *
90
	 * @return array<int, DetailsInformation>|null
91
	 */
92
	public function get_details() {
93
		return $this->details;
94
	}
95
96
	/**
97
	 * Set details.
98
	 *
99
	 * @param array<int, DetailsInformation>|null $details Details.
100
	 * @return void
101
	 */
102 2
	public function set_details( $details ) {
103 2
		$this->details = $details;
104 2
	}
105
106
	/**
107
	 * Get payment data.
108
	 *
109
	 * @return string|null
110
	 */
111
	public function get_payment_data() {
112
		return $this->payment_data;
113
	}
114
115
	/**
116
	 * Set payment data.
117
	 *
118
	 * @param string|null $payment_data Payment data.
119
	 * @return void
120
	 */
121 2
	public function set_payment_data( $payment_data ) {
122 2
		$this->payment_data = $payment_data;
123 2
	}
124
125
	/**
126
	 * Get result code.
127
	 *
128
	 * @return string
129
	 */
130 3
	public function get_result_code() {
131 3
		return $this->result_code;
132
	}
133
134
	/**
135
	 * Get PSP reference.
136
	 *
137
	 * @return string|null
138
	 */
139 2
	public function get_psp_reference() {
140 2
		return $this->psp_reference;
141
	}
142
143
	/**
144
	 * Set PSP reference.
145
	 *
146
	 * @param string|null $psp_reference PSP reference.
147
	 * @return void
148
	 */
149 2
	public function set_psp_reference( $psp_reference ) {
150 2
		$this->psp_reference = $psp_reference;
151 2
	}
152
153
	/**
154
	 * Get refusal reason.
155
	 *
156
	 * @return string|null
157
	 */
158
	public function get_refusal_reason() {
159
		return $this->refusal_reason;
160
	}
161
162
	/**
163
	 * Set refusal reason.
164
	 *
165
	 * @param string|null $refusal_reason Refusal reason.
166
	 * @return void
167
	 */
168
	public function set_refusal_reason( $refusal_reason ) {
169
		$this->refusal_reason = $refusal_reason;
170
	}
171
172
	/**
173
	 * Get redirect.
174
	 *
175
	 * @return RedirectInformation|null
176
	 */
177 2
	public function get_redirect() {
178 2
		return $this->redirect;
179
	}
180
181
	/**
182
	 * Set redirect.
183
	 *
184
	 * @param RedirectInformation|null $redirect Redirect information.
185
	 * @return void
186
	 */
187 3
	public function set_redirect( RedirectInformation $redirect = null ) {
188 3
		$this->redirect = $redirect;
189 3
	}
190
191
	/**
192
	 * Get action.
193
	 *
194
	 * @return ActionInformation|null
195
	 */
196
	public function get_action() {
197
		return $this->action;
198
	}
199
200
	/**
201
	 * Set action.
202
	 *
203
	 * @param ActionInformation|null $action Action information.
204
	 * @return void
205
	 */
206 1
	public function set_action( ActionInformation $action = null ) {
207 1
		$this->action = $action;
208 1
	}
209
210
	/**
211
	 * Create payment response from object.
212
	 *
213
	 * @param object $object Object.
214
	 * @return PaymentResponse
215
	 * @throws ValidationException Throws validation exception when object does not contains the required properties.
216
	 */
217 3
	public static function from_object( $object ) {
218 3
		$validator = new Validator();
219
220 3
		$validator->validate(
221 3
			$object,
222
			(object) array(
223 3
				'$ref' => 'file://' . realpath( __DIR__ . '/../json-schemas/payment-response.json' ),
224
			),
225 3
			Constraint::CHECK_MODE_EXCEPTIONS
226
		);
227
228
		// phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- Adyen JSON object.
229
230 3
		$payment_response = new self( $object->resultCode );
231
232 3
		if ( isset( $object->pspReference ) ) {
233 1
			$payment_response->set_psp_reference( $object->pspReference );
234
		}
235
236 3
		if ( isset( $object->action ) ) {
237 1
			$payment_response->set_action( ActionInformation::from_object( $object->action ) );
238
		}
239
240 3
		if ( isset( $object->details ) ) {
241 2
			$details = array();
242
243 2
			foreach ( $object->details as $detail ) {
244 2
				$details[] = DetailsInformation::from_object( $detail );
245
			}
246
247 2
			$payment_response->set_details( $details );
248
		}
249
250 3
		if ( isset( $object->refusalReason ) ) {
251
			$payment_response->set_refusal_reason( $object->refusalReason );
252
		}
253
254 3
		if ( isset( $object->redirect ) ) {
255 3
			$payment_response->set_redirect( RedirectInformation::from_object( $object->redirect ) );
256
		}
257
258 3
		if ( isset( $object->paymentData ) ) {
259 2
			$payment_response->set_payment_data( $object->paymentData );
260
		}
261
262
		// phpcs:enable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- Adyen JSON object.
263
264 3
		return $payment_response;
265
	}
266
267
	/**
268
	 * Get JSON.
269
	 *
270
	 * @return object
271
	 */
272
	public function get_json() {
273
		// Redirect.
274
		$redirect = $this->get_redirect();
275
276
		if ( null !== $redirect ) {
277
			$redirect = $redirect->get_json();
278
		}
279
280
		// Properties.
281
		$properties = array(
282
			'refusalReason' => $this->get_refusal_reason(),
283
			'redirect'      => $redirect,
284
			'resultCode'    => $this->get_result_code(),
285
			'paymentData'   => $this->get_payment_data(),
286
		);
287
288
		// Details.
289
		$details = $this->get_details();
290
291
		if ( null !== $details ) {
292
			$properties['details'] = array();
293
294
			foreach ( $details as $detail ) {
295
				$properties['details'][] = $detail->get_json();
296
			}
297
		}
298
299
		$properties = Util::filter_null( $properties );
300
301
		$object = (object) $properties;
302
303
		return $object;
304
	}
305
}
306