diff --git a/commerce_hdfc/commerce_hdfc.module b/commerce_hdfc/commerce_hdfc.module
index fb5163f..bc93601 100644
--- a/commerce_hdfc/commerce_hdfc.module
+++ b/commerce_hdfc/commerce_hdfc.module
@@ -1,243 +1,321 @@
-<?php
-
-/**
- * @file
- * hdfc payment gateway integration with Drupal Commerce.
- */
-
-/**
- * Implements hook_help().
- */
-function commerce_hdfc_help($path, $arg) {
-  switch ($path) {
-    case 'admin/help#commerce_hdfc':
-      $output = '';
-      $output .= '<h3>' . t('Summary') . '</h3>';
-
-      $output .= '<p>' . t('Integrates HDFC Payment Gateway with Drupal Commerce') . '</p>';
-
-      $output .= '<h3>' . t('Requirements') . '</h3>';
-      $output .= '<ul>';
-      $output .= '<li>' . t('Commerce Kickstart 7.x - https://drupal.org/project/commerce_kickstart') . '</li>';
-
-      $output .= '<p>' . t('OR') . '</p>';
-
-      $output .= '<li>' . t('Drupal 7.x - https://drupal.org/project/drupal') . '</li>';
-
-      $output .= '<ul>';
-
-      $output .= '<li>' . t('commerce module') . '</li>';
-
-      $output .= '<li>' . t('commerce_payment module') . '</li>';
-
-      $output .= '<li>' . t('commerce_ui module') . '</li>';
-
-      $output .= '<li>' . t('commerce_order module') . '</li>';
-
-      $output .= '</ul>';
-
-      $output .= '</ul>';
-
-      $output .= '<h3>' . t('Installation') . '</h3>';
-      $output .= '<ol>';
-      $output .= '<li>' . t('The module MUST be placed in /sites/all/modules so that the PATH to module will be: [DRUPAL-ROOT-FOLDER]/sites/all/modules/commerce_hdfc. Enable the module') . '</li>';
-
-      $output .= '<li>' . t('Click on configure link next to the enabled module and go to: admin/commerce/config/payment-methods') . '</li>';
-
-      $output .= '<ul>';
-
-      $output .= '<li>' . t('Enable - Commerce HDFC Payment Gateway - payment method rule') . '</li>';
-
-      $output .= '<li>' . t('Click on edit') . '</li>';
-
-      $output .= '<li>' . t('In the Actions - click on edit next to  - Enable payment method: Commerce HDFC Payment Gateway') . '</li>';
-
-      $output .= '<li>' . t('Get your merchant account from HDFC Payment Gateway. Enter the details in Payment Settings') . '</li>';
-
-      $output .= '</ul>';
-
-      $output .= '<li>' . t('Clear cache - admin/config/development/performance') . '</li>';
-
-      $output .= '</ol>';
-      return $output;
-}
-}
-
-/**
- * Payment method callback: settings form.
- */
-function hdfc_settings_form($settings = NULL) {
-  $form = array();
-  $settings = (array) $settings + array(
-    'merchant_id' => '',
-    'working_key' => '',
-    'res_ip_1' => '',
-    'res_ip_2' => '',
-    'res_ip_3' => '',
-    'site_base_url' => '',
-    'pg_auth_url' => '',
-    'pg_dual_url' => '',
-  );
-  $form['merchant_id'] = array(
-    '#type' => 'textfield',
-    '#title' => t('Tranportal ID'),
-    '#default_value' => $settings['merchant_id'],
-    '#required' => TRUE,
-  );
-  $form['working_key'] = array(
-    '#type' => 'textfield',
-    '#title' => t('Tranportal Password'),
-    '#default_value' => $settings['working_key'],
-    '#required' => TRUE,
-  );
-  $form['res_ip_1'] = array(
-    '#type' => 'textfield',
-    '#title' => t('Response IP Address 1'),
-    '#default_value' => $settings['res_ip_1'],
-    '#required' => TRUE,
-  );
-  $form['res_ip_2'] = array(
-    '#type' => 'textfield',
-    '#title' => t('Response IP Address 2'),
-    '#default_value' => $settings['res_ip_2'],
-    '#required' => TRUE,
-  );
-  $form['res_ip_3'] = array(
-    '#type' => 'textfield',
-    '#title' => t('Response IP Address 3'),
-    '#default_value' => $settings['res_ip_3'],
-    '#required' => TRUE,
-  );
-  $form['pg_auth_url'] = array(
-    '#type' => 'textfield',
-    '#title' => t('HDFC Purchase - Auth Request URL'),
-    '#description' => t("Payment Gateway's Test / Production Purchase - Auth Request URL"),
-    '#default_value' => $settings['pg_auth_url'],
-    '#required' => TRUE,
-  );
-  $form['pg_dual_url'] = array(
-    '#type' => 'textfield',
-    '#title' => t('HDFC Dual Verification Request URL'),
-    '#description' => t("Payment Gateway's Test / Production Dual Verification Request URL"),
-    '#default_value' => $settings['pg_dual_url'],
-    '#required' => TRUE,
-  );
-  $form['site_base_url'] = array(
-    '#type' => 'textfield',
-    '#title' => t('Site URL'),
-    '#description' => t('Your drupal site url eg. http://example.com ; http://example.com/drupal - NO trailing slash!'),
-    '#default_value' => $settings['site_base_url'],
-    '#required' => TRUE,
-  );
-  return $form;
-}
-
-/**
- * Implements hook_commerce_payment_method_info().
- */
-function commerce_hdfc_commerce_payment_method_info() {
-  $payment_methods = array();
-  $payment_methods['hdfc'] = array(
-    'base' => 'hdfc',
-    'title' => t("Commerce HDFC Payment Gateway"),
-    'display_title' => "Commerce HDFC Payment Gateway",
-    'short_title' => 'Commerce HDFC Payment Gateway',
-    'description' => t("Payment method - Commerce HDFC Payment Gateway"),
-    'terminal' => TRUE,
-    'offsite' => TRUE,
-    'offsite_autoredirect' => TRUE,
-  );
-  return $payment_methods;
-}
-
-/**
- * Payment method callback: redirect form.
- */
-function hdfc_redirect_form($form, &$form_state, $order, $payment_method) {
-  $wrapper = entity_metadata_wrapper('commerce_order', $order);
-  $currency_code = $wrapper->commerce_order_total->currency_code->value();
-  $amount = $wrapper->commerce_order_total->amount->value()/100;
-  $return_url = url('checkout/' . $order->order_id . '/payment/return/' . $order->data['payment_redirect_key'], array('absolute' => TRUE));
-  $merchant_id = $payment_method['settings']['merchant_id'];
-  $working_key = $payment_method['settings']['working_key'];
-  $res_ip_1 = $payment_method['settings']['res_ip_1'];
-  $res_ip_2 = $payment_method['settings']['res_ip_2'];
-  $res_ip_3 = $payment_method['settings']['res_ip_3'];
-  $pg_auth_url = $payment_method['settings']['pg_auth_url'];
-  $pg_dual_url = $payment_method['settings']['pg_dual_url'];
-  $site_base_url = $payment_method['settings']['site_base_url'];
-  $order_id = $order->order_id;
-  $form['merchant_id'] = array(
-    '#type' => 'hidden',
-    '#value' => $merchant_id,
-  );
-  $form['working_key'] = array(
-    '#type' => 'hidden',
-    '#value' => $working_key,
-  );
-  $form['res_ip_1'] = array(
-    '#type' => 'hidden',
-    '#value' => $res_ip_1,
-  );
-  $form['res_ip_2'] = array(
-    '#type' => 'hidden',
-    '#value' => $res_ip_2,
-  );
-  $form['res_ip_3'] = array(
-    '#type' => 'hidden',
-    '#value' => $res_ip_3,
-  );
-  $form['pg_auth_url'] = array(
-    '#type' => 'hidden',
-    '#value' => $pg_auth_url,
-  );
-  $form['pg_dual_url'] = array(
-    '#type' => 'hidden',
-    '#value' => $pg_dual_url,
-  );
-  $form['site_base_url'] = array(
-    '#type' => 'hidden',
-    '#value' => $site_base_url,
-  );
-  $form['amount'] = array(
-    '#type' => 'hidden',
-    '#value' => $amount ,
-  );
-  $form['order_id'] = array(
-    '#type' => 'hidden',
-    '#value' => $order_id,
-  );
-  $form['redirect_url'] = array(
-    '#type' => 'hidden',
-    '#value' => $return_url,
-  );
-  $link = $site_base_url . "/" . drupal_get_path('module', 'commerce_hdfc') . "/includes/send_perform_request.php";
-  $form['#action'] = $link;
-  return $form;
-}
-
-/**
- * Implements hook_entity_view_alter().
- */
-function commerce_hdfc_entity_view_alter(&$build, $type) {
-  switch ($type) {
-    case 'commerce_order':
-      if ($build['#view_mode'] === 'administrator') {
-        $order = $build['#entity'];
-
-        $build['status'] = array(
-          '#type' => 'fieldset',
-          '#title' => t('Order details'),
-          '#weight' => -100,
-        );
-
-        $build['status']['markup'] = array(
-          '#prefix' => '<dl>',
-          '#suffix' => '</dl>',
-          array('#markup' => sprintf('<dt>%s</dt><dd>%s<dd>', t('Status'), $order->status)),
-          array('#markup' => sprintf('<dt>%s</dt><dd>%s<dd>', t('E-mail'), $order->mail)),
-        );
-  }
-      break;
- }
-}
+<?php
+
+define('COMMERCE_FIRSTDATA_BILLING_PHONE_FIELD', 'commerce_firstdata_billing_phone');
+/**
+ * @file
+ * hdfc payment gateway integration with Drupal Commerce.
+ */
+
+/**
+ * Implements hook_help().
+ */
+function commerce_hdfc_help($path, $arg) {
+  switch ($path) {
+    case 'admin/help#commerce_hdfc':
+      $output = '';
+      $output .= '<h3>' . t('Summary') . '</h3>';
+
+      $output .= '<p>' . t('Integrates HDFC Payment Gateway with Drupal Commerce') . '</p>';
+
+      $output .= '<h3>' . t('Requirements') . '</h3>';
+      $output .= '<ul>';
+      $output .= '<li>' . t('Commerce Kickstart 7.x - https://drupal.org/project/commerce_kickstart') . '</li>';
+
+      $output .= '<p>' . t('OR') . '</p>';
+
+      $output .= '<li>' . t('Drupal 7.x - https://drupal.org/project/drupal') . '</li>';
+
+      $output .= '<ul>';
+
+      $output .= '<li>' . t('commerce module') . '</li>';
+
+      $output .= '<li>' . t('commerce_payment module') . '</li>';
+
+      $output .= '<li>' . t('commerce_ui module') . '</li>';
+
+      $output .= '<li>' . t('commerce_order module') . '</li>';
+
+      $output .= '</ul>';
+
+      $output .= '</ul>';
+
+      $output .= '<h3>' . t('Installation') . '</h3>';
+      $output .= '<ol>';
+      $output .= '<li>' . t('The module MUST be placed in /sites/all/modules so that the PATH to module will be: [DRUPAL-ROOT-FOLDER]/sites/all/modules/commerce_hdfc. Enable the module') . '</li>';
+
+      $output .= '<li>' . t('Click on configure link next to the enabled module and go to: admin/commerce/config/payment-methods') . '</li>';
+
+      $output .= '<ul>';
+
+      $output .= '<li>' . t('Enable - Commerce HDFC Payment Gateway - payment method rule') . '</li>';
+
+      $output .= '<li>' . t('Click on edit') . '</li>';
+
+      $output .= '<li>' . t('In the Actions - click on edit next to  - Enable payment method: Commerce HDFC Payment Gateway') . '</li>';
+
+      $output .= '<li>' . t('Get your merchant account from HDFC Payment Gateway. Enter the details in Payment Settings') . '</li>';
+
+      $output .= '</ul>';
+
+      $output .= '<li>' . t('Clear cache - admin/config/development/performance') . '</li>';
+
+      $output .= '</ol>';
+      return $output;
+}
+}
+
+/**
+ * Payment method callback: settings form.
+ */
+function hdfc_settings_form($settings = NULL) {
+  $form = array();
+  $settings = (array) $settings + array(
+    'merchant_id' => '',
+    'working_key' => '',
+    'res_ip_1' => '',
+    'res_ip_2' => '',
+    'res_ip_3' => '',
+    'site_base_url' => '',
+    'pg_auth_url' => '',
+    'pg_dual_url' => '',
+  );
+  $form['merchant_id'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Tranportal ID'),
+    '#default_value' => $settings['merchant_id'],
+    '#required' => TRUE,
+  );
+  $form['working_key'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Tranportal Password'),
+    '#default_value' => $settings['working_key'],
+    '#required' => TRUE,
+  );
+  $form['res_ip_1'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Response IP Address 1'),
+    '#default_value' => $settings['res_ip_1'],
+    '#required' => TRUE,
+  );
+  $form['res_ip_2'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Response IP Address 2'),
+    '#default_value' => $settings['res_ip_2'],
+    '#required' => TRUE,
+  );
+  $form['res_ip_3'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Response IP Address 3'),
+    '#default_value' => $settings['res_ip_3'],
+    '#required' => TRUE,
+  );
+  $form['pg_auth_url'] = array(
+    '#type' => 'textfield',
+    '#title' => t('HDFC Purchase - Auth Request URL'),
+    '#description' => t("Payment Gateway's Test / Production Purchase - Auth Request URL"),
+    '#default_value' => $settings['pg_auth_url'],
+    '#required' => TRUE,
+  );
+  $form['pg_dual_url'] = array(
+    '#type' => 'textfield',
+    '#title' => t('HDFC Dual Verification Request URL'),
+    '#description' => t("Payment Gateway's Test / Production Dual Verification Request URL"),
+    '#default_value' => $settings['pg_dual_url'],
+    '#required' => TRUE,
+  );
+  $form['site_base_url'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Site URL'),
+    '#description' => t('Your drupal site url eg. http://example.com ; http://example.com/drupal - NO trailing slash!'),
+    '#default_value' => $settings['site_base_url'],
+    '#required' => TRUE,
+  );
+  return $form;
+}
+
+/**
+ * Implements hook_commerce_payment_method_info().
+ */
+function commerce_hdfc_commerce_payment_method_info() {
+  $payment_methods = array();
+  $payment_methods['hdfc'] = array(
+    'base' => 'hdfc',
+    'title' => t("Commerce HDFC Payment Gateway"),
+    'display_title' => "Commerce HDFC Payment Gateway",
+    'short_title' => 'Commerce HDFC Payment Gateway',
+    'description' => t("Payment method - Commerce HDFC Payment Gateway"),
+    'terminal' => TRUE,
+    'offsite' => TRUE,
+    'offsite_autoredirect' => TRUE,
+  );
+  return $payment_methods;
+}
+
+/**
+ * Payment method callback: redirect form.
+ */
+function hdfc_redirect_form($form, &$form_state, $order, $payment_method) {
+  $wrapper = entity_metadata_wrapper('commerce_order', $order);
+  $phone = $wrapper->commerce_customer_billing->commerce_firstdata_billing_phone->value(); /* contact no. to be passed as UDF 3 */
+  $billing_address = $wrapper->commerce_customer_billing->commerce_customer_address->value(); /* billing address to be passed as UDF 4 */
+  $address = drupal_substr($billing_address['thoroughfare'], 0, 255);
+  $currency_code = $wrapper->commerce_order_total->currency_code->value();
+  $amount = $wrapper->commerce_order_total->amount->value()/100;
+  $return_url = url('checkout/' . $order->order_id . '/payment/return/' . $order->data['payment_redirect_key'], array('absolute' => TRUE));
+  $merchant_id = $payment_method['settings']['merchant_id'];
+  $working_key = $payment_method['settings']['working_key'];
+  $res_ip_1 = $payment_method['settings']['res_ip_1'];
+  $res_ip_2 = $payment_method['settings']['res_ip_2'];
+  $res_ip_3 = $payment_method['settings']['res_ip_3'];
+  $pg_auth_url = $payment_method['settings']['pg_auth_url'];
+  $pg_dual_url = $payment_method['settings']['pg_dual_url'];
+  $site_base_url = $payment_method['settings']['site_base_url'];
+  $order_id = $order->order_id;
+  $form['merchant_id'] = array(
+    '#type' => 'hidden',
+    '#value' => $merchant_id,
+  );
+  $form['working_key'] = array(
+    '#type' => 'hidden',
+    '#value' => $working_key,
+  );
+  $form['res_ip_1'] = array(
+    '#type' => 'hidden',
+    '#value' => $res_ip_1,
+  );
+  $form['res_ip_2'] = array(
+    '#type' => 'hidden',
+    '#value' => $res_ip_2,
+  );
+  $form['res_ip_3'] = array(
+    '#type' => 'hidden',
+    '#value' => $res_ip_3,
+  );
+  $form['pg_auth_url'] = array(
+    '#type' => 'hidden',
+    '#value' => $pg_auth_url,
+  );
+  $form['pg_dual_url'] = array(
+    '#type' => 'hidden',
+    '#value' => $pg_dual_url,
+  );
+  $form['site_base_url'] = array(
+    '#type' => 'hidden',
+    '#value' => $site_base_url,
+  );
+  $form['amount'] = array(
+    '#type' => 'hidden',
+    '#value' => $amount ,
+  );
+  $form['order_id'] = array(
+    '#type' => 'hidden',
+    '#value' => $order_id,
+  );
+  $form['phone'] = array(
+    '#type' => 'hidden',
+    '#value' => $phone,
+  );
+  $form['address'] = array(
+    '#type' => 'hidden',
+    '#value' => $address,
+  );
+  $form['redirect_url'] = array(
+    '#type' => 'hidden',
+    '#value' => $return_url,
+  );
+  $link = $site_base_url . "/" . drupal_get_path('module', 'commerce_hdfc') . "/includes/send_perform_request.php";
+  $form['#action'] = $link;
+  return $form;
+}
+
+/**
+ * Implementation of hook_enable()
+ **/
+function commerce_hdfc_enable() {
+  // add phone number field to billing profile type
+  commerce_hdfc_add_phone_field();
+
+  // tell user a new field has been added
+  $message_text = 'Commerce HDFC has added a new field for phone number to your "billing" customer profile. ' .
+                  'If you already have one enabled, you must !disablelink now to avoid customer ' .
+                  'confusion during checkout.';
+  $message = t($message_text, array('!disablelink' => l('disable one', 'admin/commerce/customer-profiles/types/billing/fields')));
+  drupal_set_message($message, 'warning');
+}
+
+/**
+ * commerce_customer module creates a customer profile type called "billing".
+ * This profile type only has the field "addressfield", which does not include
+ * a phone number. First Data requires a phone number for transactions, so we
+ * create our own.
+ **/
+function commerce_hdfc_add_phone_field() {
+  // Look for or add a billing phone number field to add to billing customer profile
+  $field = field_info_field(COMMERCE_FIRSTDATA_BILLING_PHONE_FIELD);
+  $instance = field_info_instance('commerce_customer_profile', COMMERCE_FIRSTDATA_BILLING_PHONE_FIELD, 'billing');
+
+  if (empty($field)) {
+    $field = array(
+      'field_name' => COMMERCE_FIRSTDATA_BILLING_PHONE_FIELD,
+      'type' => 'text',
+      'cardinality' => 1,
+      'entity_types' => array('commerce_customer_profile'),
+      'translatable' => FALSE,
+      'locked' => FALSE,
+    );
+
+    $field = field_create_field($field);
+  }
+
+  if (empty($instance)) {
+    $instance = array(
+      'field_name' => COMMERCE_FIRSTDATA_BILLING_PHONE_FIELD,
+      'entity_type' => 'commerce_customer_profile',
+      'bundle' => 'billing',
+      'label' => t('Phone Number'),
+      'required' => TRUE,
+      'widget' => array(
+        'type' => 'text_textfield',
+      ),
+      'settings' => array(),
+      'display' => array(),
+    );
+
+    // Set the default display formatters for various view modes.
+    foreach (array('default', 'customer', 'administrator') as $view_mode) {
+      $instance['display'][$view_mode] = array(
+        'label' => 'hidden',
+        'type' => 'text_default',
+        'weight' => -10,
+      );
+    }
+
+    field_create_instance($instance);
+  }
+}
+
+/**
+ * Implements hook_entity_view_alter().
+ */
+function commerce_hdfc_entity_view_alter(&$build, $type) {
+  switch ($type) {
+    case 'commerce_order':
+      if ($build['#view_mode'] === 'administrator') {
+        $order = $build['#entity'];
+
+        $build['status'] = array(
+          '#type' => 'fieldset',
+          '#title' => t('Order details'),
+          '#weight' => -100,
+        );
+
+        $build['status']['markup'] = array(
+          '#prefix' => '<dl>',
+          '#suffix' => '</dl>',
+          array('#markup' => sprintf('<dt>%s</dt><dd>%s<dd>', t('Status'), $order->status)),
+          array('#markup' => sprintf('<dt>%s</dt><dd>%s<dd>', t('E-mail'), $order->mail)),
+        );
+  }
+      break;
+ }
+}
diff --git a/commerce_hdfc/includes/process.php b/commerce_hdfc/includes/process.php
index 6235ba2..725d82a 100644
--- a/commerce_hdfc/includes/process.php
+++ b/commerce_hdfc/includes/process.php
@@ -1,149 +1,151 @@
-<?php
-
-/**
- * @file
- * Determines if the transaction is success or failure
- * and redirects accordingly.
- */
-
-$current_wd = getcwd();
-include 'header.php';
-chdir($current_wd);
-
-/*////////////////////////////////////////////////////////////////////*/
- $query = db_select('payment_hdfc_config', 'n');
- $query->condition('n.fixedcol', '1020', '=')
-       ->fields('n', array('site_base_url'));
- $sql = $query->execute()->fetchField();
- $db_site_base_url = $sql;
-
-/*////////////////////////////////////////////////////////////////////*/
-
-$transaction_id = isset($_REQUEST['restranid']) ? $_REQUEST['restranid'] : '';
-$track_id = isset($_REQUEST['restrackid']) ? $_REQUEST['restrackid'] : '';
-$amount = isset($_REQUEST['resamount']) ? $_REQUEST['resamount'] : '';
-$fcode = isset($_REQUEST['resresult']) ? $_REQUEST['resresult'] : '';
-$pay_id = isset($_REQUEST['respaymentid']) ? $_REQUEST['respaymentid'] : '';
-$refer_no = isset($_REQUEST['resref']) ? $_REQUEST['resref'] : '';
-$auth_no = isset($_REQUEST['resauth']) ? $_REQUEST['resauth'] : '';
-$avr_no = isset($_REQUEST['resavr']) ? $_REQUEST['resavr'] : '';
-$error_text = isset($_REQUEST['errortext']) ? $_REQUEST['errortext'] : '';
-$error_no = isset($_REQUEST['errorno']) ? $_REQUEST['errorno'] : '';
-
-/*////////////////////////Fetch AMOUNT from DB////////////////*/
- $query = db_select('field_data_commerce_order_total', 't');
- $query->join('commerce_order', 'n', 'n.revision_id = t.revision_id');
- $query->fields('t', array('commerce_order_total_amount'))
-       ->condition('n.order_number', $track_id, '=');
- $sql = $query->execute()->fetchField();
- $row = $sql;
- $db_amount = floor($row/100);
- $trimamt = explode(".", "$amount");
- $trimamtval = $trimamt[0];
-
-/*/////////////////////////Fetch TRACK-ID from DB////////////////*/
- $query = db_select('field_data_commerce_order_total', 't');
- $query->join('commerce_order', 'n', 'n.revision_id = t.revision_id');
- $query->fields('n', array('order_number'))
-       ->condition('n.order_number', $track_id, '=');
- $sql = $query->execute()->fetchField();
- $db_orderid = $sql;
-
-/*////////////Fetch Payment-ID from DB/////////////*/
- $query = db_select('field_data_commerce_order_total', 't');
- $query->join('commerce_order', 'n', 'n.revision_id = t.revision_id');
- $query->fields('n', array('pymid'))
-       ->condition('n.order_number', $track_id, '=');
- $sql = $query->execute()->fetchField();
- $db_payid = $sql;
-
-/*//////////Fetch Result from DB//////////////////*/
- $query = db_select('field_data_commerce_order_total', 't');
- $query->join('commerce_order', 'n', 'n.revision_id = t.revision_id');
- $query->fields('n', array('result'))
-       ->condition('n.order_number', $track_id, '=');
- $sql = $query->execute()->fetchField();
- $db_result = $sql;
-
-/*//////////////Fetch Transaction Id from DB///////////////////*/
- $query = db_select('field_data_commerce_order_total', 't');
- $query->join('commerce_order', 'n', 'n.revision_id = t.revision_id');
- $query->fields('n', array('tranid'))
-       ->condition('n.order_number', $track_id, '=');
- $sql = $query->execute()->fetchField();
- $db_tranid = $sql;
-
-/*//////Fetch Ref. No. from DB///////////////////*/
- $query = db_select('field_data_commerce_order_total', 't');
- $query->join('commerce_order', 'n', 'n.revision_id = t.revision_id');
- $query->fields('n', array('refno'))
-       ->condition('n.order_number', $track_id, '=');
- $sql = $query->execute()->fetchField();
- $db_refno = $sql;
-
-/*/////////////Fetch Auth. No. from DB//////////////*/
- $query = db_select('field_data_commerce_order_total', 't');
- $query->join('commerce_order', 'n', 'n.revision_id = t.revision_id');
- $query->fields('n', array('auth'))
-       ->condition('n.order_number', $track_id, '=');
- $sql = $query->execute()->fetchField();
- $db_auth = $sql;
-
-/*////////////Fetch AVR No. from DB/////////////*/
- $query = db_select('field_data_commerce_order_total', 't');
- $query->join('commerce_order', 'n', 'n.revision_id = t.revision_id');
- $query->fields('n', array('avr'))
-       ->condition('n.order_number', $track_id, '=');
- $sql = $query->execute()->fetchField();
- $db_avr = $sql;
-
-/*////////////Fetch Order Number from DB/////////////*/
- $query = db_select('field_data_commerce_order_total', 't');
- $query->join('commerce_order', 'n', 'n.revision_id = t.revision_id');
- $query->fields('n', array('order_number'))
-       ->condition('n.order_number', $track_id, '=');
- $sql = $query->execute()->fetchField();
- $db_ordnum = $sql;
-
-/*////////////Fetch Order Id from DB/////////////*/
- $query = db_select('commerce_order', 'n');
- $query->condition('n.order_number', $track_id, '=')
-       ->fields('n', array('order_id'));
- $sql = $query->execute()->fetchField();
- $db_id_order = $sql;
-
-/*///////////////////////////////////////////////////////////////////*/
-
-if($trimamtval == $db_amount && $track_id == $db_orderid && $pay_id == $db_payid && $fcode == $db_result && $refer_no == $db_refno && $transaction_id == $db_tranid && $auth_no == $db_auth && $avr_no == $db_avr)
-{
-  $payment_amt = $amount * 100;
-  $commerce_order = commerce_order_load($db_ordnum);
-  $name = 'checkout_complete';
-  $order_success = commerce_order_status_update($commerce_order, $name, $skip_save = FALSE, $revision = TRUE, $log = '');
-  commerce_checkout_complete($order_success);
-  $wrapper = entity_metadata_wrapper('commerce_order', $commerce_order);
-  $currency = $wrapper->commerce_order_total->currency_code->value();
-  $transaction = commerce_payment_transaction_new('hdfc', $db_id_order);
-  $transaction->amount = $payment_amt;
-  $transaction->message = t('Payment received at') . ' ' . date("d-m-Y H:i:s", REQUEST_TIME);
-  $transaction->currency_code = $currency;
-  $transaction->status = COMMERCE_PAYMENT_STATUS_SUCCESS;
-  commerce_payment_transaction_save($transaction);
-  commerce_payment_redirect_pane_next_page($order_success);
-  $url_success = $db_site_base_url . '/checkout/' . $db_id_order . '/complete';
-  header("location:". $url_success);
-}
-
-else
-{
-  $commerce_order = commerce_order_load($db_ordnum);
-  $name = 'checkout_checkout';
-  $order_failure = commerce_order_status_update($commerce_order, $name, $skip_save = FALSE, $revision = TRUE, $log = '');
-  $transaction = commerce_payment_transaction_new('hdfc', $db_id_order);
-  $transaction->message = t('There was a problem with your order: !response_code  !reason_text', array('!response_code' => check_plain($error_no), '!reason_text' => $error_text));
-  $transaction->status = COMMERCE_PAYMENT_STATUS_FAILURE;
-  commerce_payment_transaction_save($transaction);
-  commerce_payment_redirect_pane_previous_page($order_failure);
-  $url_failure = $db_site_base_url . '/checkout/' . $db_id_order . '/review';
-  header("location:". $url_failure);
-}
+<?php
+
+/**
+ * @file
+ * Determines if the transaction is success or failure
+ * and redirects accordingly.
+ */
+
+$current_wd = getcwd();
+include 'header.php';
+chdir($current_wd);
+
+/*////////////////////////////////////////////////////////////////////*/
+ $query = db_select('payment_hdfc_config', 'n');
+ $query->condition('n.fixedcol', '1020', '=')
+       ->fields('n', array('site_base_url'));
+ $sql = $query->execute()->fetchField();
+ $db_site_base_url = $sql;
+
+/*////////////////////////////////////////////////////////////////////*/
+
+$transaction_id = isset($_REQUEST['restranid']) ? $_REQUEST['restranid'] : '';
+$track_id = isset($_REQUEST['restrackid']) ? $_REQUEST['restrackid'] : '';
+$amount = isset($_REQUEST['resamount']) ? $_REQUEST['resamount'] : '';
+$fcode = isset($_REQUEST['resresult']) ? $_REQUEST['resresult'] : '';
+$pay_id = isset($_REQUEST['respaymentid']) ? $_REQUEST['respaymentid'] : '';
+$refer_no = isset($_REQUEST['resref']) ? $_REQUEST['resref'] : '';
+$auth_no = isset($_REQUEST['resauth']) ? $_REQUEST['resauth'] : '';
+$avr_no = isset($_REQUEST['resavr']) ? $_REQUEST['resavr'] : '';
+$error_text = isset($_REQUEST['errortext']) ? $_REQUEST['errortext'] : '';
+$error_no = isset($_REQUEST['errorno']) ? $_REQUEST['errorno'] : '';
+
+/*////////////////////////Fetch AMOUNT from DB////////////////*/
+ $query = db_select('field_data_commerce_order_total', 't');
+ $query->join('commerce_order', 'n', 'n.revision_id = t.revision_id');
+ $query->fields('t', array('commerce_order_total_amount'))
+       ->condition('n.order_number', $track_id, '=');
+ $sql = $query->execute()->fetchField();
+ $row = $sql;
+ $db_amount = floor($row/100);
+ $trimamt = explode(".", "$amount");
+ $trimamtval = $trimamt[0];
+
+/*/////////////////////////Fetch TRACK-ID from DB////////////////*/
+ $query = db_select('field_data_commerce_order_total', 't');
+ $query->join('commerce_order', 'n', 'n.revision_id = t.revision_id');
+ $query->fields('n', array('order_number'))
+       ->condition('n.order_number', $track_id, '=');
+ $sql = $query->execute()->fetchField();
+ $db_orderid = $sql;
+
+/*////////////Fetch Payment-ID from DB/////////////*/
+ $query = db_select('field_data_commerce_order_total', 't');
+ $query->join('commerce_order', 'n', 'n.revision_id = t.revision_id');
+ $query->fields('n', array('pymid'))
+       ->condition('n.order_number', $track_id, '=');
+ $sql = $query->execute()->fetchField();
+ $db_payid = $sql;
+
+/*//////////Fetch Result from DB//////////////////*/
+ $query = db_select('field_data_commerce_order_total', 't');
+ $query->join('commerce_order', 'n', 'n.revision_id = t.revision_id');
+ $query->fields('n', array('result'))
+       ->condition('n.order_number', $track_id, '=');
+ $sql = $query->execute()->fetchField();
+ $db_result = $sql;
+
+/*//////////////Fetch Transaction Id from DB///////////////////*/
+ $query = db_select('field_data_commerce_order_total', 't');
+ $query->join('commerce_order', 'n', 'n.revision_id = t.revision_id');
+ $query->fields('n', array('tranid'))
+       ->condition('n.order_number', $track_id, '=');
+ $sql = $query->execute()->fetchField();
+ $db_tranid = $sql;
+
+/*//////Fetch Ref. No. from DB///////////////////*/
+ $query = db_select('field_data_commerce_order_total', 't');
+ $query->join('commerce_order', 'n', 'n.revision_id = t.revision_id');
+ $query->fields('n', array('refno'))
+       ->condition('n.order_number', $track_id, '=');
+ $sql = $query->execute()->fetchField();
+ $db_refno = $sql;
+
+/*/////////////Fetch Auth. No. from DB//////////////*/
+ $query = db_select('field_data_commerce_order_total', 't');
+ $query->join('commerce_order', 'n', 'n.revision_id = t.revision_id');
+ $query->fields('n', array('auth'))
+       ->condition('n.order_number', $track_id, '=');
+ $sql = $query->execute()->fetchField();
+ $db_auth = $sql;
+
+/*////////////Fetch AVR No. from DB/////////////*/
+ $query = db_select('field_data_commerce_order_total', 't');
+ $query->join('commerce_order', 'n', 'n.revision_id = t.revision_id');
+ $query->fields('n', array('avr'))
+       ->condition('n.order_number', $track_id, '=');
+ $sql = $query->execute()->fetchField();
+ $db_avr = $sql;
+
+/*////////////Fetch Order Number from DB/////////////*/
+ $query = db_select('field_data_commerce_order_total', 't');
+ $query->join('commerce_order', 'n', 'n.revision_id = t.revision_id');
+ $query->fields('n', array('order_number'))
+       ->condition('n.order_number', $track_id, '=');
+ $sql = $query->execute()->fetchField();
+ $db_ordnum = $sql;
+
+/*////////////Fetch Order Id from DB/////////////*/
+ $query = db_select('commerce_order', 'n');
+ $query->condition('n.order_number', $track_id, '=')
+       ->fields('n', array('order_id'));
+ $sql = $query->execute()->fetchField();
+ $db_id_order = $sql;
+
+/*///////////////////////////////////////////////////////////////////*/
+
+if($trimamtval == $db_amount && $track_id == $db_orderid && $pay_id == $db_payid && $fcode == $db_result && $refer_no == $db_refno && $transaction_id == $db_tranid && $auth_no == $db_auth && $avr_no == $db_avr)
+{
+  $payment_amt = $amount * 100;
+  $commerce_order = commerce_order_load($db_ordnum);
+  $name = 'checkout_complete';
+  $order_success = commerce_order_status_update($commerce_order, $name, $skip_save = FALSE, $revision = TRUE, $log = '');
+  commerce_checkout_complete($order_success);
+  $wrapper = entity_metadata_wrapper('commerce_order', $commerce_order);
+  $currency = $wrapper->commerce_order_total->currency_code->value();
+  $transaction = commerce_payment_transaction_new('hdfc', $db_id_order);
+  $transaction->amount = $payment_amt;
+  $transaction->message = t('Payment received at') . ' ' . date("d-m-Y H:i:s", REQUEST_TIME);
+  $transaction->currency_code = $currency;
+  $transaction->status = COMMERCE_PAYMENT_STATUS_SUCCESS;
+  commerce_payment_transaction_save($transaction);
+  $_SESSION['commerce_cart_orders'][] = $db_id_order; /* Fixes checkout complete - 'page not found' issue when order created for anonymous user*/
+  commerce_payment_redirect_pane_next_page($order_success);
+  $url_success = $db_site_base_url . '/checkout/' . $db_id_order . '/complete';
+  header("location:". $url_success);
+}
+
+else
+{
+  $commerce_order = commerce_order_load($db_ordnum);
+  $name = 'checkout_checkout';
+  $order_failure = commerce_order_status_update($commerce_order, $name, $skip_save = FALSE, $revision = TRUE, $log = '');
+  $transaction = commerce_payment_transaction_new('hdfc', $db_id_order);
+  $transaction->message = t('There was a problem with your order: !response_code  !reason_text', array('!response_code' => check_plain($error_no), '!reason_text' => $error_text));
+  $transaction->status = COMMERCE_PAYMENT_STATUS_FAILURE;
+  commerce_payment_transaction_save($transaction);
+  $_SESSION['commerce_cart_orders'][] = $db_id_order;
+  commerce_payment_redirect_pane_previous_page($order_failure);
+  $url_failure = $db_site_base_url . '/checkout/' . $db_id_order . '/review';
+  header("location:". $url_failure);
+}
diff --git a/commerce_hdfc/includes/send_perform_request.php b/commerce_hdfc/includes/send_perform_request.php
index 3d7e7e2..7a44685 100644
--- a/commerce_hdfc/includes/send_perform_request.php
+++ b/commerce_hdfc/includes/send_perform_request.php
@@ -1,147 +1,187 @@
-<?php
-
-/**
- * @file
- * Connects to Payment Gateway and sends request.
- */
-
- $current_wd = getcwd();
- include 'header.php';
- chdir($current_wd);
-
- $site_base_url = isset($_POST['site_base_url']) ? $_POST['site_base_url'] : '';
- $pg_auth_url = isset($_POST['pg_auth_url']) ? $_POST['pg_auth_url'] : '';
- $pg_dual_url = isset($_POST['pg_dual_url']) ? $_POST['pg_dual_url'] : '';
- $pg_response_url1 = isset($_POST['res_ip_1']) ? $_POST['res_ip_1'] : '';
- $pg_response_url2 = isset($_POST['res_ip_2']) ? $_POST['res_ip_2'] : '';
- $pg_response_url3 = isset($_POST['res_ip_3']) ? $_POST['res_ip_3'] : '';
- $trantrackid = isset($_POST['order_id']) ? $_POST['order_id'] : '';
- $tranamount = isset($_POST['amount']) ? $_POST['amount'] : '';
- $tid = isset($_POST['merchant_id']) ? $_POST['merchant_id'] : '';
- $tpswd = isset($_POST['working_key']) ? $_POST['working_key'] : '';
-
-/* Tranportal ID. */
-
- $id = "id=" . $tid;
-
-/* Tranportal password. */
-
- $password = "password=" . $tpswd;
-
- db_update('payment_hdfc_config')
-  ->fields(array('transportal_id' => $tid,
-    'transportal_pswd' => $tpswd,
-    'site_base_url' => $site_base_url,
-    'auth_request_url' => $pg_auth_url,
-    'dual_verification_request_url' => $pg_dual_url,
-    'response_ip_one' => $pg_response_url1,
-    'response_ip_two' => $pg_response_url2,
-    'response_ip_three' => $pg_response_url3,
-  ))
-  ->condition('fixedcol', 1020)
-  ->execute();
-
-/* Action Code of the transaction, this refers to type of transaction. Action Code 1 stands of 
-Purchase transaction and action code 4 stands for Authorization (pre-auth). Merchant should 
-confirm from Bank action code enabled for the merchant by the bank. */
-
- $action = "action=1";
-
-/* Transaction language, THIS MUST BE ALWAYS USA. */
-
- $langid = "langid=USA";
-
-/* Currency code of the transaction. By default INR i.e. 356 is configured. If merchant wishes 
-to do multiple currency code transaction, merchant needs to check with bank team on the available 
-currency code. */
-
- $currencycode = "currencycode=356";
-
-/* Transaction amount send to payment gateway by merchant for processing. */
-
-$amt = "amt=" . $tranamount;
-
-/* Response URL where Payment gateway will send response once transaction processing is completed 
-Merchant MUST esure that below points in Response URL
-1- Response URL must start with http://
-2- The Response URL SHOULD NOT have any additional paramteres or query string */
-
- $responseurl = "responseurl=" . $site_base_url . "/" . drupal_get_path('module', 'commerce_hdfc') . "/includes/get_handle_response.php";
-
-/* Error URL where Payment gateway will send response in case any issues while processing the transaction 
-Merchant MUST esure that below points in errorurl 
-1- Error url must start with http://
-2- The error url SHOULD NOT have any additional paramteres or query string.
-*/
-
- $errorurl = "errorurl=" . $site_base_url. "/" . drupal_get_path('module', 'commerce_hdfc') . "/includes/process.php";
-
-/* Merchant track id. */
-
- $trackid = "trackid=" . $trantrackid;
-
-/* Now merchant sets all the inputs in one string for passing to the Payment Gateway URL. */
-
- $param = $id . "&" . $password . "&" . $action . "&" . $langid . "&" . $currencycode . "&" . $amt . "&" . $responseurl . "&" . $errorurl . "&" . $trackid;
-
-/* This is Payment Gateway Test URL where merchant sends request. This is test enviornment URL,
-production URL will be different and will be shared by Bank during production movement. */
-
- $url = $pg_auth_url;
-
-/*
-Now creating a connection and sending request
-Note - PHP CURL function is used for sending TCPIP request.
-*/
- $ch = curl_init() or die(curl_error());
- curl_setopt($ch, CURLOPT_POST,1);
- curl_setopt($ch, CURLOPT_POSTFIELDS,$param);
- curl_setopt($ch, CURLOPT_PORT, 443); // Port 443.
- curl_setopt($ch, CURLOPT_URL,$url); // Here the request is sent to payment gateway.
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,0); // Create a SSL connection object server-to-server.
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
- $data1=curl_exec($ch) or die(curl_error());
-
- curl_close($ch);
-
- $response = $data1;
-  try
-  {
-
-   $index=strpos($response,"!-");
-   $ErrorCheck=substr($response, 1, $index-1); // Find Error Keyword in response.
-
-    if($ErrorCheck == 'ERROR') // Check for Error in response.
-    {
-     $failedurl = $site_base_url . "/" . drupal_get_path('module', 'commerce_hdfc') . '/includes/process.php?Message=Transaction Failed&restrackid=' . $trantrackid . '&resamount=' . $tranamount . '&errortext=' . $response;
-     header("location:". $failedurl );
-    }
-
-    else
-    {
-
-     $i =  strpos($response,":"); // If Payment Gateway response has Payment ID & Pay page URL.
-
-     $paymentid = substr($response, 0, $i); // Updating the Payment ID received with the merchant Track Id in database at this place.
-
-     db_update('commerce_order')
-      ->fields(array(
-       'pymid' => $paymentid,
-      ))
-     ->condition('order_number', $trantrackid)
-     ->execute();
-
-     $paymentpage = substr( $response, $i + 1);
-
-     $r = $paymentpage . "?PaymentID=" . $paymentid; // Redirecting the customer browser from ME site to Payment Gateway Page with the Payment ID.
-
-     header("location:". $r );
-    }
-
-  }
-
-  catch(Exception $e)
-  {
-   var_dump($e->getMessage());
-  }
+<?php
+
+/**
+ * @file
+ * Connects to Payment Gateway and sends request.
+ */
+
+ $current_wd = getcwd();
+ include 'header.php';
+ chdir($current_wd);
+
+ $site_base_url = isset($_POST['site_base_url']) ? $_POST['site_base_url'] : '';
+ $pg_auth_url = isset($_POST['pg_auth_url']) ? $_POST['pg_auth_url'] : '';
+ $pg_dual_url = isset($_POST['pg_dual_url']) ? $_POST['pg_dual_url'] : '';
+ $pg_response_url1 = isset($_POST['res_ip_1']) ? $_POST['res_ip_1'] : '';
+ $pg_response_url2 = isset($_POST['res_ip_2']) ? $_POST['res_ip_2'] : '';
+ $pg_response_url3 = isset($_POST['res_ip_3']) ? $_POST['res_ip_3'] : '';
+ $trantrackid = isset($_POST['order_id']) ? $_POST['order_id'] : '';
+ $udf3_val = isset($_POST['phone']) ? $_POST['phone'] : '';
+ $udf4_val = isset($_POST['address']) ? $_POST['address'] : '';
+// $tranamount = isset($_POST['amount']) ? $_POST['amount'] : ''; /* Fixes amount tampering vulnerability - replaced with 'Fetch AMOUNT from DB' below */
+ $tid = isset($_POST['merchant_id']) ? $_POST['merchant_id'] : '';
+ $tpswd = isset($_POST['working_key']) ? $_POST['working_key'] : '';
+ 
+ /*////////////////////////Fetch AMOUNT from DB////////////////*/
+ $query = db_select('field_data_commerce_order_total', 't');
+ $query->join('commerce_order', 'n', 'n.revision_id = t.revision_id');
+ $query->fields('t', array('commerce_order_total_amount'))
+       ->condition('n.order_number', $trantrackid, '=');
+ $sql = $query->execute()->fetchField();
+ $row = $sql;
+ $tranamount = $row/100;
+
+  /*////////////////////////Fetch Product SKU from DB////////////////*/
+ $query = db_select('commerce_product', 't');
+ $query->join('field_data_commerce_product', 'n', 'n.commerce_product_product_id = t.product_id');
+ $query->fields('t', array('sku'))
+       ->condition('n.revision_id', $trantrackid, '=');
+ $sql = $query->execute()->fetchField();
+ $udf1_val = $sql;
+ 
+  /*////////////////////////Fetch User Email from DB////////////////*/
+ $query = db_select('field_data_commerce_order_total', 't');
+ $query->join('commerce_order', 'n', 'n.revision_id = t.revision_id');
+ $query->fields('n', array('mail'))
+       ->condition('n.order_number', $trantrackid, '=');
+ $sql = $query->execute()->fetchField();
+ $udf2_val = $sql;
+
+/* Tranportal ID. */
+
+ $id = "id=" . $tid;
+
+/* Tranportal password. */
+
+ $password = "password=" . $tpswd;
+
+ db_update('payment_hdfc_config')
+  ->fields(array('transportal_id' => $tid,
+    'transportal_pswd' => $tpswd,
+    'site_base_url' => $site_base_url,
+    'auth_request_url' => $pg_auth_url,
+    'dual_verification_request_url' => $pg_dual_url,
+    'response_ip_one' => $pg_response_url1,
+    'response_ip_two' => $pg_response_url2,
+    'response_ip_three' => $pg_response_url3,
+  ))
+  ->condition('fixedcol', 1020)
+  ->execute();
+
+/* Action Code of the transaction, this refers to type of transaction. Action Code 1 stands of 
+Purchase transaction and action code 4 stands for Authorization (pre-auth). Merchant should 
+confirm from Bank action code enabled for the merchant by the bank. */
+
+ $action = "action=1";
+
+/* Transaction language, THIS MUST BE ALWAYS USA. */
+
+ $langid = "langid=USA";
+
+/* Currency code of the transaction. By default INR i.e. 356 is configured. If merchant wishes 
+to do multiple currency code transaction, merchant needs to check with bank team on the available 
+currency code. */
+
+ $currencycode = "currencycode=356";
+
+/* Transaction amount send to payment gateway by merchant for processing. */
+
+$amt = "amt=" . $tranamount;
+
+/* Response URL where Payment gateway will send response once transaction processing is completed 
+Merchant MUST esure that below points in Response URL
+1- Response URL must start with http://
+2- The Response URL SHOULD NOT have any additional paramteres or query string */
+
+ $responseurl = "responseurl=" . $site_base_url . "/" . drupal_get_path('module', 'commerce_hdfc') . "/includes/get_handle_response.php";
+
+/* Error URL where Payment gateway will send response in case any issues while processing the transaction 
+Merchant MUST esure that below points in errorurl 
+1- Error url must start with http://
+2- The error url SHOULD NOT have any additional paramteres or query string.
+*/
+
+ $errorurl = "errorurl=" . $site_base_url. "/" . drupal_get_path('module', 'commerce_hdfc') . "/includes/process.php";
+
+/* Merchant track id. */
+
+ $trackid = "trackid=" . $trantrackid;
+ 
+/* User Defined Fileds as per Merchant or bank requirment. Merchant MUST ensure merchant 
+merchant is not passing junk values OR CRLF in any of the UDF. In below sample UDF values 
+are not utilized */
+//$udf1="udf1=Test1";
+//$udf2="udf2=Test2";
+//$udf3="udf3=Test3";
+//$udf4="udf4=Test4";
+/* Fixes the additional requirement to pass UDF values in the initial request to HDFC payment gateway */
+$udf1 = "udf1=" . $udf1_val; /* passing SKU */
+$udf2 = "udf2=" . $udf2_val; /* passing Email ID */
+$udf3 = "udf3=" . $udf3_val; /* passing Contact Number */
+$udf4 = "udf4=" . $udf4_val; /* passing Billing Address */
+// $udf5="udf5=Test5"; /* not required for the Dual Verification method */
+
+/* Now merchant sets all the inputs in one string for passing to the Payment Gateway URL. */
+
+ $param = $id . "&" . $password . "&" . $action . "&" . $langid . "&" . $currencycode . "&" . $amt . "&" . $responseurl . "&" . $errorurl . "&" . $trackid ."&" . $udf1 . "&" . $udf2 . "&" . $udf3 . "&" . $udf4;
+
+/* This is Payment Gateway Test URL where merchant sends request. This is test enviornment URL,
+production URL will be different and will be shared by Bank during production movement. */
+
+ $url = $pg_auth_url;
+
+/*
+Now creating a connection and sending request
+Note - PHP CURL function is used for sending TCPIP request.
+*/
+ $ch = curl_init() or die(curl_error());
+ curl_setopt($ch, CURLOPT_POST,1);
+ curl_setopt($ch, CURLOPT_POSTFIELDS,$param);
+ curl_setopt($ch, CURLOPT_PORT, 443); // Port 443.
+ curl_setopt($ch, CURLOPT_URL,$url); // Here the request is sent to payment gateway.
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+ curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,0); // Create a SSL connection object server-to-server.
+ curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
+ $data1=curl_exec($ch) or die(curl_error());
+
+ curl_close($ch);
+
+ $response = $data1;
+  try
+  {
+
+   $index=strpos($response,"!-");
+   $ErrorCheck=substr($response, 1, $index-1); // Find Error Keyword in response.
+
+    if($ErrorCheck == 'ERROR') // Check for Error in response.
+    {
+     $failedurl = $site_base_url . "/" . drupal_get_path('module', 'commerce_hdfc') . '/includes/process.php?Message=Transaction Failed&restrackid=' . $trantrackid . '&resamount=' . $tranamount . '&errortext=' . $response;
+     header("location:". $failedurl );
+    }
+
+    else
+    {
+
+     $i =  strpos($response,":"); // If Payment Gateway response has Payment ID & Pay page URL.
+
+     $paymentid = substr($response, 0, $i); // Updating the Payment ID received with the merchant Track Id in database at this place.
+
+     db_update('commerce_order')
+      ->fields(array(
+       'pymid' => $paymentid,
+      ))
+     ->condition('order_number', $trantrackid)
+     ->execute();
+
+     $paymentpage = substr( $response, $i + 1);
+
+     $r = $paymentpage . "?PaymentID=" . $paymentid; // Redirecting the customer browser from ME site to Payment Gateway Page with the Payment ID.
+
+     header("location:". $r );
+    }
+
+  }
+
+  catch(Exception $e)
+  {
+   var_dump($e->getMessage());
+  }
