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 +284 -175 1.2.11 → 3.1.0 View file →
@@ -6,208 +6,317 @@
6 6 if (!defined('ABSPATH')) {
7 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;
13 15 use Payplug\PayplugWoocommerce\PayplugWoocommerceHelper;
16 +use Payplug\PayplugWoocommerce\Traits\ServiceGetter;
14 17
15 18 /**
16 19 * Handle calls to PayPlug PHP client.
17 - *
18 - * @package Payplug\PayplugWoocommerce\Gateway
19 20 */
20 -class PayplugApi {
21 +class PayplugApi
22 +{
23 + use ServiceGetter;
21 24
22 - /**
23 - * @var PayplugGateway
24 - */
25 - protected $gateway;
25 + /**
26 + * @var PayplugGateway
27 + */
28 + protected $gateway;
26 29
27 - /**
28 - * PayplugApi constructor.
29 - *
30 - * @param PayplugGateway $gateway
31 - */
32 - public function __construct( $gateway ) {
33 - $this->gateway = $gateway;
34 - }
30 + private $api_payplug;
35 31
36 - /**
37 - * Configure PayPlug client.
38 - */
39 - public function init() {
40 - $current_mode = $this->gateway->get_current_mode();
41 - $key = $this->gateway->get_api_key( $current_mode );
32 + /**
33 + * @var string
34 + */
35 + private $bearer_token = '';
42 36
43 - Payplug::init(array(
44 - 'secretKey' => $key,
45 - 'apiVersion' => "2019-08-06",
46 - ));
47 - HttpClient::setDefaultUserAgentProduct(
48 - 'PayPlug-WooCommerce',
49 - PAYPLUG_GATEWAY_VERSION,
50 - sprintf( 'WooCommerce/%s', WC()->version )
51 - );
52 - }
37 + /**
38 + * PayplugApi constructor.
39 + *
40 + * @param PayplugGateway $gateway
41 + */
42 + public function __construct($gateway)
43 + {
44 + $this->gateway = $gateway;
45 + }
53 46
54 - /**
55 - * Retrieve payment data from PayPlug API.
56 - *
57 - * @param string $transaction_id
58 - *
59 - * @return null|\Payplug\Resource\Payment
60 - * @throws \Payplug\Exception\ConfigurationException
61 - */
62 - public function payment_retrieve( $transaction_id ) {
63 - return $this->do_request_with_fallback( '\Payplug\Payment::retrieve', $transaction_id );
64 - }
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 + }
65 60
66 - /**
67 - * Create a payment.
68 - *
69 - * @param array $data
70 - *
71 - * @return null|\Payplug\Resource\Payment
72 - */
73 - public function payment_create( $data ) {
74 - return $this->do_request( '\Payplug\Payment::create', [ $data ] );
75 - }
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));
76 88
77 - /**
78 - * Retrieve all refunds associated with a payment.
79 - *
80 - * @param string $transaction_id
81 - *
82 - * @return \Payplug\Resource\Refund[]
83 - * @throws \Payplug\Exception\ConfigurationException
84 - */
85 - public function refund_list( $transaction_id ) {
86 - return $this->do_request_with_fallback( '\Payplug\Refund::listRefunds', $transaction_id );
87 - }
89 + return true;
90 + }
91 + }
92 + }
88 93
89 - /**
90 - * Retrieve refund data from PayPlug API.
91 - *
92 - * @param string $transaction_id
93 - * @param string $refund_id
94 - *
95 - * @return \Payplug\Resource\Refund
96 - * @throws \Payplug\Exception\ConfigurationException
97 - */
98 - public function refund_retrieve( $transaction_id, $refund_id ) {
99 - return $this->do_request_with_fallback( '\Payplug\Refund::retrieve', [ $transaction_id, $refund_id ] );
100 - }
94 + return false;
95 + }
101 96
102 - /**
103 - * Create a refund.
104 - *
105 - * @param string $transaction_id
106 - * @param array $data
107 - *
108 - * @return null|\Payplug\Resource\Refund
109 - * @throws \Payplug\Exception\ConfigurationException
110 - * @author Clément Boirie
111 - */
112 - public function refund_create( $transaction_id, $data ) {
113 - return $this->do_request_with_fallback( '\Payplug\Refund::create', [ $transaction_id, $data ] );
114 - }
115 -
116 - /**
117 - * Simulate a oney payment
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.
118 141 *
119 - * @return array
142 + * @return string
120 143 */
121 - public function simulate_oney_payment($price)
144 + public function get_current_bearer_token(): string
122 145 {
123 - $country = wc_get_base_location();
124 - $oney_fees = ["x3_with_fees", "x4_with_fees"];
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 + {
125 238 try {
126 - $response = $this->do_request('\Payplug\OneySimulation::getSimulations', [[
127 - "amount" => (int) $price * 100,
128 - "country" => $country['country'],
129 - "operations" => $oney_fees
130 - ]]);
131 - PayplugWoocommerceHelper::oney_simulation_values($oney_fees, $response);
132 - } catch (\Payplug\Exception\PayplugServerException $e) {
133 - $response = __('Your payment schedule simulation is temporarily unavailable. You will find this information at the payment stage.', 'payplug');
134 - }
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 +
135 251 return $response;
136 252 }
137 253
138 - /**
139 - * Invoke PayPlug API. If it fail it switch to the other mode and retry the same request.
140 - *
141 - * @param callable $callback
142 - * @param array $params
143 - *
144 - * @return object
145 - * @throws \Payplug\Exception\ConfigurationException
146 - */
147 - protected function do_request_with_fallback( $callback, $params = [] ) {
148 - try {
149 - $response = $this->do_request( $callback, $params );
150 - } catch ( NotFoundException $e ) {
151 - try {
152 - $this->switch_mode();
153 - $response = $this->do_request( $callback, $params );
154 - $this->restore_mode();
155 - } catch ( \Exception $e ) {
156 - $this->restore_mode();
157 - throw $e;
158 - }
159 - }
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 + }
160 267
161 - return $response;
162 - }
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 + }
163 285
164 - /**
165 - * Invoke PayPlug API.
166 - *
167 - * @param callable $callback
168 - * @param array $params
169 - *
170 - * @return object
171 - */
172 - protected function do_request( $callback, $params = [] ) {
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 + }
173 301
174 - if ( ! is_array( $params ) ) {
175 - $params = [ $params ];
176 - }
177 -
178 - return call_user_func_array( $callback, $params );
179 - }
302 + Payplug::setSecretKey($new_key);
303 + }
180 304
181 - /**
182 - * Reconfigure PayPlug client with new secret keys.
183 - *
184 - * @throws \Payplug\Exception\ConfigurationException
185 - */
186 - protected function switch_mode() {
187 - $switched_mode = 'test' === $this->gateway->get_current_mode() ? 'live' : 'test';
188 - $new_key = $this->gateway->get_api_key( $switched_mode );
189 - if ( empty( $new_key ) ) {
190 - throw new \Exception( sprintf(
191 - 'No secret key available for the %s mode', $switched_mode
192 - ) );
193 - }
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 + }
194 319
195 - Payplug::setSecretKey( $new_key );
196 - }
197 -
198 - /**
199 - * Restore PayPlug client secret keys for the current mode.
200 - *
201 - * @throws \Payplug\Exception\ConfigurationException
202 - */
203 - protected function restore_mode() {
204 - $key = $this->gateway->get_api_key( $this->gateway->get_current_mode() );
205 - if ( empty( $key ) ) {
206 - throw new \Exception( sprintf(
207 - 'No secret key available for the %s mode', $this->gateway->get_current_mode()
208 - ) );
209 - }
210 -
211 - Payplug::setSecretKey( $key );
212 - }
320 + Payplug::setSecretKey($key);
321 + }
213 322 }