简体   繁体   English

Woocommerce 自定义支付网关重定向

[英]Woocommerce custom payment gateway redirection

I'm trying to create a custom Woocommerce payment gateway that redirects the customer to the payment gateway page to complete the checkout and I'm using the process_payment code as follows:我正在尝试创建一个自定义 Woocommerce 支付网关,将客户重定向到支付网关页面以完成结帐,我使用的process_payment代码如下:

    public function process_payment( $order_id )
    {
        global $woocommerce;

        // we need it to get any order details
        $order = wc_get_order( $order_id );

        //setting up needed variables for POST
        $moovpay = new MoovPaySDK;
        $paymentURL = //payment gateway API URL;
        $time = date('YmdHis');
        $secretKey = $this->private_key;
        $merchant_code = $this->merchant_code;
        $mid = $this->mid;
        $orderID = zeroise($order_id, 8);
        $backend_URL = //callback_URL;
        $order_amount = $order->get_total();
        $order_amount_CNY = wc_format_decimal( $order_amount * 5, 2 );;
        $order_amount_CNY_no_dot = str_replace(".", "", $order_amount_CNY);
        //initiate payment
        $response = $moovpay->purchase($secretKey, '', $backend_URL, $merchant_code, $mid, $orderID, $time, '', $order_amount_CNY_no_dot, '');

And the API returns this response (I removed the values): API 返回此响应(我删除了值):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <form id="redirectForm" action="payment-gateway-url-here" method="post">
        
            
                <input type="hidden" name="bizType" id="bizType" value="" />
            
                <input type="hidden" name="txnSubType" id="txnSubType" value="" />
            
                <input type="hidden" name="backUrl" id="backUrl" value="" />
            
                <input type="hidden" name="orderId" id="orderId" value="" />
            
                <input type="hidden" name="signature" id="signature" value="" />
            
                <input type="hidden" name="merName" id="merName" value="" />
            
                <input type="hidden" name="txnType" id="txnType" value="" />
            
                <input type="hidden" name="channelType" id="channelType" value="" />
            
                <input type="hidden" name="certId" id="certId" value="" />
            
                <input type="hidden" name="encoding" id="encoding" value="UTF-8" />
            
                <input type="hidden" name="acqInsCode" id="acqInsCode" value="" />
            
                <input type="hidden" name="version" id="version" value="" />
            
                <input type="hidden" name="merAbbr" id="merAbbr" value="" />
            
                <input type="hidden" name="accessType" id="accessType" value="" />
            
                <input type="hidden" name="reqReserved" id="reqReserved" value="" />
            
                <input type="hidden" name="txnTime" id="txnTime" value="" />
            
                <input type="hidden" name="merId" id="merId" value="" />
            
                <input type="hidden" name="merCatCode" id="merCatCode" value="" />
            
                <input type="hidden" name="currencyCode" id="currencyCode" value="" />
            
                <input type="hidden" name="signMethod" id="signMethod" value="" />
            
                <input type="hidden" name="txnAmt" id="txnAmt" value="" />
            
        
    </form>
<script type="text/javascript">
    document.all.redirectForm.submit();
</script>   
</body>
</html>

How do I use the response to perform the redirection to payment gateway?如何使用响应执行重定向到支付网关? I'm not sure if this has been asked before but I'm can't seem to find any solutions to this.我不确定以前是否有人问过这个问题,但我似乎找不到任何解决方案。 Any help or direction would be greatly appreciated.任何帮助或方向将不胜感激。

I managed to get it working, I'll post my answer here in case someone needs a solution to this.我设法让它工作,我会在这里发布我的答案,以防有人需要解决这个问题。

What I did was to output the response to a php file and then perform the redirect using it.我所做的是 output 对 php 文件的响应,然后使用它执行重定向。

public function process_payment( $order_id )
{
  global $woocommerce;
  
  // we need it to get any order details
  $order = wc_get_order( $order_id );
  
  //setting up needed variables for POST
  $moovpay = new MoovPaySDK;
  $paymentURL = ''; //payment gateway API URL;
  $time = date('YmdHis');
  $secretKey = $this->private_key;
  $merchant_code = $this->merchant_code;
  $mid = $this->mid;
  $orderID = zeroise($order_id, 8);
  $backend_URL = '' //callback_URL;
  $order_amount = $order->get_total();
  $order_amount_CNY = wc_format_decimal( $order_amount * 5, 2 );;
  $order_amount_CNY_no_dot = str_replace(".", "", $order_amount_CNY);
  //initiate payment
  $response = $moovpay->purchase($secretKey, '', $backend_URL, $merchant_code, $mid, $orderID, $time, '', $order_amount_CNY_no_dot, '');
  $fh = fopen(plugin_dir_path(__FILE__).'redirect.php', 'w+');
  fwrite($fh, $response);
  fclose($fh);
  $redirect_url = plugin_dir_url(__FILE__).'redirect.php';
  
  return array(
    'result' => 'success',
    'redirect' => $redirect_url
  );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM