【问题标题】:handling response and placing order in woocommerce after redirecting back from payment website从支付网站重定向回来后,处理响应并在 woocommerce 中下订单
【发布时间】:2017-07-26 07:29:37
【问题描述】:

我正在为 WooCommerce 开发一个支付网关插件(用于 Cyber​​source Payment)。我几乎把它开发到最后,但有一件事是拦截我完成,那就是在成功付款后从支付网关网站重定向到我的 wordpress 页面后的部分(购买确认页面 - 使用 wp 中的模板创建的新 Wordpress 页面-内容/主题/my-theme/order-confirm-template.php)。但我不知道如何处理从支付网站返回的响应。我寻找了一些 woocommerce 钩子,但没有任何效果。我发现有一个钩子 woocommerce_thankyou 但这对我也不起作用。

有人可以在下面的这两点上帮助我吗

1) 网关网站支付成功后返回时如何处理响应并正确下订单并从购物车中移除商品。

2) 我应该从网关网站重定向回哪个页面?回到同一个结帐页面或一些自定义页面,就像我一样。

任何有关代码的帮助将不胜感激。谢谢。

【问题讨论】:

    标签: wordpress woocommerce payment-gateway hook-woocommerce


    【解决方案1】:

    我在我的插件中使用了以下代码,我希望这也适用于你:)

    首先添加这段代码,

    function receipt_page($order){
        echo $this -> ResponceHandler($order);
    }
    

    现在是 ResponceHandler($order) 函数的代码,

     public function ResponceHandler($order_id){
    
    
    
    
                if(!isset($_POST['ResponseCode'])){
    
                global $woocommerce;
            echo '<p>'.__('Thank you for your order, please click the button below to pay with XYZ', 'woocommerce').'</p>';
                $order = new WC_Order($order_id);
    
                $order_id = $order_id.'_'.date("ymds");
    
    
    
                $post_data = get_post_meta($order_id,'_post_data',true);
    
                update_post_meta($order_id,'_post_data',array());
    ###Your Form Code HERE###
       echo '<form><input value="Proceed To Payment" type="submit" /> </form>'; 
    }
    ###Haandle the response###
     if(isset($_POST['ResponseCode']))
                {
                 if($_POST['ResponseCode']==0){
                                        global $woocommerce;
                                        session_start();
                                        $_SESSION['post']=$_POST;
    
                                        $order = new WC_Order($order_id);
    
                                        $order_id = $order_id.'_'.date("ymds");
    
    
    
                                        $post_data = get_post_meta($order_id,'_post_data',true);
    
                                        update_post_meta($order_id,'_post_data',array());
                                                    if($order->status != 'processing'){
    
                                                    $order ->status ='Processing';
                                                    $order->payment_complete();
    
                                                    $order -> add_order_note('XYZ Payment Gateway <br>Response message :'.$_POST['ResponseMessage'].'<br>Payment ID :'.$_POST['PaymentID'].'<br>Merchant Reference Number :'.$_POST['MerchantRefNo'].'<br>Transaction ID :'.$_POST['TransactionID'].'');
    
                                                    add_post_meta( $order->id, '_paymentid', sanitize_text_field( $_POST['PaymentID'] ) );
                                                    add_post_meta( $order->id, '_trno', sanitize_text_field( $_POST['TransactionID'] ) );
                                                    $woocommerce -> cart -> empty_cart();
                                                    wp_redirect( $order->get_checkout_order_received_url());
    
                                                }
                                    }
                else {
    
                                                    if($order->status != 'failed'){
                                                    $order ->status ='failed';
                                    echo "Payment failed!<br><br><br>Possible Error : ".$_POST['ResponseMessage']."<br>PaymentID: ".$_POST['PaymentID']."<br><br><br>We request you to save these details for further reference. <br>You can always pay for this order by clicking on your name in the top right corner and visiting your orders section."; }
                    }
    
                }
    
    
    
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 2016-05-22
      • 2023-03-25
      • 2013-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-20
      • 2023-01-08
      相关资源
      最近更新 更多