eCommerce integration case study – OSCommerce + Gestix API

Intended for web programmers, these code snippets handle, respectively, gathering the order from the oscommerce database, and posting the order to the Gestix business support system through the Gestix Webshop API.

Code Snippets

Gather an order from OSCommerce

function gnet_GetOrder( $order_id )
{ // register an order to Gestix
global $gnet_aAuth, $gnet_aConf;
 
 // get customer information
 $customer_query= tep_db_query("SELECT customers_id, billing_name, billing_company, billing_nif, billing_street_address, billing_suburb, billing_postcode, billing_city, billing_country, delivery_street_address, delivery_suburb, delivery_postcode, delivery_city, delivery_country, payment_method FROM " . TABLE_ORDERS . " WHERE orders_id='" . $order_id . "'");
 $customer_rows= tep_db_num_rows($customer_query);
 if ( $customer_rows != 1 )
 {
 echo '<pre>gnet-wsi.php: gnet_GetInvoice(): $gnet_customer_rows= ' . $customer_rows . '</pre>';
 exit(1);
 }
 $customer= tep_db_fetch_array( $customer_query );
 
 $entity_query= tep_db_query("SELECT customer_id, ent_id FROM gnet_customers_entities WHERE customer_id='" . $customer['customers_id'] . "'");
 $entity_rows= tep_db_num_rows($entity_query);
 
 /* According to GTM#739,
 when placing an order with a different billing address,
 the entity file shall not be modified in Gestix.
 Nevertheless, since no two entities can share the same taxpayerid,
 even if we don't supply ent_id to Gestix, it will modify the entity file
 if the taxpayerid matches.
 if ( $entity_rows == 0 )
 {
 $DocHeader['ent_id']= '';
 }
 else
 {
 $entity= tep_db_fetch_array( $entity_query );
 $DocHeader['ent_id']= $entity['ent_id'];
 }
 */
 
 $DocHeader['ent_name']= $customer['billing_name'];
 $DocHeader['ent_name2']= $customer['billing_company'];
 $DocHeader['ent_taxid']= $customer['billing_nif'];
// Billing address
 $DocHeader['adr_1']= $customer['billing_street_address'];
 $DocHeader['adr_2']= $customer['billing_suburb'];
 $DocHeader['adr_3']= $customer['billing_postcode'] . ' ' . $customer['billing_city'];
 $DocHeader['adr_4']= $customer['billing_country'];
 
 // Shipping address
 $DocHeader['sadr_1']= $customer['delivery_street_address'];
 $DocHeader['sadr_2']= $customer['delivery_suburb'];
 $DocHeader['sadr_3']= $customer['delivery_postcode'] . ' ' . $customer['delivery_city'];
 $DocHeader['sadr_4']= $customer['delivery_country'];
 
 // get order's products
 $order_query= tep_db_query("SELECT products_model, products_name, products_price, products_quantity, products_tax, final_price FROM " . TABLE_ORDERS_PRODUCTS . " WHERE orders_id='" . $order_id . "'");
 $order_rows= tep_db_num_rows($order_query);
 if ( $order_rows < 1 )
 {
 echo '<pre>gnet-wsi.php: gnet_GetOrder(): order_rows= ' . $order_rows . ', order_id= ' . $order_id . '</pre>';
 exit(1);
 }
 
 // cache known articles from Gestix
 $gnet_articles= gnet_GetResponse( gnet_GetArticleList(), $gnet_aConf['LongQueryTimeout'] );
 
 for ( $row= 0; $row < $order_rows; $row++ ) // for every product in order
 {
 $order= tep_db_fetch_array($order_query);
 $article_found= false;
 
 for ( $article= 0; $article < $gnet_articles['nr_of_items']; $article++ ) // for every known article
 {
 if ( $order['products_model'] == ltrim($gnet_articles['kit_id'.$article]) ) // if it matches
 {
 $Items[$row]['dit_type'.$row]= 'P';
 $Items[$row]['dit_kit_id'.$row]= $order['products_model']; // 13 chars
 $Items[$row]['dit_descr'.$row]= $order['products_name']; // 64 chars
 
 $article_found= true;
 break; // breakfor $article
 }
 }
 
 if ( $article_found == false ) // What are we supposed to do in this situations ? Answer by JG: Do exactly this!
 {
 // NOTE: We're NOT supposed to create articles in Gestix in this implementation
 // so, i'll just create a text line with model and name concatenated
 $Items[$row]['dit_type'.$row]= 'T';
 $Items[$row]['dit_kit_id'.$row]= '';
 $Items[$row]['dit_descr'.$row]= $order['products_model'] . ' ' . $order['products_name']; // 13+64=77 chars
 }
 
 $Items[$row]['dit_qty'.$row]= $order['products_quantity'];
 // this site goes like: unit_price * tax * quantity
 $Items[$row]['dit_up'.$row]= round( $order['products_price'] * ( 1 + ( $order['products_tax'] / 100 ) ), 2 );
 $Items[$row]['dit_tax'.$row]= $order['products_tax'];
 $Items[$row]['dit_s_taxinc']= '1';
 
 }
 
 // Shipping method and expenses
 $shipping_query= tep_db_query("SELECT title, value, class FROM " . TABLE_ORDERS_TOTAL . " WHERE orders_id='" . $order_id . "'");
 $shipping_rows= tep_db_num_rows($shipping_query);
 for ($row= 0; $row < $shipping_rows; $row++)
 {
 $shipping= tep_db_fetch_array($shipping_query);
 if ($shipping['class'] == 'ot_shipping')
 {
 $line= 0;
 $shipping_title= gnet_StringWrap( $shipping['title'] );
 while ( $shipping_title[$line] != '' )
 {
 $order_rows++;
 $Items[$order_rows]['dit_type'.$order_rows]= 'T';
 $Items[$order_rows]['dit_descr'.$order_rows]= $shipping_title[$line++];
 }
 
 if ( $shipping['value'] != 0 ) // pickup at store is a shipping method without costs
 {
 $Items[$order_rows]['dit_qty'.$order_rows]= '1';
 $Items[$order_rows]['dit_up'.$order_rows]= $shipping['value'];
 $Items[$order_rows]['dit_tax'.$order_rows]= '0'; // tax included in shipping values
 $Items[$order_rows]['dit_s_taxinc'.$order_rows]= '1';
 }
 
 $order_rows++;
 break;
 }
 }
 
 // Payment method
 $order_rows++;
 $Items[$order_rows]['dit_type'.$order_rows] = 'T';
 $Items[$order_rows]['dit_descr'.$order_rows] = $customer['payment_method'];
 
 // Comments
 $order_rows++;
 $comments_query= tep_db_query("SELECT comments FROM " . TABLE_ORDERS_STATUS_HISTORY . " WHERE orders_id='" . $order_id . "'");
 $comments_rows= tep_db_num_rows($comments_query);
 if ($comments_rows != 0)
 {
 $comments= tep_db_fetch_array($comments_query);
 $line= 0;
 $comments_line= gnet_StringWrap( $comments['comments'] );
 while ( $comments_line[$line] != '' )
 {
 $Items[$order_rows]['dit_type'.$order_rows]= 'T';
 $Items[$order_rows]['dit_descr'.$order_rows]= $comments_line[$line++];
 $order_rows++;
 }
 }
 
 // prepare the authentication fields
 // $gnet_aAuth['diag']= '1'; // use 0 or 1 (3 is forbiden)
 $gnet_aAuth['command']= 'order';
 //$gnet_aAuth['ordinal_number']= (1 + time()) % 100000000;
 
 $DocHeader['dhd_ser']= $gnet_aConf['DocOrder'];
 $DocHeader['dhd_ref']= $order_id;
 $DocHeader['lines']= $order_rows;
// POST the form to Gestix
 $gnet_aAuth['ordinal_number']= gnet_PostForm( $DocHeader, $Items, $order_rows );
// Wait for the answer
 $ReturnFields= gnet_GetResponse( $gnet_aAuth['ordinal_number'], $gnet_aConf['Timeout'] );
 
 if ( $ReturnFields === false )
 {
 echo '<pre>gnet-wsi.php: gnet_GetOrder(): Timeout waiting for answer: ' . $gnet_aAuth['ordinal_number'] . '</pre>';
 exit(1);
 }
 else // something came back
 {
 if (count($ReturnFields) > 0 && isset($ReturnFields['response']))
 {
 if (substr($ReturnFields['response'], 0, 2) != 'ok')
 {
 $Error= $ReturnFields['response'];
 print '<pre>gnet-wsi.php: gnet_GetOrder(): Gestix document error</pre>';
 print '<pre>' . $Error . '</pre>';
 exit(1);
 }
 else // response ok
 {
 // fill in gnet_customers_entities if needed
 if ( $entity_rows == 0 )
 {
 tep_db_query("INSERT INTO gnet_customers_entities ( customer_id, ent_id ) VALUES (" . $customer['customers_id'] . ", " . $ReturnFields['ent_id'] . ")");
 }
 }
 }
 }
} // endfunction gnet_GetOrder

