Failed Conditions
Push — develop ( 397f1c...61c5d3 )
by Reüel
05:20
created

PaymentResponse   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 226
Duplicated Lines 0 %

Test Coverage

Coverage 77.59%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 47
dl 0
loc 226
ccs 45
cts 58
cp 0.7759
rs 10
c 2
b 0
f 0
wmc 20

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