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 +283 -170 1.2.4 → 3.1.0 View file →
@@ -6,204 +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::addDefaultUserAgentProduct(
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"];
125 - $response = $this->do_request('\Payplug\OneySimulation::getSimulations', [[
126 - "amount" => (int) $price * 100,
127 - "country" => $country['country'],
128 - "operations" => $oney_fees
129 - ]]);
130 - PayplugWoocommerceHelper::oney_simulation_values($oney_fees, $response);
131 - return $response;
146 + return $this->bearer_token;
132 147 }
133 148
134 - /**
135 - * Invoke PayPlug API. If it fail it switch to the other mode and retry the same request.
136 - *
137 - * @param callable $callback
138 - * @param array $params
139 - *
140 - * @return object
141 - * @throws \Payplug\Exception\ConfigurationException
142 - */
143 - protected function do_request_with_fallback( $callback, $params = [] ) {
144 - try {
145 - $response = $this->do_request( $callback, $params );
146 - } catch ( NotFoundException $e ) {
147 - try {
148 - $this->switch_mode();
149 - $response = $this->do_request( $callback, $params );
150 - $this->restore_mode();
151 - } catch ( \Exception $e ) {
152 - $this->restore_mode();
153 - throw $e;
154 - }
155 - }
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 + }
156 162
157 - return $response;
158 - }
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 + }
159 174
160 - /**
161 - * Invoke PayPlug API.
162 - *
163 - * @param callable $callback
164 - * @param array $params
165 - *
166 - * @return object
167 - */
168 - protected function do_request( $callback, $params = [] ) {
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 + }
169 188
170 - if ( ! is_array( $params ) ) {
171 - $params = [ $params ];
172 - }
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 + }
173 203
174 - return call_user_func_array( $callback, $params );
175 - }
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 + }
176 220
177 - /**
178 - * Reconfigure PayPlug client with new secret keys.
179 - *
180 - * @throws \Payplug\Exception\ConfigurationException
181 - */
182 - protected function switch_mode() {
183 - $switched_mode = 'test' === $this->gateway->get_current_mode() ? 'live' : 'test';
184 - $new_key = $this->gateway->get_api_key( $switched_mode );
185 - if ( empty( $new_key ) ) {
186 - throw new \Exception( sprintf(
187 - 'No secret key available for the %s mode', $switched_mode
188 - ) );
189 - }
221 + public function validate_jwt($client_data, $jwt)
222 + {
223 + return $this->do_request_with_fallback('\Payplug\Authentication::validateJWT', [$client_data, $jwt]);
224 + }
190 225
191 - Payplug::setSecretKey( $new_key );
192 - }
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 + }
193 250
194 - /**
195 - * Restore PayPlug client secret keys for the current mode.
196 - *
197 - * @throws \Payplug\Exception\ConfigurationException
198 - */
199 - protected function restore_mode() {
200 - $key = $this->gateway->get_api_key( $this->gateway->get_current_mode() );
201 - if ( empty( $key ) ) {
202 - throw new \Exception( sprintf(
203 - 'No secret key available for the %s mode', $this->gateway->get_current_mode()
204 - ) );
205 - }
251 + return $response;
252 + }
206 253
207 - Payplug::setSecretKey( $key );
208 - }
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 + }
209 322 }