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