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
|
|
|
use Pronamic\WordPress\Pay\Facades\Http; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Adyen client |
17
|
|
|
* |
18
|
|
|
* @link https://github.com/adyenpayments/php/blob/master/generatepaymentform.php |
19
|
|
|
* |
20
|
|
|
* @author Remco Tolsma |
21
|
|
|
* @version 1.0.5 |
22
|
|
|
* @since 1.0.0 |
23
|
|
|
*/ |
24
|
|
|
class Client { |
25
|
|
|
/** |
26
|
|
|
* Config. |
27
|
|
|
* |
28
|
|
|
* @var Config |
29
|
|
|
*/ |
30
|
|
|
private $config; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Constructs and initializes an Adyen client object. |
34
|
|
|
* |
35
|
|
|
* @param Config $config Adyen config. |
36
|
|
|
*/ |
37
|
13 |
|
public function __construct( Config $config ) { |
38
|
13 |
|
$this->config = $config; |
39
|
13 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Send request with the specified action and parameters |
43
|
|
|
* |
44
|
|
|
* @param string $method Adyen API method. |
45
|
|
|
* @param Request $request Request object. |
46
|
|
|
* @return object |
47
|
|
|
* @throws \Exception Throws exception when error occurs. |
48
|
|
|
*/ |
49
|
11 |
|
private function send_request( $method, $request ) { |
50
|
|
|
// Request. |
51
|
11 |
|
$url = $this->config->get_api_url( $method ); |
52
|
|
|
|
53
|
10 |
|
$response = Http::request( |
54
|
10 |
|
$url, |
55
|
|
|
array( |
56
|
10 |
|
'method' => 'POST', |
57
|
|
|
'headers' => array( |
58
|
10 |
|
'X-API-key' => $this->config->get_api_key(), |
59
|
10 |
|
'Content-Type' => 'application/json', |
60
|
|
|
), |
61
|
10 |
|
'body' => \wp_json_encode( $request->get_json() ), |
62
|
|
|
) |
63
|
|
|
); |
64
|
|
|
|
65
|
10 |
|
$data = $response->json(); |
66
|
|
|
|
67
|
|
|
// Object. |
68
|
10 |
|
if ( ! \is_object( $data ) ) { |
69
|
|
|
throw new \Exception( |
70
|
|
|
\sprintf( |
71
|
|
|
'Could not JSON decode Adyen response to an object, HTTP response: "%s", HTTP body: "%s".', |
72
|
|
|
$response->status(), |
73
|
|
|
$response->body() |
74
|
|
|
), |
75
|
|
|
\intval( $response->status() ) |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// Error. |
80
|
10 |
|
if ( isset( $data->error ) ) { |
81
|
1 |
|
$error = Error::from_object( $data->error ); |
82
|
|
|
|
83
|
1 |
|
throw $error; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// Service Exception. |
87
|
|
|
// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- Adyen JSON object. |
88
|
9 |
|
if ( isset( $data->status, $data->errorCode, $data->message, $data->errorType ) ) { |
89
|
5 |
|
$service_exception = ServiceException::from_object( $data ); |
90
|
|
|
|
91
|
5 |
|
throw $service_exception; |
92
|
|
|
} |
93
|
|
|
|
94
|
4 |
|
return $data; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Create payment. |
99
|
|
|
* |
100
|
|
|
* @param PaymentRequest $request Payment request. |
101
|
|
|
* |
102
|
|
|
* @return PaymentResponse |
103
|
|
|
* |
104
|
|
|
* @throws \Exception Throws error if request fails. |
105
|
|
|
*/ |
106
|
1 |
|
public function create_payment( PaymentRequest $request ) { |
107
|
1 |
|
$data = $this->send_request( 'v51/payments', $request ); |
108
|
|
|
|
109
|
1 |
|
return PaymentResponse::from_object( $data ); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Submit additional payment details. |
114
|
|
|
* |
115
|
|
|
* @param PaymentDetailsRequest $request Payment request. |
116
|
|
|
* |
117
|
|
|
* @return PaymentResponse |
118
|
|
|
* |
119
|
|
|
* @throws \Exception Throws error if request fails. |
120
|
|
|
*/ |
121
|
|
|
public function request_payment_details( PaymentDetailsRequest $request ) { |
122
|
|
|
$data = $this->send_request( 'v51/payments/details', $request ); |
123
|
|
|
|
124
|
|
|
return PaymentResponse::from_object( $data ); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Create payment session. |
129
|
|
|
* |
130
|
|
|
* @param PaymentSessionRequest $request Payment session request. |
131
|
|
|
* |
132
|
|
|
* @return PaymentSessionResponse |
133
|
|
|
* |
134
|
|
|
* @throws \Exception Throws error if request fails. |
135
|
|
|
*/ |
136
|
1 |
|
public function create_payment_session( PaymentSessionRequest $request ) { |
137
|
1 |
|
$data = $this->send_request( 'v41/paymentSession', $request ); |
138
|
|
|
|
139
|
1 |
|
return PaymentSessionResponse::from_object( $data ); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Get payment result. |
144
|
|
|
* |
145
|
|
|
* @param PaymentResultRequest $request Payment result request. |
146
|
|
|
* |
147
|
|
|
* @return PaymentResultResponse |
148
|
|
|
* |
149
|
|
|
* @throws \Exception Throws error if request fails. |
150
|
|
|
*/ |
151
|
2 |
|
public function get_payment_result( PaymentResultRequest $request ) { |
152
|
2 |
|
$data = $this->send_request( 'v41/payments/result', $request ); |
153
|
|
|
|
154
|
1 |
|
return PaymentResultResponse::from_object( $data ); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Get payment methods. |
159
|
|
|
* |
160
|
|
|
* @link https://docs.adyen.com/api-explorer/#/PaymentSetupAndVerificationService/v51/paymentMethods |
161
|
|
|
* @link https://docs.adyen.com/checkout/drop-in-web#step-1-get-available-payment-methods |
162
|
|
|
* |
163
|
|
|
* @param PaymentMethodsRequest $request Payment methods request. |
164
|
|
|
* |
165
|
|
|
* @return PaymentMethodsResponse |
166
|
|
|
* @throws \Exception Throws error if request fails. |
167
|
|
|
*/ |
168
|
7 |
|
public function get_payment_methods( PaymentMethodsRequest $request ) { |
169
|
7 |
|
$data = $this->send_request( 'v51/paymentMethods', $request ); |
170
|
|
|
|
171
|
1 |
|
return PaymentMethodsResponse::from_object( $data ); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|