Tech Process payment gateway with Woocommerce : PART 2

...and we are back. This Tutorial would guide you throuhg the necessary steps for integrating Tech Process gateway with Woo Commerce. NOTE: the solution provided is more of a work around, but works like a charm. Before you start, make sure you have read the provious tutorial

Tech Process payment gateway with Woocommerce: PART 1

also you need to download and install, wordpress and woocommerce. My current version for wordpress is 3.4.1 and for woocommerce its 1.6.4.

If you have everything setup correctly, with some dummy products, Login to your admin and you would have something like this

Selection_012.png.scaled500

Click settings to view default gateways provided from Woocommerce.

WooCommerce_Settings  Proteins_India  WordPress_2012-08-24_20-06-59.png.scaled1000

So to get our new custom gateway in the list above we need to create a new gateway class and then load it. Though you can find a not very nice documentation from woocommerce here. But i would cover it here.

First you need to understand where your new class file would be located. Here is a screenshot to make you undertand My project is saved as "WOOPI" in "/var/www", i you are on windows and using wamp then you would have your project directory under "C:/wamp/www/".

Selection_014.png.scaled500

you can see a new folder called "banking" under "/var/www/woopi/wp-content/plugins/woocommerce/classes/gateways/". Also you can see a class file that we have to create under this new folder. So let us begin...

Create a new file with name as "class-wc-banking.php". Remember keeping "class-wc-" in the name is important.

Here are important parts of file that you need to understand.

Constructor : class-wc-banking.php

Next is an important function "payment_fields()", that would contain your fields, If you need you need to show any on check out page. As i stated in the above, you have set

$this->has_fields         = true;// true if u want to show fields at the time of checkout.

So if you want to show the fields, define your function like this.

To make things clear, these are the fields you can use form the original "Request.php" file came in teh bundle from Tech Process.

We will not be using these fields in our tutorial. But still i would be sharing all necessary information.

inportant thing to know is that the fields does not have any SUBMIT button, this is because woocommerce has its its own(ajax based) way to submit form fields.

Next is validating these form fields, the original documentation from woo commerce shows a method that would retrun false or true if the validation is passed or not.

The function goes as follows.

public function validate_fields(){       global $woocommerce;       return true;    }

Since we are not using any fields so we do not need any validations so we simply return true.

Next is the most important function that would get all the necessary value and redirect the user to payment gateway. Here is the function "process_payment($orderId)" as you see it automatically takes the order id.

Another thing to remember os thet the code in this function is made using the original "getcheck.php" and some code from the woocommerce itself.

I know its huge, but its important.

We still have not added our file to woo commerce and has not displyed it in admin. so we need a couple of more functions .

so your class would contain all those functions (as discussed above) .

Next function would be added in the same file but outside tha class code block. the function adds your class to woo commerce.

/**  * Add the gateway to WooCommerce  **/ function add_banking_gateway( $methods ) {     $methods[] = 'WC_banking'; return $methods; } add_filter('woocommerce_payment_gateways', 'add_banking_gateway' );

now your class should look like this(copy paste ode can be found here).

Now one last step to show your gateway in admin. open the file "plugins/woocommerce/woocommerce.php" and at line number 184 add a new line to include your file, look screenshot below

Selection_017.png.scaled1000

save the file and go to administrator >> woocommerce >> payment gateways (tab)

Selection_018.png.scaled1000

Simply enable your gateway, press save and "Tadaaaa". your gateway can be seen on the checkout page.

Checkout  Proteins_India_2012-08-25_14-52-28.png.scaled1000

If everything is set corect, clicking "Place order" button would take us to the payment gateway page where we can make payment. No matter if u made a payment or canceled it, response would go to the "Response.php" as I told you in my preivous blog post. Below we would redirect the user to Thank you or transaction failed pages(create them if they are not) depending upon the gateway's response code.

Open your "Response.php" (i expect that its in your root folder). Find for the code

if(trim($return_array[14])=='0300')

So this is where we set the product status and redirect user

if(trim($return_array[14])=='0300') {     // set status of order as pending     // redirect user } else{ // set status of order as failed     // redirect user }

NOTE: i tried a lot using woocommerce's classes to set the product status, but finally I studied the databse and ran raw sql queries in "Response.php".

See the screenshot to understand how woocommerce relates the necessary data

Selection_022.png.scaled1000

in database(screenshot above) you have a table called "wp_posts" that has your products, and another table called "wp_terms" that stores related terms needed.

There is table called "term_taxonomy_id" that relates "wp_posts" with "Wp_terms"

you can clearly see order status defined in table "wp_terms"

Now, go to your Response.php file and include "wp_config.php" file, this file has your database details for wordpress.

<?php session_start(); include('wp-config.php'); $conn = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); // connect to HOST mysql_select_db(DB_NAME, $conn); // selecting the DB

