PluginProbe
PayPlug for WooCommerce (Official) / 3.0.0
PayPlug for WooCommerce (Official) v3.0.0
3.0.0 2.18.0 1.0.17 1.0.18 1.0.19 1.0.20 1.0.21 1.0.22 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.1.0 1.10.0 1.10.1 1.2.1 1.2.10 1.2.11 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 All 101 releases
payplug / src / Gateway / PayplugApi.php

PayplugApi.php in PayPlug for WooCommerce (Official) 3.0.0, at src/Gateway/PayplugApi.php

362 lines 11.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Payplug\PayplugWoocommerce\Gateway;
4
5 // Exit if accessed directly
6 if (!defined('ABSPATH')) {
7 exit;
8 }
9
10 use Payplug\Core\HttpClient;
11 use Payplug\Exception\BadRequestException;
12 use Payplug\Exception\ConfigurationException;
13 use Payplug\Exception\ForbiddenException;
14 use Payplug\Exception\NotFoundException;
15 use Payplug\Exception\PayplugServerException;
16 use Payplug\Exception\UnauthorizedException;
17 use Payplug\Payplug;
18 use Payplug\PayplugWoocommerce\PayplugWoocommerceHelper;
19 use Payplug\PayplugWoocommerce\Traits\ServiceGetter;
20
21 /**
22 * Handle calls to PayPlug PHP client.
23 */
24 class PayplugApi
25 {
26 use ServiceGetter;
27
28 /**
29 * @var PayplugGateway
30 */
31 protected $gateway;
32
33 private $api_payplug;
34
35 /**
36 * @var string
37 */
38 private $bearer_token = '';
39
40 /**
41 * PayplugApi constructor.
42 *
43 * @param PayplugGateway $gateway
44 */
45 public function __construct($gateway)
46 {
47 $this->gateway = $gateway;
48 }
49
50 /**
51 * Get register url
52 *
53 * @param string $callback_uri
54 *
55 * @throws \Payplug\Exception\ConfigurationException
56 *
57 * @return object
58 */
59 public function retrieve_register_url($callback_uri)
60 {
61 return $this->do_request_with_fallback('\Payplug\Authentication::getRegisterUrl', [$callback_uri, $callback_uri]);
62 }
63
64 /**
65 * Try to refresh the JWT and update the API key if successful.
66 *
67 * @param string $current_mode The current mode (test or live) for which to refresh the JWT.
68 *
69 * @return bool
70 */
71 private function try_refresh_jwt($current_mode)
72 {
73 if (class_exists('Payplug\\PayplugWoocommerce\\Service\\Api')) {
74 $apiService = $this->get_api();
75 $configuration = $this->gateway->get_configuration();
76 $options = $configuration->get_options();
77 $oauth_client_data = isset($options['oauth_client_data']) ? json_decode($options['oauth_client_data'], true) : [];
78 $client_data = isset($oauth_client_data[$current_mode]) ? $oauth_client_data[$current_mode] : [];
79 if (!empty($client_data)) {
80 $new_jwt = $apiService->generate_jwt(
81 isset($client_data['client_id']) ? $client_data['client_id'] : '',
82 isset($client_data['client_secret']) ? $client_data['client_secret'] : ''
83 );
84 if (!empty($new_jwt) && isset($new_jwt['access_token'])) {
85 $jwt = isset($options['jwt']) ? json_decode($options['jwt'], true) : [];
86 $jwt[$current_mode] = $new_jwt;
87 $api_keys = isset($options['api_key']) ? json_decode($options['api_key'], true) : [];
88 $api_keys[$current_mode] = $new_jwt['access_token'];
89 $configuration->update_option('jwt', json_encode($jwt));
90 $configuration->update_option('api_key', json_encode($api_keys));
91
92 return true;
93 }
94 }
95 }
96
97 return false;
98 }
99
100 /**
101 * Configure PayPlug client.
102 */
103 public function init(): void
104 {
105 $current_mode = PayplugWoocommerceHelper::check_mode() ? 'live' : 'test';
106 $bearer_token = $this->get_api()->get_bearer_token($current_mode);
107 $this->bearer_token = (string) $bearer_token;
108 try {
109 $this->api_payplug = Payplug::init([
110 'secretKey' => (string) $bearer_token,
111 'apiVersion' => '2019-08-06',
112 ]);
113 HttpClient::setDefaultUserAgentProduct(
114 'PayPlug-WooCommerce',
115 PAYPLUG_GATEWAY_VERSION,
116 sprintf('WooCommerce/%s', WC()->version)
117 );
118 } catch (ConfigurationException $e) {
119 if ($this->try_refresh_jwt($current_mode)) {
120 if (!is_string($bearer_token) || empty($bearer_token)) {
121 PayplugWoocommerceHelper::payplug_logout();
122 throw $e;
123 }
124 $this->api_payplug = Payplug::init([
125 'secretKey' => (string) $bearer_token,
126 'apiVersion' => '2019-08-06',
127 ]);
128 HttpClient::setDefaultUserAgentProduct(
129 'PayPlug-WooCommerce',
130 PAYPLUG_GATEWAY_VERSION,
131 sprintf('WooCommerce/%s', WC()->version)
132 );
133 } else {
134 PayplugWoocommerceHelper::payplug_logout();
135 throw $e;
136 }
137 }
138 }
139
140 /**
141 * The bearer token resolved by init(), so callers built right after it (see
142 * PayplugGateway::init_payplug()) can reuse it instead of calling
143 * Api::get_bearer_token() again for the same mode.
144 *
145 * @return string
146 */
147 public function get_current_bearer_token(): string
148 {
149 return $this->bearer_token;
150 }
151
152 /**
153 * Retrieve payment data from PayPlug API.
154 *
155 * @param string $transaction_id
156 *
157 * @throws \Payplug\Exception\ConfigurationException
158 *
159 * @return null|\Payplug\Resource\Payment
160 */
161 public function payment_retrieve($transaction_id)
162 {
163 return $this->do_request_with_fallback('\Payplug\Payment::retrieve', $transaction_id);
164 }
165
166 /**
167 * Create a payment.
168 *
169 * @param array $data
170 *
171 * @return null|\Payplug\Resource\Payment
172 */
173 public function payment_create($data)
174 {
175 return $this->do_request('\Payplug\Payment::create', [$data]);
176 }
177
178 /**
179 * Retrieve all refunds associated with a payment.
180 *
181 * @param string $transaction_id
182 *
183 * @throws \Payplug\Exception\ConfigurationException
184 *
185 * @return \Payplug\Resource\Refund[]
186 */
187 public function refund_list($transaction_id)
188 {
189 return $this->do_request_with_fallback('\Payplug\Refund::listRefunds', $transaction_id);
190 }
191
192 /**
193 * Retrieve refund data from PayPlug API.
194 *
195 * @param string $transaction_id
196 * @param string $refund_id
197 *
198 * @throws \Payplug\Exception\ConfigurationException
199 *
200 * @return \Payplug\Resource\Refund
201 */
202 public function refund_retrieve($transaction_id, $refund_id)
203 {
204 return $this->do_request_with_fallback('\Payplug\Refund::retrieve', [$transaction_id, $refund_id]);
205 }
206
207 /**
208 * Create a refund.
209 *
210 * @param string $transaction_id
211 * @param array $data
212 *
213 * @throws \Payplug\Exception\ConfigurationException
214 *
215 * @return null|\Payplug\Resource\Refund
216 *
217 * @author Clément Boirie
218 */
219 public function refund_create($transaction_id, $data)
220 {
221 return $this->do_request_with_fallback('\Payplug\Refund::create', [$transaction_id, $data]);
222 }
223
224 /**
225 * Simulate a oney payment
226 *
227 * @return array
228 */
229 public function simulate_oney_payment($price, $oney_type = 'with_fees')
230 {
231 $country = PayplugWoocommerceHelper::get_payplug_merchant_country();
232 $oney_fees = ['x3_' . $oney_type, 'x4_' . $oney_type];
233
234 try {
235 try {
236 try {
237 try {
238 $response = $this->do_request('\Payplug\OneySimulation::getSimulations', [[
239 'amount' => intval(floatval($price) * 100),
240 'country' => $country,
241 'operations' => $oney_fees,
242 ]]);
243 PayplugWoocommerceHelper::oney_simulation_values($oney_fees, $response);
244 } catch (PayplugServerException $e) {
245 $response = __('Your payment schedule simulation is temporarily unavailable. You will find this information at the payment stage.', 'payplug');
246 }
247 } catch (BadRequestException $e) {
248 $response = __('Your payment schedule simulation is temporarily unavailable. You will find this information at the payment stage.', 'payplug');
249 }
250 } catch (UnauthorizedException $e) {
251 $response = __('Your payment schedule simulation is temporarily unavailable. You will find this information at the payment stage.', 'payplug');
252 }
253 } catch (ForbiddenException $e) {
254 $response = __('Your payment schedule simulation is temporarily unavailable. You will find this information at the payment stage.', 'payplug');
255 }
256
257 return $response;
258 }
259
260 public function validate_jwt($client_data, $jwt)
261 {
262 return $this->do_request_with_fallback('\Payplug\Authentication::validateJWT', [$client_data, $jwt]);
263 }
264
265 /**
266 * Invoke PayPlug API. If it fail it switch to the other mode and retry the same request.
267 *
268 * @param callable $callback
269 * @param array $params
270 *
271 * @throws \Payplug\Exception\ConfigurationException
272 *
273 * @return object
274 */
275 protected function do_request_with_fallback($callback, $params = [])
276 {
277 try {
278 $response = $this->do_request($callback, $params);
279 } catch (NotFoundException $e) {
280 try {
281 $this->switch_mode();
282 $response = $this->do_request($callback, $params);
283 $this->restore_mode();
284 } catch (\Exception $e) {
285 $this->restore_mode();
286 throw $e;
287 }
288 }
289
290 return $response;
291 }
292
293 /**
294 * Invoke PayPlug API.
295 *
296 * @param callable $callback
297 * @param array $params
298 *
299 * @return object
300 */
301 protected function do_request($callback, $params = [])
302 {
303 if (!is_array($params)) {
304 $params = [$params];
305 }
306
307 try {
308 return call_user_func_array($callback, $params);
309 } catch (UnauthorizedException $e) {
310 $current_mode = PayplugWoocommerceHelper::check_mode() ? 'live' : 'test';
311 if ($this->try_refresh_jwt($current_mode)) {
312 Payplug::setSecretKey((string) $this->gateway->get_api_key($current_mode));
313 try {
314 return call_user_func_array($callback, $params);
315 } catch (UnauthorizedException $e) {
316 PayplugWoocommerceHelper::payplug_logout();
317 throw $e;
318 }
319 }
320 PayplugWoocommerceHelper::payplug_logout();
321 throw $e;
322 }
323 }
324
325 /**
326 * Reconfigure PayPlug client with new secret keys.
327 *
328 * @throws \Payplug\Exception\ConfigurationException
329 */
330 protected function switch_mode(): void
331 {
332 $switched_mode = 'test' === $this->gateway->get_current_mode() ? 'live' : 'test';
333 $new_key = $this->gateway->get_api_key($switched_mode);
334 if (empty($new_key)) {
335 throw new \Exception(sprintf(
336 'No secret key available for the %s mode',
337 $switched_mode
338 ));
339 }
340
341 Payplug::setSecretKey($new_key);
342 }
343
344 /**
345 * Restore PayPlug client secret keys for the current mode.
346 *
347 * @throws \Payplug\Exception\ConfigurationException
348 */
349 protected function restore_mode(): void
350 {
351 $key = $this->gateway->get_api_key($this->gateway->get_current_mode());
352 if (empty($key)) {
353 throw new \Exception(sprintf(
354 'No secret key available for the %s mode',
355 $this->gateway->get_current_mode()
356 ));
357 }
358
359 Payplug::setSecretKey($key);
360 }
361 }
362