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 | use Exception; |
||
14 | use Pronamic\WordPress\Pay\Core\Gateway as Core_Gateway; |
||
15 | use Pronamic\WordPress\Pay\Core\XML\Security; |
||
16 | use WP_Error; |
||
17 | |||
18 | /** |
||
19 | * Adyen client |
||
20 | * |
||
21 | * @author Remco Tolsma |
||
22 | * @version 1.0.0 |
||
23 | * @since 1.0.0 |
||
24 | * @link https://github.com/adyenpayments/php/blob/master/generatepaymentform.php |
||
25 | */ |
||
26 | class Client { |
||
27 | /** |
||
28 | * Config. |
||
29 | * |
||
30 | * @var Config |
||
31 | */ |
||
32 | private $config; |
||
33 | |||
34 | /** |
||
35 | * Constructs and initializes an Adyen client object. |
||
36 | * |
||
37 | * @param Config $config Adyen config. |
||
38 | */ |
||
39 | 6 | public function __construct( Config $config ) { |
|
40 | 6 | $this->config = $config; |
|
41 | 6 | } |
|
42 | |||
43 | /** |
||
44 | * Send request with the specified action and parameters |
||
45 | * |
||
46 | * @param string $method Adyen API method. |
||
47 | * @param Request $request Request object. |
||
48 | * @return object |
||
49 | * @throws Exception Throws exception when error occurs. |
||
50 | */ |
||
51 | 6 | private function send_request( $method, $request ) { |
|
52 | // Request. |
||
53 | 6 | $url = $this->config->get_api_url( $method ); |
|
54 | |||
55 | 6 | $response = wp_remote_request( |
|
56 | 6 | $url, |
|
57 | array( |
||
58 | 6 | 'method' => 'POST', |
|
59 | 'headers' => array( |
||
60 | 6 | 'X-API-key' => $this->config->get_api_key(), |
|
61 | 6 | 'Content-Type' => 'application/json', |
|
62 | ), |
||
63 | 6 | 'body' => wp_json_encode( $request->get_json() ), |
|
64 | ) |
||
65 | ); |
||
66 | |||
67 | 6 | if ( $response instanceof WP_Error ) { |
|
68 | 1 | throw new Exception( $response->get_error_message() ); |
|
69 | } |
||
70 | |||
71 | // Body. |
||
72 | 5 | $body = wp_remote_retrieve_body( $response ); |
|
73 | |||
74 | 5 | $data = json_decode( $body ); |
|
75 | |||
76 | // JSON error. |
||
77 | 5 | $json_error = json_last_error(); |
|
78 | |||
79 | 5 | if ( JSON_ERROR_NONE !== $json_error ) { |
|
80 | throw new Exception( |
||
81 | json_last_error_msg(), |
||
82 | $json_error |
||
83 | ); |
||
84 | } |
||
85 | |||
86 | // Object. |
||
87 | 5 | if ( ! is_object( $data ) ) { |
|
88 | $code = wp_remote_retrieve_response_code( $response ); |
||
89 | |||
90 | throw new Exception( |
||
91 | sprintf( 'Could not JSON decode Adyen response to an object (HTTP Status Code: %s).', $code ), |
||
92 | intval( $code ) |
||
93 | ); |
||
94 | } |
||
95 | |||
96 | // Error. |
||
97 | 5 | if ( isset( $data->error ) ) { |
|
98 | 1 | $error = Error::from_object( $data->error ); |
|
99 | |||
100 | 1 | throw $error; |
|
101 | } |
||
102 | |||
103 | // Service Exception. |
||
104 | 4 | if ( isset( $data->status, $data->errorCode, $data->message, $data->errorType ) ) { |
|
105 | 1 | $service_exception = ServiceException::from_object( $data ); |
|
106 | |||
107 | 1 | throw $service_exception; |
|
108 | } |
||
109 | |||
110 | 3 | return $data; |
|
111 | } |
||
112 | |||
113 | /** |
||
114 | * Create payment. |
||
115 | * |
||
116 | * @param PaymentRequest $request Payment request. |
||
117 | * @return PaymentResponse |
||
118 | */ |
||
119 | 1 | public function create_payment( PaymentRequest $request ) { |
|
120 | 1 | $data = $this->send_request( 'payments', $request ); |
|
121 | |||
122 | 1 | return PaymentResponse::from_object( $data ); |
|
123 | } |
||
124 | |||
125 | /** |
||
126 | * Create payment session. |
||
127 | * |
||
128 | * @param PaymentSessionRequest $request Payment session request. |
||
129 | * @return PaymentSessionResponse |
||
130 | */ |
||
131 | 1 | public function create_payment_session( PaymentSessionRequest $request ) { |
|
132 | 1 | $data = $this->send_request( 'paymentSession', $request ); |
|
133 | |||
134 | 1 | return PaymentSessionResponse::from_object( $data ); |
|
135 | } |
||
136 | |||
137 | /** |
||
138 | * Get payment result. |
||
139 | * |
||
140 | * @param PaymentResultRequest $request Payment result request. |
||
141 | * @return PaymentResultResponse |
||
142 | */ |
||
143 | public function get_payment_result( PaymentResultRequest $request ) { |
||
144 | $data = $this->send_request( 'payments/result', $request ); |
||
145 | |||
146 | return PaymentResultResponse::from_object( $data ); |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * Get payment methods. |
||
151 | * |
||
152 | * @return array|bool |
||
153 | */ |
||
154 | 4 | public function get_payment_methods() { |
|
155 | 4 | $request = new PaymentMethodsRequest( $this->config->get_merchant_account() ); |
|
156 | |||
157 | 4 | $data = $this->send_request( 'paymentMethods', $request ); |
|
158 | |||
159 | 1 | return PaymentMethodsResponse::from_object( $data ); |
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
160 | } |
||
161 | } |
||
162 |