woocommerce对接第四方支付通道okpay

行云流水
2023-12-22 / 0 评论 / 366 阅读 / 正在检测是否收录...

前言

用wordpress+woocommerce做跨境独立站的朋友为了顺利收款,会对接各种国外的本地收款渠道。主流的用paypal等,不过通道越大。合规查的越严,小众的一些三方或四方通道,限制也会小一点。本文分享菲律宾四方通道okpay的对接过程。

对接流程

一般都是申请通道账号,根据接口文档开发插件,然后测试调试,通过后上线。支付插件主要有三个函数和签名函数。三个函数为提交订单信息获取支付链接,异步通知处理,同步跳转处理函数。

核心代码

md5签名函数

                 public function get_sign($srcArray, $merKey){
                        if(null == $srcArray){
                            return "123456";
                        }
                        //先干掉sign字段
                        $keys = array_keys($srcArray);
                        $index = array_search("sign", $keys);
                        if ($index !== FALSE) {
                            array_splice($srcArray, $index, 1);
                        }

                        //对数组排序
                        ksort($srcArray);

                        //生成待签名字符串
                        $srcData = "";
                        foreach ($srcArray as $key => $val) {
                            if($val === null || $val === "" ){
                                //值为空的跳过,不参与加密
                                continue;
                            }
                            $srcData .= "$key=$val" . "&";
                        }
                        $srcData = substr($srcData, 0, strlen($srcData) - 1);

                        //生成签名字符串
                        $sign = md5($srcData.$merKey);
                        return $sign;
                }

post提交函数

                public function http_post($url, $sendInfo)
                {
                    try {
                        $ch = curl_init();
                        curl_setopt($ch, CURLOPT_URL, $url);
                        curl_setopt($ch, CURLOPT_POST, 1);
                        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
                        curl_setopt($ch, CURLOPT_TIMEOUT, 60);

                        // Set headers for JSON content
                        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                            'Content-Type: application/json',
                            'Accept: application/json'
                        ));

                        // Encode $sendInfo as JSON
                        $json_data = json_encode($sendInfo);

                        curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
                        $curl_result = curl_exec($ch);
                        curl_close($ch);
                        return $curl_result;
                    } catch (Exception $e) {
                        return null;
                    }
                }

支付成功后异步回调处理函数

                if (($_SERVER['REQUEST_METHOD'] === 'POST') && preg_match("/wc_okpay_notify/i", $_SERVER['REQUEST_URI'])) {
                                $response = file_get_contents("php://input");
                                error_log(__METHOD__ . PHP_EOL .print_r($response, true));

                                //参数
                                $respj = json_decode($response, true, 512, JSON_BIGINT_AS_STRING);
                                //error_log(__METHOD__ . PHP_EOL .print_r($respj['data'], true));

                                if ($respj['data']) {
                                        //组织签名串
                                        //$res_sign = $this->get_sign($_POST, $this->okpay_merchant_key);

                                        #订单号
                                        $mref = $respj['data']['orderNo'];
                                        $sref = $respj['data']['businessNo'];;

                                        #写入数据库
                                        $query = "update {$wpdb->prefix}okpay_data  set order_state = 'C' where mref = '".addslashes($mref)."'";
                                        $wpdb->query($query);

                                        #后续处理
                                        $check_query = $wpdb->get_results("SELECT order_id FROM {$wpdb->prefix}okpay_data WHERE mref = '".addslashes($mref)."'", ARRAY_A);
                                        $check_query_count = count($check_query);
                                        if($check_query_count >= 1){
                                                $order_id= $check_query[0]['order_id'];

                                                $order = new WC_Order($order_id);
                                                $statustr = $this->okpay_processing;

                                                $order->update_status($statustr, __('Order has been paid by ID: ' . $sref, 'okpay-for-woocommerce'));
                                                wc_reduce_stock_levels( $order->get_id() );

                                                add_post_meta( $order_id, '_paid_date', current_time('mysql'), true );
                                                update_post_meta( $order_id, '_transaction_id', wc_clean($order_id) );

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

                }

项目完整代码

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

评论 (0)

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