| @@ -2,173 +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::setSecretKey( $key ); | |
| 43 | - HttpClient::addDefaultUserAgentProduct( | |
| 44 | - 'PayPlug-WooCommerce', | |
| 45 | - PAYPLUG_GATEWAY_VERSION, | |
| 46 | - sprintf( 'WooCommerce/%s', WC()->version ) | |
| 47 | - ); | |
| 48 | - } | |
| 37 | + /** | |
| 38 | + * PayplugApi constructor. | |
| 39 | + * | |
| 40 | + * @param PayplugGateway $gateway | |
| 41 | + */ | |
| 42 | + public function __construct($gateway) | |
| 43 | + { | |
| 44 | + $this->gateway = $gateway; | |
| 45 | + } | |
| 49 | 46 | |
| 50 | - /** | |
| 51 | - * Retrieve payment data from PayPlug API. | |
| 52 | - * | |
| 53 | - * @param string $transaction_id | |
| 54 | - * | |
| 55 | - * @return null|\Payplug\Resource\Payment | |
| 56 | - * @throws \Payplug\Exception\ConfigurationException | |
| 57 | - */ | |
| 58 | - public function payment_retrieve( $transaction_id ) { | |
| 59 | - return $this->do_request_with_fallback( '\Payplug\Payment::retrieve', $transaction_id ); | |
| 60 | - } | |
| 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 | + } | |
| 61 | 60 | |
| 62 | - /** | |
| 63 | - * Create a payment. | |
| 64 | - * | |
| 65 | - * @param array $data | |
| 66 | - * | |
| 67 | - * @return null|\Payplug\Resource\Payment | |
| 68 | - */ | |
| 69 | - public function payment_create( $data ) { | |
| 70 | - return $this->do_request( '\Payplug\Payment::create', [ $data ] ); | |
| 71 | - } | |
| 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)); | |
| 72 | 88 | |
| 73 | - /** | |
| 74 | - * Retrieve all refunds associated with a payment. | |
| 75 | - * | |
| 76 | - * @param string $transaction_id | |
| 77 | - * | |
| 78 | - * @return \Payplug\Resource\Refund[] | |
| 79 | - * @throws \Payplug\Exception\ConfigurationException | |
| 80 | - */ | |
| 81 | - public function refund_list( $transaction_id ) { | |
| 82 | - return $this->do_request_with_fallback( '\Payplug\Refund::listRefunds', $transaction_id ); | |
| 83 | - } | |
| 89 | + return true; | |
| 90 | + } | |
| 91 | + } | |
| 92 | + } | |
| 84 | 93 | |
| 85 | - /** | |
| 86 | - * Create a refund. | |
| 87 | - * | |
| 88 | - * @param string $transaction_id | |
| 89 | - * @param array $data | |
| 90 | - * | |
| 91 | - * @return null|\Payplug\Resource\Refund | |
| 92 | - * @throws \Payplug\Exception\ConfigurationException | |
| 93 | - * @author Clément Boirie | |
| 94 | - */ | |
| 95 | - public function refund_create( $transaction_id, $data ) { | |
| 96 | - return $this->do_request_with_fallback( '\Payplug\Refund::create', [ $transaction_id, $data ] ); | |
| 97 | - } | |
| 94 | + return false; | |
| 95 | + } | |
| 98 | 96 | |
| 99 | - /** | |
| 100 | - * Invoke PayPlug API. If it fail it switch to the other mode and retry the same request. | |
| 101 | - * | |
| 102 | - * @param callable $callback | |
| 103 | - * @param array $params | |
| 104 | - * | |
| 105 | - * @return object | |
| 106 | - * @throws \Payplug\Exception\ConfigurationException | |
| 107 | - */ | |
| 108 | - protected function do_request_with_fallback( $callback, $params = [] ) { | |
| 109 | - try { | |
| 110 | - $response = $this->do_request( $callback, $params ); | |
| 111 | - } catch ( NotFoundException $e ) { | |
| 112 | - try { | |
| 113 | - $this->switch_mode(); | |
| 114 | - $response = $this->do_request( $callback, $params ); | |
| 115 | - $this->restore_mode(); | |
| 116 | - } catch ( \Exception $e ) { | |
| 117 | - $this->restore_mode(); | |
| 118 | - throw $e; | |
| 119 | - } | |
| 120 | - } | |
| 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 | + } | |
| 121 | 136 | |
| 122 | - return $response; | |
| 123 | - } | |
| 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 | + } | |
| 124 | 148 | |
| 125 | - /** | |
| 126 | - * Invoke PayPlug API. | |
| 127 | - * | |
| 128 | - * @param callable $callback | |
| 129 | - * @param array $params | |
| 130 | - * | |
| 131 | - * @return object | |
| 132 | - */ | |
| 133 | - protected function do_request( $callback, $params = [] ) { | |
| 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 | + } | |
| 134 | 162 | |
| 135 | - if ( ! is_array( $params ) ) { | |
| 136 | - $params = [ $params ]; | |
| 137 | - } | |
| 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 | + } | |
| 138 | 174 | |
| 139 | - return call_user_func_array( $callback, $params ); | |
| 140 | - } | |
| 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 | + } | |
| 141 | 188 | |
| 142 | - /** | |
| 143 | - * Reconfigure PayPlug client with new secret keys. | |
| 144 | - * | |
| 145 | - * @throws \Payplug\Exception\ConfigurationException | |
| 146 | - */ | |
| 147 | - protected function switch_mode() { | |
| 148 | - $switched_mode = 'test' === $this->gateway->get_current_mode() ? 'live' : 'test'; | |
| 149 | - $new_key = $this->gateway->get_api_key( $switched_mode ); | |
| 150 | - if ( empty( $new_key ) ) { | |
| 151 | - throw new \Exception( sprintf( | |
| 152 | - 'No secret key available for the %s mode', $switched_mode | |
| 153 | - ) ); | |
| 154 | - } | |
| 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 | + } | |
| 155 | 203 | |
| 156 | - Payplug::setSecretKey( $new_key ); | |
| 157 | - } | |
| 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 | + } | |
| 158 | 220 | |
| 159 | - /** | |
| 160 | - * Restore PayPlug client secret keys for the current mode. | |
| 161 | - * | |
| 162 | - * @throws \Payplug\Exception\ConfigurationException | |
| 163 | - */ | |
| 164 | - protected function restore_mode() { | |
| 165 | - $key = $this->gateway->get_api_key( $this->gateway->get_current_mode() ); | |
| 166 | - if ( empty( $key ) ) { | |
| 167 | - throw new \Exception( sprintf( | |
| 168 | - 'No secret key available for the %s mode', $this->gateway->get_current_mode() | |
| 169 | - ) ); | |
| 170 | - } | |
| 221 | + public function validate_jwt($client_data, $jwt) | |
| 222 | + { | |
| 223 | + return $this->do_request_with_fallback('\Payplug\Authentication::validateJWT', [$client_data, $jwt]); | |
| 224 | + } | |
| 171 | 225 | |
| 172 | - Payplug::setSecretKey( $key ); | |
| 173 | - } | |
| 174 | -} | |
| 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 | +} | |