Failed Conditions
Push — develop ( 0fe18a...67c0d1 )
by Remco
03:00
created

PaymentResponse::from_object()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.1755

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 5
nop 1
dl 0
loc 16
ccs 7
cts 9
cp 0.7778
crap 4.1755
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Payment response
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
/**
14
 * Payment response
15
 *
16
 * @link https://docs.adyen.com/api-explorer/#/PaymentSetupAndVerificationService/v41/payments
17
 *
18
 * @author  Remco Tolsma
19
 * @version 1.0.0
20
 * @since   1.0.0
21
 */
22
class PaymentResponse {
23
	/**
24
	 * This field contains additional data, which may be required to return in a particular payment response.
25
	 *
26
	 * @var object|null
27
	 */
28
	private $additional_data;
0 ignored issues
show
introduced by
The private property $additional_data is not used, and could be removed.
Loading history...
29
30
	/**
31
	 * When non-empty, contains all the fields that you must submit to the `/payments/details` endpoint.
32
	 *
33
	 * @var array|null
34
	 */
35
	private $details;
0 ignored issues
show
introduced by
The private property $details is not used, and could be removed.
Loading history...
36
37
	/**
38
	 * The fraud result properties of the payment
39
	 *
40
	 * @var object|null
41
	 */
42
	private $fraud_result;
0 ignored issues
show
introduced by
The private property $fraud_result is not used, and could be removed.
Loading history...
43
44
	/**
45
	 * Contains the details that will be presented to the shopper
46
	 *
47
	 * @var object|null
48
	 */
49
	private $output_details;
0 ignored issues
show
introduced by
The private property $output_details is not used, and could be removed.
Loading history...
50
51
	/**
52
	 * When non-empty, contains a value that you must submit to the `/payments/details` endpoint.
53
	 *
54
	 * @var string|null
55
	 */
56
	private $payment_data;
0 ignored issues
show
introduced by
The private property $payment_data is not used, and could be removed.
Loading history...
57
58
	/**
59
	 * 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.
60
	 *
61
	 * `pspReference` is returned only for non-redirect payment methods.
62
	 *
63
	 * @var string|null
64
	 */
65
	private $psp_reference;
66
67
	/**
68
	 * When the payment flow requires a redirect, this object contains information about the redirect URL.
69
	 *
70
	 * @var RedirectInformation|null
71
	 */
72
	private $redirect;
73
74
	/**
75
	 * If the payment's authorisation is refused or an error occurs during authorisation, this field holds Adyen's mapped reason for the refusal or a description of the error.
76
	 *
77
	 * When a transaction fails, the authorisation response includes `resultCode` and `refusalReason` values.
78
	 *
79
	 * @var string|null
80
	 */
81
	private $refusal_reason;
0 ignored issues
show
introduced by
The private property $refusal_reason is not used, and could be removed.
Loading history...
82
83
	/**
84
	 * Code that specifies the refusal reason.
85
	 *
86
	 * @var string|null
87
	 */
88
	private $refusal_reason_code;
0 ignored issues
show
introduced by
The private property $refusal_reason_code is not used, and could be removed.
Loading history...
89
90
	/**
91
	 * The result of the payment.
92
	 *
93
	 * @var string
94
	 */
95
	private $result_code;
96
97
	/**
98
	 * Construct payment response object.
99
	 *
100
	 * @param string $result_code Result code.
101
	 */
102 1
	public function __construct( $result_code ) {
103 1
		$this->result_code = $result_code;
104 1
	}
105
106
	/**
107
	 * Get result code.
108
	 *
109
	 * @return string
110
	 */
111 1
	public function get_result_code() {
112 1
		return $this->result_code;
113
	}
114
115
	/**
116
	 * Get PSP reference.
117
	 *
118
	 * @return string|null
119
	 */
120
	public function get_psp_reference() {
121
		return $this->psp_reference;
122
	}
123
124
	/**
125
	 * Set PSP reference.
126
	 *
127
	 * @param string|null $psp_reference PSP reference.
128
	 */
129
	public function set_psp_reference( $psp_reference ) {
130
		$this->psp_reference = $psp_reference;
131
	}
132
133
	/**
134
	 * Get redirect.
135
	 *
136
	 * @return RedirectInformation|null
137
	 */
138 1
	public function get_redirect() {
139 1
		return $this->redirect;
140
	}
141
142
	/**
143
	 * Set redirect.
144
	 *
145
	 * @param RedirectInformation|null $redirect Redirect information.
146
	 */
147 1
	public function set_redirect( RedirectInformation $redirect = null ) {
148 1
		$this->redirect = $redirect;
149 1
	}
150
151
	/**
152
	 * Create payment repsonse from object.
153
	 *
154
	 * @param object $object Object.
155
	 * @return PaymentResponse
156
	 * @throws InvalidArgumentException Throws invalid argument exception when object does not contains the required properties.
157
	 */
158 1
	public static function from_object( $object ) {
159 1
		if ( ! isset( $object->resultCode ) ) {
160
			throw new InvalidArgumentException( 'Object must contain `resultCode` property.' );
0 ignored issues
show
Bug introduced by
The type Pronamic\WordPress\Pay\G...nvalidArgumentException was not found. Did you mean InvalidArgumentException? If so, make sure to prefix the type with \.
Loading history...
161
		}
162
163 1
		$payment_response = new self( $object->resultCode );
164
165 1
		if ( isset( $object->pspReference ) ) {
166
			$payment_response->set_psp_reference( $object->pspReference );
167
		}
168
169 1
		if ( isset( $object->redirect ) ) {
170 1
			$payment_response->set_redirect( RedirectInformation::from_object( $object->redirect ) );
171
		}
172
173 1
		return $payment_response;
174
	}
175
}
176