Changeset 3290195
- Timestamp:
- 05/09/2025 02:54:02 AM (11 months ago)
- Location:
- sepay-gateway/trunk
- Files:
-
- 3 edited
-
includes/class-wc-gateway-sepay.php (modified) (3 diffs)
-
includes/class-wc-sepay-api.php (modified) (1 diff)
-
sepay-gateway.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
sepay-gateway/trunk/includes/class-wc-gateway-sepay.php
r3288756 r3290195 68 68 $this->method_description .= '<br><div id="content-render">URL API của bạn là: <span id="site_url">Đang tải url ...</span></div>'; 69 69 } elseif ($this->cached_bank_account_data) { 70 $this->displayed_bank_name = $this->cached_bank_account_data['bank'][' short_name'];70 $this->displayed_bank_name = $this->cached_bank_account_data['bank']['brand_name'] ?? $this->cached_bank_account_data['bank']['short_name']; 71 71 } 72 72 … … 113 113 public function get_bank_account_data() 114 114 { 115 if ($this->cached_bank_account_data === null && $this->get_option('bank_account') ) {115 if ($this->cached_bank_account_data === null && $this->get_option('bank_account') && $this->api->is_connected()) { 116 116 $this->cached_bank_account_data = $this->api->get_bank_account($this->get_option('bank_account')); 117 117 … … 618 618 619 619 $bank_select = $this->get_option('bank_select'); 620 $bank_info = null; 621 foreach ($this->get_bank_data() as $bank) { 622 if ($bank['code'] === strtoupper($bank_select)) { 623 $bank_info = $bank; 624 break; 625 } 626 } 627 620 $bank_info = $this->get_bank_data()[$bank_select]; 621 628 622 if ($bank_info) { 629 623 $bank_bin = $bank_info['bin']; -
sepay-gateway/trunk/includes/class-wc-sepay-api.php
r3288756 r3290195 143 143 public function make_request($endpoint, $method = 'GET', $data = null) 144 144 { 145 $max_retries = 3; 146 $retry_count = 0; 147 $retry_delay = 1; // seconds 148 149 while ($retry_count < $max_retries) { 145 try { 146 $access_token = $this->get_access_token(); 147 } catch (Exception $e) { 148 $this->log_error('Failed to get access token', [ 149 'error' => $e->getMessage(), 150 ]); 151 return null; 152 } 153 154 if (!$access_token) { 155 $this->log_error('No access token available'); 156 return null; 157 } 158 159 $args = [ 160 'method' => $method, 161 'headers' => [ 162 'Authorization' => 'Bearer ' . $access_token, 163 'Content-Type' => 'application/json', 164 ], 165 'sslverify' => false, 166 'timeout' => 30, 167 ]; 168 169 if ($data !== null && $method !== 'GET') { 170 $args['body'] = json_encode($data); 171 } else if ($data !== null && $method === 'GET') { 172 $endpoint .= '?' . http_build_query($data); 173 } 174 175 $url = SEPAY_API_URL . '/api/v1/' . $endpoint; 176 177 $response = wp_remote_request($url, $args); 178 179 if (is_wp_error($response)) { 180 $this->log_error('API request failed', [ 181 'error' => $response->get_error_message(), 182 'endpoint' => $endpoint, 183 ]); 184 throw new Exception(esc_html($response->get_error_message())); 185 } 186 187 $data = json_decode(wp_remote_retrieve_body($response), true); 188 189 if (isset($data['error']) && $data['error'] === 'access_denied') { 190 $this->log_error('Access denied, attempting to refresh token', [ 191 'endpoint' => $endpoint 192 ]); 150 193 try { 151 $ access_token = $this->get_access_token();194 $this->refresh_token(); 152 195 } catch (Exception $e) { 153 $this->log_error('Failed to get accesstoken', [196 $this->log_error('Failed to refresh token', [ 154 197 'error' => $e->getMessage(), 155 'retry_count' => $retry_count156 198 ]); 157 if ($retry_count === $max_retries - 1) {158 return null;159 }160 $retry_count++;161 sleep($retry_delay);162 continue;163 }164 165 if (!$access_token) {166 $this->log_error('No access token available');167 199 return null; 168 200 } 169 170 $args = [ 171 'method' => $method, 172 'headers' => [ 173 'Authorization' => 'Bearer ' . $access_token, 174 'Content-Type' => 'application/json', 175 ], 176 'sslverify' => false, 177 'timeout' => 30, 178 ]; 179 180 if ($data !== null && $method !== 'GET') { 181 $args['body'] = json_encode($data); 182 } else if ($data !== null && $method === 'GET') { 183 $endpoint .= '?' . http_build_query($data); 184 } 185 186 $url = SEPAY_API_URL . '/api/v1/' . $endpoint; 187 188 $response = wp_remote_request($url, $args); 189 190 if (is_wp_error($response)) { 191 $this->log_error('API request failed', [ 192 'error' => $response->get_error_message(), 193 'endpoint' => $endpoint, 194 'retry_count' => $retry_count 195 ]); 196 if ($retry_count === $max_retries - 1) { 197 throw new Exception(esc_html($response->get_error_message())); 198 } 199 $retry_count++; 200 sleep($retry_delay); 201 continue; 202 } 203 204 $data = json_decode(wp_remote_retrieve_body($response), true); 205 206 if (isset($data['error']) && $data['error'] === 'access_denied') { 207 $this->log_error('Access denied, attempting to refresh token', [ 208 'endpoint' => $endpoint 209 ]); 210 try { 211 $this->refresh_token(); 212 } catch (Exception $e) { 213 $this->log_error('Failed to refresh token', [ 214 'error' => $e->getMessage(), 215 'retry_count' => $retry_count 216 ]); 217 if ($retry_count === $max_retries - 1) { 218 return null; 219 } 220 $retry_count++; 221 sleep($retry_delay); 222 continue; 223 } 224 return $this->make_request($endpoint, $method, $data); 225 } 226 227 return $data; 228 } 229 230 $this->log_error('Max retries reached for API request', [ 231 'endpoint' => $endpoint, 232 'method' => $method 233 ]); 234 return null; 201 // Thực hiện lại request chỉ một lần sau khi refresh token 202 return $this->make_request($endpoint, $method, $data); 203 } 204 205 return $data; 235 206 } 236 207 -
sepay-gateway/trunk/sepay-gateway.php
r3290192 r3290195 6 6 * Author: SePay Team 7 7 * Author URI: https://sepay.vn/ 8 * Version: 1.1. 88 * Version: 1.1.9 9 9 * Requires Plugins: woocommerce 10 10 * Text Domain: sepay-gateway … … 491 491 } 492 492 } 493 494 add_action('init', 'sepay_schedule_health_check'); 495 496 function sepay_schedule_health_check() { 497 if (!wp_next_scheduled('sepay_health_check')) { 498 wp_schedule_event(time(), 'hourly', 'sepay_health_check'); 499 } 500 } 501 502 add_action('sepay_health_check', 'sepay_perform_health_check'); 503 504 function sepay_perform_health_check() { 505 $api = new WC_SePay_API(); 506 $status = $api->get_connection_status(); 507 508 if (!$status['health_check']) { 509 try { 510 $api->refresh_token(); 511 } catch (Exception $e) { 512 if (function_exists('wc_get_logger')) { 513 $logger = wc_get_logger(); 514 $logger->error('Failed to restore connection during health check', [ 515 'source' => 'sepay', 516 'error' => $e->getMessage() 517 ]); 518 } 519 } 520 } 521 } 522 523 register_deactivation_hook(__FILE__, 'sepay_deactivate'); 524 525 function sepay_deactivate() { 526 wp_clear_scheduled_hook('sepay_health_check'); 527 } 528 529 add_action('upgrader_process_complete', 'sepay_clear_cache_after_update', 10, 2); 530 531 function sepay_clear_cache_after_update($upgrader_object, $options) { 532 if ($options['action'] === 'update' && $options['type'] === 'plugin') { 533 foreach ($options['plugins'] as $plugin) { 534 if (plugin_basename(__FILE__) === $plugin) { 535 delete_transient('wc_sepay_bank_accounts'); 536 break; 537 } 538 } 539 } 540 }
Note: See TracChangeset
for help on using the changeset viewer.