Plugin Directory

Changeset 3322376


Ignore:
Timestamp:
07/04/2025 02:12:17 PM (9 months ago)
Author:
sepayteam
Message:

fix token refresh bug

Location:
sepay-gateway
Files:
5 deleted
4 edited
11 copied

Legend:

Unmodified
Added
Removed
  • sepay-gateway/tags/1.1.15/includes/class-wc-gateway-sepay.php

    r3306117 r3322376  
    9090            }
    9191        }
    92        
     92
    9393        if (isset($_GET['disconnect']) && isset($_GET['_wpnonce'])) {
    9494            if (wp_verify_nonce($_GET['_wpnonce'], 'sepay_disconnect') && current_user_can('manage_woocommerce')) {
     
    115115        if ($this->cached_bank_account_data === null && $this->get_option('bank_account') && $this->api->is_connected()) {
    116116            $this->cached_bank_account_data = $this->api->get_bank_account($this->get_option('bank_account'));
    117            
     117
    118118            if ($this->cached_bank_account_data) {
    119119                $settings = get_option('woocommerce_sepay_settings', []);
     
    189189        }
    190190
    191         $prefix_data = array_values(array_filter($pay_code_prefixes, function($prefix) use ($pay_code_prefix) {
     191        $prefix_data = array_values(array_filter($pay_code_prefixes, function ($prefix) use ($pay_code_prefix) {
    192192            return $prefix['prefix'] === $pay_code_prefix;
    193193        }));
     
    262262    {
    263263        if (! $this->api->is_connected()) {
    264             $this->init_old_form_fields();
     264            try {
     265                $this->api->refresh_token();
     266            } catch (Exception $e) {
     267                WC_Admin_Settings::add_error('Không thể làm mới token. Vui lòng thử lại sau.');
     268                $this->init_old_form_fields();
     269                return;
     270            }
     271
     272            $this->init_form_fields();
    265273            return;
    266274        }
     
    521529        $statuses = wc_get_order_statuses();
    522530        $result = [];
    523        
     531
    524532        foreach ($statuses as $key => $label) {
    525533            $result[str_replace('wc-', '', $key)] = $label;
     
    561569                'sepay_reconnect'
    562570            );
    563            
     571
    564572            $disconnect_url = wp_nonce_url(
    565573                admin_url('admin.php?page=wc-settings&tab=checkout&section=sepay&disconnect=1'),
     
    604612        $order = wc_get_order($order_id);
    605613        $remark = $this->get_remark($order_id);
    606        
     614
    607615        if ($this->api->is_connected() && $this->cached_bank_account_data) {
    608616            $bank_account_id = $this->get_option('bank_account');
     
    616624            $account_number = $this->get_option('bank_account_number');
    617625            $account_holder_name = $this->get_option('bank_account_holder');
    618            
     626
    619627            $bank_select = $this->get_option('bank_select');
    620628            $bank_info = $this->get_bank_data()[$bank_select];
    621            
     629
    622630            if ($bank_info) {
    623631                $bank_bin = $bank_info['bin'];
    624632                $bank_logo_url = sprintf('https://my.sepay.vn/assets/images/banklogo/%s.png', strtolower($bank_info['short_name']));
    625                
     633
    626634                if ($this->bank_name_display_type == "brand_name") {
    627635                    $displayed_bank_name = $bank_info['short_name'];
  • sepay-gateway/tags/1.1.15/includes/class-wc-sepay-api.php

    r3322085 r3322376  
    3838    public function get_bank_accounts($cache = true)
    3939    {
     40        if (!$this->is_connected()) {
     41            return null;
     42        }
     43
    4044        if ($cache) {
    4145            $bank_accounts = get_transient('wc_sepay_bank_accounts');
     
    6468    public function get_company_info($cache = true)
    6569    {
     70        if (!$this->is_connected()) {
     71            return null;
     72        }
     73
    6674        if ($cache) {
    6775            $company = get_transient('wc_sepay_company');
     
    122130    public function update_company_configurations($data)
    123131    {
     132        if (!$this->is_connected()) {
     133            return null;
     134        }
     135
    124136        try {
    125137            $response = $this->make_request('company/configurations', 'PATCH', $data);
     
    143155    public function make_request($endpoint, $method = 'GET', $data = null)
    144156    {
     157        if (!$this->is_connected()) {
     158            return null;
     159        }
     160
    145161        try {
    146162            $access_token = $this->get_access_token();
     
    266282    public function is_connected()
    267283    {
    268         return !empty(get_option('wc_sepay_access_token')) && !empty(get_option('wc_sepay_refresh_token'));
     284        return !empty(get_option('wc_sepay_access_token'))
     285            && !empty(get_option('wc_sepay_refresh_token'))
     286            && get_option('wc_sepay_token_expires') > time() + 300;
    269287    }
    270288
     
    293311    public function get_webhooks($data = null)
    294312    {
     313        if (!$this->is_connected()) {
     314            return [];
     315        }
     316
    295317        try {
    296318            $response = $this->make_request('webhooks', 'GET', $data);
     
    304326    public function get_webhook($id)
    305327    {
     328        if (!$this->is_connected()) {
     329            return null;
     330        }
     331
     332        $cache = get_transient('wc_sepay_webhook_' . $id);
     333
     334        if ($cache) {
     335            return $cache;
     336        }
     337
    306338        try {
    307339            $response = $this->make_request('webhooks/' . $id);
     340
     341            if ($response['data']) {
     342                set_transient('wc_sepay_webhook_' . $id, $response['data'], 3600);
     343            }
     344
    308345            return $response['data'] ?? null;
    309346        } catch (Exception $e) {
     
    314351    public function create_webhook($bank_account_id, $webhook_id = null)
    315352    {
     353        if (!$this->is_connected()) {
     354            return null;
     355        }
     356
    316357        $api_key = wp_generate_password(32, false);
    317358
     
    363404    public function get_bank_sub_accounts($bank_account_id, $cache = true)
    364405    {
     406        if (!$this->is_connected()) {
     407            return [];
     408        }
     409
    365410        if ($cache) {
    366411            $sub_accounts = get_transient('wc_sepay_bank_sub_accounts_' . $bank_account_id);
     
    389434    public function update_webhook($webhook_id, $data)
    390435    {
     436        if (!$this->is_connected()) {
     437            return null;
     438        }
     439
    391440        try {
    392441            $response = $this->make_request('webhooks/' . $webhook_id, 'PATCH', $data);
     
    400449    public function get_user_info()
    401450    {
     451        if (!$this->is_connected()) {
     452            return null;
     453        }
     454
    402455        $user_info = get_transient('wc_sepay_user_info');
    403456
  • sepay-gateway/tags/1.1.15/readme.txt

    r3322085 r3322376  
    44 - Tags: woocommerce, payment gateway, vietqr, ngan hang, thanh toan
    55 - Requires WooCommerce at least: 2.1
    6  - Stable Tag: 1.1.14
    7  - Version: 1.1.14
     6 - Stable Tag: 1.1.15
     7 - Version: 1.1.15
    88 - Tested up to: 6.6
    99 - Requires at least: 5.6
  • sepay-gateway/tags/1.1.15/sepay-gateway.php

    r3322085 r3322376  
    66 * Author: SePay Team
    77 * Author URI: https://sepay.vn/
    8  * Version: 1.1.14
     8 * Version: 1.1.15
    99 * Requires Plugins: woocommerce
    1010 * Text Domain: sepay-gateway
  • sepay-gateway/trunk/includes/class-wc-gateway-sepay.php

    r3306117 r3322376  
    9090            }
    9191        }
    92        
     92
    9393        if (isset($_GET['disconnect']) && isset($_GET['_wpnonce'])) {
    9494            if (wp_verify_nonce($_GET['_wpnonce'], 'sepay_disconnect') && current_user_can('manage_woocommerce')) {
     
    115115        if ($this->cached_bank_account_data === null && $this->get_option('bank_account') && $this->api->is_connected()) {
    116116            $this->cached_bank_account_data = $this->api->get_bank_account($this->get_option('bank_account'));
    117            
     117
    118118            if ($this->cached_bank_account_data) {
    119119                $settings = get_option('woocommerce_sepay_settings', []);
     
    189189        }
    190190
    191         $prefix_data = array_values(array_filter($pay_code_prefixes, function($prefix) use ($pay_code_prefix) {
     191        $prefix_data = array_values(array_filter($pay_code_prefixes, function ($prefix) use ($pay_code_prefix) {
    192192            return $prefix['prefix'] === $pay_code_prefix;
    193193        }));
     
    262262    {
    263263        if (! $this->api->is_connected()) {
    264             $this->init_old_form_fields();
     264            try {
     265                $this->api->refresh_token();
     266            } catch (Exception $e) {
     267                WC_Admin_Settings::add_error('Không thể làm mới token. Vui lòng thử lại sau.');
     268                $this->init_old_form_fields();
     269                return;
     270            }
     271
     272            $this->init_form_fields();
    265273            return;
    266274        }
     
    521529        $statuses = wc_get_order_statuses();
    522530        $result = [];
    523        
     531
    524532        foreach ($statuses as $key => $label) {
    525533            $result[str_replace('wc-', '', $key)] = $label;
     
    561569                'sepay_reconnect'
    562570            );
    563            
     571
    564572            $disconnect_url = wp_nonce_url(
    565573                admin_url('admin.php?page=wc-settings&tab=checkout&section=sepay&disconnect=1'),
     
    604612        $order = wc_get_order($order_id);
    605613        $remark = $this->get_remark($order_id);
    606        
     614
    607615        if ($this->api->is_connected() && $this->cached_bank_account_data) {
    608616            $bank_account_id = $this->get_option('bank_account');
     
    616624            $account_number = $this->get_option('bank_account_number');
    617625            $account_holder_name = $this->get_option('bank_account_holder');
    618            
     626
    619627            $bank_select = $this->get_option('bank_select');
    620628            $bank_info = $this->get_bank_data()[$bank_select];
    621            
     629
    622630            if ($bank_info) {
    623631                $bank_bin = $bank_info['bin'];
    624632                $bank_logo_url = sprintf('https://my.sepay.vn/assets/images/banklogo/%s.png', strtolower($bank_info['short_name']));
    625                
     633
    626634                if ($this->bank_name_display_type == "brand_name") {
    627635                    $displayed_bank_name = $bank_info['short_name'];
  • sepay-gateway/trunk/includes/class-wc-sepay-api.php

    r3322085 r3322376  
    3838    public function get_bank_accounts($cache = true)
    3939    {
     40        if (!$this->is_connected()) {
     41            return null;
     42        }
     43
    4044        if ($cache) {
    4145            $bank_accounts = get_transient('wc_sepay_bank_accounts');
     
    6468    public function get_company_info($cache = true)
    6569    {
     70        if (!$this->is_connected()) {
     71            return null;
     72        }
     73
    6674        if ($cache) {
    6775            $company = get_transient('wc_sepay_company');
     
    122130    public function update_company_configurations($data)
    123131    {
     132        if (!$this->is_connected()) {
     133            return null;
     134        }
     135
    124136        try {
    125137            $response = $this->make_request('company/configurations', 'PATCH', $data);
     
    143155    public function make_request($endpoint, $method = 'GET', $data = null)
    144156    {
     157        if (!$this->is_connected()) {
     158            return null;
     159        }
     160
    145161        try {
    146162            $access_token = $this->get_access_token();
     
    266282    public function is_connected()
    267283    {
    268         return !empty(get_option('wc_sepay_access_token')) && !empty(get_option('wc_sepay_refresh_token'));
     284        return !empty(get_option('wc_sepay_access_token'))
     285            && !empty(get_option('wc_sepay_refresh_token'))
     286            && get_option('wc_sepay_token_expires') > time() + 300;
    269287    }
    270288
     
    293311    public function get_webhooks($data = null)
    294312    {
     313        if (!$this->is_connected()) {
     314            return [];
     315        }
     316
    295317        try {
    296318            $response = $this->make_request('webhooks', 'GET', $data);
     
    304326    public function get_webhook($id)
    305327    {
     328        if (!$this->is_connected()) {
     329            return null;
     330        }
     331
     332        $cache = get_transient('wc_sepay_webhook_' . $id);
     333
     334        if ($cache) {
     335            return $cache;
     336        }
     337
    306338        try {
    307339            $response = $this->make_request('webhooks/' . $id);
     340
     341            if ($response['data']) {
     342                set_transient('wc_sepay_webhook_' . $id, $response['data'], 3600);
     343            }
     344
    308345            return $response['data'] ?? null;
    309346        } catch (Exception $e) {
     
    314351    public function create_webhook($bank_account_id, $webhook_id = null)
    315352    {
     353        if (!$this->is_connected()) {
     354            return null;
     355        }
     356
    316357        $api_key = wp_generate_password(32, false);
    317358
     
    363404    public function get_bank_sub_accounts($bank_account_id, $cache = true)
    364405    {
     406        if (!$this->is_connected()) {
     407            return [];
     408        }
     409
    365410        if ($cache) {
    366411            $sub_accounts = get_transient('wc_sepay_bank_sub_accounts_' . $bank_account_id);
     
    389434    public function update_webhook($webhook_id, $data)
    390435    {
     436        if (!$this->is_connected()) {
     437            return null;
     438        }
     439
    391440        try {
    392441            $response = $this->make_request('webhooks/' . $webhook_id, 'PATCH', $data);
     
    400449    public function get_user_info()
    401450    {
     451        if (!$this->is_connected()) {
     452            return null;
     453        }
     454
    402455        $user_info = get_transient('wc_sepay_user_info');
    403456
  • sepay-gateway/trunk/readme.txt

    r3322085 r3322376  
    44 - Tags: woocommerce, payment gateway, vietqr, ngan hang, thanh toan
    55 - Requires WooCommerce at least: 2.1
    6  - Stable Tag: 1.1.14
    7  - Version: 1.1.14
     6 - Stable Tag: 1.1.15
     7 - Version: 1.1.15
    88 - Tested up to: 6.6
    99 - Requires at least: 5.6
  • sepay-gateway/trunk/sepay-gateway.php

    r3322085 r3322376  
    66 * Author: SePay Team
    77 * Author URI: https://sepay.vn/
    8  * Version: 1.1.14
     8 * Version: 1.1.15
    99 * Requires Plugins: woocommerce
    1010 * Text Domain: sepay-gateway
Note: See TracChangeset for help on using the changeset viewer.