now simple put the queries as

 

and THIS IS ALL.

 

i hope you find this helpful. please do comment for any help.

thank you

  • Find useful Magento
    extensions and modules customized for your ecommerce online store. Download
    great Magento widgets and install instantly. Complete Magento solutions for
    administration, checkout, customer service, online advertisement, product
    reviews, online store enhancement, discount shopping, shipping and logistics.

  • vinothkumarp

    Hi,

    I have completed the integration steps correctly. But I have some issues like this

    Warning: fopen(MerchantDetails.properties) [function.fopen]: failed to open stream: No such file or directory in /home/content/02/10441002/html/***********/wp-content/plugins/woocommerce/classes/gateways/banking/class-wc-banking.php on line 343

    Invald Property File

    Where i have to upload “MerchantDetails.properties” file?

    Please help me.

    • thecodingbox

      try changing the path at “getcheck.php“
      echo the path and you would get the idea

      • vinothkumarp

        Hi,

        http://www.*****.com/MerchantDetails.properties

        Warning: filesize() [function.filesize]: stat failed for http://www.*****.com/MerchantDetails.properties in /home/content/02/10441002/html/*****/wp-content/plugins/woocommerce/classes/gateways/banking/class-wc-banking.php on line 346

        Warning: fread() [function.fread]: Length parameter must be greater than 0 in /home/content/02/10441002/html/*****/wp-content/plugins/woocommerce/classes/gateways/banking/class-wc-banking.php on line 346

        Invald Property File

        • thecodingbox

          if the file exists, check if its being read?
          echo the output.
          else seems you file is missing some values.

          • vinothkumarp

            Hi,

            I have a success statement.

            {“result”:”success”}

            How can i redirect to payment gatway url.

            Thanks.

          • thecodingbox

            its a JSON response.

          • vinothkumarp

            Hi,

            Thanks for your response.
            But, I don’t know how to give checkout page to payment gatway url.

          • thecodingbox

            sorry i am not really sure about that.

  • vinothkumarp

    Hi,

    Thanks for your support.

    I need another one help from you.

    How can i sent email notification to login user after successful transaction.

    Thanks.

  • Arun Kumar

    I apply this method in my client website. every thing ok. but after clicking place order button i got error

    • the page you are redirecting it to is wrong and does not exist

      • Arun Kumar

        Hi Harpreet Bhatia, thank you for your quick reply.

        I am already done your first post. everything ok. my request and response urls are quite different.

        I changed request url :checkout.php

        response url :finishorder.php

        I am using woocommerce 2.0 version
        This is the code i did.
        http://pastie.org/private/funhmrjhihkqiteqqvo0w

        Please check the code and correct my error.

  • DhAval ThaKor

    Hi harpreet, I am using woocommerce 2.0.20

    I am not able to integrate tech process payment gateway by your method.

    I really appreciate your quick answer.

    Thnks

    • i have not really used it recently, So you are all by yourselves.
      But the basic idea of getting new class should work fine.

      • DhAval ThaKor

        I will do
        Thanks for your quick reply.

      • arvind

        sir please post for latest version of woocmmerce. because you are the only one in entire google who knows about techprocess payment integration. thanks

  • Hi Harpreet,

    I realize you have not used this in a while, but it was working great for me until 2.1.6 any ideas? Now at checkout, I get a 404.

    • Sorry I have no idea, I am currently occupied to give it a shot.

  • What’s up to every , as I am actually eager of reading this website’s post to be updated regularly.
    It contains good stuff.

  • arvind

    sir as you said finally go to line sr number 184 and add banking file, but in our woocommerce.php file there is no any such thing. then how to add.response will be appricated.

    $key();

    }

    }

    /**

    * WooCommerce Constructor.

    */

    public function __construct() {

    $this->define_constants();

    $this->includes();

    $this->init_hooks();

    do_action( ‘woocommerce_loaded’ );

    }

    /**

    * Hook into actions and filters.

    * @since 2.3

    */

    private function init_hooks() {

    register_activation_hook( __FILE__, array( ‘WC_Install’, ‘install’ ) );

    add_action( ‘after_setup_theme’, array( $this, ‘setup_environment’ ) );

    add_action( ‘after_setup_theme’, array( $this, ‘include_template_functions’ ), 11 );

    add_action( ‘init’, array( $this, ‘init’ ), 0 );

    add_action( ‘init’, array( ‘WC_Shortcodes’, ‘init’ ) );

    add_action( ‘init’, array( ‘WC_Emails’, ‘init_transactional_emails’ ) );

    }

    /**

    * Define WC Constants.

    */

    private function define_constants() {

    $upload_dir = wp_upload_dir();

    $this->define( ‘WC_PLUGIN_FILE’, __FILE__ );

    $this->define( ‘WC_PLUGIN_BASENAME’, plugin_basename( __FILE__ ) );

    $this->define( ‘WC_VERSION’, $this->version );

    $this->define( ‘WOOCOMMERCE_VERSION’, $this->version );

    $this->define( ‘WC_ROUNDING_PRECISION’, 4 );

    $this->define( ‘WC_DISCOUNT_ROUNDING_MODE’, 2 );

    $this->define( ‘WC_TAX_ROUNDING_MODE’, ‘yes’ === get_option( ‘woocommerce_prices_include_tax’, ‘no’ ) ? 2 : 1 );

    $this->define( ‘WC_DELIMITER’, ‘|’ );

    $this->define( ‘WC_LOG_DIR’, $upload_dir[‘basedir’] . ‘/wc-logs/’ );

    $this->define( ‘WC_SESSION_CACHE_GROUP’, ‘wc_session_id’ );

    }

    /**

    * Define constant if not already set.

    *

    * @param string $name

    * @param string|bool $value

    */

    private function define( $name, $value ) {

    if ( ! defined( $name ) ) {

    define( $name, $value );

    }

    }

    /**

    * What type of request is this?

    * string $type ajax, frontend or admin.

    *

    * @return bool

    */

    private function is_request( $type ) {

    switch ( $type ) {

    case ‘admin’ :

    return is_admin();

    case ‘ajax’ :

    return defined( ‘DOING_AJAX’ );

    case ‘cron’ :

    return defined( ‘DOING_CRON’ );

    case ‘frontend’ :

    return ( ! is_admin() || defined( ‘DOING_AJAX’ ) ) && ! defined( ‘DOING_CRON’ );

    }

    }

    /**

    * Include required core files used in admin and on the frontend.

    */

    public function includes() {

    include_once( ‘includes/class-wc-autoloader.php’ );

    include_once( ‘includes/wc-core-functions.php’ );

    include_once( ‘includes/wc-widget-functions.php’ );

    include_once( ‘includes/wc-webhook-functions.php’ );

    include_once( ‘includes/class-wc-install.php’ );

    include_once( ‘includes/class-wc-geolocation.php’ );

    include_once( ‘includes/class-wc-download-handler.php’ );

    include_once( ‘includes/class-wc-comments.php’ );

    include_once( ‘includes/class-wc-post-data.php’ );

    include_once( ‘includes/class-wc-ajax.php’ );

    if ( $this->is_request( ‘admin’ ) ) {

    include_once( ‘includes/admin/class-wc-admin.php’ );

    }

    if ( $this->is_request( ‘frontend’ ) ) {

    $this->frontend_includes();

    }

    if ( $this->is_request( ‘frontend’ ) || $this->is_request( ‘cron’ ) ) {

    include_once( ‘includes/abstracts/abstract-wc-session.php’ );

    include_once( ‘includes/class-wc-session-handler.php’ );

    }

    if ( $this->is_request( ‘cron’ ) && ‘yes’ === get_option( ‘woocommerce_allow_tracking’, ‘no’ ) ) {

    include_once( ‘includes/class-wc-tracker.php’ );

    }

    $this->query = include( ‘includes/class-wc-query.php’ ); // The main query class

    $this->api = include( ‘includes/class-wc-api.php’ ); // API Class

    include_once( ‘includes/class-wc-auth.php’ ); // Auth Class

    include_once( ‘includes/class-wc-post-types.php’ ); // Registers post types

    include_once( ‘includes/abstracts/abstract-wc-product.php’ ); // Products

    include_once( ‘includes/abstracts/abstract-wc-order.php’ ); // Orders

    include_once( ‘includes/abstracts/abstract-wc-settings-api.php’ ); // Settings API (for gateways, shipping, and integrations)

    include_once( ‘includes/abstracts/abstract-wc-shipping-method.php’ ); // A Shipping method

    include_once( ‘includes/abstracts/abstract-wc-payment-gateway.php’ ); // A Payment gateway

    include_once( ‘includes/abstracts/abstract-wc-integration.php’ ); // An integration with a service

    include_once( ‘includes/class-wc-product-factory.php’ ); // Product factory

    include_once( ‘includes/class-wc-countries.php’ ); // Defines countries and states

    include_once( ‘includes/class-wc-integrations.php’ ); // Loads integrations

    include_once( ‘includes/class-wc-cache-helper.php’ ); // Cache Helper

    if ( defined( ‘WP_CLI’ ) && WP_CLI ) {

    include_once( ‘includes/class-wc-cli.php’ );

    }

    }

    /**

    * Include required frontend files.

    */

    public function frontend_includes() {

    include_once( ‘includes/wc-cart-functions.php’ );

    include_once( ‘includes/wc-notice-functions.php’ );

    include_once( ‘includes/wc-template-hooks.php’ );

    include_once( ‘includes/class-wc-template-loader.php’ ); // Template Loader

    include_once( ‘includes/class-wc-frontend-scripts.php’ ); // Frontend Scripts

    include_once( ‘includes/class-wc-form-handler.php’ ); // Form Handlers

    include_once( ‘includes/class-wc-cart.php’ ); // The main cart class

    include_once( ‘includes/class-wc-tax.php’ ); // Tax class

    include_once( ‘includes/class-wc-customer.php’ ); // Customer class

    include_once( ‘includes/class-wc-shortcodes.php’ ); // Shortcodes class

    include_once( ‘includes/class-wc-https.php’ ); // https Helper

    include_once( ‘includes/class-wc-embed.php’ ); // Embeds

    }

    /**

    * Function used to Init WooCommerce Template Functions – This makes them pluggable by plugins and themes.

    */

    public function include_template_functions() {

    include_once( ‘includes/wc-template-functions.php’ );

    }

    /**

    * Init WooCommerce when WordPress Initialises.

    */

    public function init() {

    // Before init action.

    do_action( ‘before_woocommerce_init’ );

    // Set up localisation.

    $this->load_plugin_textdomain();

    // Load class instances.

    $this->product_factory = new WC_Product_Factory(); // Product Factory to create new product instances

    $this->order_factory = new WC_Order_Factory(); // Order Factory to create new order instances

    $this->countries = new WC_Countries(); // Countries class

    $this->integrations = new WC_Integrations(); // Integrations class

    // Session class, handles session data for users – can be overwritten if custom handler is needed.

    if ( $this->is_request( ‘frontend’ ) || $this->is_request( ‘cron’ ) ) {

    $session_class = apply_filters( ‘woocommerce_session_handler’, ‘WC_Session_Handler’ );

    $this->session = new $session_class();

    }

    // Classes/actions loaded for the frontend and for ajax requests.

    if ( $this->is_request( ‘frontend’ ) ) {

    $this->cart = new WC_Cart(); // Cart class, stores the cart contents

    $this->customer = new WC_Customer(); // Customer class, handles data such as customer location

    }

    $this->load_webhooks();

    // Init action.

    do_action( ‘woocommerce_init’ );

    }

    /**

    * Load Localisation files.

    *

    * Note: the first-loaded translation file overrides any following ones if the same translation is present.

    *

    * Locales found in:

    * – WP_LANG_DIR/woocommerce/woocommerce-LOCALE.mo

    * – WP_LANG_DIR/plugins/woocommerce-LOCALE.mo

    */

    public function load_plugin_textdomain() {

    $locale = apply_filters( ‘plugin_locale’, get_locale(), ‘woocommerce’ );

    load_textdomain( ‘woocommerce’, WP_LANG_DIR . ‘/woocommerce/woocommerce-‘ . $locale . ‘.mo’ );

    load_plugin_textdomain( ‘woocommerce’, false, plugin_basename( dirname( __FILE__ ) ) . ‘/i18n/languages’ );

    }

    /**

    * Ensure theme and server variable compatibility and setup image sizes.

    */

    public function setup_environment() {

    /**

    * @deprecated 2.2 Use WC()->template_path()

    */

    $this->define( ‘WC_TEMPLATE_PATH’, $this->template_path() );

    $this->add_thumbnail_support();

    $this->add_image_sizes();

    }

    /**

    * Ensure post thumbnail support is turned on.

    */

    private function add_thumbnail_support() {

    if ( ! current_theme_supports( ‘post-thumbnails’ ) ) {

    add_theme_support( ‘post-thumbnails’ );

    }

    add_post_type_support( ‘product’, ‘thumbnail’ );

    }

    /**

    * Add WC Image sizes to WP.

    *

    * @since 2.3

    */

    private function add_image_sizes() {

    $shop_thumbnail = wc_get_image_size( ‘shop_thumbnail’ );

    $shop_catalog = wc_get_image_size( ‘shop_catalog’ );

    $shop_single = wc_get_image_size( ‘shop_single’ );

    add_image_size( ‘shop_thumbnail’, $shop_thumbnail[‘width’], $shop_thumbnail[‘height’], $shop_thumbnail[‘crop’] );

    add_image_size( ‘shop_catalog’, $shop_catalog[‘width’], $shop_catalog[‘height’], $shop_catalog[‘crop’] );

    add_image_size( ‘shop_single’, $shop_single[‘width’], $shop_single[‘height’], $shop_single[‘crop’] );

    }

    /**

    * Get the plugin url.

    * @return string

    */

    public function plugin_url() {

    return untrailingslashit( plugins_url( ‘/’, __FILE__ ) );

    }

    /**

    * Get the plugin path.

    * @return string

    */

    public function plugin_path() {

    return untrailingslashit( plugin_dir_path( __FILE__ ) );

    }

    /**

    * Get the template path.

    * @return string

    */

    public function template_path() {

    return apply_filters( ‘woocommerce_template_path’, ‘woocommerce/’ );

    }

    /**

    * Get Ajax URL.

    * @return string

    */

    public function ajax_url() {

    return admin_url( ‘admin-ajax.php’, ‘relative’ );

    }

    /**

    * Return the WC API URL for a given request.

    *

    * @param string $request

    * @param mixed $ssl (default: null)

    * @return string

    */

    public function api_request_url( $request, $ssl = null ) {

    if ( is_null( $ssl ) ) {

    $scheme = parse_url( home_url(), PHP_URL_SCHEME );

    } elseif ( $ssl ) {

    $scheme = ‘https’;

    } else {

    $scheme = ‘http’;

    }

    if ( strstr( get_option( ‘permalink_structure’ ), ‘/index.php/’ ) ) {

    $api_request_url = trailingslashit( home_url( ‘/index.php/wc-api/’ . $request, $scheme ) );

    } elseif ( get_option( ‘permalink_structure’ ) ) {

    $api_request_url = trailingslashit( home_url( ‘/wc-api/’ . $request, $scheme ) );

    } else {

    $api_request_url = add_query_arg( ‘wc-api’, $request, trailingslashit( home_url( ”, $scheme ) ) );

    }

    return esc_url_raw( apply_filters( ‘woocommerce_api_request_url’, $api_request_url, $request, $ssl ) );

    }

    /**

    * Load & enqueue active webhooks.

    *

    * @since 2.2

    */

    private function load_webhooks() {

    if ( false === ( $webhooks = get_transient( ‘woocommerce_webhook_ids’ ) ) ) {

    $webhooks = get_posts( array(

    ‘fields’ => ‘ids’,

    ‘post_type’ => ‘shop_webhook’,

    ‘post_status’ => ‘publish’,

    ‘posts_per_page’ => -1

    ) );

    set_transient( ‘woocommerce_webhook_ids’, $webhooks );

    }

    foreach ( $webhooks as $webhook_id ) {

    $webhook = new WC_Webhook( $webhook_id );

    $webhook->enqueue();

    }

    }

    /**

    * Get Checkout Class.

    * @return WC_Checkout

    */

    public function checkout() {

    return WC_Checkout::instance();

    }

    /**

    * Get gateways class.

    * @return WC_Payment_Gateways

    */

    public function payment_gateways() {

    return WC_Payment_Gateways::instance();

    }

    /**

    * Get shipping class.

    * @return WC_Shipping

    */

    public function shipping() {

    return WC_Shipping::instance();

    }

    /**

    * Email Class.

    * @return WC_Emails

    */

    public function mailer() {

    return WC_Emails::instance();

    }

    }

    endif;

    /**

    * Main instance of WooCommerce.

    *

    * Returns the main instance of WC to prevent the need to use globals.

    *

    * @since 2.1

    * @return WooCommerce

    */

    function WC() {

    return WooCommerce::instance();

    }

    // Global for backwards compatibility.

    $GLOBALS[‘woocommerce’] = WC();

  • Poonam Mhatre

    Hello Sir,
    I can’t find that copy paste code. Its giving ‘Bad Gateway’ error. Can you please send me that code?
    Thanks in advance!