成功付款后,订单状态仍为“待付款”(自定义付款网关)

时间:2019-11-11 07:44:24

标签: wordpress woocommerce payment-gateway

我正在为woocommerce开发一个自定义付款网关,一切都很好,但是付款成功后,订单仍然“待付款”

__construct()功能中,我检查付款服务提供商的响应

<?php
//when payment done and redirected with payment reference code
if(isset($_REQUEST['transaction_id'])){
    paytabs_set_cookie('transaction_id', $_REQUEST['transaction_id']);
    $order  = new WC_Order($order_id);   
    $this->order = $order;
    $this->complete_transaction($order->get_id(), $_REQUEST['transaction_id']);
}

这是complete_transaction()函数

<?php
/*
When transaction completed it is check the status 
is transaction completed or rejected
*/
function complete_transaction($order_id, $transaction_id) {
    global $woocommerce;       
    $order = new WC_Order( $order_id );

    $request_string=array(
                'secret_key'        => $_COOKIE['secret_key'],
                'merchant_email'    => $_COOKIE['merchant_email'],
                'transaction_id'    => $transaction_id,
                'order_id'          => $order_id
            );
    $gateway_url=$this->liveurl.'apiv2/verify_payment_transaction';
    $getdataresponse=$this->sendRequest($gateway_url, $request_string);
    $object=json_decode($getdataresponse, true);


    if (isset($object->response_code)) {
        //if get response successfull
        if($object->response_code == '100'){

            // thankyou and set error message 
            $this->msg['class'] = 'woocommerce_message';
            $check=$order->payment_complete();

            // Reduce stock levels
            $order->reduce_order_stock();

            // Remove cart
            $woocommerce->cart->empty_cart();   

            wc_add_notice( '' . __( 'Thank you for shopping with us. Your account has been charged and your transaction is successful.
            We will be shipping your order to you soon.', 'woocommerce' ), 'success' );     

            wp_redirect( $this->get_checkout_order_received_url( $order ) );
            exit;

        }else{

            // Change the status to pending / unpaid
            $order->update_status('failed', __('Payment Cancelled', 'error'));    

            // Add error for the customer when we return back to the cart
            $message=$object->result;

            wc_add_notice( '<strong></strong> ' . __($message, 'error' ), 'error' ); 

            // Redirect back to the last step in the checkout process
            wp_redirect( $this->get_cancel_order_url( $order ) );
            exit;          
        }

    }
}

但是订单状态仍然为“待付款”。 现在,我需要在检查付款提供商的答复后将订单状态更改为“已完成”。

1 个答案:

答案 0 :(得分:0)

固定

此行所引起的问题

wp_redirect( $this->get_checkout_order_received_url( $order ) );

它必须是$order而不是$this,所以行是这样的

wp_redirect( $order->get_checkout_order_received_url( $order ) );