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