Changeset 3258184
- Timestamp:
- 03/19/2025 05:51:49 AM (13 months ago)
- Location:
- mustangpay/trunk
- Files:
-
- 5 edited
-
mustangpay.php (modified) (5 diffs)
-
src/Init.php (modified) (2 diffs)
-
src/MustangpayApiConstantsV1.php (modified) (1 diff)
-
src/MustangpayApiUtilsV1.php (modified) (19 diffs)
-
src/PaymentGateway.php (modified) (23 diffs)
Legend:
- Unmodified
- Added
- Removed
-
mustangpay/trunk/mustangpay.php
r3258140 r3258184 22 22 } 23 23 24 // 在 WooCommerce 订单页面添加自定义退款按钮 mustangpay 25 //add_action('woocommerce_admin_order_data_after_order_details', 'mustangpay_custom_refund_button', 10, 2); //访客下面 26 //add_action('woocommerce_admin_order_data_after_shipping_address', 'mustangpay_custom_refund_button', 10, 2);//配送下面 27 28 add_action('woocommerce_order_item_add_action_buttons', 'mustangpay_custom_refund_button', 10, 2);//配送下面 24 // Add a custom refund button 'mustangpay' on the WooCommerce order page 25 26 add_action('woocommerce_order_item_add_action_buttons', 'mustangpay_custom_refund_button', 10, 2); 29 27 30 28 remove_action('admin_init', 'check_admin_referer'); … … 36 34 //is add 37 35 if ($order->get_status() == 'processing' && $merchantPayMethon=='CardPayment') { 38 // 获取订单总金额36 // Get the total order amount 39 37 $order_total = $order->get_total(); 40 // 获取订单退款金额38 // Get the total refund amount of the order 41 39 $total_refunded = 0; 42 40 foreach ($order->get_refunds() as $refund) { … … 98 96 success:function(data){ 99 97 if (data.success == true) { 100 //付款层高98 101 99 if(data.success == true){ 102 100 //orderStatus … … 148 146 149 147 function mustangpay_custom_currency( $currencies ) { 150 $currencies['ZAR'] = __('ZAR Currency', 'mustangpay'); // ZAR 是货币代码,'ZAR Currency' 是显示名称148 $currencies['ZAR'] = __('ZAR Currency', 'mustangpay'); // 'ZAR' is the currency code, and 'ZAR Currency' is the display name 151 149 return $currencies; 152 150 } … … 169 167 170 168 if ( PHP_VERSION_ID < 70200 ) { 171 // 显示警告信息 169 172 170 if ( is_admin() ) { 173 171 add_action( 'admin_notices', function () 174 172 { 175 // translators: %1$s 代表所需的最低 PHP 版本,%2$s 代表当前使用的 PHP 版本 176 printf('<div class="error"><p>%s</p></div>', esc_html__('mustangpay支付需要PHP%1$s以上版本才能运行,您当前的PHP版本为%2$s,请升级到PHP到%1$s或更新的版本,否则插件没有任何作用.', 'mustangpay')); 173 printf('<div class="error"><p>%s</p></div>', esc_html__('The Mustangpay payment plugin requires PHP %1$s or higher to run. Your current PHP version is %2$s. Please upgrade to PHP %1$s or a later version; otherwise, the plugin will not function.', 'mustangpay')); 177 174 } ); 178 175 } -
mustangpay/trunk/src/Init.php
r3258140 r3258184 49 49 50 50 /** 51 * 避免 TranslatePress 插件翻译签名字符串51 * Avoid translating signature strings with the TranslatePress plugin 52 52 */ 53 53 function ignore_translate_strings( $options ) { … … 58 58 59 59 /** 60 * 插件插件设置链接60 * Plugin settings link 61 61 */ 62 62 function add_settings_link( $links ) { -
mustangpay/trunk/src/MustangpayApiConstantsV1.php
r3258143 r3258184 12 12 const BASE_URL = 'https://openapi.mustangpay.co.za'; 13 13 //The address is the official testing environment access request address for mustangpay 14 // https://openapi-dev.mstpay-inc.com;14 // 15 15 const TEST_BASE_URL = 'https://openapi.mustangpay.co.za'; 16 16 -
mustangpay/trunk/src/MustangpayApiUtilsV1.php
r3258140 r3258184 16 16 { 17 17 18 /** @var \WC_Logger Logger 实例*/18 /** @var \WC_Logger Logger */ 19 19 public $log = false; 20 20 /** 21 * @var bool 日志是否启用21 * @var bool 22 22 */ 23 23 public $is_debug_mod = false; … … 58 58 59 59 if (isset($_POST['nonce']) && check_ajax_referer('mustangpay', 'nonce', false)) { 60 // 处理请求60 // 61 61 } else { 62 // Nonce 验证失败,拒绝请求62 // Nonce 63 63 die('Nonce verification failed'); 64 64 } 65 65 66 // 获取订单 ID(你可以通过 AJAX 请求传递订单 ID)66 // get order ID 67 67 $order_id = isset($_POST['order_id']) ? intval($_POST['order_id']) : 0; 68 68 if ($order_id <= 0) { … … 89 89 //var_dump($return_url);exit; 90 90 91 // 获取 $_POST 中的 selectedPayments 数组92 93 // 检查 $_POST['selectedPayments'] 是否存在并进行清理94 91 $payMethods = isset($_POST['selectedPayments']) 95 92 ? array_map('sanitize_text_field', $_POST['selectedPayments']) 96 93 : []; 97 94 98 // 定义合法的支付方式列表99 95 $validPayMethods = ['CardPayment', 'InstantEFT']; 100 96 101 // 验证每个支付方式是否合法102 97 foreach ($payMethods as $key => $method) { 103 98 if (!in_array($method, $validPayMethods)) { 104 // 如果存在非法支付方式,移除该元素105 99 unset($payMethods[$key]); 106 100 } 107 101 } 108 102 109 // 重新索引数组110 103 $payMethods = array_values($payMethods); 111 104 … … 206 199 207 200 if (isset($_POST['refund_nonce']) && check_ajax_referer('mustangpay', 'refund_nonce', false)) { 208 // 处理请求201 209 202 } else { 210 203 wp_send_json_error(array('message' => __( 'Nonce verification failed', 'mustangpay' ))); … … 214 207 $config=new PaymentGateway(); 215 208 216 // 验证请求是否有效217 209 if (!isset($_POST['refund_amount']) || !isset($_POST['order_id'])) { 218 210 wp_send_json_error(array('message' => __( 'Missing refund amount or order ID', 'mustangpay' ))); 219 211 } 220 212 221 // 获取退款金额和原因213 // Obtain refund amount and reason 222 214 $refund_amount = floatval($_POST['refund_amount']); 223 215 … … 226 218 $refund_reason = sanitize_text_field($_POST['refund_reason']); 227 219 228 // 验证退款金额和原因220 // Verify refund amount and reason 229 221 if ($refund_amount <= 0) { 230 222 wp_send_json_error(array('message' => __( 'Invalid refund amount', 'mustangpay' ))); … … 232 224 233 225 234 // 获取订单 ID(你可以通过 AJAX 请求传递订单 ID)235 226 $order_id = isset($_POST['order_id']) ? intval($_POST['order_id']) : 0; 236 227 … … 239 230 } 240 231 241 // 获取订单对象242 232 $order = wc_get_order($order_id); 243 233 if (!$order) { … … 256 246 } 257 247 258 // 获取订单总金额248 // Obtain the total amount of the order 259 249 $order_total = $order->get_total(); 260 // 获取订单退款金额250 // Order refund amount 261 251 $total_refunded = 0; 262 252 foreach ($order->get_refunds() as $refund) { … … 301 291 302 292 if(isset($respose['data']['orderStatus']) && $respose['data']['orderStatus'] == 'Success'){ 303 // 执行退款操作(通过 WooCommerce 的退款功能或者自定义退款逻辑)293 //Perform refund operation 304 294 $refund = wc_create_refund(array( 305 295 'amount' => $refund_amount, 306 296 'reason' => $refund_reason, 307 297 'order_id' => $order->get_id(), 308 'line_items' => array(), // 可根据需要填写退款的商品项目309 'refund_payment' => false, // 是否退款支付310 'restock_items' => true, // 是否退还库存298 'line_items' => array(), // You can fill in the refund items as needed 299 'refund_payment' => false, // Whether to refund payment 300 'restock_items' => true, // Do you want to return the inventory 311 301 )); 312 302 313 // 检查退款结果303 // Check the refund result 314 304 if (is_wp_error($refund)) { 315 wp_send_json_error(array('message' => ' 退款失败: ' . $refund->get_error_message()));305 wp_send_json_error(array('message' => 'Refund failed: ' . $refund->get_error_message())); 316 306 } 317 307 $refund_note = 'Refund OrderNO:' . $trade_order_id; 318 $order->add_order_note($refund_note); // 将备注添加到订单的历史记录中308 $order->add_order_note($refund_note); // Add notes to the order history 319 309 wp_send_json_success(array('message' => 'Refund successful', 'data' => array('orderStatus' => 'Success'))); 320 310 }else if(isset($respose['data']['orderStatus']) && $respose['data']['orderStatus'] == 'Pending'){ … … 360 350 if ($body === null) { 361 351 wp_send_json_error(array('message' => __( 'Verification failed, please check if your configuration information is correct', 'mustangpay' ))); 362 //throw new \RuntimeException("收到的响应:验签失败");352 363 353 } 364 354 … … 366 356 367 357 } catch (RequestException $e) { 368 //error_log("Mustangpay接口失败: " . $e->getMessage());369 358 return null; 370 359 } … … 444 433 445 434 private static function http_post($url, $data) { 446 // 使用 wp_remote_post 代替 cURL435 447 436 $response = wp_remote_post($url, array( 448 437 'method' => 'POST', … … 456 445 ), 457 446 'timeout' => 60, 458 'sslverify' => false, // SSL 证书验证关闭447 'sslverify' => false, 459 448 )); 460 449 … … 463 452 } 464 453 465 // 获取响应体454 466 455 $body = wp_remote_retrieve_body($response); 467 456 … … 469 458 } 470 459 471 /* 472 private static function http_post($url,$data){ 473 if(!function_exists('curl_init')){ 474 throw new Exception('php未安装curl组件',500); 475 } 476 $ch = curl_init(); 477 curl_setopt($ch, CURLOPT_TIMEOUT, 60); 478 curl_setopt($ch,CURLOPT_URL, $url); 479 curl_setopt($ch,CURLOPT_REFERER,get_option('siteurl')); 480 curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE); 481 curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE); 482 curl_setopt($ch, CURLOPT_HEADER, FALSE); 483 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 484 curl_setopt($ch, CURLOPT_POST, TRUE); 485 curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 486 curl_setopt($ch,CURLOPT_CUSTOMREQUEST,"POST"); 487 curl_setopt($ch,CURLOPT_HTTPHEADER,[ 488 "Accept-Encoding: gzip, deflate, br", 489 "Connection: keep-alive", 490 "Content-Type: application/json", 491 "User-Agent: PostmanRuntime-ApipostRuntime/1.1.0" 492 ]); 493 494 $response = curl_exec($ch); 495 $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 496 $error=curl_error($ch); 497 curl_close($ch); 498 499 return $response; 500 } 501 */ 502 function get_user_ip() { 460 function get_user_ip() { 503 461 $ip = ''; 504 // 检查 HTTP_X_FORWARDED_FOR 是否存在,常用于代理服务器或负载均衡器462 // Check if HTTP_X_FORWARDED_FOR exists, commonly used in proxy servers or load balancers 505 463 if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { 506 464 $forwarded_for = sanitize_text_field(wp_unslash($_SERVER['HTTP_X_FORWARDED_FOR'])); 507 // 如果有多个代理,X_FORWARDED_FOR 会包含一个逗号分隔的IP链,取第一个非私有IP465 // If there are multiple proxies, X_FORWARDED_FOR will contain a comma-separated IP chain. Take the first non-private IP 508 466 $ip_array = explode(',', $forwarded_for); 509 $ip = trim($ip_array[0]); // 获取第一个IP467 $ip = trim($ip_array[0]); // Get the first IP 510 468 $ip = filter_var($ip, FILTER_VALIDATE_IP); 511 469 if (!$ip) { … … 513 471 } 514 472 } 515 // 如果没有 HTTP_X_FORWARDED_FOR,检查HTTP_CLIENT_IP473 // If HTTP_X_FORWARDED_FOR is not available, check HTTP_CLIENT_IP 516 474 elseif (!empty($_SERVER['HTTP_CLIENT_IP'])) { 517 475 $client_ip = sanitize_text_field(wp_unslash($_SERVER['HTTP_CLIENT_IP'])); 518 476 $ip = filter_var($client_ip, FILTER_VALIDATE_IP); 519 477 if (!$ip) { 520 // 处理无效 IP 地址的情况478 // Handle the case of an invalid IP address 521 479 $ip = ''; 522 480 } 523 481 } 524 // 如果都没有,使用REMOTE_ADDR482 // If neither of the above is available, use REMOTE_ADDR 525 483 else { 526 484 if (isset($_SERVER['REMOTE_ADDR'])) { … … 528 486 $ip = filter_var($remote_ip, FILTER_VALIDATE_IP); 529 487 if (!$ip) { 530 // 处理无效 IP 地址的情况488 // Handle the case of an invalid IP address 531 489 $ip = ''; 532 490 } … … 535 493 536 494 return $ip; 537 } 538 495 } 539 496 public function get_order_title($order, $limit = 98) { 540 497 $order_id = method_exists($order, 'get_id')? $order->get_id():$order->id; -
mustangpay/trunk/src/PaymentGateway.php
r3258140 r3258184 11 11 class PaymentGateway extends \WC_Payment_Gateway 12 12 { 13 /** @var \WC_Logger Logger 实例*/13 /** @var \WC_Logger */ 14 14 public $log = false; 15 15 /** 16 * @var bool 日志是否启用16 * @var bool Whether the log is enabled 17 17 */ 18 18 public $is_debug_mod = false; 19 19 /** 20 * 网关支持的功能20 * Supported functions of the gateway 21 21 * 22 22 * @var array … … 29 29 30 30 31 // 支付方法的全局 ID31 32 32 $this->id = Mustangpay_Custom_Payment_ID; 33 // 支付网关页面显示的支付网关标题 34 //$this->method_title = __('Mustang Pay',Mustangpay_Custom_Payment); 33 35 34 $this->method_title = __('Mustang Pay', 'mustangpay'); 36 35 37 36 38 //$this->method_title='12';39 40 // 支付网关设置页面显示的支付网关标题41 //$this->method_description = __('Mustang Pay gateway payment provides functions such as cash register payment.',Mustangpay_Custom_Payment);42 37 $this->method_description = __('Mustang Pay gateway payment provides functions such as cash register payment.', 'mustangpay'); 43 // 被 init_settings() 加载的基础设置38 // init_settings() 44 39 $this->init_form_fields(); 45 40 $this->init_settings(); 46 41 47 // 前端显示的支付网关名称42 // The payment gateway name displayed on the front-end 48 43 $this->title = $this->get_option( 'title' ); 49 44 … … 54 49 $this->title = 'Mustang Pay'; 55 50 56 // 支付网关标题51 // Payment gateway title 57 52 58 53 $this->icon = apply_filters( 'mustangpay_wechat_icon', Mustangpay_Custom_Payment_URL . '/images/pay.svg' ); … … 63 58 $this->has_fields = false; 64 59 //$this->multi_currency_enabled = in_array( 'woocommerce-multilingual/wpml-woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ), true ) && get_option( 'icl_enable_multi_currency' ) === 'yes'; 65 // 保存设置60 // Save Settings 66 61 if ( is_admin() ) { 67 62 add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, [ $this, 'process_admin_options' ] ); … … 69 64 70 65 71 // 添加URL66 // add URL 72 67 add_action( 'woocommerce_api_hpj-wc-wechatpay-query', [ $this, 'query_order' ] ); 73 68 add_action( 'woocommerce_api_hpj-wc-wechatpay-notify', [ $this, 'listen_notify' ] ); … … 79 74 80 75 /** 81 * 网关设置76 * Gateway settings 82 77 */ 83 78 public function init_form_fields() { … … 146 141 'description'=>'' 147 142 ), 148 /*'appid' => array(149 'title' => __( 'APP ID', Mustangpay_Custom_Payment ),150 'type' => 'text',151 'css' => 'width:400px',152 'default'=>'',153 'section' => 'default',154 'description'=>''155 ),156 'appsecret' => array(157 'title' => __( 'APP Secret', Mustangpay_Custom_Payment ),158 'type' => 'text',159 'css' => 'width:400px',160 'default'=>'',161 'section' => 'default'162 ),*/163 /* 'tranasction_url' => array(164 'title' => __( 'Transaction_url', Mustangpay_Custom_Payment ),165 'type' => 'text',166 'css' => 'width:400px',167 'default'=>'http://43.135.157.188:8812',168 'section' => 'default',169 'description'=>''170 ),*/171 172 143 ); 173 144 } 174 145 175 146 /** 176 * 管理选项147 * Manage Options 177 148 */ 178 149 public function admin_options() { ?> … … 201 172 202 173 /** 203 * WooCommerce 支付处理function/method.174 * WooCommerce Payment Processing function/method. 204 175 * 205 176 * @inheritdoc … … 235 206 236 207 function getCurrentUrl() { 237 // 检查是否是HTTPS238 $protocol = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') ? "https" : "http";239 240 $host='';241 // 获取主机名(域名)242 if (isset($_SERVER['HTTP_HOST'])) {243 // 去除可能存在的反斜杠244 $host = sanitize_text_field(wp_unslash($_SERVER['HTTP_HOST']));245 } else {246 // 如果 HTTP_HOST 不存在,可以根据实际需求进行处理,比如设置为空字符串247 $host = '';248 }249 250 251 if (isset($_SERVER['REQUEST_URI'])) {252 $uri = sanitize_text_field(wp_unslash($_SERVER['REQUEST_URI']));253 } else {254 // 如果 REQUEST_URI 不存在,可以根据实际需求进行处理,比如设置为空字符串255 $uri = '';256 }257 258 // 拼接完整URL259 $url = $protocol . "://" . $host . $uri;260 261 return $url;262 }208 // Check if it is HTTPS 209 $protocol = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') ? "https" : "http"; 210 211 $host=''; 212 // Get the hostname (domain name) 213 if (isset($_SERVER['HTTP_HOST'])) { 214 // Remove possible backslashes 215 $host = sanitize_text_field(wp_unslash($_SERVER['HTTP_HOST'])); 216 } else { 217 // If HTTP_HOST does not exist, it can be handled according to actual needs, such as setting it to an empty string 218 $host = ''; 219 } 220 221 222 if (isset($_SERVER['REQUEST_URI'])) { 223 $uri = sanitize_text_field(wp_unslash($_SERVER['REQUEST_URI'])); 224 } else { 225 // If REQUEST_URI does not exist, it can be handled according to actual needs, such as setting it to an empty string 226 $uri = ''; 227 } 228 229 // Concatenate the complete URL 230 $url = $protocol . "://" . $host . $uri; 231 232 return $url; 233 } 263 234 264 235 … … 286 257 //var_dump($total_amount); 287 258 288 //创建订单接口259 289 260 $create_order_url= WC()->api_request_url('hpj-wc-mustangpay-create-order'); 290 261 … … 414 385 .payment-option { 415 386 align-items: center; 416 margin-right: 20px; /* 控制选项之间的间距 */387 margin-right: 20px; 417 388 } 418 389 419 390 .payment-option input[type='checkbox'] { 420 391 margin-right: 8px; 421 transform: scale(1.2); /* 适当增大复选框尺寸 */392 transform: scale(1.2); 422 393 } 423 394 … … 486 457 if (\$(this).hasClass('disabled')) { 487 458 console.log('disabled'); 488 return; // 阻止进一步执行459 return; 489 460 } 490 461 … … 493 464 var url = '{$escaped_create_order_url}'; 494 465 \$('input[name=\"payMethod\"]:checked').each(function() { 495 selectedPayments.push(\$(this).val()); // 获取选中的复选框的值466 selectedPayments.push(\$(this).val()); 496 467 }); 497 468 … … 556 527 let currentDate = new Date(); 557 528 558 // 获取年、月、日、小时、分钟、秒529 559 530 let year = currentDate.getFullYear(); 560 let month = currentDate.getMonth() + 1; // 月份是从 0 开始的,所以加 1531 let month = currentDate.getMonth() + 1; 561 532 let day = currentDate.getDate(); 562 533 let hours = currentDate.getHours(); … … 564 535 let seconds = currentDate.getSeconds(); 565 536 566 // 格式化:如果小于 10 补充 0537 567 538 month = month < 10 ? '0' + month : month; 568 539 day = day < 10 ? '0' + day : day; … … 571 542 seconds = seconds < 10 ? '0' + seconds : seconds; 572 543 573 // 拼接成订单号格式(YYYYMMDDHHMMSS)544 574 545 let orderNumber = year + month + day + hours + minutes + seconds; 575 546 return orderNumber; … … 593 564 594 565 /** 595 * 监听支付返回566 * Monitor payment returns 596 567 */ 597 568 public function query_order() { … … 646 617 647 618 /** 648 * 处理支付接口异步返回的信息619 * Handle the asynchronous return information from the payment interface. 649 620 */ 650 621 public function listen_notify() { 651 // $this->log('-------------------------------------'); 652 // $data = $_POST; 653 //$this->log($data); 654 655 656 // 1. 验证请求方法 657 if ($_SERVER['REQUEST_METHOD'] !== 'POST') { 658 status_header(405); 659 exit(esc_html__('Method Not Allowed', 'mustangpay')); 660 } 661 662 $rawData = $_POST; 663 if(empty($rawData)){ 664 $rawData = file_get_contents("php://input"); 665 } 666 667 // 基础验证 668 if (empty($rawData)) { 669 $this->log(__('Empty notification data received', 'mustangpay')); 670 status_header(400); 671 exit(esc_html__('Bad Request', 'mustangpay')); 672 } 673 674 675 $this->log($rawData); 676 $content=json_decode($rawData,true); 677 678 //$mp=new MustangpayApiUtilsV1(); 679 $data=MustangpayApiUtilsV1::merchantDecrypt($content); 680 681 $data=json_decode($data,true); 682 683 if ($data === null && json_last_error()!== JSON_ERROR_NONE) { 684 $this->log('data_json_decode_error'); 685 return; 686 } 687 688 689 // 5. 严格验证数据结构 690 $required_keys = ['merchantOrderNo', 'orderStatus', 'merchantId']; 691 foreach ($required_keys as $key) { 692 if (!isset($data[$key])) { 693 exit('Missing required field:'.$key); 694 } 695 } 696 697 698 $merchantOrderNo = sanitize_text_field(wp_unslash($data['merchantOrderNo'])); 699 700 $orderStatus = sanitize_text_field(wp_unslash($data['orderStatus'])); 701 702 $merchantId = sanitize_text_field(wp_unslash($data['merchantId'])); 703 622 // $this->log('-------------------------------------'); 623 // $data = $_POST; 624 // $this->log($data); 625 626 // 1. Validate the request method 627 if ($_SERVER['REQUEST_METHOD'] !== 'POST') { 628 status_header(405); 629 exit(esc_html__('Method Not Allowed', 'mustangpay')); 630 } 631 632 $rawData = $_POST; 633 if (empty($rawData)) { 634 $rawData = file_get_contents("php://input"); 635 } 636 637 // Basic validation 638 if (empty($rawData)) { 639 $this->log(__('Empty notification data received', 'mustangpay')); 640 status_header(400); 641 exit(esc_html__('Bad Request', 'mustangpay')); 642 } 643 644 $this->log($rawData); 645 $content = json_decode($rawData, true); 646 647 // $mp = new MustangpayApiUtilsV1(); 648 $data = MustangpayApiUtilsV1::merchantDecrypt($content); 649 650 $data = json_decode($data, true); 651 652 if ($data === null && json_last_error()!== JSON_ERROR_NONE) { 653 $this->log('data_json_decode_error'); 654 return; 655 } 656 657 // 5. Strictly validate the data structure 658 $required_keys = ['merchantOrderNo', 'orderStatus', 'merchantId']; 659 foreach ($required_keys as $key) { 660 if (!isset($data[$key])) { 661 exit('Missing required field: ' . $key); 662 } 663 } 664 665 $merchantOrderNo = sanitize_text_field(wp_unslash($data['merchantOrderNo'])); 666 $orderStatus = sanitize_text_field(wp_unslash($data['orderStatus'])); 667 $merchantId = sanitize_text_field(wp_unslash($data['merchantId'])); 668 704 669 if (!is_numeric($merchantId)) { 705 exit('merchant Id is not a valid number!'); 706 } 707 708 709 // 验证 $orderStatus 是否为 success 或者 error 710 if (!in_array($orderStatus, ['Success', 'Error'])) { 711 exit('OrderStatus must be either success or error'); 712 } 713 670 exit('merchant Id is not a valid number!'); 671 } 672 673 // Validate if $orderStatus is either success or error 674 if (!in_array($orderStatus, ['Success', 'Error'])) { 675 exit('OrderStatus must be either Success or Error'); 676 } 677 714 678 $parts = explode("_", $merchantOrderNo); 715 // 验证分割后数组长度是否足够 716 if (count($parts) < 2) { 717 exit('merchantOrderNo Error!'); 718 } 719 720 $order_id = $parts[1]; 721 // 验证 $order_id 是否为有效的数字 722 if (!is_numeric($order_id)) { 723 exit('The obtained order ID is not a valid number!'); 724 } 725 726 727 $order = wc_get_order($order_id); 728 if(!$order){ 729 exit(esc_html__('Unknow Order (id:'.esc_html($order_id).')', 'mustangpay')); 730 } 731 732 if(!(method_exists($order, 'is_paid')?$order->is_paid():in_array($order->get_status(), array( 'processing', 'completed' )))&&$data['orderStatus']=='Success'){ 733 $this->log('payment_complete'); 734 $order->payment_complete($merchantOrderNo); 735 //$order->payment_complete($data['transacton_id']); 736 WC()->cart->empty_cart(); 737 update_post_meta($order_id, '_merchant_order_no', $merchantOrderNo); 738 // translators: %s 代表交易的商户订单号 739 $order->add_order_note(sprintf( __( 'payment complete (Transaction ID: %s)', 'mustangpay' ),$merchantOrderNo)); 740 741 $mp=$this->get_order($merchantId,$merchantOrderNo); 742 if(empty($mp)){ 743 $this->log('Order status not obtained:'.$merchantOrderNo); 744 }else{ 745 update_post_meta($order_id, '_merchant_order_pay_method',$mp); 746 } 747 748 } 749 print 'SUCCESS'; 750 exit; 751 } 752 679 // Validate if the length of the split array is sufficient 680 if (count($parts) < 2) { 681 exit('merchantOrderNo Error!'); 682 } 683 684 $order_id = $parts[1]; 685 // Validate if $order_id is a valid number 686 if (!is_numeric($order_id)) { 687 exit('The obtained order ID is not a valid number!'); 688 } 689 690 $order = wc_get_order($order_id); 691 if (!$order) { 692 exit(esc_html__('Unknown Order (id: ' . esc_html($order_id) . ')', 'mustangpay')); 693 } 694 695 if (!(method_exists($order, 'is_paid')? $order->is_paid() : in_array($order->get_status(), array('processing', 'completed'))) && $data['orderStatus'] == 'Success') { 696 $this->log('payment_complete'); 697 $order->payment_complete($merchantOrderNo); 698 // $order->payment_complete($data['transacton_id']); 699 WC()->cart->empty_cart(); 700 update_post_meta($order_id, '_merchant_order_no', $merchantOrderNo); 701 // translators: %s represents the merchant order number of the transaction 702 $order->add_order_note(sprintf(__('Payment complete (Transaction ID: %s)', 'mustangpay'), $merchantOrderNo)); 703 704 $mp = $this->get_order($merchantId, $merchantOrderNo); 705 if (empty($mp)) { 706 $this->log('Order status not obtained: ' . $merchantOrderNo); 707 } else { 708 update_post_meta($order_id, '_merchant_order_pay_method', $mp); 709 } 710 } 711 print 'SUCCESS'; 712 exit; 713 } 753 714 public function listen_refund() { 754 715 print 'SUCCESS'; … … 769 730 } 770 731 771 /* private function http_post($url,$data){ 772 if(!function_exists('curl_init')){ 773 throw new Exception('php未安装curl组件',500); 774 } 775 $ch = curl_init(); 776 curl_setopt($ch, CURLOPT_TIMEOUT, 60); 777 curl_setopt($ch,CURLOPT_URL, $url); 778 curl_setopt($ch,CURLOPT_REFERER,get_option('siteurl')); 779 curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE); 780 curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE); 781 curl_setopt($ch, CURLOPT_HEADER, FALSE); 782 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 783 curl_setopt($ch, CURLOPT_POST, TRUE); 784 curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 785 $response = curl_exec($ch); 786 $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 787 $error=curl_error($ch); 788 curl_close($ch); 789 790 return $response; 791 }*/ 732 792 733 793 734 794 735 private function http_post($url, $data) { 795 // 检查是否启用了 HTTP 请求功能736 // Check if the wp_remote_post function exists 796 737 if (!function_exists('wp_remote_post')) { 797 wp_send_json_error(array('message' => __( 'WordPress HTTP request function wp_remote_post is not available')));738 wp_send_json_error(array('message' => __('WordPress HTTP request function wp_remote_post is not available'))); 798 739 } 799 740 800 // 定义请求头741 // Set request headers 801 742 $headers = array( 802 'Content-Type' => 'application/json',803 'User-Agent' => 'PostmanRuntime/1.1.0',804 'Accept' => '*/*',743 'Content-Type' => 'application/json', 744 'User-Agent' => 'PostmanRuntime/1.1.0', 745 'Accept' => '*/*', 805 746 'Accept-Encoding' => 'gzip, deflate, br', 806 747 ); 807 748 808 // 使用 wp_remote_post 发送请求749 // Send the request using wp_remote_post 809 750 $response = wp_remote_post($url, array( 810 'body' => $data, // 请求体(数据)811 'headers' => $headers, // 请求头812 'timeout' => 60, // 设置超时时间813 'sslverify' => false, // 禁用 SSL 验证(视需求可更改)751 'body' => $data, // Request body (data) 752 'headers' => $headers, // Request headers 753 'timeout' => 60, // Set the timeout 754 'sslverify' => false, // Disable SSL verification (can be changed as needed) 814 755 )); 815 756 816 // 检查是否有错误757 // Check for errors 817 758 if (is_wp_error($response)) { 818 759 $error_message = $response->get_error_message(); 819 wp_send_json_error(array('message' => "Request failed: ".esc_html($error_message)));760 wp_send_json_error(array('message' => "Request failed: ".esc_html($error_message))); 820 761 } 821 762 822 // 获取响应数据763 // Get the response body 823 764 $response_body = wp_remote_retrieve_body($response); 824 765 return $response_body; 825 } 766 } 826 767 827 828 /* public function isWebApp(){ 829 $_SERVER['ALL_HTTP'] = isset($_SERVER['ALL_HTTP']) ? $_SERVER['ALL_HTTP'] : ''; 830 $mobile_browser = '0'; 831 if(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|iphone|ipad|ipod|android|xoom)/i', strtolower($_SERVER['HTTP_USER_AGENT']))) 832 $mobile_browser++; 833 if((isset($_SERVER['HTTP_ACCEPT'])) and (strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml') !== false)) 834 $mobile_browser++; 835 if(isset($_SERVER['HTTP_X_WAP_PROFILE'])) 836 $mobile_browser++; 837 if(isset($_SERVER['HTTP_PROFILE'])) 838 $mobile_browser++; 839 840 $mobile_ua = isset($_SERVER['HTTP_USER_AGENT']) ? strtolower(substr(sanitize_text_field($_SERVER['HTTP_USER_AGENT']), 0, 4)) : ''; 841 842 843 // $mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'],0,4)); 844 $mobile_agents = array( 845 'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac', 846 'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno', 847 'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-', 848 'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-', 849 'newt','noki','oper','palm','pana','pant','phil','play','port','prox', 850 'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar', 851 'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-', 852 'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp', 853 'wapr','webc','winw','winw','xda','xda-' 854 ); 855 if(in_array($mobile_ua, $mobile_agents)) 856 $mobile_browser++; 857 if(strpos(strtolower($_SERVER['ALL_HTTP']), 'operamini') !== false) 858 $mobile_browser++; 859 // Pre-final check to reset everything if the user is on Windows 860 if(strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'windows') !== false) 861 $mobile_browser=0; 862 // But WP7 is also Windows, with a slightly different characteristic 863 if(strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'windows phone') !== false) 864 $mobile_browser++; 865 if($mobile_browser>0) 866 return true; 867 else 868 return false; 869 }*/ 768 870 769 871 770 public function get_order_title($order, $limit = 98) { … … 891 790 892 791 893 function get_user_ip() {792 function get_user_ip() { 894 793 $ip = ''; 895 // 检查 HTTP_X_FORWARDED_FOR 是否存在,常用于代理服务器或负载均衡器794 // Check if HTTP_X_FORWARDED_FOR exists, commonly used in proxy servers or load balancers 896 795 if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { 897 796 $forwarded_for = sanitize_text_field(wp_unslash($_SERVER['HTTP_X_FORWARDED_FOR'])); 898 // 如果有多个代理,X_FORWARDED_FOR 会包含一个逗号分隔的IP链,取第一个非私有IP797 // If there are multiple proxies, X_FORWARDED_FOR will contain a comma-separated IP chain. Take the first non-private IP 899 798 $ip_array = explode(',', $forwarded_for); 900 $ip = trim($ip_array[0]); // 获取第一个IP799 $ip = trim($ip_array[0]); // Get the first IP 901 800 $ip = filter_var($ip, FILTER_VALIDATE_IP); 902 801 if (!$ip) { … … 904 803 } 905 804 } 906 // 如果没有 HTTP_X_FORWARDED_FOR,检查HTTP_CLIENT_IP805 // If HTTP_X_FORWARDED_FOR is not available, check HTTP_CLIENT_IP 907 806 elseif (!empty($_SERVER['HTTP_CLIENT_IP'])) { 908 807 $client_ip = sanitize_text_field(wp_unslash($_SERVER['HTTP_CLIENT_IP'])); 909 808 $ip = filter_var($client_ip, FILTER_VALIDATE_IP); 910 809 if (!$ip) { 911 // 处理无效 IP 地址的情况810 // Handle the case of an invalid IP address 912 811 $ip = ''; 913 812 } 914 813 } 915 // 如果都没有,使用REMOTE_ADDR814 // If neither of the above is available, use REMOTE_ADDR 916 815 else { 917 816 if (isset($_SERVER['REMOTE_ADDR'])) { … … 919 818 $ip = filter_var($remote_ip, FILTER_VALIDATE_IP); 920 819 if (!$ip) { 921 // 处理无效 IP 地址的情况820 // Handle the case of an invalid IP address 922 821 $ip = ''; 923 822 } … … 926 825 927 826 return $ip; 928 } 827 } 929 828 930 829 /** 931 * Logger 辅助功能830 * Logger 932 831 * 933 832 * @param $message
Note: See TracChangeset
for help on using the changeset viewer.