[Drupal] How to insert new order to SAP Business One using B1WebAPI in Drupal

| | 2 min read

SAP Business one helps to manage sales, customer relationships and financial operations in our Drupal site. We had one of our eCommerce Drupal site integrated with SAP and the requirement was to pass the order details to SAP Business one. We used Zedsuite integration toolkit to connect Drupal with SAP. Read on to know more.

Order data is handled in SALES-AR module of SAP. Zedsuite integration toolkit provides an API which has prebuilt function for insertion. Insertion is handled by accessing the order object provided by B1WebAPI. The order details needs to be posted in XML format(provided by B1WebAPI) to B1WebAPI which then transfers the order object to SAP. We posted the XML file to B1WebAPI using CURL script. Our Drupal site was built using ubercart so the order details needed to be passed to SAP in hook_uc_checkout_complete.

In hook_uc_checkout_complete pass the required order details in variables and build the XML file using theme function(You would need to create custom theme to retrieve the XML data). After retrieving the XML format and the URL post data to B1WebAPI. Have a look at the code below.

function mymodule_uc_checkout_complete($order, $account) {
  $variables['uid'] = $uid;
  $variables['oid'] = $order->order_id;
  $variables['created'] = $order->created;
  $variables['user_display_link'] = '/{B1WebAPI_URL}/' . $uid . '.aspx';
  $variables['user_edit_link'] = '/{B1WebAPI_URL}/' . $uid. '.aspx';
  $variables['name'] = $name;
  $variables['total'] = $order->order_total;
  $variables['payment_method'] = $order->payment_method;
  ...
  ...
  ...
  
  $xml = theme('mymodule_order_xml', $variables);
  //Connect to B1WebAPI.
  mymodule_postxml_b1webapi($xml, $url);  
}

/**
 * Function to connect to B1WebAPI using CURL.
 * 
 * @param $xml
 *   XML for creating content in B1WebAPI.
 * @param $url
 *   url to access the B1WebAPI.
 */
function mymodule_postxml_b1webapi($xml, $url) {

  //Connection to B1WebAPI using CURL script.
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POST,TRUE);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array (
    'Content-type: application/xml', 
    'Content-length: ' . strlen($xml)
  ));
  curl_exec($ch);
  
  //Close connection to B1WebAPI using CURL.
  curl_close($ch);
}

Hope this helps. Contact us if you are looking out to integrate your Drupal site with SAP.