Woocommerce custom payment gateway workflow using redirection

I am new to WooCommerce custom payment gateway(PG) integrations.

Initial Page: The checkout page of WooCommerce. I have created a custom plugin based on instructions from here:
http://www.sitepoint.com/building-a-woocommerce-payment-extension/
and
http://docs.woothemes.com/document/payment-gateway-api/

Read More

So, when I visit the checkout page, I can see my payment gateway at the bottom. My Code:

Constructor:

public function __construct() {
    $this->id                   = 'xxxx_payment_gateway';
    $this->title                = __( "xxxx Payment Gateway", 'woocommerce-xxxx-gateway' );
    $this->icon                 = null;
    $this->method_title         = __( 'xxxx Payment Gateway', 'woocommerce-xxxx-gateway' );
    $this->method_description   = __( 'Take payments via xxxx Payment Gateway - uses the xxxx Payment Gateway SDK.', 'woocommerce-xxxx-gateway' );
    $this->has_fields           = true;

    $this->init_form_fields();
    $this->init_settings();

    // Save settings
    if ( is_admin() ) {
        add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( &$this, 'process_admin_options' ) );
    }
}

process_payment

function process_payment( $order_id ) {
    $customer_order = wc_get_order( $order_id );
    $country_list = array(
        "IN"=>"IND",
    );
    $environment_url = 'http://example.com/pgic/pgserv.php';
    $payload = array(
        "x_invoice_num"         => str_replace( "#", "", $customer_order->get_order_number() ),
        "x_merchant_id"         => $this->merchant_id, 
        // Order total
        "x_amount"              => 3,//$customer_order->order_total,
        // Billing Information
        "x_first_name"          => $customer_order->billing_first_name,
        ....
        // Shipping Information
        "x_ship_to_first_name"  => $customer_order->shipping_first_name,
        ....
        "x_cust_id"             => $customer_order->user_id,
        "x_customer_ip"         => $_SERVER['REMOTE_ADDR'],
    );
    $response = wp_remote_post( $environment_url, 
        array(
            'method'    => 'POST',
            'body'      => http_build_query( $payload ),
            'timeout'   => 90,
            'sslverify' => false,
        ) 
    );
    $forwardURL = trim(wp_remote_retrieve_body( $response ));

    if ( is_wp_error( $response ) ) 
    {
        // Return failure redirect
        return array(
            'result'    => 'failure',
            'redirect'  => 'failed.php'
        );
    }
    else{
        // Reduce stock levels
        // $order->reduce_order_stock();

        // Remove cart
        // WC()->cart->empty_cart();

        // Return thankyou redirect
        return array(
            'result'    => 'success',
            'redirect'  => $forwardURL //$this->get_return_url( $customer_order )
        );
    }
}//process_payment

My PG vendor gave me a following process to integrate.

Page 1: The above plugin process_payment passes on to the payment gateway processing to the pgserv.php.

Page 2: Automatically my bank’s page comes up (because of the credit card of my bank that I use) where I provide my OTP and all.

Page 3: Once that step is complete, the payment gateway forwards from there to another success landing page (let’s call it pgresponse.php) within my website that returns the transaction result.

This is where the problem begins.
I tried redirecting/submitting to an independent page (submitaction.php) where I try to mark the order complete/payment complete and empty the cart. My Code:

global $woocommerce, $post;
$order = new WC_Order($post->ID);
$payment_status = $order->payment_complete();

No matter what I do, the order in this case doesn’t update status to payment complete. Even $payment_status doesn’t return anything.

Questions:

  1. What do I do?

  2. I intend to write a hook for process_order_status to send mails. The way I am planning would be to write the following code in the plugin:

    public function process_order_status( $order, $payment_id, $status, $auth_code ) {
        echo "Payment details :: $order, $payment_id, $status, $auth_code";
        error_log("Payment details :: $order, $payment_id, $status, $auth_code");
        if ( 'APPROVED' == $status ) {
            // Payment complete
            $order->payment_complete( $payment_id );
            // Add order note
            $order->add_order_note( sprintf( __( 'Payment approved (ID: %s, Auth Code: %s)', 'woocommerce' ), $payment_id, $auth_code ) );
            // Remove cart
            WC()->cart->empty_cart();
            return true;
        }
        return false;
    }//process_order_status
    

Would that be correct? If only I could get the order to update the payment status, I am sure this method would be called, correct?

I am desperate for some help. Any link or just direction would also be fine. Thanks a lot in advance.

Related posts

1 comment

  1. You need to decode payload being sent to the payment provider. Do something like this.

    public function check_ipn_response($payload)
    {
        $raw_input = json_decode(file_get_contents('php://input'),TRUE);
        $order_id = $raw_input['callback_data'];
        // get order_id
        $order = new WC_Order($order_id);
        // Update order status once order is complete
        $order->update_status( 'processing', _x( 'Payment Complete', 'Check payment method', 'wc-gateway-paymentsgy' ) );
        wp_die();
    }
    

Comments are closed.