nochex integration

Posts: 17
Joined: 03/27/2008

Hi, i am nearly there with gettin ubercart setup with nochex. im stuck now however and wondered if anyone could help?

I have been editing the

function uc_cart_checkout_review_form_submit()

so when the user clicks the submit order page they are forwarded to the nochex site with the details passed in the url string. here is what the function looks like at the moment:

function uc_cart_checkout_review_form_submit($form_id, $form_values) {
  switch ($form_values['op']) {
    case variable_get('uc_checkout_submit_button', t('Submit order')):
      // Invoke hook_order($op = 'submit') to test to make sure the order can
      // be completed... used for auto payment in uc_credit.module.
      $order = uc_order_load($_SESSION['cart_order']);
      $pass = module_invoke_all('order', 'submit', $order, NULL);

      foreach ($pass as $result) {
        $msg_type = 'status';
        if ($result['pass'] === FALSE) {
          $error = TRUE;
          $msg_type = 'error';
        }
        if (!empty($result['message'])) {
          drupal_set_message($result['message'], $msg_type);
        }
      }

      if ($error === TRUE) {
        $_SESSION['do_review'] = TRUE;
        return 'cart/checkout/review';
      }
      else {
        $_SESSION['do_complete'] = TRUE;
        return 'https://www.nochex.com/nochex.dll/checkout?
email=sales@boxed.com
&amount='.'3.99'.
'&firstname='.'joe'.
'&lastname='.'blogs'.
'&email_address_sender='.'joe@bloggs.com'.
'&firstline='.'1 Street Lane'.
'&town='.'Leeds'.
'&postcode='.'LS2 7EE';
      }
  }
}

that passes static values to nochex and works well. But how can i pass the total price and other details from the review order page dynamically?

Any help would be great

Thanks

Andy

Posts: 4
Joined: 03/31/2008

Hi Andy,
We must have lost our connection on #drupal-ubercart. Did you follow all the instructions in the Nochex Payment Page Integration Guide (http://nochex.com/images/Nochex_Payment_Pages.pdf)?

Posts: 17
Joined: 03/27/2008

hi mate, yeah dunno what happened there. ill be on in about half on hour. i have been working on the code and this is what i got so far: why doesnt it step into the

function uc_nochex_complete

when it reaches the confirmation page. i Must be missing something.

<?php
// $Id$

/**
* @file
* Integrates noxchex.com's redirected payment service.
*
* Development sponsored by .
*/

/*******************************************************************************
* Hook Functions (Drupal)
******************************************************************************/

/**
* Implementation of hook_menu().
*/
function uc_nochex_menu($may_cache) {
  if ($may_cache) {
    $items[] = array(
      'path' => 'content/order-complete',
      'title' => t('Order complete'),
      'callback' => 'uc_nochex_complete',
      'access' => user_access('access content'),
      'type' => MENU_CALLBACK,
    );
  }

  return $items;
}

/**
* Implementation of hook_form_alter().
*/
function uc_nochex_form_alter($form_id, &$form) {
  if ($form_id == 'uc_cart_checkout_review_form' && ($order_id = intval($_SESSION['cart_order'])) > 0) {
    $order = uc_order_load($order_id);

    if ($order->payment_method == 'nochex') {
      unset($form['submit']);
      $form['#prefix'] = '<table style="display: inline; padding-top: 1em;"><tr><td>';
      $form['#suffix'] = '</td><td>'. drupal_get_form('uc_nochex_form', $order) .'</td></tr></table>';
    }
  }
}


/*******************************************************************************
* Callback Functions, Forms, and Tables
******************************************************************************/

/**
* Callback for Nochex payment method settings.
*/

function uc_payment_method_nochex($op, &$arg1) {
  switch ($op) {
  
    case 'settings':

      $form['uc_nochex_test'] = array(
        '#type' => 'checkbox',
        '#title' => t('Enable test mode, allowing you to process fake orders for testing purposes.'),
        '#default_value' => variable_get('uc_nochex_test', FALSE),
      );
      $form['uc_nochex_hidebilling'] = array(
        '#type' => 'checkbox',
        '#title' => t('Hide Billing address on payment page, so customers cant edit it.'),
        '#default_value' => variable_get('uc_nochex_hidebilling', FALSE),
      );
      $form['uc_nochex_sid'] = array(
        '#type' => 'textfield',
        '#title' => t('Nochex email address'),
        '#description' => t('Your Nochex account email.'),
        '#default_value' => variable_get('uc_nochex_sid', ''),
        '#size' => 16,
      );
      $form['uc_nochex_method_title'] = array(
        '#type' => 'textfield',
        '#title' => t('Payment method title'),
        '#default_value' => variable_get('uc_nochex_method_title', t('Credit Card:')),
      );
      $form['uc_nochex_checkout_button'] = array(
        '#type' => 'textfield',
        '#title' => t('Order review submit button text'),
        '#description' => t('Provide Nochex specific text for the submit button on the order review page.'),
        '#default_value' => variable_get('uc_nochex_checkout_button', t('Submit Order')),
      );
      return $form;
 
}
}

