after payment redirect (code included here)

Posts: 17
Joined: 03/27/2008

i managed to get a payment module working to pay by nochex. the only thing i need to do now is once they have paid, they need to redirected back to my website. do i need to pass some parameters to confirm that they have paid? nochex allows you to pass parameters back to my website, do you know what i have to pass for it to work? and where they should be directed to?

The 2 options i have are

redirect url = (this is the page they will be redirected to after payment)

and

callback url = (this will allow me to pass some paramaters)

Thanks

Andy

heres my code at the moment (based on 2checkout code)

<?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' => 'cart/nochex/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>';
    }
  }
}


/*******************************************************************************
* Hook Functions (Ubercart)
******************************************************************************/

/**
* Implementation of hook_payment_method().
*/
function uc_nochex_payment_method() {
  $path = base_path() . drupal_get_path('module', 'uc_nochex');
  $title = variable_get('uc_nochex_method_title', t('Credit Card'));
  $title .= '<br /><img src="'. $path .'/merchant-services.jpg" style="position: relative; left: 2.5em;">';

  $methods[] = array(
    'id' => 'nochex',
    'name' => t('Nochex'),
    'title' => $title,
    'review' => variable_get('uc_nochex_check', FALSE) ? t('Credit card/eCheck') : t('Credit card'),
    'desc' => t('Redirect to Nochex to pay by credit card.'),
    'callback' => 'uc_payment_method_nochex',
    'weight' => 3,
    'checkout' => TRUE,
    'no_gateway' => TRUE,
  );

  return $methods;
}


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

/**
* Callback for Nochex payment method settings.
*/
function uc_payment_method_nochex($op, &$arg1) {
  switch ($op) {
    case 'cart-details':
      if (variable_get('uc_nochex_check', FALSE)) {
        if ($_SESSION['pay_method'] == 'CK') {
          $sel[2] = ' selected="selected"';
        }
        else {
          $sel[1] = ' selected="selected"';
        }
        unset($_SESSION['pay_method']);
        $details = '<div class="form-item"><b>'. t('Select your payment type:')
                  .'</b> <select name="pay_method" class="form-select" id="edit-pay-method">'
                  .'<option value="CC"'. $sel[1] .'>'. t('Credit card') .'</option>'
                  .'<option value="CK"'. $sel[2] .'>'. t('Online check') .'</option></select></div>';
      }
      return $details;

    case 'cart-process':
      $_SESSION['pay_method'] = $_POST['pay_method'];
      return;

    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_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/cart/nochex/complete/",
    'cancel_url' => "http://www.headboxed.com/cart/nochex/cancel/",
    '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.' '. $order->billing_country, 0, 256),
    'billing_postcode' => substr($order->delivery_postal_code, 0, 16),
    'callback_url' => "http://www.headboxed.com/cart/nochex/complete/?". order_id,
    'email_address' => substr($order->primary_email, 0, 64),
   
    'test_transaction' => variable_get('uc_nochex_test', TRUE) ? '100' : '',
    'test_success_url' => "http://www.headboxed.com/cart/nochex/complete/"
  );

  $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;
}



//NEEDS WORK ***



function uc_nochex_complete($cart_id = 0) {
  watchdog('nochex', t('Receiving new order notification for order !order_id.', array('!order_id' => check_plain($_POST['merchant_order_id']))));

  $order = uc_order_load($_POST['merchant_order_id']);

  if ($order === FALSE || uc_order_status_data($order->order_status, 'state') != 'in_checkout') {
    print t('An error has occurred during payment.  Please contact us to ensure your order has submitted.');
    exit();
  }

  if ($_POST['demo'] != 'Y') {
    $key = $_POST['key'];
    $valid = md5(variable_get('uc_nochex_secret_word', 'tango') . $_POST['sid'] . $_POST['merchant_order_id'] . $_POST['total']);
    if (strtolower($key) != strtolower($valid)) {
      uc_order_comment_save($order->order_id, 0, t('Attempted unverified nochex completion for this order.'), 'admin');
    }
  }

  $order->billing_street1 = $_POST['street_address'];
  $order->billing_street2 = $_POST['street_address2'];
  $order->city = $_POST['city'];
  $order->billing_postal_code = $_POST['zip'];
  $order->billing_phone = $_POST['phone'];

  $zone_id = db_result(db_query("SELECT zone_id FROM {uc_zones} WHERE zone_code LIKE '%s'", $_POST['state']));
  if (!empty($zone_id)) {
    $order->billing_zone = $zone_id;
  }

  $country_id = db_result(db_query("SELECT country_id FROM {uc_countries} WHERE country_name LIKE '%s'", $_POST['country']));
  if (!empty($zone_id)) {
    $order->billing_country = $country_id;
  }

  if (strtolower($_POST['email']) !== strtolower($order->primary_email)) {
    uc_order_comment_save($order->order_id, 0, t('Customer used a different e-mail address during payment: !email', array('!email' => check_plain($_POST['email']))), 'admin');
  }

  if ($_POST['credit_card_processed'] == 'Y' && is_numeric($_POST['total'])) {
    $comment = t('Paid by !type, nochex.com order #!order.', array('!type' => $_POST['pay_method'] == 'CC' ? t('credit card') : t('echeck'), '!order' => check_plain($_POST['order_number'])));
    uc_payment_enter($order->order_id, 'nochex', $_POST['total'], 0, NULL, $comment);
  }
  else {
    drupal_set_message(t('Your order will be processed as soon as your payment clears at nochex.com.'));
    uc_order_comment_save($order_id, 0, t('!type payment is pending approval at nochex.com.', array('!type' => $_POST['pay_method'] == 'CC' ? t('Credit card') : t('eCheck'))), 'admin');
  }

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

  $output .= uc_cart_complete_sale($order);

  // Add a comment to let sales team know this came in through the site.
  uc_order_comment_save($order->order_id, 0, t('Order created through website.'), 'admin');

  print $output;
  exit();
}