1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Adyen client |
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
|
|
|
* Adyen client |
15
|
|
|
* |
16
|
|
|
* @link https://github.com/adyenpayments/php/blob/master/generatepaymentform.php |
17
|
|
|
* |
18
|
|
|
* @author Remco Tolsma |
19
|
|
|
* @version 1.0.4 |
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
|
12 |
|
public function __construct( Config $config ) { |
36
|
12 |
|
$this->config = $config; |
37
|
12 |
|
} |
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
|
|
|
// Data. |
75
|
10 |
|
$data = json_decode( $body ); |
76
|
|
|
|
77
|
|
|
// JSON error. |
78
|
10 |
|
$json_error = json_last_error(); |
79
|
|
|
|
80
|
10 |
|
if ( \JSON_ERROR_NONE !== $json_error ) { |
81
|
2 |
|
throw new \Exception( |
82
|
2 |
|
\sprintf( |
83
|
2 |
|
'Could not JSON decode Adyen response, HTTP response: "%s %s", HTTP body length: "%d", JSON error: "%s".', |
84
|
2 |
|
$response_code, |
85
|
2 |
|
$response_message, |
86
|
2 |
|
\strlen( $body ), |
87
|
2 |
|
\json_last_error_msg() |
88
|
|
|
), |
89
|
|
|
$json_error |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
// Object. |
94
|
8 |
|
if ( ! \is_object( $data ) ) { |
95
|
1 |
|
throw new \Exception( |
96
|
1 |
|
\sprintf( |
97
|
1 |
|
'Could not JSON decode Adyen response to an object, HTTP response: "%s %s", HTTP body: "%s".', |
98
|
1 |
|
$response_code, |
99
|
1 |
|
$response_message, |
100
|
1 |
|
$body |
101
|
|
|
), |
102
|
1 |
|
\intval( $response_code ) |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// Error. |
107
|
7 |
|
if ( isset( $data->error ) ) { |
108
|
2 |
|
$error = Error::from_object( $data->error ); |
109
|
|
|
|
110
|
2 |
|
throw $error; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
// Service Exception. |
114
|
|
|
// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- Adyen JSON object. |
115
|
5 |
|
if ( isset( $data->status, $data->errorCode, $data->message, $data->errorType ) ) { |
116
|
1 |
|
$service_exception = ServiceException::from_object( $data ); |
117
|
|
|
|
118
|
1 |
|
throw $service_exception; |
119
|
|
|
} |
120
|
|
|
|
121
|
4 |
|
return $data; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Create payment. |
126
|
|
|
* |
127
|
|
|
* @param PaymentRequest $request Payment request. |
128
|
|
|
* |
129
|
|
|
* @return PaymentResponse |
130
|
|
|
* |
131
|
|
|
* @throws \Exception Throws error if request fails. |
132
|
|
|
*/ |
133
|
1 |
|
public function create_payment( PaymentRequest $request ) { |
134
|
1 |
|
$data = $this->send_request( 'payments', $request ); |
135
|
|
|
|
136
|
1 |
|
return PaymentResponse::from_object( $data ); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Create payment session. |
141
|
|
|
* |
142
|
|
|
* @param PaymentSessionRequest $request Payment session request. |
143
|
|
|
* |
144
|
|
|
* @return PaymentSessionResponse |
145
|
|
|
* |
146
|
|
|
* @throws \Exception Throws error if request fails. |
147
|
|
|
*/ |
148
|
1 |
|
public function create_payment_session( PaymentSessionRequest $request ) { |
149
|
1 |
|
$data = $this->send_request( 'paymentSession', $request ); |
150
|
|
|
|
151
|
1 |
|
return PaymentSessionResponse::from_object( $data ); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Get payment result. |
156
|
|
|
* |
157
|
|
|
* @param PaymentResultRequest $request Payment result request. |
158
|
|
|
* |
159
|
|
|
* @return PaymentResultResponse |
160
|
|
|
* |
161
|
|
|
* @throws \Exception Throws error if request fails. |
162
|
|
|
*/ |
163
|
2 |
|
public function get_payment_result( PaymentResultRequest $request ) { |
164
|
2 |
|
$data = $this->send_request( 'payments/result', $request ); |
165
|
|
|
|
166
|
1 |
|
return PaymentResultResponse::from_object( $data ); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Get payment methods. |
171
|
|
|
* |
172
|
|
|
* @return PaymentMethodsResponse |
173
|
|
|
* |
174
|
|
|
* @throws \Exception Throws error if request fails. |
175
|
|
|
*/ |
176
|
7 |
|
public function get_payment_methods() { |
177
|
7 |
|
$request = new PaymentMethodsRequest( $this->config->get_merchant_account() ); |
178
|
|
|
|
179
|
7 |
|
$data = $this->send_request( 'paymentMethods', $request ); |
180
|
|
|
|
181
|
1 |
|
return PaymentMethodsResponse::from_object( $data ); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|