PluginProbe
PayPlug for WooCommerce (Official) / 3.0.0
PayPlug for WooCommerce (Official) v3.0.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 1.2.6 All 101 releases
payplug / src / Gateway / PayplugPermissions.php

PayplugPermissions.php in PayPlug for WooCommerce (Official) 3.0.0, at src/Gateway/PayplugPermissions.php

137 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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