wp-woocommerce-pay/class-wc-gateway-easypay-notify.php
feibisi daf7932d59
Some checks are pending
gitleaks 密钥泄露扫描 / gitleaks (push) Waiting to run
feicode/ai-security No obvious risky pattern in latest diff
WordPress 插件 CI / ci (push) Successful in -8h1m15s
fix: phpcbf 自动修复 + phpcs.xml 代码规范配置
- phpcbf 自动修复 3893 处格式问题
- 添加 phpcs.xml 排除不适用的规则
- 安全相关规则降级为 warning 保持可见

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-02-18 15:19:03 +08:00

81 lines
2.4 KiB
PHP
Executable file
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
class WC_Gateway_Easypay_Notify {
private $gateway;
private $hpos_support;
public function __construct( $gateway ) {
$this->gateway = $gateway;
// 初始化HPOS支持
if ( class_exists( 'EasyPay_HPOS_Support' ) ) {
$this->hpos_support = new EasyPay_HPOS_Support();
}
add_action( 'woocommerce_api_wc_gateway_easypay', array( $this, 'check_easypay_response' ) );
}
public function check_easypay_response() {
require 'inc/easypay.config.php';
require_once 'lib/epay_notify.class.php';
$alipayNotify = new AlipayNotify( $alipay_config );
$verify_result = $alipayNotify->verifyNotify();
if ( $verify_result ) {
$out_trade_no = $_GET['out_trade_no'];
$trade_no = $_GET['trade_no'];
$trade_status = $_GET['trade_status'];
$order = wc_get_order( $out_trade_no );
if ( ! $order ) {
wp_die( 'Order not found' );
}
// 检查订单状态,避免重复处理
if ( $order->get_status() === 'completed' || $order->get_status() === 'processing' ) {
wp_die( 'Order already processed' );
}
// 使用HPOS兼容的方法更新订单元数据
if ( $this->hpos_support ) {
$this->hpos_support->update_order_meta( $order, '_easypay_trade_no', $trade_no );
$this->hpos_support->update_order_meta( $order, '_easypay_trade_status', $trade_status );
$this->hpos_support->update_order_meta( $order, '_easypay_payment_completed_time', current_time( 'timestamp' ) );
}
if ( $_GET['trade_status'] == 'TRADE_FINISHED' || $_GET['trade_status'] == 'TRADE_SUCCESS' ) {
$order->add_order_note( '文派易付交易完成,交易号:' . $trade_no );
$order->payment_complete( $trade_no );
// 如果所有商品都是虚拟商品,直接完成订单
if ( $this->is_virtual_order( $order ) ) {
$order->update_status( 'completed', '虚拟商品订单自动完成' );
}
}
echo 'success';
} else {
echo 'fail';
}
}
/**
* 检查订单是否全为虚拟商品
* @param WC_Order $order 订单对象
* @return bool 如果全为虚拟商品返回true否则返回false
*/
private function is_virtual_order( $order ) {
foreach ( $order->get_items() as $item ) {
if ( 'line_item' == $item->get_type() ) {
$product = $item->get_product();
if ( $product && ! $product->is_virtual() ) {
// 一旦发现有实物商品返回false
return false;
}
}
}
return true;
}
}