0
votes

I was develop a custom payment gateway for woocommerce, everything is good but after payment success the order still "pending payment"

In the __construct() function I check for payment provider respond

<?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']);
}

and here is the complete_transaction() function

<?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;          
        }

    }
}

But the order status still "pending payment". now I need to change the order status to "Completed" after check the payment provider respond.

1

1 Answers

0
votes

Fixed

The problem because of this line

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

it must be $order instead of $this so the line is like this

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