1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Adyen client |
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
|
|
|
/** |
14
|
|
|
* Adyen client |
15
|
|
|
* |
16
|
|
|
* @link https://github.com/adyenpayments/php/blob/master/generatepaymentform.php |
17
|
|
|
* |
18
|
|
|
* @author Remco Tolsma |
19
|
|
|
* @version 1.0.5 |
20
|
|
|
* @since 1.0.0 |
21
|
|
|
*/ |
22
|
|
|
class Client { |
23
|
|
|
/** |
24
|
|
|
* Config. |
25
|
|
|
* |
26
|
|
|
* @var Config |
27
|
|
|
*/ |
28
|
|
|
private $config; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Constructs and initializes an Adyen client object. |
32
|
|
|
* |
33
|
|
|
* @param Config $config Adyen config. |
34
|
|
|
*/ |
35
|
13 |
|
public function __construct( Config $config ) { |
36
|
13 |
|
$this->config = $config; |
37
|
13 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Send request with the specified action and parameters |
41
|
|
|
* |
42
|
|
|
* @param string $method Adyen API method. |
43
|
|
|
* @param Request $request Request object. |
44
|
|
|
* @return object |
45
|
|
|
* @throws \Exception Throws exception when error occurs. |
46
|
|
|
*/ |
47
|
11 |
|
private function send_request( $method, $request ) { |
48
|
|
|
// Request. |
49
|
11 |
|
$url = $this->config->get_api_url( $method ); |
50
|
|
|
|
51
|
10 |
|
$response = \wp_remote_request( |
52
|
10 |
|
$url, |
53
|
|
|
array( |
54
|
10 |
|
'method' => 'POST', |
55
|
|
|
'headers' => array( |
56
|
10 |
|
'X-API-key' => $this->config->get_api_key(), |
57
|
10 |
|
'Content-Type' => 'application/json', |
58
|
|
|
), |
59
|
10 |
|
'body' => \wp_json_encode( $request->get_json() ), |
60
|
|
|
) |
61
|
|
|
); |
62
|
|
|
|
63
|
10 |
|
if ( $response instanceof \WP_Error ) { |
64
|
|
|
throw new \Exception( $response->get_error_message() ); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// Body. |
68
|
10 |
|
$body = \wp_remote_retrieve_body( $response ); |
69
|
|
|
|
70
|
|
|
// Response. |
71
|
10 |
|
$response_code = \wp_remote_retrieve_response_code( $response ); |
72
|
10 |
|
$response_message = \wp_remote_retrieve_response_message( $response ); |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* On PHP 7 or higher the `json_decode` function will return `null` and |
76
|
|
|
* `json_last_error` will return `4` (Syntax error). On PHP 5.6 or lower |
77
|
|
|
* the `json_decode` will also return `null`, but json_last_error` will |
78
|
|
|
* return `0` (No error). Therefor we check if the HTTP response body |
79
|
|
|
* is an empty string. |
80
|
|
|
* |
81
|
|
|
* @link https://3v4l.org/ |
82
|
|
|
*/ |
83
|
10 |
|
if ( '' === $body ) { |
84
|
|
|
throw new \Exception( |
85
|
|
|
\sprintf( |
86
|
|
|
'Adyen response is empty, HTTP response: "%s %s".', |
87
|
|
|
$response_code, |
88
|
|
|
$response_message |
89
|
|
|
) |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
// JSON. |
94
|
10 |
|
$data = json_decode( $body ); |
95
|
|
|
|
96
|
|
|
// JSON error. |
97
|
10 |
|
$json_error = json_last_error(); |
98
|
|
|
|
99
|
10 |
|
if ( \JSON_ERROR_NONE !== $json_error ) { |
100
|
|
|
throw new \Exception( |
101
|
|
|
\sprintf( |
102
|
|
|
'Could not JSON decode Adyen response, HTTP response: "%s %s", HTTP body length: "%d", JSON error: "%s".', |
103
|
|
|
$response_code, |
104
|
|
|
$response_message, |
105
|
|
|
\strlen( $body ), |
106
|
|
|
\json_last_error_msg() |
107
|
|
|
), |
108
|
|
|
$json_error |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
// Object. |
113
|
10 |
|
if ( ! \is_object( $data ) ) { |
114
|
|
|
throw new \Exception( |
115
|
|
|
\sprintf( |
116
|
|
|
'Could not JSON decode Adyen response to an object, HTTP response: "%s %s", HTTP body: "%s".', |
117
|
|
|
$response_code, |
118
|
|
|
$response_message, |
119
|
|
|
$body |
120
|
|
|
), |
121
|
|
|
\intval( $response_code ) |
122
|
|
|
); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
// Error. |
126
|
10 |
|
if ( isset( $data->error ) ) { |
127
|
1 |
|
$error = Error::from_object( $data->error ); |
128
|
|
|
|
129
|
1 |
|
throw $error; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
// Service Exception. |
133
|
|
|
// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- Adyen JSON object. |
134
|
9 |
|
if ( isset( $data->status, $data->errorCode, $data->message, $data->errorType ) ) { |
135
|
5 |
|
$service_exception = ServiceException::from_object( $data ); |
136
|
|
|
|
137
|
5 |
|
throw $service_exception; |
138
|
|
|
} |
139
|
|
|
|
140
|
4 |
|
return $data; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Create payment. |
145
|
|
|
* |
146
|
|
|
* @param PaymentRequest $request Payment request. |
147
|
|
|
* |
148
|
|
|
* @return PaymentResponse |
149
|
|
|
* |
150
|
|
|
* @throws \Exception Throws error if request fails. |
151
|
|
|
*/ |
152
|
1 |
|
public function create_payment( PaymentRequest $request ) { |
153
|
1 |
|
$data = $this->send_request( 'v51/payments', $request ); |
154
|
|
|
|
155
|
1 |
|
return PaymentResponse::from_object( $data ); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Submit additional payment details. |
160
|
|
|
* |
161
|
|
|
* @param PaymentDetailsRequest $request Payment request. |
162
|
|
|
* |
163
|
|
|
* @return PaymentResponse |
164
|
|
|
* |
165
|
|
|
* @throws \Exception Throws error if request fails. |
166
|
|
|
*/ |
167
|
|
|
public function request_payment_details( PaymentDetailsRequest $request ) { |
168
|
|
|
$data = $this->send_request( 'v51/payments/details', $request ); |
169
|
|
|
|
170
|
|
|
return PaymentResponse::from_object( $data ); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Create payment session. |
175
|
|
|
* |
176
|
|
|
* @param PaymentSessionRequest $request Payment session request. |
177
|
|
|
* |
178
|
|
|
* @return PaymentSessionResponse |
179
|
|
|
* |
180
|
|
|
* @throws \Exception Throws error if request fails. |
181
|
|
|
*/ |
182
|
1 |
|
public function create_payment_session( PaymentSessionRequest $request ) { |
183
|
1 |
|
$data = $this->send_request( 'v41/paymentSession', $request ); |
184
|
|
|
|
185
|
1 |
|
return PaymentSessionResponse::from_object( $data ); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Get payment result. |
190
|
|
|
* |
191
|
|
|
* @param PaymentResultRequest $request Payment result request. |
192
|
|
|
* |
193
|
|
|
* @return PaymentResultResponse |
194
|
|
|
* |
195
|
|
|
* @throws \Exception Throws error if request fails. |
196
|
|
|
*/ |
197
|
2 |
|
public function get_payment_result( PaymentResultRequest $request ) { |
198
|
2 |
|
$data = $this->send_request( 'v41/payments/result', $request ); |
199
|
|
|
|
200
|
1 |
|
return PaymentResultResponse::from_object( $data ); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Get payment methods. |
205
|
|
|
* |
206
|
|
|
* @link https://docs.adyen.com/api-explorer/#/PaymentSetupAndVerificationService/v51/paymentMethods |
207
|
|
|
* @link https://docs.adyen.com/checkout/drop-in-web#step-1-get-available-payment-methods |
208
|
|
|
* |
209
|
|
|
* @param PaymentMethodsRequest $request Payment methods request. |
210
|
|
|
* |
211
|
|
|
* @return PaymentMethodsResponse |
212
|
|
|
* @throws \Exception Throws error if request fails. |
213
|
|
|
*/ |
214
|
7 |
|
public function get_payment_methods( PaymentMethodsRequest $request ) { |
215
|
7 |
|
$data = $this->send_request( 'v51/paymentMethods', $request ); |
216
|
|
|
|
217
|
1 |
|
return PaymentMethodsResponse::from_object( $data ); |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|