前言
参考:使用WooCommerce支付网关 API 创建自定义支付网关的方法
支付流程
主要模块
请求上游接口
process_payment(), woocommerce发起支付动作函数
function process_payment($order_id)
{
$order = new WC_Order($order_id);
//$current_version = get_option( 'woocommerce_version', null );
//error_log($current_version);
return array
(
'result' => 'success',
'redirect' => esc_url_raw(add_query_arg('order-pay', $order->get_id(), add_query_arg('key', $order->get_order_key(), wc_get_page_permalink( 'checkout' ))))
);
}
receipt_page(),订单支付前准被页面函数
function receipt_page($order)
{
echo '<p>'.__('Thank you for your order, please click the button below to pay.', 'superxpay-for-woocommerce').'</p>';
//error_log($_SERVER['HTTP_REFERER']);
//解决函数被调用2次的问题
if ( strpos($_SERVER['HTTP_REFERER'], 'order-pay') === false ) {
echo $this->generate_form($order);
}
}
generate_form(),订单参数准备函数,请求上游接口,获取支付链接
异步回调接口
function check_ipn_response()
{
global $woocommerce, $wpdb;
if (($_SERVER['REQUEST_METHOD'] === 'POST') && preg_match("/wc_superxpay_notify/i", $_SERVER['REQUEST_URI'])) {
$response = file_get_contents("php://input");
error_log(__METHOD__ . PHP_EOL .print_r($response, true));
if ( $response ) {
$res_data = json_decode($response, true, 512, JSON_BIGINT_AS_STRING);
$private_key = html_entity_decode($this->superxpay_merchantkey);
$statustr = $this->superxpay_processing;
$checksum = md5($res_data['OrderNo'] . '&' . $res_data['MerchantNo'] . '&' . $res_data['Amount'] . '&' . $res_data['OutTradeNo'] . '&' . $res_data['Status'] . '&' . $private_key);
//error_log($checksum);
//error_log($res_data['Sign']);
if ($checksum == $res_data['Sign']) {
$tm_ref = $res_data['OutTradeNo'];
$check_query = $wpdb->get_results("SELECT orderid,order_state FROM {$wpdb->prefix}superxpay_data WHERE ref = '".addslashes($tm_ref)."'", ARRAY_A);
$check_query_count = count($check_query);
//error_log("SELECT orderid,order_state FROM {$wpdb->prefix}superxpay_data WHERE ref = '".addslashes($tm_ref)."'");
//error_log($check_query_count);
if( $check_query_count >= 1 ) {
if($check_query[0]['order_state'] == 'I' && $res_data['Status'] == '1') {
$query = "update {$wpdb->prefix}superxpay_data set order_state='C' where ref='".addslashes($tm_ref)."'";
$wpdb->query($query);
$inv_id = $check_query[0]['orderid'];
$order = new WC_Order($inv_id);
$order->update_status($statustr, __('Order has been paid by ID: ' . $res_data['OrderNo'], 'superxpay-for-woocommerce'));
wc_reduce_stock_levels( $order->get_id() );
add_post_meta( $inv_id, '_paid_date', current_time('mysql'), true );
update_post_meta( $inv_id, '_transaction_id', wc_clean($tm_ref) );
$order->payment_complete(wc_clean($tm_ref));
$woocommerce->cart->empty_cart();
}
}
}
}
//接口返回
exit("SUCCESS");
}
}
同步接口处理逻辑
function check_superpay_return()
{
global $woocommerce, $wpdb;
//error_log($_SERVER['REQUEST_URI']);
if (($_SERVER['REQUEST_METHOD'] === 'GET') && preg_match("/wc_superxpay_return/i", $_SERVER['REQUEST_URI'])) {
error_log(__METHOD__ . PHP_EOL .print_r($_GET, true));
$tm_ref = $_GET['mref'];
error_log($tm_ref);
$check_query = $wpdb->get_results("SELECT orderid,order_state FROM {$wpdb->prefix}superxpay_data WHERE ref = '".addslashes($tm_ref)."'", ARRAY_A);
$check_query_count = count($check_query);
if($check_query_count >= 1){
$inv_id = $check_query[0]['orderid'];
$inv_state = $check_query[0]['order_state'];
switch ( $inv_state ) {
case 'C':
$order = new WC_Order($inv_id);
wp_redirect(esc_url_raw(add_query_arg('key', $order->get_order_key(), add_query_arg('order-received', $inv_id, $this->get_return_url($order)))));
break;
default:
wp_redirect( wc_get_cart_url() );
}
exit;
}
}
wp_redirect(home_url());
}
FAQ
请求上游接口,函数被调用2次的问题
if ( strpos($_SERVER['HTTP_REFERER'], 'order-pay') === false ) {
echo $this->generate_form($order);
}
checkout页面自动跳转
wc_enqueue_js('
setInterval(function(){
$.blockUI({
message: "' . esc_js(__('Thank you for your order. We are now redirecting you to superxpay to make payment.', 'superxpay-for-woocommerce')) . '",
baseZ: 99999,
overlayCSS:
{
background: "#fff",
opacity: 0.6
},
css: {
padding: "20px",
zindex: "9999999",
textAlign: "center",
color: "#555",
border: "3px solid #aaa",
backgroundColor:"#fff",
cursor: "wait",
lineHeight: "24px",
}
});
var start = new Date().getTime();
while (new Date().getTime() < start + 1000);
document.getElementById("submit_superxpay_payment_button").click();
},1000);
');
学习到了,感谢博主
似懂非懂
受益匪浅,感谢博主。
受益匪浅,感谢博主。
好的,非常感谢
写的非常好,这个有demo么,参考一下
大佬,请教一下,开发支付插件是要有stripe账号对吧?我在安装woocommerce的时候需要填stripe信息
stripe只是支付通道的一种
大佬请教一下 怎么写一个woocommerce对接易支付接口的插件啊
你懂开发吗?如果有编程基础的话,网上参考一些文档。然后照着葫芦画个瓢,哈哈。主要是有耐心,几天就搞定了。如果不懂开发,也简单。两个字,花钱。
蛤蟆跳水 不懂不懂 那大概要多少
要看具体要求,你有网站要对接吗?还是需要插件给别人用
就是自己用,弄了个wp网店,别的都有,就是缺个对接的插件,网上找了个,不过测试付款的时候404
加我Q: 1940728253
找找
很好,学习了