Post an order to Gestix

// gnet-postform.php
// Copyright 2008 Westix-ti.com
// Writen by Rui Ferreira @ www.gestix.com 
function gnet_PostForm( $DocHeader= array(), $Items= array(), $Lines= 0)
{
// send a post to Gestix; return ordinal number
  global $gnet_aAuth; // see Gestix Webshop integration guide
 
 // generate a random number
 $gnet_aAuth['ordinal_number']= rand( 1, 98765432);
 // Set authentication
 $ch= curl_init();
 curl_setopt($ch, CURLOPT_URL, $gnet_aAuth['gestix_url']);
 curl_setopt($ch, CURLOPT_POST, 1);
 $PostOutput= http_build_query($gnet_aAuth);
 
 // Set document header, if any
 if ( count($DocHeader) != 0 )
 {
 $PostOutput .= '&';
 $PostOutput .= http_build_query($DocHeader);
 }
 
 // Set item lines, if any
 for ($row= 0; $row < $Lines; $row++)
 {
 $PostOutput .= '&';
 $PostOutput .= http_build_query($Items[$row]);
 }
 
 // Send all
 curl_setopt($ch, CURLOPT_POSTFIELDS, $PostOutput);
 $output= curl_exec($ch);
 curl_close($ch);
 
 return $gnet_aAuth['ordinal_number'];
} // endfunction gnet_PostFrom


 

Deixe uma resposta/Leave a reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s