/[drupal]/contributions/modules/magento/magento_orders/magento_orders.module
ViewVC logotype

Diff of /contributions/modules/magento/magento_orders/magento_orders.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.1 by mtopolov, Mon May 25 10:30:49 2009 UTC revision 1.2 by mtopolov, Mon Nov 16 10:38:48 2009 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id: magento_orders.module  // $Id: magento_orders.module
3  /**  /**
4    * This module allows to synchronize orders with Magento.   * This module allows to synchronize orders with Magento.
5   */   */
6  define('MAGENTO_ORDERS_PRODUCT_COUNT_PREFIX', 'product_count_');  define('MAGENTO_ORDERS_PRODUCT_COUNT_PREFIX', 'product_count_');
7  define('MAGENTO_ORDERS_PRODUCT_DELETE_PREFIX', 'delete_item_');  define('MAGENTO_ORDERS_PRODUCT_DELETE_PREFIX', 'delete_item_');
# Line 11  define('MAGENTO_ORDERS_DEFAULT_CURRENCY' Line 11  define('MAGENTO_ORDERS_DEFAULT_CURRENCY'
11  define('MAGENTO_ORDERS_PAY_PAL_GATEWAY', 'https://www.sandbox.paypal.com/cgi-bin/webscr');  define('MAGENTO_ORDERS_PAY_PAL_GATEWAY', 'https://www.sandbox.paypal.com/cgi-bin/webscr');
12  define('MAGENTO_ORDERS_THROBBER_IMAGE', drupal_get_path('module', 'magento_orders') . '/files/progress.gif');  define('MAGENTO_ORDERS_THROBBER_IMAGE', drupal_get_path('module', 'magento_orders') . '/files/progress.gif');
13  /**  /**
14  * Implementation of hook_menu().   * Implementation of hook_menu().
15  */   */
16  function magento_orders_menu () {  function magento_orders_menu () {
17  global $user;    global $user;
18          $items['cart/add'] = array(    $items['cart/add'] = array(
19                  'page callback' => 'magento_orders_add_to_cart',        'page callback' => 'magento_orders_add_to_cart',
20                  'access arguments' => array('access content'),        'access arguments' => array('access content'),
21                  'type' => MENU_CALLBACK,        'type' => MENU_CALLBACK,
22          );    );
23          $items['cart/delete'] = array(    $items['cart/delete'] = array(
24                  'page callback' => 'magento_orders_delete_from_cart',        'page callback' => 'magento_orders_delete_from_cart',
25                  'access arguments' => array('access content'),        'access arguments' => array('access content'),
26                  'type' => MENU_CALLBACK,        'type' => MENU_CALLBACK,
27          );    );
28          $items['cart/clear'] = array(    $items['cart/clear'] = array(
29                  'page callback' => 'magento_orders_clear_cart',        'page callback' => 'magento_orders_clear_cart',
30                  'access arguments' => array('access content'),        'access arguments' => array('access content'),
31                  'type' => MENU_CALLBACK,        'type' => MENU_CALLBACK,
32          );    );
33          $items['cart'] = array(    $items['cart'] = array(
34                  'title' => t('Your shopping cart'),        'title' => t('Your shopping cart'),
35                  'page callback' => 'magento_orders_cart',        'page callback' => 'magento_orders_cart',
36                  'access arguments' => array('access content'),        'access arguments' => array('access content'),
37                  'type' => MENU_CALLBACK,        'type' => MENU_CALLBACK,
38          );    );
39          $items['order/checkout'] = array(    $items['order/checkout'] = array(
40          'title' => t('Order Checkout: Billing and Shipping Information'),        'title' => t('Order Checkout: Billing and Shipping Information'),
41                  'page callback' => 'magento_orders_checkout_order',        'page callback' => 'magento_orders_checkout_order',
42                  'access arguments' => array('access content'),        'access arguments' => array('access content'),
43                  'type' => MENU_CALLBACK,        'type' => MENU_CALLBACK,
44          );    );
45          $items['order/checkout_step2'] = array(    $items['order/checkout_step2'] = array(
46          'title' => t('Order Checkout: Payment and Shipment Information'),        'title' => t('Order Checkout: Payment and Shipment Information'),
47                  'page callback' => 'magento_orders_checkout_order_step2',        'page callback' => 'magento_orders_checkout_order_step2',
48                  'access arguments' => array('access content'),        'access arguments' => array('access content'),
49                  'type' => MENU_CALLBACK,        'type' => MENU_CALLBACK,
50          );    );
51          $items['order/checkout_step3'] = array(    $items['order/checkout_step3'] = array(
52          'title' => t('Order Checkout: Order Review'),        'title' => t('Order Checkout: Order Review'),
53                  'page callback' => 'magento_orders_checkout_order_step3',        'page callback' => 'magento_orders_checkout_order_step3',
54                  'access arguments' => array('access content'),        'access arguments' => array('access content'),
55                  'type' => MENU_CALLBACK,        'type' => MENU_CALLBACK,
56          );    );
57          $items['order/response'] = array(    $items['order/response'] = array(
58          'title' => t('Checkout Response'),        'title' => t('Checkout Response'),
59                  'page callback' => 'magento_orders_response',        'page callback' => 'magento_orders_response',
60                  'access arguments' => array('access content'),        'access arguments' => array('access content'),
61                  'type' => MENU_CALLBACK,        'type' => MENU_CALLBACK,
62          );    );
63          $items['order/cancel'] = array(    $items['order/cancel'] = array(
64          'title' => t('Order Cancel'),        'title' => t('Order Cancel'),
65                  'page callback' => 'magento_orders_checkout_cancel',        'page callback' => 'magento_orders_checkout_cancel',
66                  'access arguments' => array('access content'),        'access arguments' => array('access content'),
67                  'type' => MENU_CALLBACK,        'type' => MENU_CALLBACK,
68          );    );
69          $items['order/complete'] = array(    $items['order/complete'] = array(
70          'title' => t('Order Complete'),        'title' => t('Order Complete'),
71                  'page callback' => 'magento_orders_checkout_complete',        'page callback' => 'magento_orders_checkout_complete',
72                  'access arguments' => array('access content'),        'access arguments' => array('access content'),
73                  'type' => MENU_CALLBACK,        'type' => MENU_CALLBACK,
74          );    );
75          return $items;    return $items;
76  }  }
77  /**  /**
78   * Implementation of hook_theme().   * Implementation of hook_theme().
79   */   */
80  function magento_orders_theme() {  function magento_orders_theme() {
81          return array(    return array(
82                  'magento_orders_cart' => array(    'magento_orders_cart' => array(
83                  'arguments' => array('form' => NULL),    'arguments' => array('form' => NULL),
84                  ),    'path' => drupal_get_path('module', 'magento_orders'),
85          );    'template' => 'templates/cart-page',
86      ),
87      'magento_orders_cart_block' => array(
88      'arguments' => array('message' => NULL, 'products' => NULL, 'quote' => NULL, 'currency' => NULL),
89      'path' => drupal_get_path('module', 'magento_orders'),
90      'template' => 'templates/cart-block',
91    
92      ),
93      );
94  }  }
95  /**  /**
96   * Implementation of hook_nodeapi).   * Implementation of hook_nodeapi().
97   */   */
98  function magento_orders_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {  function magento_orders_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
99          if ($node->type == 'product' && $op == 'view') {    if ($node->type == 'product' || $node->type == 'product_offer' && $op == 'view') {
100                  $path = drupal_get_path('module', 'magento_orders');      $path = drupal_get_path('module', 'magento_orders');
101                  drupal_add_js($path . '/js/cart.js');      drupal_add_js($path . '/js/cart.js');
102          }      $data = array('magento' => array('add_url' => url('cart/add'), 'trobber_image' => MAGENTO_ORDERS_THROBBER_IMAGE));
103        drupal_add_js($data, 'setting');
104      }
105  }  }
106  /**  /**
107   * Implementation of hook_link().   * Implementation of hook_link().
108   */   */
109  function magento_orders_link($type, $node = 0, $teaser = 0) {  //function magento_orders_link($type, $node = 0, $teaser = 0) {
110          if (($type == 'node')&&($node->type=='product')) {  //      if (($type == 'node')&&($node->type=='product')) {
111                  $links=array();  //              $links=array();
112                  if ($node->field_stock_availability[0]['value']== 1) {  //              if ($node->field_stock_availability[0]['value']== 1) {
113                          $links['add_to_cart'] = array(  //                      $links['add_to_cart'] = array(
114                          'title'     => '<div class="add-to-cart-link-title">'. t('Add to Cart').'</div>' . ' <span id="throbber_' . $node->field_product_id[0]['value'] . '"></span>',  //                      'title'     => '<div class="add-to-cart-link-title">'. t('Add to Cart').'</div>' . ' <span id="throbber_' . $node->field_product_id[0]['value'] . '"></span>',
115                                  'attributes' => array('class' => 'add-to-cart-link', 'onclick' => '(add_item(' . $node->field_product_id[0]['value'] . ',"' . urlencode($node->title) . '","' . url('cart/add') . '","' . MAGENTO_ORDERS_THROBBER_IMAGE . '"))'),  //                              'attributes' => array('class' => 'add-to-cart-link', 'onclick' => '(add_item(' . $node->field_product_id[0]['value'] . ',"' . urlencode($node->title) . '","' . url('cart/add') . '","' . MAGENTO_ORDERS_THROBBER_IMAGE . '"))'),
116                          'html'      => TRUE,  //                      'html'      => TRUE,
117                          );  //                      );
118                  }  //              }
119        return $links;  //      return $links;
120          }  //      }
121  }  //}
122  /**  /**
123   * Delete item from cart and reload cart block.   * Delete item from cart and reload cart block.
124   */   */
125  function magento_orders_delete_from_cart() {  function magento_orders_delete_from_cart() {
126          $id = $_POST['id'];    global $user;
127          // if this item is in cart - remove it    $customer = magento_api_customer_get_customer_id_by_email(array('email' => $user->mail));
128          if (is_numeric($id) && !empty($_SESSION['order']['products'][$id]) ) {    $_SESSION['order']['magento_user'] = $customer;
129                  unset($_SESSION['order']['products'][$id]);  
130          }    $id = $_POST['id'];
131          $_SESSION['order']['quote'] = magento_api_quote_create($_SESSION['order']);    // if this item is in cart - remove it
132  return print magento_orders_show_cart(NULL, urldecode($_POST['title']));    if (is_numeric($id) && !empty($_SESSION['order']['products'][$id]) ) {
133        unset($_SESSION['order']['products'][$id]);
134      }
135      $_SESSION['order']['quote'] = magento_orders_create_quote($_SESSION['order']); //magento_api_quote_create
136      return print magento_orders_show_cart(NULL, urldecode($_POST['title']));
137  }  }
138  /**  /**
139   * Clear cart and reload cart block   * Clear cart and reload cart block
140   */   */
141  function magento_orders_clear_cart() {  function magento_orders_clear_cart() {
142          unset($_SESSION['order']);    unset($_SESSION['order']);
143  return print magento_orders_show_cart();    return print magento_orders_show_cart();
144  }  }
145  /**  /**
146   * Add item to cart and reload cart block.   * Add item to cart and reload cart block.
147   */   */
148  function magento_orders_add_to_cart() {  function magento_orders_add_to_cart() {
149          $err = FALSE;    global $user;
150          // If qty is numeric and more that null - create quote in Magento    if ($user->uid) {
151          if (is_numeric($_POST['qty']) && $_POST['qty'] > 0) {      $customer = magento_api_customer_get_customer_id_by_email(array('email' => $user->mail));
152                  // Get product ID and Qty      $_SESSION['order']['magento_user'] = $customer;
153                  $_SESSION['order']['products'][$_POST['id']]['id'] = $_POST['id'];    }
154                  $_SESSION['order']['products'][$_POST['id']]['qty'] = $_SESSION['order']['products'][$_POST['id']]['qty'] + $_POST['qty'];  
155                  // Try to create quote in Magento    $err = FALSE;
156                  $quote = magento_api_quote_create($_SESSION['order']);    // If qty is numeric and more that null - create quote in Magento
157                  if ($quote == FALSE) {    if (is_numeric($_POST['qty']) && $_POST['qty'] > 0) {
158                          // Failed to create quote    // Get product ID and Qty
159                          $err = TRUE;      $_SESSION['order']['products'][$_POST['id']]['id'] = $_POST['id'];
160                          unset($_SESSION['order']['products'][$_POST['id']]);      $_SESSION['order']['products'][$_POST['id']]['qty'] = $_SESSION['order']['products'][$_POST['id']]['qty'] + $_POST['qty'];
161                          return print(t('There was some error during creating quote in Magento'));      // Try to create quote in Magento
162                  }else {      //$quote = magento_api_quote_create($_SESSION['order']);
163                          // if successfully create new quote in Magento - update product Cart      $quote = magento_orders_create_quote($_SESSION['order']);
164                          $_SESSION['order']['quote'] = $quote;      if ($quote == FALSE) {
165                          $_SESSION['order']['products'][$_POST['id']] = array(      // Failed to create quote
166                                  'id' => $_POST['id'],        $err = TRUE;
167                                  'amount' => $quote['products'][$_POST['id']]['price'],        unset($_SESSION['order']['products'][$_POST['id']]);
168                                  'qty' => $quote['products'][$_POST['id']]['qty'],        return print(t('There was some error during creating quote in Magento'));
169                                  'title' => urldecode($_POST['title']),      }else {
170                          );      // if successfully create new quote in Magento - update product Cart
171                  }        $_SESSION['order']['quote'] = $quote;
172                  // Get Currency from Magento        $_SESSION['order']['products'][$_POST['id']] = array(
173                  if (empty($_SESSION['order']['currency'])) {            'id' => $_POST['id'],
174                          $StoreId = magento_stores_get_default_store(magento_stores_get_default_website());            'original_price' => $quote['products'][$_POST['id']]['original_price'],
175                          $currency = magento_api_config_info('currency/options/base',$StoreId);            'amount' => $quote['products'][$_POST['id']]['price'],
176                          $_SESSION['order']['currency'] = $currency ? $currency : MAGENTO_ORDERS_DEFAULT_CURRENCY;            'qty' => $quote['products'][$_POST['id']]['qty'],
177                  }            'title' => urldecode($_POST['title']),
178                  return print magento_orders_show_cart(urldecode($_POST['title']));        );
179          }else return print(t('Qty must be a number and more that null'));      }
180        // Get Currency from Magento
181        if (empty($_SESSION['order']['currency'])) {
182          $StoreId = magento_stores_get_default_store(magento_stores_get_default_website());
183          $currency = magento_api_config_info('currency/options/base',$StoreId);
184          $_SESSION['order']['currency'] = $currency ? $currency : MAGENTO_ORDERS_DEFAULT_CURRENCY;
185        }
186        return print magento_orders_show_cart(urldecode($_POST['title']));
187      }else return print(t('Qty must be a number and more that null'));
188  }  }
189  /**  /**
190   * Display shopping cart for block.   * Display shopping cart for block.
191   */   */
192  function magento_orders_show_cart($added_product = NULL, $deleted_product = NULL) {  function magento_orders_show_cart($added_product = NULL, $deleted_product = NULL) {
193          $path = drupal_get_path('module', 'magento_orders');    $path = drupal_get_path('module', 'magento_orders');
194          drupal_add_js($path . '/js/cart.js');    drupal_add_js($path . '/js/cart.js');
195          // if some item was added - show message about it    $data = array('magento' => array('clear_url' => url('cart/clear')));
196          if ($added_product != NULL) {    drupal_add_js($data, 'setting');
197                  $output .= ('<div class="messages status">' . $added_product . t(' was successfully added to your cart') . '</div>');    $messages = array();
198          }    // if some item was added - show message about it
199          // if some item was deleted - show message about it    if ($added_product != NULL) {
200          if ($deleted_product != NULL) {      $messages[] = t('!product was successfully added to your cart', array('!product' => $added_product));
201                  $output .= ('<div class="messages status">' . $deleted_product . t(' was successfully deleted from your cart') . '</div>');    }
202          }    // if some item was deleted - show message about it
203          $header = array(t('Name'), t('Price'), t('Qty'), t('Total'));    if ($deleted_product != NULL) {
204          $rows = array();      $message[] = t('!product was successfully deleted from your cart', array('!product' => $deleted_product));
205          if (!empty($_SESSION['order']['products'])) {    }
206      if (!empty($_SESSION['order']['products'])) {
207                  foreach ($_SESSION['order']['products'] as $id => $item) {      $products = $_SESSION['order']['products'];
208                          $row = array();    }
209                          $row[] = $item['title'];    if ($_SESSION['order']['quote']) {
210                          $row[] = $item['amount'] . ' ' . $_SESSION['order']['currency'];      $quote = $_SESSION['order']['quote'];
211                          $row[] = $item['qty'];    }
212                          $row[] = ($item['qty'] * $item['amount']) . ' ' . $_SESSION['order']['currency'];    if ($_SESSION['order']['currency']) {
213                          $row[] = '<a onclick=(delete_item(' . $id . ',"' . urlencode($item['title']) . '","' . url('cart/delete') . '"))>X</a>';      $currency = t($_SESSION['order']['currency']);
214                          $rows[] = $row;    }
215                  }    return theme('magento_orders_cart_block', $messages, $products, $quote, $currency);
         $output .= theme('table', $header, $rows, array('class' => 'show-table'));  
         // Show cart totals  
         if ($_SESSION['order']['quote']) {  
                 $output .= '<div id = "cart_subtotal">Subtotal: ' . $_SESSION['order']['quote']['subtotal'] . ' ' . $_SESSION['order']['currency'] . '</div>';  
                 if (!empty($_SESSION['order']['quote']['discount_amount'])) $output .= '<div id = "cart_discount">Discount: -' . $_SESSION['order']['quote']['discount_amount'] . ' ' . $_SESSION['order']['currency'] . '</div>';  
                 if (!empty($_SESSION['order']['quote']['taxes'])) {  
                         foreach ($_SESSION['order']['quote']['taxes'] as $id => $tax) {  
                                 $output .= '<div id = "cart_tax">Tax (' . $tax['percent'] . '%): ' . $tax['amount']  . ' ' . $_SESSION['order']['currency'] . '</div>';  
                         }  
                 }  
                 $output .= '<div id = "cart_total">Total: ' . $_SESSION['order']['quote']['grand_total'] . ' ' . $_SESSION['order']['currency'] . '</div>';  
         }  
         $output .= '<br>' . l(t('Proceed to Checkout'),'order/checkout');  
         $output .= '<br><a onclick=(clear_cart("' . url('cart/clear') . '"))>Clear Cart</a>';  
         } else $output = t('There is no items in your shopping cart');  
         return $output;  
216  }  }
217  /**  /**
218   * Implementation of hook_block().   * Implementation of hook_block().
219   */   */
220  function magento_orders_block($op = 'list', $delta = 0, $edit = array()) {  function magento_orders_block($op = 'list', $delta = 0, $edit = array()) {
221  global $user;    switch ($op) {
222          switch ($op) {      case 'list':
223                  case 'list':        $block['user']['info'] = t("Shopping Cart");
224                          $block['user']['info'] = t("Shopping Cart");        return $block;
225                          return $block;        break;
226                  break;  
227        case 'view':
228                  case 'view':        switch ($delta) {
229                          switch ($delta) {          case 'user':
230                                  case 'user':            $block['subject'] = l(t('Your shopping cart'), 'cart');
231                                          $output = magento_orders_show_cart();            $block['content'] = magento_orders_show_cart();
232                                          return array(            return $block;
233                                  'subject' => l(t('Your shopping cart'),'cart'),        }
234                                  'content' => $output,    }
                         );  
                         break;  
                         }  
         }  
235  }  }
236  /**  /**
237   * FAPI definition for the shopping cart form.   * FAPI definition for the shopping cart form.
# Line 238  global $user; Line 240  global $user;
240   * @see magento_orders_cart_update()   * @see magento_orders_cart_update()
241   * @see theme_magento_orders_cart()   * @see theme_magento_orders_cart()
242   */   */
243  function magento_orders_cart(){  function magento_orders_cart() {
244          $output = drupal_get_form(magento_orders_view_cart_form);    $output = drupal_get_form(magento_orders_view_cart_form);
245          return $output;    return $output;
246  }  }
247  /**  /**
248   * Display a form for shopping cart page.   * Display a form for shopping cart page.
249   *   *
250   * @param $form_state   * @param $form_state
251   *   The form state array.   *   The form state array.
252   * @return   * @return
253   *   An array containing the form elements to be displayed.   *   An array containing the form elements to be displayed.
254   */   */
255  function magento_orders_view_cart_form() {  function magento_orders_view_cart_form() {
256          if (!empty($_SESSION['order']['products'])) {    $form = array();
257                  foreach ($_SESSION['order']['products'] as $id => $item) {    $path = drupal_get_path('module', 'magento_orders');
258                          $form['title'][$id] = array(    drupal_add_js($path . '/js/cart.js');
259                                  '#value' => $item['title'],    if (!empty($_SESSION['order']['products'])) {
260                          );      $countries = cache_get('magento_order_cart_allowed_countries','cache');
261                          $form['amount_' . $id] = array(      if (!$countries) {
262                                  '#value' => $item['amount'] . ' ' . $_SESSION['order']['currency'],        $countries = magento_api_directory_country_list_simple(true);
263                          );        $time = variable_get('magento_products_cache_custom_time', 24);
264                          $form[MAGENTO_ORDERS_PRODUCT_COUNT_PREFIX . $id] = array(        cache_set('magento_order_cart_allowed_countries', $countries,'cache', strtotime ("+". $time ." hour"));
265                                  '#default_value' => $item['qty'],      } else {
266                                  '#type' => 'textfield',        $countries = $countries->data;
267                                  '#size' => 10,      }
268                          );      $form['address']['country_id'] = array (
269                          $form['subtotal_' . $id] = array(          '#type' => 'select',
270                                  '#value' => ($item['qty'] * $item['amount']) . ' ' . $_SESSION['order']['currency'],          '#title' => t('Country'),
271                          );          '#options' => $countries ? $countries : array(0 => t('None avaliable')),
272                          $form[MAGENTO_ORDERS_PRODUCT_DELETE_PREFIX . $id] = array (          '#required' => FALSE,
273                                  '#type' => 'checkbox',          //TODO add setting for this
274                          );          '#default_value' => $_SESSION['order']['shipping_country'] ? $_SESSION['order']['shipping_country'] : 'FR');
275                  }  
276        $form['get_quote'] = array(
277                  $form['submit'] = array(          '#type' => 'submit',
278                          '#value' => l(t('Proceed to Checkout'),'order/checkout'),          '#value' => t('Get a Quote'),
279                  );          '#submit' => array('magento_orders_cart_update'),
280                  $form['update'] = array(      );
281                          '#type' => 'submit',      //TODO this is for Cigale project only
282                          '#value' => t('Update Cart'),      $form['get_quote']['#prefix'] = '<div style="display: none">';
283                          '#submit' => array('magento_orders_cart_update'),      $form['get_quote']['#suffix'] = '</div>';
284                  );  
285                  $form['delete'] = array(  
286                          '#type' => 'submit',      if ($_SESSION['order']['quote']['shipping_rates']) {
287                          '#value' => t('Delete selected items'),        $rates = array();
288                          '#submit' => array('magento_orders_cart_update'),        foreach ($_SESSION['order']['quote']['shipping_rates'] as $rate) {
289                  );          if (isset($rate['price'])) {
290                  $form['clear'] = array(            $rates[$rate['code']] = $rate['carrier_title']." - ".$rate['method_title']." (".sprintf("%01.2f ",$rate['price']).t('EUR').")";
291                          '#value' => l(t('Clear Cart'),'order/cancel'),          }
292                  );        }
293                  $form['#theme'] = 'magento_orders_cart';        $default_val = $_SESSION['order']['shipping_rate'];
294                  $form['#validate'] = array('magento_orders_cart_validate');  
295          } else        if (!empty($rates)) {
296                  $form['message'] = array (          if (count($rates) == 1) {
297                          '#value' => t('There is no items in your shopping cart'),            $rate_codes = array_keys($rates);
298                  );            $default_val = $rate_codes[0];
299  return $form;            $form['address']['shipping_rates'] = array (
300                  '#type' => 'value',
301                  '#title' => t('Shipping rates'),
302                  '#value' => $default_val
303              );
304              // Cover value in div that allow use css to set same position as we have with radios
305              $value = '<div class = "cover">'. $rates[$default_val] .'</div>';
306              $form['address']['shipping_rates'] = array (
307                  '#type' => 'item',
308                  '#prefix' => '<div class = "shipping_rates_single_item">',
309                  '#title' => t('Shipping rates'),
310                  '#value' => $value,
311                  '#suffix' => '</div>',
312              );
313            } else {
314              $rate_codes = array_keys($rates);
315              $default_val = $rate_codes[0];
316              $form['address']['shipping_rates'] = array (
317                  '#type' => 'radios',
318                  '#title' => t('Shipping rates'),
319                  '#options' => $rates,
320                  '#required' => FALSE,
321                  '#default_value' => $default_val
322              );
323    
324              $form['update_total'] = array(
325                  '#type' => 'submit',
326                  '#value' => t('Update total'),
327                  '#submit' => array('magento_orders_cart_update'),
328              );
329            }
330          }
331        }
332    
333        $currency = t($_SESSION['order']['currency']);
334        foreach ($_SESSION['order']['products'] as $id => $item) {
335              // Get item cost excl tax
336          magento_products_cache_full_info_by_id($item['id']);
337              $info = cache_get('product_info_' . $item['id'],'cache');
338    
339          $nid = magento_products_get_nid($item['id']);
340          if (empty($nid))  $nid = magento_products_get_offer_nid($item['id']);
341          $form['products'][$id]['title'] = array(
342              '#value' => l($item['title'], 'node/'.$nid));
343          // Get price excl tax, if exists
344              if ($info->data['price_excl_tax'] && empty($_SESSION['order']['quote']['taxes'])) {
345                    if (floatval($item['amount']) < floatval($item['original_price'])) {
346                            $form['products'][$id]['amount'] = array(
347                                    '#value' => sprintf("%01.2f $currency",$item['amount'] / (1 + $info->data['tax_percent']/100) )
348                            );
349                    $form['products'][$id]['original_price'] = array(
350                                    '#value' => sprintf("%01.2f $currency", $info->data['price_excl_tax'])
351                    );
352                    $item['amount'] = $item['amount'] / (1 + $info->data['tax_percent']/100);
353            }else {
354                    $form['products'][$id]['amount'] = array(
355                                    '#value' => sprintf("%01.2f $currency", $info->data['price_excl_tax'])
356                            );
357                            $item['amount'] = $info->data['price_excl_tax'];
358            }
359              } else {
360                    $form['products'][$id]['amount'] = array(
361              '#value' => sprintf("%01.2f $currency", $item['amount']));
362            if (floatval($item['amount']) < floatval($item['original_price'])) {
363                    $form['products'][$id]['original_price'] = array(
364                '#value' => sprintf("%01.2f $currency",$item['original_price']));
365            }
366              }
367          $form['products'][$id][MAGENTO_ORDERS_PRODUCT_COUNT_PREFIX.$id] = array(
368              '#default_value' => $item['qty'],
369              '#type' => 'textfield',
370              '#size' => 10,
371          );
372          $form['products'][$id]['subtotal'] = array(
373                                    '#value' => sprintf("%01.2f $currency", $item['amount'] * $item['qty']),
374          );
375          $form['products'][$id][MAGENTO_ORDERS_PRODUCT_DELETE_PREFIX.$id] = array (
376              '#type' => 'checkbox',
377          );
378          $form['products'][$id]['update'] = array(
379              '#type' => 'submit',
380              '#value' => t('Update Cart'),
381              '#submit' => array('magento_orders_cart_update'),
382          );
383        }
384    
385        if ($_SESSION['order']['quote']) {
386          $form['quote']['subtotal'] = array(
387              '#value' => sprintf("%01.2f $currency", $_SESSION['order']['quote']['subtotal']),
388          );
389          if ($_SESSION['order']['quote']['discount_amount']) {
390            $form['quote']['discount_amount'] = array(
391                '#value' => sprintf("%01.2f $currency",$_SESSION['order']['quote']['discount_amount'])
392            );
393          }
394          $form['quote']['shipping_amount'] = array(
395              '#value' => sprintf("%01.2f $currency",$_SESSION['order']['quote']['shipping_amount'])
396          );
397          $form['quote']['grand_total'] = array(
398              '#value' => sprintf("%01.2f $currency",$_SESSION['order']['quote']['grand_total'])
399          );
400          if (!empty($_SESSION['order']['quote']['taxes'])) {
401            $tax_amount = 0;
402            foreach ($_SESSION['order']['quote']['taxes'] as $tax) {
403              $tax_amount += $tax['amount'];
404            }
405            $form['quote']['taxes'] = array(
406                '#value' => sprintf("%01.2f $currency",$tax_amount)
407            );
408          }
409        }
410        $form['submit'] = array(
411            '#value' => l(t('Proceed to Checkout'),'order/checkout'),
412        );
413        $form['update'] = array(
414            '#type' => 'submit',
415            '#value' => t('Update Cart'),
416            '#submit' => array('magento_orders_cart_update'),
417        );
418        $form['delete'] = array(
419            '#type' => 'submit',
420            '#value' => t('Delete selected items'),
421            '#submit' => array('magento_orders_cart_update'),
422        );
423        $form['clear'] = array(
424            '#value' => l(t('Clear Cart'),'order/cancel'),
425        );
426        $form['#theme'] = 'magento_orders_cart';
427        $form['#validate'] = array('magento_orders_cart_validate');
428      } else
429        $form['message'] = array (
430            '#value' => t('There is no items in your shopping cart'),
431        );
432      return $form;
433  }  }
434  /**  /**
435   * Format a Cart page.   * Format a Cart page.
436   *   *
437   * @ingroup themeable   * @ingroup themeable
438   */   */
439  function theme_magento_orders_cart($form){  function theme_magento_orders_cart($form) {
440          $header = array(t('Name'), t('Price'), t('Qty'), t('Total'), t('Delete'));    $header = array(t('Name'), t('Price'), t('Qty'), t('Total'), t('Delete'));
441          $rows = array();    $rows = array();
442          foreach (element_children($form['title']) as $id) {    foreach (element_children($form['title']) as $id) {
443                  $row = array();      $row = array();
444                  $row[] = drupal_render($form['title'][$id]);      $row[] = drupal_render($form['title'][$id]);
445                  $row[] = drupal_render($form['amount_' . $id]);      $row[] = drupal_render($form['amount_' . $id]);
446                  $row[] = drupal_render($form[MAGENTO_ORDERS_PRODUCT_COUNT_PREFIX . $id]);      $row[] = drupal_render($form[MAGENTO_ORDERS_PRODUCT_COUNT_PREFIX . $id]);
447                  $row[] = drupal_render($form['subtotal_' . $id]);      $row[] = drupal_render($form['subtotal_' . $id]);
448                  $row[] = drupal_render($form[MAGENTO_ORDERS_PRODUCT_DELETE_PREFIX . $id]);      $row[] = drupal_render($form[MAGENTO_ORDERS_PRODUCT_DELETE_PREFIX . $id]);
449                  $rows[] = $row;      $rows[] = $row;
450          }    }
451          $output = theme('table', $header, $rows, array('class' => 'show-table'));    $output = theme('table', $header, $rows, array('class' => 'show-table'));
452          // Show cart totals    // Show cart totals
453          if ($_SESSION['order']['quote']) {    if ($_SESSION['order']['quote']) {
454                  $output .= '<div id = "cart_subtotal">Subtotal: ' . $_SESSION['order']['quote']['subtotal'] . ' ' . $_SESSION['order']['currency'] . '</div>';      $currency = t($_SESSION['order']['currency']);
455                  if (!empty($_SESSION['order']['quote']['discount_amount'])) $output .= '<div id = "cart_discount">Discount: -' . $_SESSION['order']['quote']['discount_amount'] . ' ' . $_SESSION['order']['currency'] . '</div>';      $output .= '<div id = "cart_subtotal">Subtotal: ' . $_SESSION['order']['quote']['subtotal'] . ' ' . $currency . '</div>';
456                  if (!empty($_SESSION['order']['quote']['taxes'])) {      if (!empty($_SESSION['order']['quote']['discount_amount'])) $output .= '<div id = "cart_discount">Discount: -' . $_SESSION['order']['quote']['discount_amount'] . ' ' . $currency . '</div>';
457                          foreach ($_SESSION['order']['quote']['taxes'] as $id => $tax) {      if (!empty($_SESSION['order']['quote']['taxes'])) {
458                                  $output .= '<div id = "cart_tax">Tax (' . $tax['percent'] . '%): ' . $tax['amount']  . ' ' . $_SESSION['order']['currency'] . '</div>';        foreach ($_SESSION['order']['quote']['taxes'] as $id => $tax) {
459                          }          $output .= '<div id = "cart_tax">Tax (' . $tax['percent'] . '%): ' . $tax['amount']  . ' ' . $currency . '</div>';
460                  }        }
461                  $output .= '<div id = "cart_total">Total: ' . $_SESSION['order']['quote']['grand_total'] . ' ' . $_SESSION['order']['currency'] . '</div>';      }
462          }      $output .= '<div id = "cart_total">Total: ' . $_SESSION['order']['quote']['grand_total'] . ' ' . $currency . '</div>';
463          $output .=  drupal_render($form);    }
464  return $output;    $output .=  drupal_render($form);
465      return $output;
466  }  }
467  /**  /**
468   * Validate updating of shopping cart.   * Validate updating of shopping cart.
469   * Check, if qty is not numeric or is minus   * Check, if qty is not numeric or is minus
470   *   *
471   * @param $form_state   * @param $form_state
472   *   The form state array.   *   The form state array.
473   * @param $form   * @param $form
474   *   Form array.   *   Form array.
475   *   *
476   */   */
477  function magento_orders_cart_validate($form, &$form_state) {  function magento_orders_cart_validate($form, &$form_state) {
478          if ($form_state['values']['op'] == t('Update Cart')) {    if ($form_state['values']['op'] == t('Update Cart')) {
479                  // Update product qty    // Update product qty
480                  foreach ($form_state['values'] as $key => $item) {      foreach ($form_state['values'] as $key => $item) {
481                          if (strstr($key, MAGENTO_ORDERS_PRODUCT_COUNT_PREFIX)) {        if (strstr($key, MAGENTO_ORDERS_PRODUCT_COUNT_PREFIX)) {
482                                  $id = substr($key, strlen(MAGENTO_ORDERS_PRODUCT_COUNT_PREFIX));          $id = substr($key, strlen(MAGENTO_ORDERS_PRODUCT_COUNT_PREFIX));
483                                  // Check, if item is minus or not numeric          // Check, if item is minus or not numeric
484                                  if (is_numeric($item) && $item < 0) form_set_error($key,'Qty must be more that null');          if (is_numeric($item) && $item < 0) form_set_error($key,'Qty must be more that null');
485                                  else if (!is_numeric($item)) form_set_error($key,'Qty must be numeric');          else if (!is_numeric($item)) form_set_error($key,'Qty must be numeric');
486                          }        }
487                  }      }
488          }    }
489  }  }
490  /**  /**
491   * Update shopping cart.   * Update shopping cart.
492   * Update qty of items or delete selected items.   * Update qty of items or delete selected items.
493   *   *
494   * @param $form_state   * @param $form_state
495   *   The form state array.   *   The form state array.
496   * @param $form   * @param $form
497   *   Form array.   *   Form array.
498   *   *
499   */   */
500  function magento_orders_cart_update($form, &$form_state) {  function magento_orders_cart_update($form, &$form_state) {
501          if ($form_state['values']['op'] == t('Update Cart')) {    global $user;
502                  // Update product qty    if ($user->uid) {
503                  foreach ($form_state['values'] as $key => $item) {      $customer = magento_api_customer_get_customer_id_by_email(array('email' => $user->mail));
504                          if (strstr($key, MAGENTO_ORDERS_PRODUCT_COUNT_PREFIX)) {      $_SESSION['order']['magento_user'] = $customer;
505                                  $id = substr($key, strlen(MAGENTO_ORDERS_PRODUCT_COUNT_PREFIX));    }
506                                  // if qty = 0 - delete item form cart  
507                                  if (is_numeric($item) && $item == 0) unset($_SESSION['order']['products'][$id]);    $rate = $form_state['values']['shipping_rates'];
508                                  // if qty is numeric and more than 0 - update item's qty    if ($rate) {
509                                  else if (is_numeric($item) && $item > 0) $_SESSION['order']['products'][$id]['qty'] = $item;      $_SESSION['order']['shipping_rate'] = $rate;
510                          }    }
511                  }    if ($form_state['values']['op'] == t('Update Cart')) {
512          }elseif ($form_state['values']['op'] == t('Delete selected items')) {    // Update product qty
513                  // Delete selected items      foreach ($form_state['values'] as $key => $item) {
514                  foreach ($form_state['values'] as $key => $item) {        if (strstr($key, MAGENTO_ORDERS_PRODUCT_COUNT_PREFIX)) {
515                          if (strstr($key, MAGENTO_ORDERS_PRODUCT_DELETE_PREFIX)) {          $id = substr($key, strlen(MAGENTO_ORDERS_PRODUCT_COUNT_PREFIX));
516                                  $id = substr($key, strlen(MAGENTO_ORDERS_PRODUCT_DELETE_PREFIX));          // if qty = 0 - delete item form cart
517                                  // if item is selected - delete it from cart          if (is_numeric($item) && $item == 0) unset($_SESSION['order']['products'][$id]);
518                                  if ($item == 1) unset($_SESSION['order']['products'][$id]);          // if qty is numeric and more than 0 - update item's qty
519                          }          else if (is_numeric($item) && $item > 0) $_SESSION['order']['products'][$id]['qty'] = $item;
520                  }        }
521          }      }
522          // Create new quote and update product price    }elseif ($form_state['values']['op'] == t('Delete selected items')) {
523          $quote = magento_api_quote_create($_SESSION['order']);    // Delete selected items
524          if ($quote) {      foreach ($form_state['values'] as $key => $item) {
525                  $_SESSION['order']['quote'] = $quote;        if (strstr($key, MAGENTO_ORDERS_PRODUCT_DELETE_PREFIX)) {
526                  foreach ($_SESSION['order']['quote']['products'] as $id => $item) {          $id = substr($key, strlen(MAGENTO_ORDERS_PRODUCT_DELETE_PREFIX));
527                          $_SESSION['order']['products'][$id]['amount'] = $_SESSION['order']['quote']['products'][$id]['price'];          // if item is selected - delete it from cart
528                  }          if ($item == 1) unset($_SESSION['order']['products'][$id]);
529          }else drupal_set_message(t('There was some error during creating quote in Magento'),'error');        }
530        }
531        if (empty($_SESSION['order']['products'])) return;
532      } elseif ($form_state['values']['op'] == t('Get a Quote')) {
533      // Update product qty
534        foreach ($form_state['values'] as $key => $item) {
535          if (strstr($key, MAGENTO_ORDERS_PRODUCT_COUNT_PREFIX)) {
536            $id = substr($key, strlen(MAGENTO_ORDERS_PRODUCT_COUNT_PREFIX));
537            // if qty = 0 - delete item form cart
538            if (is_numeric($item) && $item == 0) unset($_SESSION['order']['products'][$id]);
539            // if qty is numeric and more than 0 - update item's qty
540            else if (is_numeric($item) && $item > 0) $_SESSION['order']['products'][$id]['qty'] = $item;
541          }
542        }
543        //Get All Shipping Rates
544        unset($_SESSION['order']['shipping_rate']);
545        $_SESSION['order']['shipping_country'] = $form_state['values']['country_id'];
546      }
547    
548      // Create new quote and update product price
549      //$quote = magento_api_quote_create($_SESSION['order']);
550      $quote = magento_orders_create_quote($_SESSION['order']);
551      if ($quote) {
552        $_SESSION['order']['quote'] = $quote;
553    
554        // check if cart was cleared
555        if ($_SESSION['order']['quote']['products']) {
556          foreach ($_SESSION['order']['quote']['products'] as $id => $item) {
557            $_SESSION['order']['products'][$id]['amount'] = $_SESSION['order']['quote']['products'][$id]['price'];
558          }
559        }
560      }else drupal_set_message(t('There was some error during creating quote in Magento'),'error');
561  }  }
562  /**  /**
563   * FAPI definition for the checkout order form.   * FAPI definition for the checkout order form.
564   *   *
565    * @ingroup forms   * @ingroup forms
566    * @see magento_orders_checkout_order_submit()   * @see magento_orders_checkout_order_submit()
567   */   */
568  function magento_orders_checkout_order() {  function magento_orders_checkout_order() {
569          if (empty($_SESSION['order']['products'])) drupal_goto('catalog');    if (empty($_SESSION['order']['products'])) drupal_goto('catalog');
570          $path = drupal_get_path('module', 'magento_orders');    $path = drupal_get_path('module', 'magento_orders');
571          drupal_add_js($path . '/js/same_address.js');    drupal_add_js($path . '/js/same_address.js');
572    
573          $output = drupal_get_form('magento_orders_checkout_order_form');    $output = drupal_get_form('magento_orders_checkout_order_form');
574  return $output;    return $output;
575  }  }
576  /**  /**
577   * Display a form for first step of making order.   * Display a form for first step of making order.
578   * Enter billing and shipping addresses to this form.   * Enter billing and shipping addresses to this form.
579   *   *
580   * @param $form_state   * @param $form_state
581   *   The form state array.   *   The form state array.
582   * @return   * @return
583   *   An array containing the form elements to be displayed.   *   An array containing the form elements to be displayed.
584   */   */
585  function magento_orders_checkout_order_form($form_state) {  function magento_orders_checkout_order_form() {
586          $result = magento_api_directory_country_list_simple();    $result = magento_api_directory_country_list_simple(true);
587    
588          $form[MAGENTO_ORDERS_BILLING_PREFIX . 'address'] = array (    $form[MAGENTO_ORDERS_BILLING_PREFIX . 'address'] = array (
589                  '#type' => 'fieldset',        '#type' => 'fieldset',
590                  '#attributes' => array('class' => 'billing_address'),        '#attributes' => array('class' => 'billing_address'),
591                  '#title' => t('Billing Address'),        '#title' => t('Billing Address'),
592                  '#collapsible' => TRUE,        '#collapsible' => TRUE,
593                  '#collapsed' => FALSE,        '#collapsed' => FALSE,
594          );    );
595          $form[MAGENTO_ORDERS_BILLING_PREFIX . 'address'][MAGENTO_ORDERS_BILLING_PREFIX . 'firstname'] = array (    $form[MAGENTO_ORDERS_BILLING_PREFIX . 'address'][MAGENTO_ORDERS_BILLING_PREFIX . 'firstname'] = array (
596                  '#type' => 'textfield',        '#type' => 'textfield',
597                  '#title' => t('First Name'),        '#title' => t('First Name'),
598                  '#required' => TRUE,        '#required' => TRUE,
599                  '#default_value' => $_SESSION['order'] ['billing_information']['firstname'] ? $_SESSION['order'] ['billing_information']['firstname'] : '',        '#default_value' => $_SESSION['order'] ['billing_information']['firstname'] ? $_SESSION['order'] ['billing_information']['firstname'] : '',
600          );    );
601          $form[MAGENTO_ORDERS_BILLING_PREFIX . 'address'][MAGENTO_ORDERS_BILLING_PREFIX . 'lastname'] = array (    $form[MAGENTO_ORDERS_BILLING_PREFIX . 'address'][MAGENTO_ORDERS_BILLING_PREFIX . 'lastname'] = array (
602                  '#type' => 'textfield',        '#type' => 'textfield',
603                  '#title' => t('Last Name'),        '#title' => t('Last Name'),
604                  '#required' => TRUE,        '#required' => TRUE,
605                  '#default_value' => $_SESSION['order'] ['billing_information']['lastname'] ? $_SESSION['order'] ['billing_information']['lastname'] : '',        '#default_value' => $_SESSION['order'] ['billing_information']['lastname'] ? $_SESSION['order'] ['billing_information']['lastname'] : '',
606          );    );
607          $form[MAGENTO_ORDERS_BILLING_PREFIX . 'address'][MAGENTO_ORDERS_BILLING_PREFIX . 'country_id'] = array (    $form[MAGENTO_ORDERS_BILLING_PREFIX . 'address'][MAGENTO_ORDERS_BILLING_PREFIX . 'country_id'] = array (
608                  '#type' => 'select',        '#type' => 'select',
609                  '#title' => t('Country'),        '#title' => t('Country'),
610                  '#options' => $result ? $result : array(0 => t('None avaliable')),        '#options' => $result ? $result : array(0 => t('None avaliable')),
611                  '#required' => TRUE,        '#required' => TRUE,
612                  '#default_value' => $_SESSION['order'] ['billing_information']['country_id'] ? $_SESSION['order'] ['billing_information']['country_id'] : '',        '#default_value' => $_SESSION['order'] ['billing_information']['country_id'] ? $_SESSION['order'] ['billing_information']['country_id'] : '',
613          );    );
614          $form[MAGENTO_ORDERS_BILLING_PREFIX . 'address'][MAGENTO_ORDERS_BILLING_PREFIX . 'city'] = array (    $form[MAGENTO_ORDERS_BILLING_PREFIX . 'address'][MAGENTO_ORDERS_BILLING_PREFIX . 'city'] = array (
615                  '#type' => 'textfield',        '#type' => 'textfield',
616                  '#title' => t('City'),        '#title' => t('City'),
617                  '#required' => TRUE,        '#required' => TRUE,
618                  '#default_value' => $_SESSION['order'] ['billing_information']['city'] ? $_SESSION['order'] ['billing_information']['city'] : '',        '#default_value' => $_SESSION['order'] ['billing_information']['city'] ? $_SESSION['order'] ['billing_information']['city'] : '',
619          );    );
620          $form[MAGENTO_ORDERS_BILLING_PREFIX . 'address'][MAGENTO_ORDERS_BILLING_PREFIX . 'street'] = array (    $form[MAGENTO_ORDERS_BILLING_PREFIX . 'address'][MAGENTO_ORDERS_BILLING_PREFIX . 'street'] = array (
621                  '#type' => 'textfield',        '#type' => 'textfield',
622                  '#title' => t('Street'),        '#title' => t('Street'),
623                  '#required' => TRUE,        '#required' => TRUE,
624                  '#default_value' => $_SESSION['order'] ['billing_information']['street'] ? $_SESSION['order'] ['billing_information']['street'] : '',        '#default_value' => $_SESSION['order'] ['billing_information']['street'] ? $_SESSION['order'] ['billing_information']['street'] : '',
625          );    );
626          $form[MAGENTO_ORDERS_BILLING_PREFIX . 'address'][MAGENTO_ORDERS_BILLING_PREFIX . 'postcode'] = array (    $form[MAGENTO_ORDERS_BILLING_PREFIX . 'address'][MAGENTO_ORDERS_BILLING_PREFIX . 'postcode'] = array (
627                  '#type' => 'textfield',        '#type' => 'textfield',
628                  '#title' => t('Zip/Postal Code'),        '#title' => t('Zip/Postal Code'),
629                  '#required' => TRUE,        '#required' => TRUE,
630                  '#default_value' => $_SESSION['order'] ['billing_information']['postcode'] ? $_SESSION['order'] ['billing_information']['postcode'] : '',        '#default_value' => $_SESSION['order'] ['billing_information']['postcode'] ? $_SESSION['order'] ['billing_information']['postcode'] : '',
631          );    );
632          $form[MAGENTO_ORDERS_BILLING_PREFIX . 'address'][MAGENTO_ORDERS_BILLING_PREFIX . 'telephone'] = array (    $form[MAGENTO_ORDERS_BILLING_PREFIX . 'address'][MAGENTO_ORDERS_BILLING_PREFIX . 'telephone'] = array (
633                  '#type' => 'textfield',        '#type' => 'textfield',
634                  '#title' => t('Phone number'),        '#title' => t('Phone number'),
635                  '#required' => TRUE,        '#required' => TRUE,
636                  '#default_value' => $_SESSION['order'] ['billing_information']['telephone'] ? $_SESSION['order'] ['billing_information']['telephone'] : '',        '#default_value' => $_SESSION['order'] ['billing_information']['telephone'] ? $_SESSION['order'] ['billing_information']['telephone'] : '',
637          );    );
638          $form[MAGENTO_ORDERS_BILLING_PREFIX . 'address']['same_address'] = array (    $form[MAGENTO_ORDERS_BILLING_PREFIX . 'address']['same_address'] = array (
639                  '#type' => 'checkbox',        '#type' => 'checkbox',
640                  '#title' => t('Use same shiping address'),        '#title' => t('Use same shiping address'),
641          );    );
642    
643          $form[MAGENTO_ORDERS_SHIPPING_PREFIX . 'address'] = array (    $form[MAGENTO_ORDERS_SHIPPING_PREFIX . 'address'] = array (
644                  '#type' => 'fieldset',        '#type' => 'fieldset',
645                  '#attributes' => array('class' => 'shipping_address'),        '#attributes' => array('class' => 'shipping_address'),
646                  '#title' => t('Shipping Address'),        '#title' => t('Shipping Address'),
647                  '#collapsible' => TRUE,        '#collapsible' => TRUE,
648                  '#collapsed' => FALSE,        '#collapsed' => FALSE,
649          );    );
650          $form[MAGENTO_ORDERS_SHIPPING_PREFIX . 'address'][MAGENTO_ORDERS_SHIPPING_PREFIX . 'firstname'] = array (    $form[MAGENTO_ORDERS_SHIPPING_PREFIX . 'address'][MAGENTO_ORDERS_SHIPPING_PREFIX . 'firstname'] = array (
651                  '#type' => 'textfield',        '#type' => 'textfield',
652                  '#title' => t('First Name'),        '#title' => t('First Name'),
653                  '#required' => TRUE,        '#required' => TRUE,
654                  '#default_value' => $_SESSION['order'] ['shipping_information']['firstname'] ? $_SESSION['order'] ['billing_information']['firstname'] : '',        '#default_value' => $_SESSION['order'] ['shipping_information']['firstname'] ? $_SESSION['order'] ['billing_information']['firstname'] : '',
655          );    );
656          $form[MAGENTO_ORDERS_SHIPPING_PREFIX . 'address'][MAGENTO_ORDERS_SHIPPING_PREFIX . 'lastname'] = array (    $form[MAGENTO_ORDERS_SHIPPING_PREFIX . 'address'][MAGENTO_ORDERS_SHIPPING_PREFIX . 'lastname'] = array (
657                  '#type' => 'textfield',        '#type' => 'textfield',
658                  '#title' => t('Last Name'),        '#title' => t('Last Name'),
659                  '#required' => TRUE,        '#required' => TRUE,
660                  '#default_value' => $_SESSION['order'] ['shipping_information']['lastname'] ? $_SESSION['order'] ['billing_information']['lastname'] : '',        '#default_value' => $_SESSION['order'] ['shipping_information']['lastname'] ? $_SESSION['order'] ['billing_information']['lastname'] : '',
661          );    );
662          $form[MAGENTO_ORDERS_SHIPPING_PREFIX . 'address'][MAGENTO_ORDERS_SHIPPING_PREFIX . 'country_id'] = array (    $form[MAGENTO_ORDERS_SHIPPING_PREFIX . 'address'][MAGENTO_ORDERS_SHIPPING_PREFIX . 'country_id'] = array (
663                  '#type' => 'select',        '#type' => 'select',
664                  '#title' => t('Country'),        '#title' => t('Country'),
665                  '#options' => $result ? $result : array(0 => t('None avaliable')),        '#options' => $result ? $result : array(0 => t('None avaliable')),
666                  '#required' => TRUE,        '#required' => TRUE,
667                  '#default_value' => $_SESSION['order'] ['shipping_information']['country_id'] ? $_SESSION['order'] ['billing_information']['country_id'] : '',        '#default_value' => $_SESSION['order'] ['shipping_information']['country_id'] ? $_SESSION['order'] ['billing_information']['country_id'] : '',
668          );    );
669          $form[MAGENTO_ORDERS_SHIPPING_PREFIX . 'address'][MAGENTO_ORDERS_SHIPPING_PREFIX . 'city'] = array (    $form[MAGENTO_ORDERS_SHIPPING_PREFIX . 'address'][MAGENTO_ORDERS_SHIPPING_PREFIX . 'city'] = array (
670                  '#type' => 'textfield',        '#type' => 'textfield',
671                  '#title' => t('City'),        '#title' => t('City'),
672                  '#required' => TRUE,        '#required' => TRUE,
673                  '#default_value' => $_SESSION['order'] ['shipping_information']['city'] ? $_SESSION['order'] ['billing_information']['city'] : '',        '#default_value' => $_SESSION['order'] ['shipping_information']['city'] ? $_SESSION['order'] ['billing_information']['city'] : '',
674          );    );
675          $form[MAGENTO_ORDERS_SHIPPING_PREFIX . 'address'][MAGENTO_ORDERS_SHIPPING_PREFIX . 'street'] = array (    $form[MAGENTO_ORDERS_SHIPPING_PREFIX . 'address'][MAGENTO_ORDERS_SHIPPING_PREFIX . 'street'] = array (
676                  '#type' => 'textfield',        '#type' => 'textfield',
677                  '#title' => t('Street'),        '#title' => t('Street'),
678                  '#required' => TRUE,        '#required' => TRUE,
679                  '#default_value' => $_SESSION['order'] ['shipping_information']['street'] ? $_SESSION['order'] ['billing_information']['street'] : '',        '#default_value' => $_SESSION['order'] ['shipping_information']['street'] ? $_SESSION['order'] ['billing_information']['street'] : '',
680          );    );
681          $form[MAGENTO_ORDERS_SHIPPING_PREFIX . 'address'][MAGENTO_ORDERS_SHIPPING_PREFIX . 'postcode'] = array (    $form[MAGENTO_ORDERS_SHIPPING_PREFIX . 'address'][MAGENTO_ORDERS_SHIPPING_PREFIX . 'postcode'] = array (
682                  '#type' => 'textfield',        '#type' => 'textfield',
683                  '#title' => t('Zip/Postal Code'),        '#title' => t('Zip/Postal Code'),
684                  '#required' => TRUE,        '#required' => TRUE,
685                  '#default_value' => $_SESSION['order'] ['shipping_information']['postcode'] ? $_SESSION['order'] ['billing_information']['postcode'] : '',        '#default_value' => $_SESSION['order'] ['shipping_information']['postcode'] ? $_SESSION['order'] ['billing_information']['postcode'] : '',
686          );    );
687          $form[MAGENTO_ORDERS_SHIPPING_PREFIX . 'address'][MAGENTO_ORDERS_SHIPPING_PREFIX . 'telephone'] = array (    $form[MAGENTO_ORDERS_SHIPPING_PREFIX . 'address'][MAGENTO_ORDERS_SHIPPING_PREFIX . 'telephone'] = array (
688                  '#type' => 'textfield',        '#type' => 'textfield',
689                  '#title' => t('Phone number'),        '#title' => t('Phone number'),
690                  '#required' => TRUE,        '#required' => TRUE,
691                  '#default_value' => $_SESSION['order'] ['shipping_information']['telephone'] ? $_SESSION['order'] ['billing_information']['telephone'] : '',        '#default_value' => $_SESSION['order'] ['shipping_information']['telephone'] ? $_SESSION['order'] ['billing_information']['telephone'] : '',
692          );    );
   
         $form['submit'] = array(  
                 '#type' => 'submit',  
                 '#value' => t('Continue'),  
                 '#submit' => array('magento_orders_checkout_order_form_submit'),  
         );  
         $form['cancel'] = array(  
                 '#value' => l(t('Cancel and Clear Order'), 'order/cancel'),  
         );  
693    
694  return $form;    $form['submit'] = array(
695          '#type' => 'submit',
696          '#value' => t('Continue'),
697          '#submit' => array('magento_orders_checkout_order_form_submit'),
698      );
699      $form['cancel'] = array(
700          '#value' => l(t('Cancel and Clear Order'), 'order/cancel'),
701      );
702    
703      return $form;
704  }  }
705  /**  /**
706   * Save billing and shipping information to session.   * Save billing and shipping information to session.
707   *   *
708   * @param $form_state   * @param $form_state
709   *   The form state array.   *   The form state array.
710   * @param $form   * @param $form
711   *   Form array.   *   Form array.
712   *   *
713   */   */
714  function magento_orders_checkout_order_form_submit($form, &$form_state) {  function magento_orders_checkout_order_form_submit($form, &$form_state) {
715  global $user;    global $user;
716          foreach ($form_state['values'] as $key => $item) {    foreach ($form_state['values'] as $key => $item) {
717                  if (($key == MAGENTO_ORDERS_SHIPPING_PREFIX . 'country_id' && $item == 0) || ($key == MAGENTO_ORDERS_BILLING_PREFIX . 'country_id' && $item == 0)) $error = TRUE;      if (($key == MAGENTO_ORDERS_SHIPPING_PREFIX . 'country_id' && $item == 0) || ($key == MAGENTO_ORDERS_BILLING_PREFIX . 'country_id' && $item == 0)) $error = TRUE;
718                  else $error = FALSE;      else $error = FALSE;
719    
720                  if (strstr($key, MAGENTO_ORDERS_BILLING_PREFIX)) {      if (strstr($key, MAGENTO_ORDERS_BILLING_PREFIX)) {
721                          $_SESSION['order']['billing_information'][substr($key, strlen(MAGENTO_ORDERS_BILLING_PREFIX))] = $item;        $_SESSION['order']['billing_information'][substr($key, strlen(MAGENTO_ORDERS_BILLING_PREFIX))] = $item;
722                  }      }
723                  elseif(strstr($key, MAGENTO_ORDERS_SHIPPING_PREFIX)) {      elseif(strstr($key, MAGENTO_ORDERS_SHIPPING_PREFIX)) {
724                          $_SESSION['order']['shipping_information'][substr($key, strlen(MAGENTO_ORDERS_SHIPPING_PREFIX))] = $item;        $_SESSION['order']['shipping_information'][substr($key, strlen(MAGENTO_ORDERS_SHIPPING_PREFIX))] = $item;
725                  }      }
726          }    }
727          // Get Customer ID by email of Drupal user    // Get Customer ID by email of Drupal user
728          if ($customer = magento_api_customer_get_customer_id_by_email(array('email' => $user->mail))) $_SESSION['order']['magento_user'] = $customer;    if ($customer = magento_api_customer_get_customer_id_by_email(array('email' => $user->mail))) $_SESSION['order']['magento_user'] = $customer;
729          else $error = TRUE;    else $error = TRUE;
730    
731          if ($error) drupal_set_message(t('There was some error during getting information from Magento'),'error');    if ($error) drupal_set_message(t('There was some error during getting information from Magento'),'error');
732          else drupal_goto('order/checkout_step2');    else drupal_goto('order/checkout_step2');
733  }  }
734  /**  /**
735   * FAPI definition for the checkout order step 2 form.   * FAPI definition for the checkout order step 2 form.
736   *   *
737    * @ingroup forms   * @ingroup forms
738    * @see magento_orders_checkout_order_step2_submit()   * @see magento_orders_checkout_order_step2_submit()
739   */   */
740  function magento_orders_checkout_order_step2() {  function magento_orders_checkout_order_step2() {
741          if (empty($_SESSION['order']['products']) || empty($_SESSION['order']['shipping_information']) || empty($_SESSION['order']['billing_information'])) drupal_goto('');    if (empty($_SESSION['order']['products']) || empty($_SESSION['order']['shipping_information']) || empty($_SESSION['order']['billing_information'])) drupal_goto('');
742          $output = drupal_get_form('magento_orders_checkout_order_step2_form');    $output = drupal_get_form('magento_orders_checkout_order_step2_form');
743  return $output;    return $output;
744  }  }
745  /**  /**
746   * Display a form for second step of making order.   * Display a form for second step of making order.
747   * Choose payment and shipment method in this form.   * Choose payment and shipment method in this form.
748   *   *
749   * @param $form_state   * @param $form_state
750   *   The form state array.   *   The form state array.
751   * @return   * @return
752   *   An array containing the form elements to be displayed.   *   An array containing the form elements to be displayed.
753   */   */
754  function magento_orders_checkout_order_step2_form($form_state) {  function magento_orders_checkout_order_step2_form() {
755          // Get data about avaliable shipments and payments from Magento  // Get data about avaliable shipments and payments from Magento
756          $payments = magento_api_payment_list();    $payments = magento_api_payment_list();
757          $shipments = magento_api_shipment_list_simple();    $shipments = magento_api_shipment_list_simple();
758    
759          if ($payments && $shipments) {    if ($payments && $shipments) {
760                  $form['methods'] = array (      $form['methods'] = array (
761                          '#type' => 'fieldset',          '#type' => 'fieldset',
762                          '#title' => t('Payment & Shipment Methods'),          '#title' => t('Payment & Shipment Methods'),
763                          '#collapsible' => TRUE,          '#collapsible' => TRUE,
764                          '#collapsed' => FALSE,          '#collapsed' => FALSE,
765                  );      );
766                  $form['methods']['payment_method'] = array (      $form['methods']['payment_method'] = array (
767                          '#type' => 'radios',          '#type' => 'radios',
768                          '#title' => t('Payment Method'),          '#title' => t('Payment Method'),
769                          '#options' => $payments,          '#options' => $payments,
770                          '#default_value' => $_SESSION['order'] ['payment_method'] ? $_SESSION['order'] ['payment_method'] : 0,          '#default_value' => $_SESSION['order'] ['payment_method'] ? $_SESSION['order'] ['payment_method'] : 0,
771                          '#required' => TRUE,          '#required' => TRUE,
772                  );      );
773                  $form['methods']['shipment_method'] = array (      $form['methods']['shipment_method'] = array (
774                          '#type' => 'radios',          '#type' => 'radios',
775                          '#title' => t('Shipment Method'),          '#title' => t('Shipment Method'),
776                          '#options' => $shipments,          '#options' => $shipments,
777                          '#required' => TRUE,          '#required' => TRUE,
778                          '#default_value' => $_SESSION['order'] ['shipment_method'] ? $_SESSION['order'] ['shipment_method'] : 0,          '#default_value' => $_SESSION['order'] ['shipment_method'] ? $_SESSION['order'] ['shipment_method'] : 0,
779                  );      );
780                  $form['back'] = array(      $form['back'] = array(
781                          '#value' => l(t('Back to Previous Step'), 'order/checkout'),          '#value' => l(t('Back to Previous Step'), 'order/checkout'),
782                  );      );
783                  $form['submit'] = array(      $form['submit'] = array(
784                          '#type' => 'submit',          '#type' => 'submit',
785                          '#value' => t('Continue'),          '#value' => t('Continue'),
786                          '#submit' => array('magento_orders_checkout_order_step2_form_submit'),          '#submit' => array('magento_orders_checkout_order_step2_form_submit'),
787                  );      );
788    
789                  $form['#submit'] = array('magento_orders_checkout_order_step2_form_submit');      $form['#submit'] = array('magento_orders_checkout_order_step2_form_submit');
790          }else {    }else {
791                  // If there was some error in XML-RPC show error    // If there was some error in XML-RPC show error
792                  drupal_set_message(t('No payments or shipments avaliable'),'error');      drupal_set_message(t('No payments or shipments avaliable'),'error');
793          }    }
794          $form['cancel'] = array(    $form['cancel'] = array(
795                          '#value' => l(t('Cancel and Clear Order'), 'order/cancel'),        '#value' => l(t('Cancel and Clear Order'), 'order/cancel'),
796          );    );
797  return $form;    return $form;
798  }  }
799  /**  /**
800   * Save data about payment and shipment methods to session.   * Save data about payment and shipment methods to session.
801   * Send to Magento information about current order and save response about created order from Magento.   * Send to Magento information about current order and save response about created order from Magento.
802   *   *
803   * @param $form_state   * @param $form_state
804   *   The form state array.   *   The form state array.
805   * @param $form   * @param $form
806   *   Form array.   *   Form array.
807   *   *
808   */   */
809  function magento_orders_checkout_order_step2_form_submit($form, &$form_state) {  function magento_orders_checkout_order_step2_form_submit($form, &$form_state) {
810          // Save payment and shipment methods  // Save payment and shipment methods
811          $_SESSION['order']['payment_method'] = $form_state['values']['payment_method'];    $_SESSION['order']['payment_method'] = $form_state['values']['payment_method'];
812          $_SESSION['order']['shipment_method'] = $form_state['values']['shipment_method'];    $_SESSION['order']['shipment_method'] = $form_state['values']['shipment_method'];
813                  // Send order data to Magento for creating order and get response    // Send order data to Magento for creating order and get response
814                  $result = magento_api_order_create($_SESSION['order']);    $result = magento_api_order_create($_SESSION['order']);
815                  if ($result) {    if ($result) {
816                          $_SESSION['order']['order_complete'] = $result;      $_SESSION['order']['order_complete'] = $result;
817                          drupal_goto('order/checkout_step3');      drupal_goto('order/checkout_step3');
818                  }else    }else
819                          drupal_set_message(t('There was some error during order creating'),'error');      drupal_set_message(t('There was some error during order creating'),'error');
820  }  }
821  /**  /**
822   * FAPI definition for the checkout order final step form.   * FAPI definition for the checkout order final step form.
823   *   *
824    * @ingroup forms   * @ingroup forms
825    * @see magento_orders_checkout_order_step3_submit()   * @see magento_orders_checkout_order_step3_submit()
826   */   */
827  function magento_orders_checkout_order_step3() {  function magento_orders_checkout_order_step3() {
828          if (empty($_SESSION['order']['products']) || empty($_SESSION['order']['shipping_information']) || empty($_SESSION['order']['billing_information']) || empty($_SESSION['order']['order_complete'])) drupal_goto('');    if (empty($_SESSION['order']['products']) || empty($_SESSION['order']['shipping_information']) || empty($_SESSION['order']['billing_information']) || empty($_SESSION['order']['order_complete'])) drupal_goto('');
829          // Show information about Shopping Cart    // Show information about Shopping Cart
830          $header = array(t('Name'), t('Price'), t('Qty'), t('Total'));    $header = array(t('Name'), t('Price'), t('Qty'), t('Total'));
831          $rows = array();    $rows = array();
832          foreach ($_SESSION['order']['products'] as $id => $item) {    $currency = t($_SESSION['order']['currency']);
833                  $row = array();    foreach ($_SESSION['order']['products'] as $id => $item) {
834                  $row[] = $item['title'];      $row = array();
835                  $row[] = $item['amount'] . ' ' . $_SESSION['order']['currency'];      $row[] = $item['title'];
836                  $row[] = $item['qty'];      $row[] = $item['amount'] . ' ' . $currency;
837                  $row[] = ($item['qty'] * $item['amount']) . ' ' . $_SESSION['order']['currency'];      $row[] = $item['qty'];
838        $row[] = ($item['qty'] * $item['amount']) . ' ' . $currency;
839          $rows[] = $row;  
840          }      $rows[] = $row;
841          $output = theme('table', $header, $rows, array('class' => 'show-table'));    }
842          $output .= drupal_get_form('magento_orders_checkout_order_step3_form');    $output = theme('table', $header, $rows, array('class' => 'show-table'));
843  return $output;    $output .= drupal_get_form('magento_orders_checkout_order_step3_form');
844      return $output;
845  }  }
846  /**  /**
847   * Display a form for final step of making order.   * Display a form for final step of making order.
848   * Displays order total review.   * Displays order total review.
849   *   *
850   * @param $form_state   * @param $form_state
851   *   The form state array.   *   The form state array.
852   * @return   * @return
853   *   An array containing the form elements to be displayed.   *   An array containing the form elements to be displayed.
854   */   */
855  function magento_orders_checkout_order_step3_form($form_state) {  function magento_orders_checkout_order_step3_form($form_state) {
856          // Show order totals  // Show order totals
857          $form['order_totals'] = array (    $form['order_totals'] = array (
858                  '#type' => 'fieldset',        '#type' => 'fieldset',
859                  '#title' => t('Order #' . $_SESSION['order']['order_complete']['order_increment_id'] . ' totals'),        '#title' => t('Order #' . $_SESSION['order']['order_complete']['order_increment_id'] . ' totals'),
860                  '#collapsible' => TRUE,        '#collapsible' => TRUE,
861                  '#collapsed' => FALSE,        '#collapsed' => FALSE,
862          );    );
863          $form['order_totals']['subtotal'] = array (    $currency = t($_SESSION['order']['currency']);
864                  '#type' => 'item',    $form['order_totals']['subtotal'] = array (
865                  '#title' => t('Subtotal'),        '#type' => 'item',
866                  '#value' => $_SESSION['order']['order_complete']['subtotal'] . ' ' . $_SESSION['order']['currency'],        '#title' => t('Subtotal'),
867          );        '#value' => $_SESSION['order']['order_complete']['subtotal'] . ' ' . $currency,
868          $form['order_totals']['discount'] = array (    );
869                  '#type' => 'item',    $form['order_totals']['discount'] = array (
870                  '#title' => t('Discount'),        '#type' => 'item',
871                  '#value' => $_SESSION['order']['order_complete']['discount_amount'] . ' ' . $_SESSION['order']['currency'],        '#title' => t('Discount'),
872          );        '#value' => $_SESSION['order']['order_complete']['discount_amount'] . ' ' . $currency,
873          $form['order_totals']['tax'] = array (    );
874                  '#type' => 'item',    $form['order_totals']['tax'] = array (
875                  '#title' => t('Tax'),        '#type' => 'item',
876                  '#value' => $_SESSION['order']['order_complete']['tax_amount'] . ' ' . $_SESSION['order']['currency'],        '#title' => t('Tax'),
877          );        '#value' => $_SESSION['order']['order_complete']['tax_amount'] . ' ' . $currency,
878          $form['order_totals']['shipping'] = array (    );
879                  '#type' => 'item',    $form['order_totals']['shipping'] = array (
880                  '#title' => t('Shipping & Handling'),        '#type' => 'item',
881                  '#value' => $_SESSION['order']['order_complete']['shipping_amount'] . ' ' . $_SESSION['order']['currency'],        '#title' => t('Shipping & Handling'),
882          );        '#value' => $_SESSION['order']['order_complete']['shipping_amount'] . ' ' . $currency,
883          $form['order_totals']['total'] = array (    );
884                  '#type' => 'item',    $form['order_totals']['total'] = array (
885                  '#title' => t('Total'),        '#type' => 'item',
886                  '#value' => $_SESSION['order']['order_complete']['grand_total'] . ' ' . $_SESSION['order']['currency'],        '#title' => t('Total'),
887          );        '#value' => $_SESSION['order']['order_complete']['grand_total'] . ' ' . $currency,
888      );
889          if ($_SESSION['order']['payment_method'] == 'paypal_standard') {  
890                  // Get data about Merchant PayPal account    if ($_SESSION['order']['payment_method'] == 'paypal_standard') {
891                  $result = magento_api_payment_paypal_wps_account_info();    // Get data about Merchant PayPal account
892                  if ($result) $form['#action'] = MAGENTO_ORDERS_PAY_PAL_GATEWAY;      $result = magento_api_payment_paypal_wps_account_info();
893                  else $error = TRUE;      if ($result) $form['#action'] = MAGENTO_ORDERS_PAY_PAL_GATEWAY;
894                  // Prepare data for PayPal      else $error = TRUE;
895                  $form['cmd'] = array('#type' => 'hidden', '#value' => '_cart');      // Prepare data for PayPal
896                  $form['business'] = array('#type' => 'hidden', '#value' => $result['account']);      $form['cmd'] = array('#type' => 'hidden', '#value' => '_cart');
897                  $form['upload'] = array('#type' => 'hidden', '#value' => 1);      $form['business'] = array('#type' => 'hidden', '#value' => $result['account']);
898                  $form['charset'] = array('#type' => 'hidden', '#value' => 'utf-8');      $form['upload'] = array('#type' => 'hidden', '#value' => 1);
899        $form['charset'] = array('#type' => 'hidden', '#value' => 'utf-8');
900                  $form['amount'] = array('#type' => 'hidden', '#value' => $_SESSION['order']['order_complete']['grand_total']);  
901                  $form['no_shipping'] = array('#type' => 'hidden', '#value' => 1);      $form['amount'] = array('#type' => 'hidden', '#value' => $_SESSION['order']['order_complete']['grand_total']);
902        $form['no_shipping'] = array('#type' => 'hidden', '#value' => 1);
903                  $form['item_name_1'] = array('#type' => 'hidden', '#value' => $result['name'] . ' (including discount)');  
904                  $form['amount_1'] = array('#type' => 'hidden', '#value' => ($_SESSION['order']['order_complete']['subtotal'] - $_SESSION['order']['order_complete']['discount_amount']));      $form['item_name_1'] = array('#type' => 'hidden', '#value' => $result['name'] . ' (including discount)');
905                  $form['item_number_1'] = array('#type' => 'hidden', '#value' => $_SESSION['order']['order_complete']['order_increment_id']);      $form['amount_1'] = array('#type' => 'hidden', '#value' => ($_SESSION['order']['order_complete']['subtotal'] - $_SESSION['order']['order_complete']['discount_amount']));
906        $form['item_number_1'] = array('#type' => 'hidden', '#value' => $_SESSION['order']['order_complete']['order_increment_id']);
907                  $form['tax_cart'] = array('#type' => 'hidden', '#value' => $_SESSION['order']['order_complete']['tax_amount']);  
908                  $form['handling_cart'] = array('#type' => 'hidden', '#value' => $_SESSION['order']['order_complete']['shipping_amount']);      $form['tax_cart'] = array('#type' => 'hidden', '#value' => $_SESSION['order']['order_complete']['tax_amount']);
909        $form['handling_cart'] = array('#type' => 'hidden', '#value' => $_SESSION['order']['order_complete']['shipping_amount']);
910                  $form['return'] = array('#type' => 'hidden', '#value' => url('order/complete',array('absolute' => TRUE)));  
911                  $form['notify_url'] = array('#type' => 'hidden', '#value' => url('order/response',array('absolute' => TRUE)));      $form['return'] = array('#type' => 'hidden', '#value' => url('order/complete',array('absolute' => TRUE)));
912                  $form['cancel_return'] = array('#type' => 'hidden', '#value' => url('order/checkout_step3',array('absolute' => TRUE)));      $form['notify_url'] = array('#type' => 'hidden', '#value' => url('order/response',array('absolute' => TRUE)));
913        $form['cancel_return'] = array('#type' => 'hidden', '#value' => url('order/checkout_step3',array('absolute' => TRUE)));
914                  $form['custom'] = array('#type' => 'hidden', '#value' => $_SESSION['order']['order_complete']['order_increment_id']);  
915                  $form['currency_code'] = array('#type' => 'hidden', '#value' => $_SESSION['order']['currency'] ? $_SESSION['order']['currency'] : MAGENTO_ORDERS_DEFAULT_CURRENCY);      $form['custom'] = array('#type' => 'hidden', '#value' => $_SESSION['order']['order_complete']['order_increment_id']);
916                  $form['rm'] = array('#type' => 'hidden', '#value' => 2);      $form['currency_code'] = array('#type' => 'hidden', '#value' => $_SESSION['order']['currency'] ? $_SESSION['order']['currency'] : MAGENTO_ORDERS_DEFAULT_CURRENCY);
917          }      $form['rm'] = array('#type' => 'hidden', '#value' => 2);
918          $form['back'] = array(    }
919                  '#value' => l(t('Back to Previous Step'), 'order/checkout_step2'),    $form['back'] = array(
920          );        '#value' => l(t('Back to Previous Step'), 'order/checkout_step2'),
921          if ($error) drupal_set_message(t('Could not get information about Merchant PayPal account form Magento'));    );
922          else $form['submit'] = array(    if ($error) drupal_set_message(t('Could not get information about Merchant PayPal account form Magento'));
923                          '#type' => 'submit',    else $form['submit'] = array(
924                          '#value' => t('Continue'),          '#type' => 'submit',
925                          '#submit' => array('magento_orders_checkout_complete'),          '#value' => t('Continue'),
926                   );          '#submit' => array('magento_orders_checkout_complete'),
927        );
928          $form['cancel'] = array(  
929                  '#value' => l(t('Cancel and Clear Order'), 'order/cancel'),    $form['cancel'] = array(
930          );        '#value' => l(t('Cancel and Clear Order'), 'order/cancel'),
931      );
932  return $form;  
933      return $form;
934  }  }
935  /**  /**
936   * Show message about completed order checkout, clear session data and redirect to frontpage   * Show message about completed order checkout, clear session data and redirect to frontpage
937   *   *
938   */   */
939  function magento_orders_checkout_complete() {  function magento_orders_checkout_complete() {
940          drupal_set_message(t('Your order #' . $_SESSION['order']['order_complete']['order_increment_id'] . ' has been completed'));    drupal_set_message(t('Your order #' . $_SESSION['order']['order_complete']['order_increment_id'] . ' has been completed'));
941          unset($_SESSION['order']);    unset($_SESSION['order']);
942          drupal_goto('');    drupal_goto('');
943  }  }
944  /**  /**
945   * Cancel current order, clear session data and redirect to frontpage   * Cancel current order, clear session data and redirect to frontpage
946   *   *
947   * @param $form_state   * @param $form_state
948   *   The form state array.   *   The form state array.
949   * @param $form   * @param $form
950   *   Form array.   *   Form array.
951   *   *
952   */   */
953  function magento_orders_checkout_cancel() {  function magento_orders_checkout_cancel() {
954          // sending data to Magento for order cancelling  // sending data to Magento for order cancelling
955          if (!empty($_SESSION['order']['order_complete']['order_increment_id'])) {    if (!empty($_SESSION['order']['order_complete']['order_increment_id'])) {
956                  magento_api_sales_order_cancel($_SESSION['order']['order_complete']['order_increment_id']);      magento_api_sales_order_cancel($_SESSION['order']['order_complete']['order_increment_id']);
957                  drupal_set_message(t('Your order has been cancelled'));      drupal_set_message(t('Your order has been cancelled'));
958          }    }
959          unset($_SESSION['order']);    unset($_SESSION['order']);
960    
961          $return = $_GET['return'];    $return = $_GET['return'];
962          if (!empty($return)) drupal_goto($return);    if (!empty($return)) drupal_goto($return);
963          else drupal_goto('');    else drupal_goto('');
964  }  }
965  /**  /**
966   * Get response from PayPal and change order's status due to payment status.   * Get response from PayPal and change order's status due to payment status.
967   *   *