# payplug/3.1.0/src/Gateway/PayplugPermissions.php

PayPlug for WooCommerce (Official), version 3.1.0. 137 lines.

- Page: https://pluginprobe.com/plugins/payplug/3.1.0/code/src/Gateway/PayplugPermissions.php
- Raw: https://pluginprobe.com/plugins/payplug/3.1.0/raw/src/Gateway/PayplugPermissions.php
- Modified: 2026-09-25T08:27:34+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/payplug/3.1.0/code/src/Gateway/PayplugPermissions.php#L10-L20`.

```php
<?php

namespace Payplug\PayplugWoocommerce\Gateway;

// Exit if accessed directly
if (!defined('ABSPATH')) {
    exit;
}

use Payplug\Authentication;
use Payplug\Exception\PayplugException;
use Payplug\Payplug;
use Payplug\PayplugWoocommerce\PayplugWoocommerceHelper;

class PayplugPermissions
{
    public const OPTION_NAME = 'payplug_permission';
    public const LIVE_MODE = 'use_live_mode';
    public const SAVE_CARD = 'can_save_cards';
    public const USE_ONEY = 'can_use_oney';

    /**
     * The current mode for the gateway.
     *
     * @var string
     */
    private $gateway_mode;

    /**
     * The current key for account permissions.
     *
     * @var string
     */
    private $current_key;

    /**
     * @var array
     */
    private $permissions;

    /**
     * PayplugPermissions constructor.
     *
     * @param PayplugGateway $gateway
     * @param string|null $bearer_token Already-resolved bearer token for the gateway's
     *                                  current mode (see PayplugGateway::init_payplug()).
     *                                  Resolved here if not given.
     */
    public function __construct(PayplugGateway $gateway, ?string $bearer_token = null)
    {
        $this->gateway_mode = $gateway->get_configuration()->get_option('mode') ? 'live' : 'test';

        // get_bearer_token() refreshes the JWT if it's close to expiry (OAuth2 accounts).
        $this->current_key = $bearer_token ?? (string) $gateway->get_api()->get_bearer_token($this->gateway_mode);
        $this->load_permissions();
    }

    /**
     * Get all permissions.
     *
     * @return array
     */
    public function get_permissions()
    {
        return $this->permissions;
    }

    /**
     * Check if user has specific permission.
     *
     * @param string $user_can
     *
     * @return bool
     */
    public function has_permissions($user_can)
    {
        if (empty($user_can)) {
            return false;
        }

        return isset($this->permissions[$user_can]) && true === $this->permissions[$user_can];
    }

    /**
     * Delete permissions for the current mode.
     *
     * @return bool
     */
    public function clear_permissions()
    {
        return delete_transient($this->get_key());
    }

    /**
     * Load permissions for the current mode.
     */
    protected function load_permissions()
    {
        $payplug_permissions = get_transient($this->get_key());
        if (!empty($payplug_permissions)) {
            $this->permissions = $payplug_permissions;

            return true;
        }

        if (empty($this->current_key)) {
            return false;
        }

        try {
            $response = Authentication::getPermissions(new Payplug($this->current_key));
            $this->permissions = !empty($response) ? $response : [];
            set_transient($this->get_key(), $this->permissions, DAY_IN_SECONDS);

            return true;
        } catch (PayplugException $e) {

            //logout
            PayplugGateway::log(sprintf('Account request error from PayPlug API : %s', wc_print_r($e->getErrorObject(), true)), 'error');
            PayplugWoocommerceHelper::payplug_logout();
            $this->permissions = [];
        }

        return false;
    }

    /**
     * Build the key to retrieve the permissions.
     *
     * @return string
     */
    protected function get_key()
    {
        return self::OPTION_NAME . '_' . $this->gateway_mode;
    }
}

```