// Form to build the submission to Nochex.com.
function uc_nochex_form($order) {
  $country = uc_get_country_data(array('country_id' => $order->billing_country));
  if ($country === FALSE) {
    $country = array(0 => array('country_iso_code_3' => 'USA'));
  }

  $data = array(
    'merchant_id' => variable_get('uc_nochex_sid', ''),
    'logo' => "http://www.headboxed.com/themes/greenNblack/img/header_bg.png",
    'amount' => uc_currency_format($order->order_total, FALSE, FALSE, '.'),
    'order_id' => $order->order_id,
    'success_url' => "http://www.headboxed.com/content/order-complete/",
    'cancel_url' => "http://www.headboxed.com/content/order-complete/",
    'billing_fullname' =>  substr($order->billing_first_name .' '. $order->billing_last_name, 0, 128),
    'billing_address' => substr($order->billing_street1. ' '. $order->billing_street2. ' '. $order->billing_city. ' '. $order->billing_zone.' '. $country[0]['country_iso_code_2'], 0, 256),
    'billing_postcode' => substr($order->billing_postal_code, 0, 16),
    'customer_phone_number' => substr($order->billing_phone, 0, 16),
    'callback_url' => url('http://www.headboxed.com/content/order-complete/'. uc_cart_get_id(), NULL, NULL, TRUE),
    'email_address' => substr($order->primary_email, 0, 64),
    'hide_billing_details' => variable_get('uc_nochex_hidebilling', TRUE) ? 'true' : 'false',
   
    'test_transaction' => variable_get('uc_nochex_test', TRUE) ? '100' : '',
    'test_success_url' => url('http://www.headboxed.com/content/order-complete/', NULL, NULL, TRUE)

  );

  $i = 0;
  foreach ($order->products as $product) {
    $i++;
    $data['c_prod_'. $i] = $product->model .','. $product->qty;
    $data['c_name_'. $i] = $product->title;
    $data['c_description_'. $i] = $desc;
    $data['c_price_'. $i] = uc_currency_format($product->price, FALSE, FALSE, '.');
  }

  $form['#action'] = 'https://secure.nochex.com';

  foreach ($data as $name => $value) {
    $form[$name] = array('#type' => 'hidden', '#value' => $value);
  }

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => variable_get('uc_nochex_checkout_button', t('Submit Order')),
  );

  return $form;
}



function uc_nochex_complete($cart_id = 0) {
 
  $order = uc_order_load($_POST['order_id']);

  // change order status to payment revieved
  uc_order_update_status($order, 'payment_received');

  // Empty the cart.
  uc_cart_empty($cart_id);

  exit();
}

Posts: 290
Joined: 11/19/2007
Bug FinderGetting busy with the Ubercode.

You seem to be using the lack of order status update and cart emptying as the way of identifying whether or not uc_nocheck_complete is being called, but putting a drupal_set_message() might be a better way of seeing that the function is stepped into.

uc_nocheck_complete isn't getting a cart_id passed to it and so it won't empty the user's cart. You'll probably want to use uc_cart_get_id() there.

Also, if you are posting to a 3rd party payment processor and then having it redirect back to you, you could print_r($_POST) in your function and make sure that they are returning 'order_id' to you and not 'o_id' or 'orderid' or something like that. If they aren't passing 'order_id' then that would explain why the status is not being updated.

Posts: 17
Joined: 03/27/2008

Hi , thanks for your comment, print_r($_POST) result is Array()

I got it working, now the function looks like this.

function uc_nochex_complete($cart_id = 0) {

   $order = uc_order_load($_POST['order_id']);
 
   // change order status to payment revieved
   uc_order_update_status($order->order_id, 'payment_received');
 
   // Empty the cart.
   uc_cart_empty($cart_id);

   exit();
}

Can anyone see any problems that might arise with this code? there doesnt seem to be much error checking or anything?

Thanks

Andy

Posts: 290
Joined: 11/19/2007
Bug FinderGetting busy with the Ubercode.

If $_POST is an empty array, then how is this working to extract an order id from it? In specifying the return URL with nochex are you able to pass the order_id as part of the URL and extract it from there?

Posts: 17
Joined: 03/27/2008

im not sure, but it seems to be working, heres the code for the return url, i guess i can include a parameter here?

'success_url' => "content/order-complete/"

you know how i would i do it?

Thanks

Andy