PluginProbe
PayPlug for WooCommerce (Official) / 2.18.0
PayPlug for WooCommerce (Official) v2.18.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) 2.18.0, at src/Gateway/PayplugApi.php

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