PluginProbe
PayPlug for WooCommerce (Official) / 3.1.0
PayPlug for WooCommerce (Official) v3.1.0
3.1.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 All 102 releases
payplug / src / Gateway / PayplugApi.php

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

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