WordPress支付接口对接教程是指通过安装支付插件或编写自定义支付网关,将第三方支付平台(如支付宝、微信支付、Stripe)的支付请求、异步通知、订单状态更新接入WordPress网站的一套操作流程。
获取支付接口参数
在开始对接前,需要前往支付平台开放平台或商户后台创建应用,获取以下参数:
商户号或App ID

API密钥或应用私钥
支付网关地址
异步通知回调地址
同步跳转地址
以支付宝为例,开放平台会提供应用AppID、应用私钥、支付宝公钥、网关地址 https://openapi.alipay.com/gateway.do,微信支付则需要商户号、APIv3密钥、证书序列号等。
创建WordPress自定义支付网关插件
在 wp-content/plugins/custom-payment-gateway 目录下创建插件主文件 custom-payment-gateway.php。
<?php
/**
*PluginName:CustomPaymentGateway
*Description:自定义WordPress支付网关对接示例
*Version:1.0.0
*Author:YourName
*/
if(!defined('ABSPATH')){
exit;
}
//检查WooCommerce是否启用
add_action('plugins_loaded','custom_payment_gateway_init',11);
functioncustom_payment_gateway_init(){
if(!class_exists('WC_Payment_Gateway')){
return;
}
classWC_Custom_Payment_GatewayextendsWC_Payment_Gateway{
publicfunction__construct(){
$this->id='custom_payment';
$this->icon='';
$this->has_fields=true;
$this->method_title='自定义支付网关';
$this->method_description='通过自定义代码对接第三方支付接口';
$this->init_form_fields();
$this->init_settings();
$this->title=$this->get_option('title');
$this->description=$this->get_option('description');
$this->merchant_id=$this->get_option('merchant_id');
$this->api_key=$this->get_option('api_key');
$this->gateway_url=$this->get_option('gateway_url');
$this->notify_url=home_url('/wc-api/custom_payment');
add_action('woocommerce_update_options_payment_gateways_'.$this->id,array($this,'process_admin_options'));
add_action('woocommerce_api_custom_payment',array($this,'handle_callback'));
}
publicfunctioninit_form_fields(){
$this->form_fields=array(
'enabled'=>array(
'title'=>'启用/禁用',
'type'=>'checkbox',
'label'=>'启用该支付网关',
'default'=>'yes'
),
'title'=>array(
'title'=>'标题',
'type'=>'text',
'description'=>'用户在结算页面看到的支付方式名称',
'default'=>'自定义支付'
),
'description'=>array(
'title'=>'描述',
'type'=>'textarea',
'description'=>'用户在结算页面看到的描述文字',
'default'=>'使用第三方支付接口完成付款'
),
'merchant_id'=>array(
'title'=>'商户号',
'type'=>'text',
'description'=>'支付平台分配的商户号或AppID'
),
'api_key'=>array(
'title'=>'API密钥',
'type'=>'password',
'description'=>'支付平台分配的API密钥或应用私钥'
),
'gateway_url'=>array(
'title'=>'支付网关地址',
'type'=>'text',
'description'=>'第三方支付网关请求地址'
)
);
}
publicfunctionprocess_payment($order_id){
$order=wc_get_order($order_id);
$params=array(
'merchant_id'=>$this->merchant_id,
'order_id'=>$order->get_order_number(),
'amount'=>$order->get_total(),
'currency'=>get_woocommerce_currency(),
'notify_url'=>$this->notify_url,
'return_url'=>$this->get_return_url($order),
'timestamp'=>time()
);
$params['sign']=$this->generate_sign($params,$this->api_key);
$redirect_url=add_query_arg($params,$this->gateway_url);
returnarray(
'result'=>'success',
'redirect'=>$redirect_url
);
}
publicfunctiongenerate_sign($params,$api_key){
ksort($params);
$sign_str='';
foreach($paramsas$key=>$value){
if($key!='sign'&&$value!==''){
$sign_str.=$key.'='.$value.'&';
}
}
$sign_str=rtrim($sign_str,'&');
returnhash_hmac('sha256',$sign_str,$api_key);
}
publicfunctionhandle_callback(){
$raw_data=file_get_contents('php://input');
$callback_data=json_decode($raw_data,true);
if(empty($callback_data)){
$callback_data=$_POST;
}
$sign=isset($callback_data['sign'])?$callback_data['sign']:'';
unset($callback_data['sign']);
$local_sign=$this->generate_sign($callback_data,$this->api_key);
if(!hash_equals($local_sign,$sign)){
wp_die('签名验证失败','InvalidSign',array('response'=>400));
}
$order_id=isset($callback_data['order_id'])?intval($callback_data['order_id']):0;
$order=wc_get_order($order_id);
if(!$order){
wp_die('订单不存在','OrderNotFound',array('response'=>404));
}
$status=isset($callback_data['status'])?sanitize_text_field($callback_data['status']):'';
if($status==='success'){
$order->payment_complete();
$order->add_order_note('支付成功,交易号:'.sanitize_text_field($callback_data['transaction_id']));
}elseif($status==='failed'){
$order->update_status('failed','支付失败');
}
echo'success';
exit;
}
}
add_filter('woocommerce_payment_gateways','add_custom_payment_gateway');
functionadd_custom_payment_gateway($gateways){
$gateways[]='WC_Custom_Payment_Gateway';
return$gateways;
}
}启用并配置支付网关
上传插件并启用后,进入WordPress后台的WooCommerce -> 设置 -> 付款,找到“自定义支付网关”。
勾选“启用该支付网关”
填写支付方式标题和描述
填写商户号、API密钥、支付网关地址
保存更改
构造支付请求参数
在 process_payment 方法中,订单支付参数会跳转到第三方支付网关,不同支付平台参数名不同,但核心字段一致:
商户标识:用于识别接入方
订单号:必须唯一,通常使用WooCommerce订单编号
支付金额:以元或分为单位,根据平台要求转换
货币类型:人民币、美元等
回调地址:支付成功后第三方平台异步通知的URL
签名:防止参数被改动
签名生成逻辑使用 hash_hmac('sha256', $sign_str, $api_key),支付宝使用RSA2签名,微信支付V3使用RSA签名,需要根据官方SDK调整。
配置支付回调地址
第三方支付平台通常需要配置以下两类地址:
同步跳转地址:支付完成后浏览器跳回网站,用于展示支付结果
异步通知地址:支付平台服务器发送交易结果通知,用于更新订单状态
异步通知地址必须在支付平台商户后台正确配置,且必须可以被公网访问,在示例代码中,通知地址为:
https://yourdomain.com/wc-api/custom_payment
WordPress的 woocommerce_api_custom_payment 钩子会自动捕获该URL的请求并调用 handle_callback 方法。
处理异步通知与验签
异步通知是支付对接中最关键的环节,支付平台会以POST方式发送交易结果,包含订单号、交易状态、交易金额、交易流水号等字段。
处理流程:
接收原始请求数据,支持JSON和表单格式
取出签名字段并从原始数据中移除
使用相同算法生成本地签名
使用
hash_equals防止时序攻破,比较签名是否一致校验订单金额、商户号等业务参数
根据交易状态更新WooCommerce订单
返回
success字符串通知支付平台停止重试
如果订单金额与通知金额不一致,必须终止处理,防止资损。
更新订单状态
支付成功后,需要调用 $order->payment_complete(),该方法会将订单状态更新为处理中或已完成,并记录支付时间,如果支付失败,使用 update_status('failed', '支付失败')。
为了让用户能在订单详情页看到支付流水号,可以添加订单备注:
$order->add_order_note('支付成功,交易号:'.$transaction_id);安全注意事项
支付接口涉及资金,安全配置必须严格:
API密钥和私钥不能写死在代码中,应存于WordPress数据库设置或环境变量
回调必须验签,不能仅凭请求参数更新订单
使用HTTPS,防止传输过程数据被窃取
对订单金额做严格类型转换,避免浮点误差
限制回调IP,如有条件可增加IP白名单
在支付平台后台配置允许的回调域名
测试对接流程
正式上线前,需要在支付平台的沙箱或测试环境中进行完整测试:
创建测试订单并点击支付
检查跳转地址和签名参数
模拟成功支付回调,观察订单状态是否更新
模拟错误签名回调,确认系统拒绝处理
模拟重复回调,确认订单不会被重复更新
验证退款、部分退款等附加功能(如已接入)
测试通过后再切换为正式环境参数。