Integrating NetSuite Checkout with Drupal Commerce

Submitted by Brandon Cone on 04/22/2013 - 10:01:am

Drupal Commerce is a powerful ecommerce system.  However, if you already have an ecommerce provider, it may not make sense to stand up a replica store using Drupal Commerce just to sell online what you already sell online.  

We ran into this scenario recently when building a website for a client who wanted a Drupal site and storefront but wanted to finalize checkout and payment through their NetSuite ecommerce portal.

The process for this is actually pretty straightforward once you know the requirements and expectations from NetSuite.  We installed Drupal Commerce and the commerce_product, commerce_cart, and commerce_checkout sub modules.  Below are the steps required to get your cart items to submit to NetSuite for checkout and payment.

1. Each NetSuite product has a NetSuite product id which you may or may not use as the product's SKU number.  This might also be a separate field as an attribute of the product.  However you choose to do it, you need to configure your product so that it contains its NetSuite Product ID somehow.

2. Create and enable products for sale on your site.  If you're new to Commerce, you'll notice that it works differently than UberCart in that products and nodes are separate entities referenced through entity reference fields.  To sell a product you need to create the product and then displayit on your site through a node

3. Redirect the shopping cart's "Checkout" button to use your submit handler (bypassing the Commerce Checkout and Payment steps).

  1. /**
  2.  * Implements hook_form_alter().
  3.  */
  4. function my_module_form_alter(&$form, &$form_state, $form_id) {
  5.   /**************************
  6.   * Change submit handler for       *
  7.   * checkout button on cart page. *
  8.   ************************* */
  9.   if (strpos($form_id, 'views_form_commerce_cart_form_') === 0) {
  10.     $form['actions']['checkout']['#submit'] = array('my_module_checkout_submit');
  11.     // Netsuite expects that the its cart is loaded on this page to accept the items.
  12.     //  Update the below URL with your Netsuite IDs (c=[my id] and n=[netsuite code]).
  13.     $form['cart_contents'] = array(
  14.       '#markup' => '<iframe height="0" width="0"
  15.     );
  16.   }
  17. }

4. Implement your submission handler which will submit the items to NetSuite.

  1. function my_module_checkout_submit($form, $form_state) {
  2. /*
  3. NetSuite expects the URLs to look differently for single or multiple item submissions.
  4. Single item
  5.  
  6. Multiple item
  7. /app/site/backend/additemtocart.nl?c=427310&n=4&buyid=multi&multi=[internal id],[quantity];[internal id 2 ],[quantity 2]
  8.  
  9. */
  10.   // Tail contains your NetSuite account information
  11.   $tail = 'c=[netsuite_id]&n=[netsuite_code]&buyid=';
  12.  
  13.   //order provides order number, order total, email address, user id (use to get customer name)
  14.   $order = $form_state['order'];
  15.   //line items provides sku, individual price, and currenct_code
  16.   $line_items = $form_state['line_items'];
  17.   if (count($line_items) > 1) {
  18.     $tail .= 'multi&multi=';
  19.     foreach ($line_items as $id => $item) {
  20.       $product = commerce_product_load($item->commerce_product['und'][0]['product_id']);
  21.       $tail .= urlencode($product->field_ns_product_id['und'][0]['value']) .',' .urlencode(intval($item->quantity)) .';';
  22.     }
  23.   }
  24.   else {
  25.     $item = array_pop($line_items);
  26.     $product = commerce_product_load($item->commerce_product['und'][0]['product_id']);
  27.     $tail .= urlencode($product->field_ns_product_id['und'][0]['value']) .'&qty=' .urlencode(intval($item->quantity));
  28.   }
  29.    $url .= $tail;
  30.   // We don't want to redirect directly from here so set a session variable which
  31.   // we can use on the next page load to redirect us to the NetSuite page.
  32.   $_SESSION['checkout_url'] = $url;
  33. }
  34.  
  35. /**
  36.  * Implements hook_page_alter()
  37.  */
  38. function my_module_page_alter(&$page) {
  39.   elseif (isset($_SESSION) && array_key_exists('checkout_url',$_SESSION)) {
  40.     $url = $_SESSION['checkout_url'];
  41.     unset($_SESSION['checkout_url']);
  42.     // Redirect to the NetSuite checkout url and kill the current process.
  43.     drupal_goto($url);
  44.   }
  45. }

That's all you need to do to submit products from your Drupal site to a NetSuite ecommerce store for purchase.  Happy selling!

Blog Photo Credited to Helena Jacoba