第三方支付HKPay接口对接过程要点总结

行云流水
2023-09-13 / 0 评论 / 1,235 阅读 / 正在检测是否收录...

前言

HKPay是香港的一条支付通道,可用于外贸独立站做境外电商收款。

核心函数

所有支付通道的对接流程,几乎都是一样的。一般都是组织参数、签名、提交然后处理返回结果。然后跳转到收银页面,等待用户支付。用户支付后,返回网站。同时异步接口接收结果通知并处理。

字符串拼接函数

计算md5签名之前,用到的字符串拼接函数。
                //拼接字符串
                public function param_ck_null($kq_va,$kq_na)
                {
                        if($kq_va == ""){
                                $kq_va="";
                        }else{
                                return $kq_va=$kq_na.'='.$kq_va.'&';
                        }
                }

数据提交函数

采用post/json提交方式
                //提交数据到上游接口函数
                public function send_post_request($url, $body, $headers, $method)
                {
                        $curl = curl_init();

                        curl_setopt_array($curl, array(
                            CURLOPT_URL => $url,
                            CURLOPT_RETURNTRANSFER => true,
                            CURLOPT_ENCODING => '',
                            CURLOPT_MAXREDIRS => 10,
                            CURLOPT_TIMEOUT => 0,
                            CURLOPT_FOLLOWLOCATION => true,
                            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                            CURLOPT_CUSTOMREQUEST => $method,
                            CURLOPT_POSTFIELDS => json_encode($body),
                            CURLOPT_HTTPHEADER => $headers,
                        ));

                        $response = curl_exec($curl);

                        curl_close($curl);

                        return $response;
                }

获取订单结果url

用户支付之后,不管成功还是失败都需要跳转回到网站。此函数用于获取返回网站需要的网址。
                //获取订单结果url
                public function get_order_checkout_url($orderid)
                {
                        //完成支付url
                        $rurl = WC()->api_request_url( 'wc_hkpay_return' ) ;
                        $check = strpos($rurl, '?');
                        if ( $check !== false) {
                                $rurl = $rurl . "&mref=" . $orderid;
                        } else {
                                $rurl = $rurl . "?mref=" . $orderid;
                        }

                        return $rurl;
                }

展示支付二维码

返回的是二维码数据,需要在网站上展示
                function generate_form($order_id)
                {
                        $html_str = '';
                        global $wpdb;
                        $order = wc_get_order($order_id);

                        //url查询
                        $check_query = $wpdb->get_results("SELECT ref,paydata,paytype FROM {$wpdb->prefix}hkpay_data WHERE orderid = '".addslashes($order_id)."' order by id desc", ARRAY_A);
                        $check_query_count = count($check_query);
                        if($check_query_count >= 1){
                                $mref = $check_query[0]['ref'];
                                $paydata = $check_query[0]['paydata'];
                                $paytype = $check_query[0]['paytype'];

                                $completed_url = $this->get_order_checkout_url($mref);

                                if ($paytype ==='800201') {
                                         $html_str =  '<span>Scan the QR code to complete the payment</span>'.
                                                '<div>'.
                                                '<img src="http://chart.apis.google.com/chart?chs=150x150&cht=qr&chld=L|0&chl='.rawurlencode($paydata).'" alt="QR code" widhtHeight="150" widhtHeight="150"/>'.
                                                '</div>'.
                                                '<div>'.
                                                        '<a class="button alt" id="submit_uepay_payment_button" href="'.$completed_url.'">'.__('Pay Completed', 'uepay-for-woocommerce').'</a>'.
                                                        '<a class="button cancel" href="'.$order->get_cancel_order_url().'">'.__('Cancel', 'uepay-for-woocommerce').'</a>'.
                                                '</div>';

                                }
                        }

                        return $html_str;

                }

提交订单并处理返回

获取支付链接,并处理返回
                        $response = $this->send_post_request($req_api, $Body, $Headers, 'POST');
                        $result = json_decode($response);

                        //打印结果
                        //error_log(__METHOD__ . PHP_EOL .print_r($result, true));
                        if ($result->code != 0 ) {
                                error_log(__METHOD__ . PHP_EOL . 'Code:' . $result->code . PHP_EOL. ' Error:' . $result->msg);
                                //抛出异常
                                throw new Exception("Unable to reach hkpay Payments (" . $result->msg . ")");
                        }

                        if ($result->code == 0 ) {
                                $OrderNo = $result->data->payOrderId;
                                $paydata = $result->data->payData;
                                #写入数据库
                                $query = "insert into {$wpdb->prefix}hkpay_data (ref, ordercode, orderid, total_cost, currency, paydata, paytype, order_state, timestamp)
                                values ('".$mref."', '".$OrderNo."','". $order_id . "',".$amount.",'". $currency_code."','". $paydata ."','".$payType."','I', now())";
                                $wpdb->query($query);

                                //跳转payurl
                                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' ))))
                                );
                        } else {
                                error_log(__METHOD__ . PHP_EOL . 'Code:' . $result->code . PHP_EOL. ' Error:' . $result->msg);
                                throw new Exception("Unable to redirect payurl (" . $result->msg . ")");
                        }

完整支付插件

【支付插件】woocommerce对接hkpay三方支付接口

评论 (0)

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