I am developing a custom payment gateway for woocommerce, and payment gateway requires a registration call before processing the payment and than needs to submit a form to redirect to the payment page of gateway where user will put his card details.

Redirection with form submit is required as have to pass a transaction ID and URL that I am getting in register call:

Here is the actual flow of my payment gateway:

1- Registration call ( have to pass orderID, Amount, ReturnURL )
I am doing this in my payment_fields() function than i am putting the return values in a hidden form.
2- Redirection call ( have to pass TransactionID, PortalURL in a form where PortalURL is my form action and TransactionID is a hidden field )

Here is my payment gateway class:

<?php

    class WC_Etisalat_Payment_Gateway extends WC_Payment_Gateway {

        public function __construct(){

            $this->id = 'epg';
            $this->icon = '';
            $this->has_fields = false;
            $this->method_title = 'Etisalat Payment Gateway';
            $this->method_description = 'Pay with your UAE Credit, Debit or Prepaid cards.';
            $this->supports           = array(
                'products',
                'refunds',
            );

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

            $this->enabled = $this->get_option('enabled');
            $this->title = $this->get_option('title');
            $this->description = $this->get_option('description');

            add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );

            add_action( 'admin_notices', array( $this,  'do_ssl_check' ) );

        }

        public function do_ssl_check() {
            if( $this->enabled == "yes" ) {
                if( get_option( 'woocommerce_force_ssl_checkout' ) == "no" ) {
                    echo "<div class="error"><p>". sprintf( __( "<strong>%s</strong> is enabled and WooCommerce is not forcing the SSL certificate on your checkout page. Please ensure that you have a valid SSL certificate and that you are <a href="%s">forcing the checkout pages to be secured.</a>" ), $this->method_title, admin_url( 'admin.php?page=wc-settings&tab=checkout' ) ) ."</p></div>";
                }
            }
        }

        public function init_form_fields() {
            $this->form_fields = array(
                'enabled' => array(
                    'title'   => 'Enable/Disable',
                    'type'    => 'checkbox',
                    'label'   => 'Enable Etisalat Payment Gateway',
                    'default' => ''
                ),
                'title' => array(
                    'title'       => 'Title',
                    'type'        => 'text',
                    'description' => 'This controls the title for the payment method the customer sees during checkout.',
                    'default'     => 'Etisalat Payment Gateway'
                ),
                'description' => array(
                    'title'       => 'Description',
                    'type'        => 'textarea',
                    'description' => 'Payment method description that the customer will see on your checkout.',
                    'default'     => 'Pay with your UAE Credit, Debit or Prepaid cards.'
                ),
                'epg_merchant_id' => array(
                    'title'     => 'EPG Merchant ID',
                    'type'      => 'text'
                ),
                'epg_merchant_password' => array(
                    'title'     => 'EPG Password',
                    'type'      => 'password'
                )
            );
        }

        public function admin_options() {
            ?>

            <h2>Etisalat Payment Gateway</h2>

            <table class="form-table">

                <?php $this->generate_settings_html(); ?>

            </table>

            <?php
        }

        public function init_epg_register( $order_id, $amount, $return_url ) {

            $cert = plugin_dir_path( __FILE__ )."cert.pem";

            $opts = array(
                'ssl' => array(
                    'verify_peer'       => false,
                    'verify_peer_name'  => false,
                )
            );
            $options = array(
                'trace'         => 1,
                'keep_alive'    => true,
                'exceptions'    => 0,
                'local_cert'    => $cert,
                'passphrase'    => $this->get_option('epg_merchant_password'),
                'stream_context' => stream_context_create($opts),
                'cache_wsdl'    => WSDL_CACHE_NONE
            );
            $client = new SoapClient("https://demo-ipg.comtrust.ae:2443/MerchantAPI.svc?singleWsdl", $options);

            $params = array(
                'Register' => '',
                'request' => array(
                    'Customer'      => $this->get_option('epg_merchant_id'),
                    'Language'      => 'en',
                    'version'       => 2,
                    'Amount'        => $amount,
                    'Currency'      => 'USD',
                    'OrderID'       => $order_id,
                    'OrderInfo'     => $order_id,
                    'OrderName'     => $order_id,
                    'ReturnPath'    => $return_url,
                    'TransactionHint' => 'VCC:Y'
                )
            );

            $result = $client->Register($params);

            $response = json_encode( $result );

            $decode_data = json_decode($response);
            $reg_result = $decode_data->RegisterResult;

            return $reg_result;

        }

        public function process_payment( $order_id ) {

            global $woocommerce;

            $order = wc_get_order( $order_id );

            $order->update_status( 'on-hold', __( 'Awaiting offline payment', 'wc-gateway-offline' ) );

            $order->reduce_order_stock();

            $woocommerce->cart->empty_cart();

            return array(
                'result'    => 'success'
            );

        }

        public function payment_fields(){

            global $woocommerce;

            $order_id = $woocommerce->session->order_awaiting_payment;

            $order = wc_get_order( $order_id );

            $register = $this->init_epg_register( $order_id, $woocommerce->cart->total, $this->get_return_url( $order ) );

            $environment_url = $register->PaymentPortal;

            $transaction_id = $register->TransactionID;

            if ( $description = $this->get_description() ) {
                echo wpautop( wptexturize( $description ) );
            }

            ?>
            <form id="epg_payment_call" action="<?php echo esc_url( $environment_url ); ?>" method="post">
                <!--<input type='hidden' name='Price' value='<?php echo esc_attr( $woocommerce->cart->total ); ?>'/>
                <input type='hidden' name='ReturnURL' value='<?php echo esc_attr( $this->get_return_url( $order ) ); ?>'/>-->
                <input type='hidden' name='TransactionID' value='<?php echo esc_attr( $transaction_id ); ?>'/>
                <input type="submit" value="Place Order">
            </form>
            <?php
        }

    }

Now here my problem is that, I need OrderID, OrderTotal and get_return_url() in payment_fields() function.

I already all the below methods but none is working for me.

global $wp;
    $order_id = $wp->query_vars['order-pay'];
    $order = new WC_Order( $order_id );

Not working:

get_query_var('order-pay');

Not working:

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

Can anybody help me in sorting this our. As i am stuck here. Read all the documentation of woocommerce and searched a lot on stack overflow but none is working for me.

I am using latest version of WooCommerce and WP.

Read more here: How to Get Order ID in payment_fields() in woocommerce Payment gateway class


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: