1

I've tested Ubercart with Sagepay sandbox and it's very inconvenience to enter every time the payment. Another disadvantage is that when working on internal hosts, the redirection URL from external payment gateways never works, therefore when testing, payment is never completed.

How it's possible to bypass payment after checkout and make the order completed for testing purposes? Is there any option or module?

4 Answers 4

2

One way to do this using the combination of ubercart custom payment module and ubercart discount coupon module, i.e creating a coupon of 100% discount and to bypass the payment process you will need the ubercart custom payment module (by default it provides the rule that can use it).

Ubercart Discount Coupons: Coupons can apply either a fixed price discount or a percentage discount to the order subtotal or to selected product prices.

Coupons are configured at Store administration > Coupons. Global settings for the module are configured at Store administration > Configuration > Coupon module settings.

Ubercart Custom Payment module:

provides a "Free Order" payment method, and a default Rules configuration is provided to enable this.

1

Drupal 6

You can use the following PHP code:

<?php

  // See: http://www.ubercart.org/forum/development/17963/programmatically_submit_entire_ubercart_order_using_drupal_execute_debugging

  /*
   * Node IDs for the products:
   *   4844 - My product
   */

  $nid = 4844;

  $form_state = array(
    'values' => array(
      'nid' => $nid,
      'qty' => 1,
    ),
  );
  $node = node_load($nid);
  drupal_execute("uc_product_add_to_cart_form", $form_state, $node);

  // Load the relevant include files for checkout, as normally done by hook_menu
  module_load_include('inc', 'uc_cart', 'uc_cart.pages');

  $uc_cart_checkout_form_state = array();

  // Define billing information in the form_state variable
  // Note: When using Sagepay in test mode, to simulate successful payment the first line of the address must be '88' and the postcode should be '412'.

  // Customer e-mail
  $uc_cart_checkout_form_state['values']['panes']['customer']['primary_email']         = '[email protected]';

  // Billing details
  $uc_cart_checkout_form_state['values']['panes']['billing']['billing_first_name']     = 'Test';
  $uc_cart_checkout_form_state['values']['panes']['billing']['billing_last_name']      = 'Order';
  $uc_cart_checkout_form_state['values']['panes']['billing']['billing_address_select'] = '0';
  $uc_cart_checkout_form_state['values']['panes']['billing']['billing_company']        = 'Company LTD';
  $uc_cart_checkout_form_state['values']['panes']['billing']['billing_street1']        = '88 Test Street';
  $uc_cart_checkout_form_state['values']['panes']['billing']['billing_street2']        = '';
  $uc_cart_checkout_form_state['values']['panes']['billing']['billing_city']           = 'London';
  $uc_cart_checkout_form_state['values']['panes']['billing']['billing_country']        = '826';
  $uc_cart_checkout_form_state['values']['panes']['billing']['billing_zone']           = '2806';
  $uc_cart_checkout_form_state['values']['panes']['billing']['billing_postal_code']    = '412';
  $uc_cart_checkout_form_state['values']['panes']['billing']['billing_phone']          = '0123';
  $uc_cart_checkout_form_state['values']['panes']['uc_termsofservice_agreement_checkout']['tos_agree']['agreed'] = 'agreed';

  // VAT and Discounts
  $uc_cart_checkout_form_state['values']['panes']['vat_number']['vat_number']                  = '';
  $uc_cart_checkout_form_state['values']['panes']['uc_discounts']['uc-discounts-codes']        = '';
  $uc_cart_checkout_form_state['values']['panes']['uc_discounts']['uc-discounts-placeholder']  = '';
  $uc_cart_checkout_form_state['values']['panes']['uc_discounts']['uc-discounts-button']       = '';

  // Payment details
  $uc_cart_checkout_form_state['values']['panes']['payment']['current_total'] = NULL;
  $uc_cart_checkout_form_state['values']['panes']['payment']['payment_method'] = 'free_order';
  // $uc_cart_checkout_form_state['values']['panes']['payment']['payment_method'] = 'uc_sagepayserver'; // enable when testing SagePay payments
  // Etc etc etc

  // Define the credit card information in the $_POST directly.
  // See uc_payment_method_credit($op = 'cart-process') for why this is necessary.
  $_POST['cc_type']      = 'uc_credit_visa';
  $_POST['cc_owner']     = 'Test Order';
  $_POST['cc_number']    = '4929000000006';
  $_POST['cc_exp_month'] = '12';
  $_POST['cc_exp_year']  = '2018';
  $_POST['cc_cvv']       = '123';

  // Programmatically emulate the hitting of the 'Review order' button
  $uc_cart_checkout_form_state['values']['op'] = t('Review order');
  drupal_execute('uc_cart_checkout_form', $uc_cart_checkout_form_state);

  if ($uc_cart_checkout_form_state['redirect'] == 'cart/checkout/review') {
    // We're good to move forward!!
    $uc_cart_checkout_review_form_state = array();

    // Define the session's sescrd info in the $_POST directly
    // See uc_credit_cart_review_pre_form_submit() for why this is necessary.
    $_POST['sescrd'] = base64_encode($_SESSION['sescrd']);

    //Programmatically emulate the hitting of the 'Submit order' button
    $uc_cart_checkout_review_form_state['values']['op'] = t('Submit order');
    drupal_execute('uc_cart_checkout_review_form', $uc_cart_checkout_review_form_state);

    if ($uc_cart_checkout_review_form_state['redirect'] == 'cart/checkout/complete') {
      // The order checks out -- go ahead and complete it using uc_cart_checkout_complete()!
      drupal_set_message(uc_cart_checkout_complete());
    }
  }

?>

The above file can be pasted into: order-6.php and then you can use it as a drush command as below:

drush -v scr order-6.php

Optionally you can use -u user to testing the orders using different user. For Drupal 7, mainly you have to change drupal_execute() with drupal_form_submit().


Commerce

For Commerce example, please see:

Programmatically creating an order in Drupal Commerce for anonymous users redirecting to payment page

1

Drupal 6

I've defined some custom bypassing function as below:

/**
 * Implementation of hook_menu().
 */
function foo_menu() {

  // URL for user to be directed to on test order completion
  $items['test/order/%/complete'] = array(
    'title' => 'Fake payment bypass.',
    'page callback' => 'foo_test_order_complete',
    'page arguments' => array(2),
    // 'access callback' => variable_get('my_environment', 'live') <> 'live', // disabled on live
    'access callback' => 'user_access',
    'access arguments' => array('administer site configuration'), // Only admin can test that
    'type' => MENU_CALLBACK,
  );

  return $items;
}

/**
 * Test order completion page
 */
function foo_test_order_complete($order_id) {

  if (empty($_SESSION['cart_order'])) {
    $_SESSION['cart_order'] = $order_id;
  }

  $order = uc_order_load($_SESSION['cart_order']);

  // This lets us know it's a legitimate access of the complete page.
  $_SESSION['do_complete'] = TRUE;
  module_load_include('inc', 'uc_cart', 'uc_cart.pages');
  uc_cart_checkout_complete();
  drupal_goto('cart/checkout/complete');

}

So when submit your Review order, after that you can go to:

test/order/12345/complete where 12345 is number of your order or just:

test/order/current/complete to load the order from the session.

These URLs are accessible only by admin, but you can free to extend the permissions as you wish.

0

Drupal 6 & 7

You may add free payment method called 'No payment required' and set-up the right Rules conditions to these payments, e.g.

  • Condition: Order balance comparison; Parameter: Order: [commerce_order], Operator: = 0
  • Based on specific user role (e.g. for admin).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.