| @@ -7,13 +7,10 @@ | ||
| 7 | 7 | exit; |
| 8 | 8 | } |
| 9 | 9 | |
| 10 | 10 | use Payplug\Core\HttpClient; |
| 11 | -use Payplug\Exception\BadRequestException; | |
| 12 | 11 | use Payplug\Exception\ConfigurationException; |
| 13 | -use Payplug\Exception\ForbiddenException; | |
| 14 | 12 | use Payplug\Exception\NotFoundException; |
| 15 | -use Payplug\Exception\PayplugServerException; | |
| 16 | 13 | use Payplug\Exception\UnauthorizedException; |
| 17 | 14 | use Payplug\Payplug; |
| 18 | 15 | use Payplug\PayplugWoocommerce\PayplugWoocommerceHelper; |
| 19 | 16 | use Payplug\PayplugWoocommerce\Traits\ServiceGetter; |
| @@ -32,8 +29,13 @@ | ||
| 32 | 29 | |
| 33 | 30 | private $api_payplug; |
| 34 | 31 | |
| 35 | 32 | /** |
| 33 | + * @var string | |
| 34 | + */ | |
| 35 | + private $bearer_token = ''; | |
| 36 | + | |
| 37 | + /** | |
| 36 | 38 | * PayplugApi constructor. |
| 37 | 39 | * |
| 38 | 40 | * @param PayplugGateway $gateway |
| 39 | 41 | */ |
| @@ -94,12 +96,13 @@ | ||
| 94 | 96 | |
| 95 | 97 | /** |
| 96 | 98 | * Configure PayPlug client. |
| 97 | 99 | */ |
| 98 | - public function init() | |
| 100 | + public function init(): void | |
| 99 | 101 | { |
| 100 | 102 | $current_mode = PayplugWoocommerceHelper::check_mode() ? 'live' : 'test'; |
| 101 | 103 | $bearer_token = $this->get_api()->get_bearer_token($current_mode); |
| 104 | + $this->bearer_token = (string) $bearer_token; | |
| 102 | 105 | try { |
| 103 | 106 | $this->api_payplug = Payplug::init([ |
| 104 | 107 | 'secretKey' => (string) $bearer_token, |
| 105 | 108 | 'apiVersion' => '2019-08-06', |
| @@ -131,8 +134,20 @@ | ||
| 131 | 134 | } |
| 132 | 135 | } |
| 133 | 136 | |
| 134 | 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 | + } | |
| 148 | + | |
| 149 | + /** | |
| 135 | 150 | * Retrieve payment data from PayPlug API. |
| 136 | 151 | * |
| 137 | 152 | * @param string $transaction_id |
| 138 | 153 | * |
| @@ -202,44 +217,8 @@ | ||
| 202 | 217 | { |
| 203 | 218 | return $this->do_request_with_fallback('\Payplug\Refund::create', [$transaction_id, $data]); |
| 204 | 219 | } |
| 205 | 220 | |
| 206 | - /** | |
| 207 | - * Simulate a oney payment | |
| 208 | - * | |
| 209 | - * @return array | |
| 210 | - */ | |
| 211 | - public function simulate_oney_payment($price, $oney_type = 'with_fees') | |
| 212 | - { | |
| 213 | - $country = PayplugWoocommerceHelper::get_payplug_merchant_country(); | |
| 214 | - $oney_fees = ['x3_' . $oney_type, 'x4_' . $oney_type]; | |
| 215 | - | |
| 216 | - try { | |
| 217 | - try { | |
| 218 | - try { | |
| 219 | - try { | |
| 220 | - $response = $this->do_request('\Payplug\OneySimulation::getSimulations', [[ | |
| 221 | - 'amount' => intval(floatval($price) * 100), | |
| 222 | - 'country' => $country, | |
| 223 | - 'operations' => $oney_fees, | |
| 224 | - ]]); | |
| 225 | - PayplugWoocommerceHelper::oney_simulation_values($oney_fees, $response); | |
| 226 | - } catch (PayplugServerException $e) { | |
| 227 | - $response = __('Your payment schedule simulation is temporarily unavailable. You will find this information at the payment stage.', 'payplug'); | |
| 228 | - } | |
| 229 | - } catch (BadRequestException $e) { | |
| 230 | - $response = __('Your payment schedule simulation is temporarily unavailable. You will find this information at the payment stage.', 'payplug'); | |
| 231 | - } | |
| 232 | - } catch (UnauthorizedException $e) { | |
| 233 | - $response = __('Your payment schedule simulation is temporarily unavailable. You will find this information at the payment stage.', 'payplug'); | |
| 234 | - } | |
| 235 | - } catch (ForbiddenException $e) { | |
| 236 | - $response = __('Your payment schedule simulation is temporarily unavailable. You will find this information at the payment stage.', 'payplug'); | |
| 237 | - } | |
| 238 | - | |
| 239 | - return $response; | |
| 240 | - } | |
| 241 | - | |
| 242 | 221 | public function validate_jwt($client_data, $jwt) |
| 243 | 222 | { |
| 244 | 223 | return $this->do_request_with_fallback('\Payplug\Authentication::validateJWT', [$client_data, $jwt]); |
| 245 | 224 | } |
| @@ -308,9 +287,9 @@ | ||
| 308 | 287 | * Reconfigure PayPlug client with new secret keys. |
| 309 | 288 | * |
| 310 | 289 | * @throws \Payplug\Exception\ConfigurationException |
| 311 | 290 | */ |
| 312 | - protected function switch_mode() | |
| 291 | + protected function switch_mode(): void | |
| 313 | 292 | { |
| 314 | 293 | $switched_mode = 'test' === $this->gateway->get_current_mode() ? 'live' : 'test'; |
| 315 | 294 | $new_key = $this->gateway->get_api_key($switched_mode); |
| 316 | 295 | if (empty($new_key)) { |
| @@ -327,9 +306,9 @@ | ||
| 327 | 306 | * Restore PayPlug client secret keys for the current mode. |
| 328 | 307 | * |
| 329 | 308 | * @throws \Payplug\Exception\ConfigurationException |
| 330 | 309 | */ |
| 331 | - protected function restore_mode() | |
| 310 | + protected function restore_mode(): void | |
| 332 | 311 | { |
| 333 | 312 | $key = $this->gateway->get_api_key($this->gateway->get_current_mode()); |
| 334 | 313 | if (empty($key)) { |
| 335 | 314 | throw new \Exception(sprintf( |