php

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

行云流水
2023-07-09 / 0 评论 / 384 阅读 / 正在检测是否收录...

前言

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

获取支付链接

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

                //构建提交变量
                $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

//注册一个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

<?php
    if (($_SERVER['REQUEST_METHOD'] === 'GET') && preg_match("/payload/i", $_SERVER['REQUEST_URI'])) {
        $url = urldecode($_GET['url']);
    }else{
        $url = '';
    }
?>
<!DOCTYPE html>
<html>
<head>
  <style>
    .iframe-container {
      width: 400px;
      height: 300px;
      overflow: hidden;
    }
  </style>
</head>
<body>
  <div class="iframe-container">
    <iframe src="<?php echo $url; ?>" frameborder="0"></iframe>
  </div>
</body>
</html>

评论 (0)

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