WooCommerce对接第三方支付插件开发

it不难
2022-04-29 / 16 评论 / 503 阅读 / 正在检测是否收录...

前言

参考:使用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);
            ');

完整项目代码

wordpress商城第四方支付插件

6

评论 (16)

取消
  1. 头像
    lyon
    中国广东省 · Windows 10 · Google Chrome
    沙发

    学习到了,感谢博主表情

    回复
  2. 头像
    lok
    中国广东省 · Windows 10 · Google Chrome
    板凳

    似懂非懂

    回复
  3. 头像
    leslie
    中国广东省 · Windows 10 · Google Chrome
    地毯

    受益匪浅,感谢博主。

    回复
  4. 头像
    TY5461
    · Windows 10 · Google Chrome
    第4楼

    受益匪浅,感谢博主。

    回复
  5. 头像
    summer
    中国上海市 · MacOS · Google Chrome
    第5楼
    @

    好的,非常感谢

    回复
  6. 头像
    summer
    中国上海市 · MacOS · Google Chrome
    第6楼

    写的非常好,这个有demo么,参考一下

    回复
  7. 头像
    summer
    中国上海市 · MacOS · Google Chrome
    第7楼

    大佬,请教一下,开发支付插件是要有stripe账号对吧?我在安装woocommerce的时候需要填stripe信息

    回复
    1. 头像
      it不难 作者
      中国江苏省 · MacOS · Google Chrome
      @ summer

      stripe只是支付通道的一种

      回复
  8. 头像
    lin
    中国河北省 · Android · Google Chrome
    第8楼

    大佬请教一下 怎么写一个woocommerce对接易支付接口的插件啊

    回复
    1. 头像
      it不难 作者
      · MacOS · Google Chrome
      @ lin

      你懂开发吗?如果有编程基础的话,网上参考一些文档。然后照着葫芦画个瓢,哈哈。主要是有耐心,几天就搞定了。如果不懂开发,也简单。两个字,花钱。

      回复
      1. 头像
        lin
        中国河北省 · Android · Google Chrome
        @ it不难

        蛤蟆跳水 不懂不懂 那大概要多少表情

        回复
        1. 头像
          it不难 作者
          · MacOS · Google Chrome
          @ lin

          要看具体要求,你有网站要对接吗?还是需要插件给别人用

          回复
          1. 头像
            lin
            · Android · Google Chrome
            @ it不难

            就是自己用,弄了个wp网店,别的都有,就是缺个对接的插件,网上找了个,不过测试付款的时候404

            回复
            1. 头像
              it不难 作者
              · MacOS · Google Chrome
              @ lin

              加我Q: 1940728253

              回复
  9. 头像
    嗷嗷
    中国吉林省 · MacOS · Google Chrome
    第9楼

    找找

    回复
  10. 头像
    it不难 作者
    中国江苏省 · MacOS · Google Chrome
    第10楼

    很好,学习了

    回复