在woocommerce成功或付款失败后更新订单

时间:2016-11-25 09:48:49

标签: php wordpress payment-gateway woocommerce-rest-api

我为woocommerce集成了一个自定义支付网关,我已成功完成它并能够输出成功/失败并正确重定向以及清空购物车.. 重定向正确 Emptys购物车 不更新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;

} }

1 个答案:

答案 0 :(得分: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支付网关类中添加这两个功能,并根据您的需要进行修改。

希望这有帮助!