Test Failed
Push — develop ( 61c5d3...602393 )
by Reüel
06:05
created

PaymentResponse   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 266
Duplicated Lines 0 %

Test Coverage

Coverage 77.59%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 58
c 3
b 0
f 0
dl 0
loc 266
rs 10
ccs 45
cts 58
cp 0.7759
wmc 26

16 Methods

Rating   Name   Duplication   Size   Complexity  
A set_details() 0 2 1
A set_action() 0 2 1
A get_payment_data() 0 2 1
A set_payment_data() 0 2 1
A set_redirect() 0 2 1
A get_details() 0 2 1
A get_redirect() 0 2 1
A set_refusal_reason() 0 2 1
A get_action() 0 2 1
A get_refusal_reason() 0 2 1
A get_psp_reference() 0 2 1
B from_object() 0 48 8
A __construct() 0 2 1
A get_json() 0 23 4
A set_psp_reference() 0 2 1
A get_result_code() 0 2 1
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
61
	 */
62
	private $payment_data;
63
64
	/**
65
	 * Refusal reason.
66
	 *
67
	 * @var string
68
	 */
69
	private $refusal_reason;
70
71
	/**
72
	 * The result of the payment.
73
	 *
74
	 * @var string
75
	 */
76 4
	private $result_code;
77 4
78 4
	/**
79
	 * Construct payment response object.
80
	 *
81
	 * @param string $result_code Result code.
82
	 */
83
	public function __construct( $result_code ) {
84
		$this->result_code = $result_code;
85
	}
86
87
	/**
88
	 * Get details.
89
	 *
90
	 * @return array<int, DetailsInformation>|null
91
	 */
92
	public function get_details() {
93
		return $this->details;
94 2
	}
95 2
96 2
	/**
97
	 * Set details.
98
	 *
99
	 * @param array|null $details Details.
100
	 */
101
	public function set_details( $details ) {
102
		$this->details = $details;
103
	}
104
105
	/**
106
	 * Get payment data.
107
	 *
108
	 * @return string
109
	 */
110
	public function get_payment_data() {
111
		return $this->payment_data;
112 2
	}
113 2
114 2
	/**
115
	 * Set payment data.
116
	 *
117
	 * @param string $payment_data Payment data.
118
	 */
119
	public function set_payment_data( $payment_data ) {
120
		$this->payment_data = $payment_data;
121 3
	}
122 3
123
	/**
124
	 * Get result code.
125
	 *
126
	 * @return string
127
	 */
128
	public function get_result_code() {
129
		return $this->result_code;
130 2
	}
131 2
132
	/**
133
	 * Get PSP reference.
134
	 *
135
	 * @return string|null
136
	 */
137
	public function get_psp_reference() {
138
		return $this->psp_reference;
139
	}
140 2
141 2
	/**
142 2
	 * Set PSP reference.
143
	 *
144
	 * @param string|null $psp_reference PSP reference.
145
	 * @return void
146
	 */
147
	public function set_psp_reference( $psp_reference ) {
148
		$this->psp_reference = $psp_reference;
149 2
	}
150 2
151
	/**
152
	 * Get refusal reason.
153
	 *
154
	 * @return string
155
	 */
156
	public function get_refusal_reason() {
157
		return $this->refusal_reason;
158
	}
159 3
160 3
	/**
161 3
	 * Set refusal reason.
162
	 *
163
	 * @param string $refusal_reason Refusal reason.
164
	 */
165
	public function set_refusal_reason( $refusal_reason ) {
166
		$this->refusal_reason = $refusal_reason;
167
	}
168
169
	/**
170
	 * Get redirect.
171
	 *
172
	 * @return RedirectInformation|null
173
	 */
174
	public function get_redirect() {
175
		return $this->redirect;
176
	}
177
178 1
	/**
179 1
	 * Set redirect.
180 1
	 *
181
	 * @param RedirectInformation|null $redirect Redirect information.
182
	 * @return void
183
	 */
184
	public function set_redirect( RedirectInformation $redirect = null ) {
185
		$this->redirect = $redirect;
186
	}
187
188
	/**
189 3
	 * Get action.
190 3
	 *
191
	 * @return ActionInformation|null
192 3
	 */
193 3
	public function get_action() {
194
		return $this->action;
195 3
	}
196
197 3
	/**
198
	 * Set action.
199
	 *
200
	 * @param ActionInformation|null $action Action information.
201
	 * @return void
202 3
	 */
203
	public function set_action( ActionInformation $action = null ) {
204 3
		$this->action = $action;
205 1
	}
206
207
	/**
208 3
	 * Create payment response from object.
209 1
	 *
210
	 * @param object $object Object.
211
	 * @return PaymentResponse
212 3
	 * @throws ValidationException Throws validation exception when object does not contains the required properties.
213 2
	 */
214
	public static function from_object( $object ) {
215 2
		$validator = new Validator();
216 2
217
		$validator->validate(
218
			$object,
219 2
			(object) array(
220
				'$ref' => 'file://' . realpath( __DIR__ . '/../json-schemas/payment-response.json' ),
221
			),
222 3
			Constraint::CHECK_MODE_EXCEPTIONS
223 3
		);
224
225
		// phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- Adyen JSON object.
226 3
227 2
		$payment_response = new self( $object->resultCode );
228
229
		if ( isset( $object->pspReference ) ) {
230
			$payment_response->set_psp_reference( $object->pspReference );
231
		}
232 3
233
		if ( isset( $object->action ) ) {
234
			$payment_response->set_action( ActionInformation::from_object( $object->action ) );
235
		}
236
237
		if ( isset( $object->details ) ) {
238
			$details = array();
239
240
			foreach ( $object->details as $detail ) {
241
				$details[] = DetailsInformation::from_object( $detail );
242
			}
243
244
			$payment_response->set_details( $details );
245
		}
246
247
		if ( isset( $object->refusalReason ) ) {
248
			$payment_response->set_refusal_reason( $object->refusalReason );
249
		}
250
251
		if ( isset( $object->redirect ) ) {
252
			$payment_response->set_redirect( RedirectInformation::from_object( $object->redirect ) );
253
		}
254
255
		if ( isset( $object->paymentData ) ) {
256
			$payment_response->set_payment_data( $object->paymentData );
257
		}
258
259
		// phpcs:enable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- Adyen JSON object.
260
261
		return $payment_response;
262
	}
263
264
	/**
265
	 * Get JSON.
266
	 *
267
	 * @return object
268
	 */
269
	public function get_json() {
270
		$properties      = array(
271
			'refusalReason' => $this->get_refusal_reason(),
272
			'redirect'      => ( null === $this->get_redirect() ? null : $this->get_redirect()->get_json() ),
273
			'resultCode'    => $this->get_result_code(),
274
			'paymentData'   => $this->get_payment_data(),
275
		);
276
277
		if ( null !== $this->get_details() ) {
278
			$details = array();
279
280
			foreach ( $this->get_details() as $detail ) {
281
				$details[] = $detail->get_json();
282
			}
283
284
			$properties['details'] = $details;
285
		}
286
287
		$properties = Util::filter_null( $properties );
288
289
		$object = (object) $properties;
290
291
		return $object;
292
	}
293
}
294