PaymentResultResponse::set_result_code()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
/**
3
 * Payment result 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 InvalidArgumentException;
14
use JsonSchema\Constraints\Constraint;
15
use JsonSchema\Exception\ValidationException;
16
use JsonSchema\Validator;
17
18
/**
19
 * Payment result response
20
 *
21
 * @link https://docs.adyen.com/api-explorer/#/PaymentSetupAndVerificationService/v41/payments/result
22
 *
23
 * @author  Remco Tolsma
24
 * @version 1.0.0
25
 * @since   1.0.0
26
 */
27
class PaymentResultResponse extends ResponseObject {
28
	/**
29
	 * A unique value that you provided in the initial `/paymentSession` request as a `reference` field.
30
	 *
31
	 * @var string
32
	 */
33
	private $merchant_reference;
34
35
	/**
36
	 * The payment method used in the transaction.
37
	 *
38
	 * @var string
39
	 */
40
	private $payment_method;
41
42
	/**
43
	 * The shopperLocale value provided in the payment request
44
	 *
45
	 * @var string
46
	 */
47
	private $shopper_locale;
48
49
	/**
50
	 * The result of the payment.
51
	 *
52
	 * @var string|null
53
	 */
54
	private $result_code;
55
56
	/**
57
	 * 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.
58
	 *
59
	 * @var string|null
60
	 */
61
	private $psp_reference;
62
63
	/**
64
	 * Construct payment result response object.
65
	 *
66
	 * @param string $merchant_reference Merchant reference.
67
	 * @param string $payment_method     Payment method.
68
	 * @param string $shopper_locale     Shopper locale.
69
	 */
70 3
	public function __construct( $merchant_reference, $payment_method, $shopper_locale ) {
71 3
		$this->merchant_reference = $merchant_reference;
72 3
		$this->payment_method     = $payment_method;
73 3
		$this->shopper_locale     = $shopper_locale;
74 3
	}
75
76
	/**
77
	 * Get merchant reference.
78
	 *
79
	 * @return string
80
	 */
81 2
	public function get_merchant_reference() {
82 2
		return $this->merchant_reference;
83
	}
84
85
	/**
86
	 * Get payment method.
87
	 *
88
	 * @return string
89
	 */
90 2
	public function get_payment_method() {
91 2
		return $this->payment_method;
92
	}
93
94
	/**
95
	 * Get shopper locale.
96
	 *
97
	 * @return string
98
	 */
99 2
	public function get_shopper_locale() {
100 2
		return $this->shopper_locale;
101
	}
102
103
	/**
104
	 * Get result code.
105
	 *
106
	 * @return string|null
107
	 */
108 2
	public function get_result_code() {
109 2
		return $this->result_code;
110
	}
111
112
	/**
113
	 * Set result code.
114
	 *
115
	 * @param string|null $result_code Result code.
116
	 * @return void
117
	 */
118 2
	public function set_result_code( $result_code ) {
119 2
		$this->result_code = $result_code;
120 2
	}
121
122
	/**
123
	 * Get PSP reference.
124
	 *
125
	 * @return string|null
126
	 */
127 2
	public function get_psp_reference() {
128 2
		return $this->psp_reference;
129
	}
130
131
	/**
132
	 * Set PSP reference.
133
	 *
134
	 * @param string|null $psp_reference PSP reference.
135
	 * @return void
136
	 */
137 2
	public function set_psp_reference( $psp_reference ) {
138 2
		$this->psp_reference = $psp_reference;
139 2
	}
140
141
	/**
142
	 * Create payment result repsonse from object.
143
	 *
144
	 * @param object $object Object.
145
	 * @return PaymentResultResponse
146
	 * @throws ValidationException Throws validation exception when object does not contains the required properties.
147
	 */
148 1
	public static function from_object( $object ) {
149 1
		$validator = new Validator();
150
151 1
		$validator->validate(
152 1
			$object,
153
			(object) array(
154 1
				'$ref' => 'file://' . realpath( __DIR__ . '/../json-schemas/payment-result.json' ),
155
			),
156 1
			Constraint::CHECK_MODE_EXCEPTIONS
157
		);
158
159
		// phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- Adyen JSON object.
160
161 1
		$response = new self(
162 1
			$object->merchantReference,
163 1
			$object->paymentMethod,
164 1
			$object->shopperLocale
165
		);
166
167 1
		if ( isset( $object->pspReference ) ) {
168 1
			$response->set_psp_reference( $object->pspReference );
169
		}
170
171 1
		if ( isset( $object->resultCode ) ) {
172 1
			$response->set_result_code( $object->resultCode );
173
		}
174
175
		// phpcs:enable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- Adyen JSON object.
176
177 1
		$response->set_original_object( $object );
178
179 1
		return $response;
180
	}
181
}
182