Update order after success or failed payment in woocommerce
<p>I integrated a custom payment gateway for woocommerce, I have successfully done it and able to output success / failure and redirects correctly along with emptying the cart.. 
Redirects correctly
Emptys cart
Does not update order_status or add_order_note</p>

<pre><code>function check_pg_response($order_id)
{
global $woocommerce;
    $msg['class']   = 'error';
    $msg['message'] = $this-&gt;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 &lt; $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 -&gt; status !=='completed'){
$order_id = (int) $order_id;
$order = new WC_Order( $order_id );

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

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

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


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

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

    }
    $woocommerce-&gt;set_messages();
}
$redirect_url = get_permalink(woocommerce_get_page_id('myaccount'));
wp_redirect($redirect_url);
exit;
</code></pre>

<p>}
}</p>