| 1 |
<?php |
| 2 |
|
| 3 |
namespace Payplug\PayplugWoocommerce\Gateway; |
| 4 |
|
| 5 |
// Exit if accessed directly |
| 6 |
if (!defined('ABSPATH')) { |
| 7 |
exit; |
| 8 |
} |
| 9 |
|
| 10 |
use Payplug\Authentication; |
| 11 |
use Payplug\Exception\PayplugException; |
| 12 |
use Payplug\Payplug; |
| 13 |
use Payplug\PayplugWoocommerce\PayplugWoocommerceHelper; |
| 14 |
|
| 15 |
class PayplugPermissions |
| 16 |
{ |
| 17 |
public const OPTION_NAME = 'payplug_permission'; |
| 18 |
public const LIVE_MODE = 'use_live_mode'; |
| 19 |
public const SAVE_CARD = 'can_save_cards'; |
| 20 |
public const USE_ONEY = 'can_use_oney'; |
| 21 |
|
| 22 |
/** |
| 23 |
* The current mode for the gateway. |
| 24 |
* |
| 25 |
* @var string |
| 26 |
*/ |
| 27 |
private $gateway_mode; |
| 28 |
|
| 29 |
/** |
| 30 |
* The current key for account permissions. |
| 31 |
* |
| 32 |
* @var string |
| 33 |
*/ |
| 34 |
private $current_key; |
| 35 |
|
| 36 |
/** |
| 37 |
* @var array |
| 38 |
*/ |
| 39 |
private $permissions; |
| 40 |
|
| 41 |
/** |
| 42 |
* PayplugPermissions constructor. |
| 43 |
* |
| 44 |
* @param PayplugGateway $gateway |
| 45 |
* @param string|null $bearer_token Already-resolved bearer token for the gateway's |
| 46 |
* current mode (see PayplugGateway::init_payplug()). |
| 47 |
* Resolved here if not given. |
| 48 |
*/ |
| 49 |
public function __construct(PayplugGateway $gateway, ?string $bearer_token = null) |
| 50 |
{ |
| 51 |
$this->gateway_mode = $gateway->get_configuration()->get_option('mode') ? 'live' : 'test'; |
| 52 |
|
| 53 |
// get_bearer_token() refreshes the JWT if it's close to expiry (OAuth2 accounts). |
| 54 |
$this->current_key = $bearer_token ?? (string) $gateway->get_api()->get_bearer_token($this->gateway_mode); |
| 55 |
$this->load_permissions(); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Get all permissions. |
| 60 |
* |
| 61 |
* @return array |
| 62 |
*/ |
| 63 |
public function get_permissions() |
| 64 |
{ |
| 65 |
return $this->permissions; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Check if user has specific permission. |
| 70 |
* |
| 71 |
* @param string $user_can |
| 72 |
* |
| 73 |
* @return bool |
| 74 |
*/ |
| 75 |
public function has_permissions($user_can) |
| 76 |
{ |
| 77 |
if (empty($user_can)) { |
| 78 |
return false; |
| 79 |
} |
| 80 |
|
| 81 |
return isset($this->permissions[$user_can]) && true === $this->permissions[$user_can]; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Delete permissions for the current mode. |
| 86 |
* |
| 87 |
* @return bool |
| 88 |
*/ |
| 89 |
public function clear_permissions() |
| 90 |
{ |
| 91 |
return delete_transient($this->get_key()); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Load permissions for the current mode. |
| 96 |
*/ |
| 97 |
protected function load_permissions() |
| 98 |
{ |
| 99 |
$payplug_permissions = get_transient($this->get_key()); |
| 100 |
if (!empty($payplug_permissions)) { |
| 101 |
$this->permissions = $payplug_permissions; |
| 102 |
|
| 103 |
return true; |
| 104 |
} |
| 105 |
|
| 106 |
if (empty($this->current_key)) { |
| 107 |
return false; |
| 108 |
} |
| 109 |
|
| 110 |
try { |
| 111 |
$response = Authentication::getPermissions(new Payplug($this->current_key)); |
| 112 |
$this->permissions = !empty($response) ? $response : []; |
| 113 |
set_transient($this->get_key(), $this->permissions, DAY_IN_SECONDS); |
| 114 |
|
| 115 |
return true; |
| 116 |
} catch (PayplugException $e) { |
| 117 |
|
| 118 |
//logout |
| 119 |
PayplugGateway::log(sprintf('Account request error from PayPlug API : %s', wc_print_r($e->getErrorObject(), true)), 'error'); |
| 120 |
PayplugWoocommerceHelper::payplug_logout(); |
| 121 |
$this->permissions = []; |
| 122 |
} |
| 123 |
|
| 124 |
return false; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Build the key to retrieve the permissions. |
| 129 |
* |
| 130 |
* @return string |
| 131 |
*/ |
| 132 |
protected function get_key() |
| 133 |
{ |
| 134 |
return self::OPTION_NAME . '_' . $this->gateway_mode; |
| 135 |
} |
| 136 |
} |
| 137 |
|