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
← All changes | src/Gateway/PayplugApi.php +290 -158 1.1.0 → 3.1.0 View file →
@@ -2,189 +2,321 @@
2 2
3 3 namespace Payplug\PayplugWoocommerce\Gateway;
4 4
5 5 // Exit if accessed directly
6 -if ( ! defined( 'ABSPATH' ) ) {
7 - exit;
6 +if (!defined('ABSPATH')) {
7 + exit;
8 8 }
9 9
10 +use Payplug\Core\HttpClient;
11 +use Payplug\Exception\ConfigurationException;
10 12 use Payplug\Exception\NotFoundException;
13 +use Payplug\Exception\UnauthorizedException;
11 14 use Payplug\Payplug;
12 -use Payplug\Core\HttpClient;
15 +use Payplug\PayplugWoocommerce\PayplugWoocommerceHelper;
16 +use Payplug\PayplugWoocommerce\Traits\ServiceGetter;
13 17
14 18 /**
15 19 * Handle calls to PayPlug PHP client.
16 - *
17 - * @package Payplug\PayplugWoocommerce\Gateway
18 20 */
19 -class PayplugApi {
21 +class PayplugApi
22 +{
23 + use ServiceGetter;
20 24
21 - /**
22 - * @var PayplugGateway
23 - */
24 - protected $gateway;
25 + /**
26 + * @var PayplugGateway
27 + */
28 + protected $gateway;
25 29
26 - /**
27 - * PayplugApi constructor.
28 - *
29 - * @param PayplugGateway $gateway
30 - */
31 - public function __construct( $gateway ) {
32 - $this->gateway = $gateway;
33 - }
30 + private $api_payplug;
34 31
35 - /**
36 - * Configure PayPlug client.
37 - */
38 - public function init() {
39 - $current_mode = $this->gateway->get_current_mode();
40 - $key = $this->gateway->get_api_key( $current_mode );
32 + /**
33 + * @var string
34 + */
35 + private $bearer_token = '';
41 36
42 - Payplug::init(array(
43 - 'secretKey' => $key,
44 - 'apiVersion' => "2019-08-06",
45 - ));
46 - HttpClient::addDefaultUserAgentProduct(
47 - 'PayPlug-WooCommerce',
48 - PAYPLUG_GATEWAY_VERSION,
49 - sprintf( 'WooCommerce/%s', WC()->version )
50 - );
51 - }
37 + /**
38 + * PayplugApi constructor.
39 + *
40 + * @param PayplugGateway $gateway
41 + */
42 + public function __construct($gateway)
43 + {
44 + $this->gateway = $gateway;
45 + }
52 46
53 - /**
54 - * Retrieve payment data from PayPlug API.
55 - *
56 - * @param string $transaction_id
57 - *
58 - * @return null|\Payplug\Resource\Payment
59 - * @throws \Payplug\Exception\ConfigurationException
60 - */
61 - public function payment_retrieve( $transaction_id ) {
62 - return $this->do_request_with_fallback( '\Payplug\Payment::retrieve', $transaction_id );
63 - }
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 + }
64 60
65 - /**
66 - * Create a payment.
67 - *
68 - * @param array $data
69 - *
70 - * @return null|\Payplug\Resource\Payment
71 - */
72 - public function payment_create( $data ) {
73 - return $this->do_request( '\Payplug\Payment::create', [ $data ] );
74 - }
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));
75 88
76 - /**
77 - * Retrieve all refunds associated with a payment.
78 - *
79 - * @param string $transaction_id
80 - *
81 - * @return \Payplug\Resource\Refund[]
82 - * @throws \Payplug\Exception\ConfigurationException
83 - */
84 - public function refund_list( $transaction_id ) {
85 - return $this->do_request_with_fallback( '\Payplug\Refund::listRefunds', $transaction_id );
86 - }
89 + return true;
90 + }
91 + }
92 + }
87 93
88 - /**
89 - * Retrieve refund data from PayPlug API.
90 - *
91 - * @param string $transaction_id
92 - * @param string $refund_id
93 - *
94 - * @return \Payplug\Resource\Refund
95 - * @throws \Payplug\Exception\ConfigurationException
96 - */
97 - public function refund_retrieve( $transaction_id, $refund_id ) {
98 - return $this->do_request_with_fallback( '\Payplug\Refund::retrieve', [ $transaction_id, $refund_id ] );
99 - }
94 + return false;
95 + }
100 96
101 - /**
102 - * Create a refund.
103 - *
104 - * @param string $transaction_id
105 - * @param array $data
106 - *
107 - * @return null|\Payplug\Resource\Refund
108 - * @throws \Payplug\Exception\ConfigurationException
109 - * @author Clément Boirie
110 - */
111 - public function refund_create( $transaction_id, $data ) {
112 - return $this->do_request_with_fallback( '\Payplug\Refund::create', [ $transaction_id, $data ] );
113 - }
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 + }
114 136
115 - /**
116 - * Invoke PayPlug API. If it fail it switch to the other mode and retry the same request.
117 - *
118 - * @param callable $callback
119 - * @param array $params
120 - *
121 - * @return object
122 - * @throws \Payplug\Exception\ConfigurationException
123 - */
124 - protected function do_request_with_fallback( $callback, $params = [] ) {
125 - try {
126 - $response = $this->do_request( $callback, $params );
127 - } catch ( NotFoundException $e ) {
128 - try {
129 - $this->switch_mode();
130 - $response = $this->do_request( $callback, $params );
131 - $this->restore_mode();
132 - } catch ( \Exception $e ) {
133 - $this->restore_mode();
134 - throw $e;
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 + }
137 148
138 - return $response;
139 - }
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 + }
140 162
141 - /**
142 - * Invoke PayPlug API.
143 - *
144 - * @param callable $callback
145 - * @param array $params
146 - *
147 - * @return object
148 - */
149 - protected function do_request( $callback, $params = [] ) {
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 + }
150 174
151 - if ( ! is_array( $params ) ) {
152 - $params = [ $params ];
153 - }
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 + }
154 188
155 - return call_user_func_array( $callback, $params );
156 - }
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 + }
157 203
158 - /**
159 - * Reconfigure PayPlug client with new secret keys.
160 - *
161 - * @throws \Payplug\Exception\ConfigurationException
162 - */
163 - protected function switch_mode() {
164 - $switched_mode = 'test' === $this->gateway->get_current_mode() ? 'live' : 'test';
165 - $new_key = $this->gateway->get_api_key( $switched_mode );
166 - if ( empty( $new_key ) ) {
167 - throw new \Exception( sprintf(
168 - 'No secret key available for the %s mode', $switched_mode
169 - ) );
170 - }
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 + }
171 220
172 - Payplug::setSecretKey( $new_key );
173 - }
221 + public function validate_jwt($client_data, $jwt)
222 + {
223 + return $this->do_request_with_fallback('\Payplug\Authentication::validateJWT', [$client_data, $jwt]);
224 + }
174 225
175 - /**
176 - * Restore PayPlug client secret keys for the current mode.
177 - *
178 - * @throws \Payplug\Exception\ConfigurationException
179 - */
180 - protected function restore_mode() {
181 - $key = $this->gateway->get_api_key( $this->gateway->get_current_mode() );
182 - if ( empty( $key ) ) {
183 - throw new \Exception( sprintf(
184 - 'No secret key available for the %s mode', $this->gateway->get_current_mode()
185 - ) );
186 - }
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 + }
187 250
188 - Payplug::setSecretKey( $key );
189 - }
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 + }
190 322 }