wp-woocommerce-pay/class-wc-gateway-easypay-wx.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

115 lines
3.6 KiB
PHP
Executable file

<?php
class WC_Gateway_Easypay_wx extends WC_Payment_Gateway {
private $hpos_support;
public function __construct() {
// 必要的字段
$this->id = 'easypay_wx';
$this->icon = WC_Freepay_URL . '/assets/logo/wx-logo.svg';
$this->has_fields = false;
$this->method_title = '文派易付——微信';
$this->method_description = '文派易付——微信设置';
// 初始化HPOS支持
if ( class_exists( 'EasyPay_HPOS_Support' ) ) {
$this->hpos_support = new EasyPay_HPOS_Support();
}
// 必须调用的方法
$this->init_form_fields();
$this->init_settings();
// 设置request或者response对象会用到的变量
$this->title = $this->get_option( 'title' );
$this->description = $this->get_option( 'description' );
// 保存后台设置的数据
add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
// 实例化异步通知的处理
new WC_Gateway_Easypay_Notify( $this );
}
//配置列表项
public function init_form_fields() {
$this->form_fields = array(
'enabled' => array(
'title' => '启用/禁用',
'type' => 'checkbox',
'label' => '是否启用微信网关,默认为禁用。',
'default' => 'no',
),
'title' => array(
'title' => '标题',
'type' => 'text',
'description' => '在结算时看到的当前支付方式的名称',
'default' => '微信支付',
'desc_tip' => true,
),
'description' => array(
'title' => '描述',
'type' => 'text',
'desc_tip' => true,
'description' => '结算时当前支付方式的描述',
'default' => '本次交易将使用微信付款',
),
);
}
//核心订单处理
public function process_payment( $order_id ) {
global $woocommerce;
$order = wc_get_order( $order_id );
// 使用HPOS兼容的方法存储订单元数据
if ( $this->hpos_support ) {
$this->hpos_support->update_order_meta( $order, '_easypay_payment_method', $this->id );
$this->hpos_support->update_order_meta( $order, '_easypay_payment_start_time', current_time( 'timestamp' ) );
}
$price = $order->order_total;
$order_items = $order->get_items();
$product_num = count( $order_items );
foreach ( $order_items as $item_id => $item_data ) {
$product = $item_data->get_product();
$product_name = $product->get_name();
break;
}
if ( $product_num > 1 ) {
$product_name .= '等' . $product_num . '件商品';
}
$type = 'wxpay';
require 'inc/easypay.config.php';
require_once 'lib/epay_submit.class.php';
$parameter = array(
'pid' => trim( $alipay_config['partner'] ),
'type' => $type,
'notify_url' => plugin_dir_url( __FILE__ ) . 'inc/easypay_return.php',
'return_url' => home_url() . '/wc-api/wc_gateway_easypay',
'out_trade_no' => $order_id,
'name' => $product_name,
'money' => $price,
);
$alipaySubmit = new AlipaySubmit( $alipay_config );
// add note for payment
$msg = '顾客正在使用文派易付-微信方式进行支付';
$order->add_order_note( $msg );
// 使用HPOS兼容的方法存储支付参数
if ( $this->hpos_support ) {
$this->hpos_support->update_order_meta( $order, '_easypay_payment_params', $parameter );
}
$woocommerce->cart->empty_cart(); //清空购物车
$para = urlencode( json_encode( $alipaySubmit->payment_redirect( $parameter ) ) );
return array(
'result' => 'success',
'redirect' => plugin_dir_url( __FILE__ ) . 'payment_redirect.php?para=' . $para,
);
}
}