1

我为 woocommerce 集成了一个自定义支付网关,我已经成功完成并能够输出成功/失败并正确重定向以及清空购物车.. 正确重定向 Emptys cart 不更新 order_status 或 add_order_note

function check_pg_response($order_id)
{
global $woocommerce;
    $msg['class']   = 'error';
    $msg['message'] = $this->settings['error_msg']." - Error: No Response From Payment Gateway.";

if (isset($_REQUEST['msg'])){
$response = $_REQUEST['msg'];
$response_data = array();
$resp = explode("|", $response);
$dataSize=sizeof($resp);
    for($i = 0; $i < $dataSize; $i++) 
                {
                    $information=explode('=',$resp[$i]);
                    if($i==0)   $txn_status=$information[1]; //001   
                    if($i==1)   $txn_msg=$information[1]; // success or failure
                    if($i==2)   $txn_err_msg=$information[1]; // canceled by user   
                    if($i==3)   $txn_ref=$information[1];
                    if($i==4)   $bank_cd=$information[1];
                    if($i==5)   $txn_id=$information[1];    
                    if($i==6)   $txn_amt=$information[1];
                    if($i==7)   $txn_time=$information[1];    
                }
        }
if($order -> status !=='completed'){
$order_id = (int) $order_id;
$order = new WC_Order( $order_id );

    if ($txn_msg=='success'){
        $transauthorised = true;
        $msg['message']  = $this->settings['success_msg'];
        $msg['class']    = 'success';
if ($order->status != 'processing') {
        $order->payment_complete();
        $order->update_status('completed', __('Payment Successful.', 'wptut'));
        $order->add_order_note('PG ID: '.$clnt_txn_ref.'<br/> Transaction ID: '.$tpsl_txn_id.'<br/>Bank CD: '.$tpsl_bank_cd);
        $woocommerce -> cart -> empty_cart();}

        } 
        else if($txn_msg=='failure'){
        $msg['class']   = 'error';
        $msg['message'] = $this->settings['declined_msg']." - Error: Payment Gateway Declined order.";
        $order->update_status('failed', __('Payment has been cancelled.', 'wptut'));
        $order->add_order_note('PG payment failed<br/>Techprocess ID: '.$txn_ref.'<br/>Payment Gateway Message: '.$txn_err_msg);
        $woocommerce -> cart -> empty_cart();
    }else{
        $msg['class']   = 'error';
        $msg['message'] = $this->settings['error_msg']." - Error: Unknown Error";
    }

    if ($transauthorised == false) {
        $order->update_status('failed');
        $order->add_order_note($msg['message']);
    }
    }


if (function_exists('wc_add_notice')) {
    wc_add_notice($msg['message'], $msg['class']);

} else {
    if ($msg['class'] == 'success') {
        $woocommerce->add_message($msg['message']);
    } else {
        $woocommerce->add_error($msg['message']);

    }
    $woocommerce->set_messages();
}
$redirect_url = get_permalink(woocommerce_get_page_id('myaccount'));
wp_redirect($redirect_url);
exit;

} }

4

1 回答 1

0

在 Woocommerce 中,要制作自定义支付网关,您必须重写WC_Payment_Gateway's类函数。

do_order_complete_tasks函数在订单完成之前处理操作。

 protected function do_order_complete_tasks()
     {

          global $woocommerce;

          // return if order status id already completed.
          if ($this->order->status == 'completed')
               return;

          $this->order->payment_complete();
          $woocommerce->cart->empty_cart();

          // add order note 
          $this->order->add_order_note(sprintf("Order completed with Transaction Id of '%s'", $this->transactionId));

     }

process_payment函数 处理支付的订单流程

    function process_payment($order_id)
    {
        global $woocommerce;
        $this->order        = new WC_Order($order_id);

            // calling do_order_complete_tasks() function. 
            $this->do_order_complete_tasks();

            return array(
                'result' => 'success',
                'redirect' => $this->get_return_url($this->order)
            );

    }

在您的自定义 WC 支付网关类中添加这两个函数并根据您的需要进行修改。

希望这有帮助!

于 2016-11-25T10:51:34.727 回答