woocommerce对接第三方支付DDMpay知识点总结

2023-11-17 13:44:19 浏览数 (3)

前言

第三方,第四方支付接口接多了,无非就是那么几步。这次客户有一个新的需求是不跳转到第三方支付网关。同时刚好支持iframe方式加载。记录一下改动代码。

获取支付链接

通过post提交支付参数,上游返回支付网关关于此订单的支付链接地址,将地址传输给单独注册的支付页面

代码语言:javascript复制
                //构建提交变量
                $args = array(
                        'timeout'     => 45,
                        'redirection' => 5,
                        'httpversion' => '1.0',
                        'blocking'    => true,
                        'body'        => $Body,
                );

                $postRequest = wp_remote_post($gateway_api, $args);

                if ($postRequest['response']['code'] === 200) {
                        $result = json_decode($postRequest['body'], true, 512, JSON_BIGINT_AS_STRING);
                } else {
                        error_log(__METHOD__ . PHP_EOL . 'Code:' . $postRequest['response']['code'] . PHP_EOL. ' Error:' . $postRequest['response']['message']);

                        throw new Exception("Unable to reach Viva Payments (" . $postRequest['response']['message'] . ")");
                }

                //error_log(__METHOD__ . PHP_EOL .print_r($result, true));

                if ($result['code'] == 200) {
                        //写入数据库
                        $query = "insert into {$wpdb->prefix}ddmpay_data (ref, ordercode, email, orderid, total_cost, currency, order_state, timestamp) values ('". $mref . "', '0', '". $customer_mail . "','" . $order_id . "','" . $amount . "', '" . $currency_code . "', 'I', now())";
                        $wpdb->query($query);

                        //跳转支付,通过iframe加载
                        return array(
                            'result' => 'success',
                            'redirect' => $site_url.'/?payload=1&url='.urlencode($result['data']['payurl'])
                        );
                } else {
                        throw new Exception("Unable to redirect  payurl  (" . $result['msg'] . ")");
                }

注册加载页面

在插件安装文件setup.php内注册

setup.php

代码语言:javascript复制
//注册一个pay加载页面
add_action('init', 'register_ddpay_query_variables');
add_action('template_redirect', 'redirect_to_ddpay_templates');

function register_ddpay_query_variables() {
        add_filter('query_vars', function($vars) {
            $vars[] = 'payload';
            return $vars;
        });
}

function redirect_to_ddpay_templates() {
        if (get_query_var('payload')) {
            include dirname(__FILE__) . '/payload.php';
            exit();
        }
}

payload.php

代码语言:javascript复制
    .iframe-container {
      width: 400px;
      height: 300px;
      overflow: hidden;
    }

0 人点赞