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/PayplugPermissions.php +109 -86 1.0.22 → 3.1.0 View file →
@@ -2,112 +2,135 @@
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 10 use Payplug\Authentication;
11 11 use Payplug\Exception\PayplugException;
12 +use Payplug\Payplug;
13 +use Payplug\PayplugWoocommerce\PayplugWoocommerceHelper;
12 14
13 -class PayplugPermissions {
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';
14 21
15 - const OPTION_NAME = 'payplug_permission';
16 - const LIVE_MODE = 'use_live_mode';
17 - const SAVE_CARD = 'can_save_cards';
22 + /**
23 + * The current mode for the gateway.
24 + *
25 + * @var string
26 + */
27 + private $gateway_mode;
18 28
19 - /**
20 - * @var PayplugGateway
21 - */
22 - private $gateway;
29 + /**
30 + * The current key for account permissions.
31 + *
32 + * @var string
33 + */
34 + private $current_key;
23 35
24 - /**
25 - * The current mode for the gateway.
26 - *
27 - * @var string
28 - */
29 - private $gateway_mode;
36 + /**
37 + * @var array
38 + */
39 + private $permissions;
30 40
31 - /**
32 - * @var array
33 - */
34 - private $permissions;
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';
35 52
36 - /**
37 - * PayplugPermissions constructor.
38 - *
39 - * @param PayplugGateway $gateway
40 - */
41 - public function __construct( PayplugGateway $gateway ) {
42 - $this->gateway = $gateway;
43 - $this->gateway_mode = $gateway->get_current_mode();
44 - $this->load_permissions();
45 - }
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 + }
46 57
47 - /**
48 - * Get all permissions.
49 - *
50 - * @return array
51 - */
52 - public function get_permissions() {
53 - return $this->permissions;
54 - }
58 + /**
59 + * Get all permissions.
60 + *
61 + * @return array
62 + */
63 + public function get_permissions()
64 + {
65 + return $this->permissions;
66 + }
55 67
56 - /**
57 - * Check if user has specific permission.
58 - *
59 - * @param string $user_can
60 - *
61 - * @return bool
62 - */
63 - public function has_permissions( $user_can ) {
64 - if ( empty( $user_can ) ) {
65 - return false;
66 - }
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 + }
67 80
68 - return isset( $this->permissions[ $user_can ] ) && true === $this->permissions[ $user_can ];
69 - }
81 + return isset($this->permissions[$user_can]) && true === $this->permissions[$user_can];
82 + }
70 83
71 - /**
72 - * Delete permissions for the current mode.
73 - *
74 - * @return bool
75 - */
76 - public function clear_permissions() {
77 - return delete_transient( $this->get_key() );
78 - }
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 + }
79 93
80 - /**
81 - * Load permissions for the current mode.
82 - */
83 - protected function load_permissions() {
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;
84 102
85 - $payplug_permissions = get_transient( $this->get_key() );
86 - if ( ! empty( $payplug_permissions ) ) {
87 - $this->permissions = $payplug_permissions;
103 + return true;
104 + }
88 105
89 - return true;
90 - }
106 + if (empty($this->current_key)) {
107 + return false;
108 + }
91 109
92 - try {
93 - $response = Authentication::getPermissions();
94 - $this->permissions = ! empty( $response ) ? $response : [];
95 - set_transient( $this->get_key(), $this->permissions, DAY_IN_SECONDS );
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);
96 114
97 - return true;
98 - } catch ( PayplugException $e ) {
99 - $this->permissions = [];
100 - }
115 + return true;
116 + } catch (PayplugException $e) {
101 117
102 - return false;
103 - }
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 + }
104 123
105 - /**
106 - * Build the key to retrieve the permissions.
107 - *
108 - * @return string
109 - */
110 - protected function get_key() {
111 - return self::OPTION_NAME . '_' . $this->gateway_mode;
112 - }
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 + }
113 136 }