| 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 |
const OPTION_NAME = 'payplug_permission'; |
| 18 |
const LIVE_MODE = 'use_live_mode'; |
| 19 |
const SAVE_CARD = 'can_save_cards'; |
| 20 |
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 |
*/ |
| 46 |
public function __construct(PayplugGateway $gateway) |
| 47 |
{ |
| 48 |
$this->gateway_mode = $gateway->get_configuration()->get_option('mode'); |
| 49 |
$api_key = json_decode($gateway->get_configuration()->get_option('api_key'), true); |
| 50 |
|
| 51 |
// todo: check if we should get the jwt instead api key |
| 52 |
if (!isset($api_key['live']) || !isset($api_key['test'])) { |
| 53 |
$this->current_key = ''; |
| 54 |
} else { |
| 55 |
$this->current_key = 'live' === $this->gateway_mode ? $api_key['live'] : $api_key['test']; |
| 56 |
} |
| 57 |
$this->load_permissions(); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Get all permissions. |
| 62 |
* |
| 63 |
* @return array |
| 64 |
*/ |
| 65 |
public function get_permissions() |
| 66 |
{ |
| 67 |
return $this->permissions; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Check if user has specific permission. |
| 72 |
* |
| 73 |
* @param string $user_can |
| 74 |
* |
| 75 |
* @return bool |
| 76 |
*/ |
| 77 |
public function has_permissions($user_can) |
| 78 |
{ |
| 79 |
if (empty($user_can)) { |
| 80 |
return false; |
| 81 |
} |
| 82 |
|
| 83 |
return isset($this->permissions[$user_can]) && true === $this->permissions[$user_can]; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Delete permissions for the current mode. |
| 88 |
* |
| 89 |
* @return bool |
| 90 |
*/ |
| 91 |
public function clear_permissions() |
| 92 |
{ |
| 93 |
return delete_transient($this->get_key()); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Load permissions for the current mode. |
| 98 |
*/ |
| 99 |
protected function load_permissions() |
| 100 |
{ |
| 101 |
$payplug_permissions = get_transient($this->get_key()); |
| 102 |
if (!empty($payplug_permissions)) { |
| 103 |
$this->permissions = $payplug_permissions; |
| 104 |
|
| 105 |
return true; |
| 106 |
} |
| 107 |
|
| 108 |
if (empty($this->current_key)) { |
| 109 |
return false; |
| 110 |
} |
| 111 |
|
| 112 |
try { |
| 113 |
$response = Authentication::getPermissions(new Payplug($this->current_key)); |
| 114 |
$this->permissions = !empty($response) ? $response : []; |
| 115 |
set_transient($this->get_key(), $this->permissions, DAY_IN_SECONDS); |
| 116 |
|
| 117 |
return true; |
| 118 |
} catch (PayplugException $e) { |
| 119 |
|
| 120 |
//logout |
| 121 |
PayplugGateway::log(sprintf('Account request error from PayPlug API : %s', wc_print_r($e->getErrorObject(), true)), 'error'); |
| 122 |
PayplugWoocommerceHelper::payplug_logout(); |
| 123 |
$this->permissions = []; |
| 124 |
} |
| 125 |
|
| 126 |
return false; |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Build the key to retrieve the permissions. |
| 131 |
* |
| 132 |
* @return string |
| 133 |
*/ |
| 134 |
protected function get_key() |
| 135 |
{ |
| 136 |
return self::OPTION_NAME . '_' . $this->gateway_mode; |
| 137 |
} |
| 138 |
} |
| 139 |
|