wordpress外贸独立站对接第三方支付通道easylink

行云流水
2024-01-08 / 0 评论 / 616 阅读 / 正在检测是否收录...

前言

做跨境电商如何收款是所有商家都需要关注的。自由度最高的是基于wordpress的外贸独立站,独立站建好以后需要对接收款通道。easylink是香港的支付通道。下面分享对接过程。

支付流程

交易流程是指访问网站的客户从选择商品下单到支付成功的整个流程。

插件开发

熟悉整个流程后,就要根据通道提供的接口文档编写代码。代码编写的主要模块有签名函数,有订单提交,异步通知处理,同步跳转函数等。

签名

本通道采用sha256加密方式
function buildMysign($sort_para, $key, $sign_type = "MD5") {

        $prestr = createLinkstring ( $sort_para );
        $prestr = $prestr .'&'. $key;
        $mysgin = sign ( $prestr, $sign_type );
        return $mysgin;
}

function createLinkstring($para) {
        $arg = "";
        foreach ($para as $key => $val) {
        if ($key == "accessKey" || $key == "merchantCardNumber" || $key == "BankId" || $key == "merReserved")
                continue;
        else
                $arg .= $key . "=" . $val . "&";
        }
        $arg = rtrim($arg, '&');
        return $arg;
}

function paraFilter($para) {
        $para_filter = array ();
        foreach ($para as $key => $val) {
                        $para_filter [$key] = $para [$key];
        }
        return $para_filter;
}

function argSort($para) {
        ksort ( $para );
        reset ( $para );
        return $para;
}

function sign($prestr, $sign_type = 'MD5') {
        $sign = '';
        $sign = hash($sign_type, $prestr);
        return $sign;
}

订单提交

订单提交是组织好官方需要的字段,通过api接口提交并获取结果
class Submit {
    function buildRequestPara($para_temp) {
        $key = '********';
        $sign_method = 'SHA256';

        $para_filter = paraFilter($para_temp);

        $para_sort = argSort($para_filter);

        $mysign = buildMysign($para_sort, trim($key), strtoupper(trim($sign_method)));

        $para_sort['accessKey'] = $mysign;

        return $para_sort;
    }

    function getMsg($para_temp, $gateway) {
        $respondData = '';
        $para = $this->buildRequestPara($para_temp);
        $ch = curl_init ($gateway);
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt ( $ch, CURLOPT_BINARYTRANSFER, true);
        curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt ( $ch, CURLOPT_POSTFIELDS, $para);
        $output = curl_exec ( $ch );
        curl_close ( $ch );
        $respondData = json_decode($output);
        return $respondData;
    }
}

异步通知

在用户支付成功后,通道会主动发起支付成功的通知到网站。需要专门的函数来处理支付结果。
    function check_easylink_ipn_response()
    {
        global $woocommerce, $wpdb;
        require_once("lib/notify.class.php");

        if (($_SERVER['REQUEST_METHOD'] === 'POST') && preg_match("/wc_easylink_notify/i", $_SERVER['REQUEST_URI'])) {
            $Notify = new Notify();
            $verify_result = $Notify->verifyNotify();

            if ( $verify_result ) {
                if ($_POST["respCode"] != "00") {
                    error_log(__METHOD__ . PHP_EOL . $_POST["respMsg"]);
                } elseif ( $_POST['status'] == '1' ) {
                    $sref = $_POST['paymentId'];
                    $statustr = $this->easylink_processing;
                    $check_query = $wpdb->get_results("SELECT order_id,order_state,mref FROM {$wpdb->prefix}easylink_data WHERE sref = '".addslashes($sref)."'", ARRAY_A);
                               $check_query_count = count($check_query);

                    if( $check_query_count >= 1 ) {
                        if($check_query[0]['order_state'] == 'I') {
                            $query = "update {$wpdb->prefix}easylink_data set order_state='C'  where sref='".addslashes($sref)."'";
                                        $wpdb->query($query);

                            $inv_id = $check_query[0]['order_id'];
                            $mref = $check_query[0]['mref'];

                                        $order = new WC_Order($inv_id);
                            $order->update_status($statustr, __('Order has been paid by ID: ' . $mref, 'easylink-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($mref) );

                            $order->payment_complete(wc_clean($mref));
                                        $woocommerce->cart->empty_cart();
                        }
                    }

                }
            }
            //接口返回
            exit('success');
        }

    }

同步跳转

在网站处理支付成功的消息的同时,客户浏览器需要跳转到支付成功页面。
function check_easylink_return()
    {
        global $woocommerce, $wpdb;

        if (($_SERVER['REQUEST_METHOD'] === 'GET') && preg_match("/wc_easylink_return/i", $_SERVER['REQUEST_URI'])) {
            //error_log(__METHOD__ . PHP_EOL .print_r($_GET, true));
            $mref = $_GET['mref'];

            sleep(1);
            $check_query = $wpdb->get_results("SELECT order_id,order_state FROM {$wpdb->prefix}easylink_data WHERE mref = '".addslashes($mref)."'", ARRAY_A);
            $check_query_count = count($check_query);
            if($check_query_count >= 1){
                $inv_id = $check_query[0]['order_id'];
                $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());
    }

完整的支付插件

【支付插件】woocommerce对接香港支付通道easylink

插件设置

插件安装通过wordpress的后台页面上传然后激活即可,设置也非常简单。

评论 (0)

取消
只有登录/注册用户才可评论