home *** CD-ROM | disk | FTP | other *** search
/ Practical Internet Web Designer 90 / PIWD90.iso / pc / contents / ecommerce / software / osCommerce.exe / oscommerce-2.2ms2 / catalog / checkout_process.php < prev    next >
PHP Script  |  2003-05-28  |  16KB  |  281 lines

  1. <?php
  2. /*
  3.   $Id: checkout_process.php,v 1.128 2003/05/28 18:00:29 hpdl Exp $
  4.  
  5.   osCommerce, Open Source E-Commerce Solutions
  6.   http://www.oscommerce.com
  7.  
  8.   Copyright (c) 2003 osCommerce
  9.  
  10.   Released under the GNU General Public License
  11. */
  12.  
  13.   include('includes/application_top.php');
  14.  
  15. // if the customer is not logged on, redirect them to the login page
  16.   if (!tep_session_is_registered('customer_id')) {
  17.     $navigation->set_snapshot(array('mode' => 'SSL', 'page' => FILENAME_CHECKOUT_PAYMENT));
  18.     tep_redirect(tep_href_link(FILENAME_LOGIN, '', 'SSL'));
  19.   }
  20.   
  21.   if (!tep_session_is_registered('sendto')) {
  22.     tep_redirect(tep_href_link(FILENAME_CHECKOUT_PAYMENT, '', 'SSL'));
  23.   }
  24.  
  25.   if ( (tep_not_null(MODULE_PAYMENT_INSTALLED)) && (!tep_session_is_registered('payment')) ) {
  26.     tep_redirect(tep_href_link(FILENAME_CHECKOUT_PAYMENT, '', 'SSL'));
  27.  }
  28.  
  29. // avoid hack attempts during the checkout procedure by checking the internal cartID
  30.   if (isset($cart->cartID) && tep_session_is_registered('cartID')) {
  31.     if ($cart->cartID != $cartID) {
  32.       tep_redirect(tep_href_link(FILENAME_CHECKOUT_SHIPPING, '', 'SSL'));
  33.     }
  34.   }
  35.  
  36.   include(DIR_WS_LANGUAGES . $language . '/' . FILENAME_CHECKOUT_PROCESS);
  37.  
  38. // load selected payment module
  39.   require(DIR_WS_CLASSES . 'payment.php');
  40.   $payment_modules = new payment($payment);
  41.  
  42. // load the selected shipping module
  43.   require(DIR_WS_CLASSES . 'shipping.php');
  44.   $shipping_modules = new shipping($shipping);
  45.  
  46.   require(DIR_WS_CLASSES . 'order.php');
  47.   $order = new order;
  48.  
  49. // load the before_process function from the payment modules
  50.   $payment_modules->before_process();
  51.  
  52.   require(DIR_WS_CLASSES . 'order_total.php');
  53.   $order_total_modules = new order_total;
  54.  
  55.   $order_totals = $order_total_modules->process();
  56.  
  57.   $sql_data_array = array('customers_id' => $customer_id,
  58.                           'customers_name' => $order->customer['firstname'] . ' ' . $order->customer['lastname'],
  59.                           'customers_company' => $order->customer['company'],
  60.                           'customers_street_address' => $order->customer['street_address'],
  61.                           'customers_suburb' => $order->customer['suburb'],
  62.                           'customers_city' => $order->customer['city'],
  63.                           'customers_postcode' => $order->customer['postcode'], 
  64.                           'customers_state' => $order->customer['state'], 
  65.                           'customers_country' => $order->customer['country']['title'], 
  66.                           'customers_telephone' => $order->customer['telephone'], 
  67.                           'customers_email_address' => $order->customer['email_address'],
  68.                           'customers_address_format_id' => $order->customer['format_id'], 
  69.                           'delivery_name' => $order->delivery['firstname'] . ' ' . $order->delivery['lastname'], 
  70.                           'delivery_company' => $order->delivery['company'],
  71.                           'delivery_street_address' => $order->delivery['street_address'], 
  72.                           'delivery_suburb' => $order->delivery['suburb'], 
  73.                           'delivery_city' => $order->delivery['city'], 
  74.                           'delivery_postcode' => $order->delivery['postcode'], 
  75.                           'delivery_state' => $order->delivery['state'], 
  76.                           'delivery_country' => $order->delivery['country']['title'], 
  77.                           'delivery_address_format_id' => $order->delivery['format_id'], 
  78.                           'billing_name' => $order->billing['firstname'] . ' ' . $order->billing['lastname'], 
  79.                           'billing_company' => $order->billing['company'],
  80.                           'billing_street_address' => $order->billing['street_address'], 
  81.                           'billing_suburb' => $order->billing['suburb'], 
  82.                           'billing_city' => $order->billing['city'], 
  83.                           'billing_postcode' => $order->billing['postcode'], 
  84.                           'billing_state' => $order->billing['state'], 
  85.                           'billing_country' => $order->billing['country']['title'], 
  86.                           'billing_address_format_id' => $order->billing['format_id'], 
  87.                           'payment_method' => $order->info['payment_method'], 
  88.                           'cc_type' => $order->info['cc_type'], 
  89.                           'cc_owner' => $order->info['cc_owner'], 
  90.                           'cc_number' => $order->info['cc_number'], 
  91.                           'cc_expires' => $order->info['cc_expires'], 
  92.                           'date_purchased' => 'now()', 
  93.                           'orders_status' => $order->info['order_status'], 
  94.                           'currency' => $order->info['currency'], 
  95.                           'currency_value' => $order->info['currency_value']);
  96.   tep_db_perform(TABLE_ORDERS, $sql_data_array);
  97.   $insert_id = tep_db_insert_id();
  98.   for ($i=0, $n=sizeof($order_totals); $i<$n; $i++) {
  99.     $sql_data_array = array('orders_id' => $insert_id,
  100.                             'title' => $order_totals[$i]['title'],
  101.                             'text' => $order_totals[$i]['text'],
  102.                             'value' => $order_totals[$i]['value'], 
  103.                             'class' => $order_totals[$i]['code'], 
  104.                             'sort_order' => $order_totals[$i]['sort_order']);
  105.     tep_db_perform(TABLE_ORDERS_TOTAL, $sql_data_array);
  106.   }
  107.  
  108.   $customer_notification = (SEND_EMAILS == 'true') ? '1' : '0';
  109.   $sql_data_array = array('orders_id' => $insert_id, 
  110.                           'orders_status_id' => $order->info['order_status'], 
  111.                           'date_added' => 'now()', 
  112.                           'customer_notified' => $customer_notification,
  113.                           'comments' => $order->info['comments']);
  114.   tep_db_perform(TABLE_ORDERS_STATUS_HISTORY, $sql_data_array);
  115.  
  116. // initialized for the email confirmation
  117.   $products_ordered = '';
  118.   $subtotal = 0;
  119.   $total_tax = 0;
  120.  
  121.   for ($i=0, $n=sizeof($order->products); $i<$n; $i++) {
  122. // Stock Update - Joao Correia
  123.     if (STOCK_LIMITED == 'true') {
  124.       if (DOWNLOAD_ENABLED == 'true') {
  125.         $stock_query_raw = "SELECT products_quantity, pad.products_attributes_filename 
  126.                             FROM " . TABLE_PRODUCTS . " p
  127.                             LEFT JOIN " . TABLE_PRODUCTS_ATTRIBUTES . " pa
  128.                              ON p.products_id=pa.products_id
  129.                             LEFT JOIN " . TABLE_PRODUCTS_ATTRIBUTES_DOWNLOAD . " pad
  130.                              ON pa.products_attributes_id=pad.products_attributes_id
  131.                             WHERE p.products_id = '" . tep_get_prid($order->products[$i]['id']) . "'";
  132. // Will work with only one option for downloadable products
  133. // otherwise, we have to build the query dynamically with a loop
  134.         $products_attributes = $order->products[$i]['attributes'];
  135.         if (is_array($products_attributes)) {
  136.           $stock_query_raw .= " AND pa.options_id = '" . $products_attributes[0]['option_id'] . "' AND pa.options_values_id = '" . $products_attributes[0]['value_id'] . "'";
  137.         }
  138.         $stock_query = tep_db_query($stock_query_raw);
  139.       } else {
  140.         $stock_query = tep_db_query("select products_quantity from " . TABLE_PRODUCTS . " where products_id = '" . tep_get_prid($order->products[$i]['id']) . "'");
  141.       }
  142.       if (tep_db_num_rows($stock_query) > 0) {
  143.         $stock_values = tep_db_fetch_array($stock_query);
  144. // do not decrement quantities if products_attributes_filename exists
  145.         if ((DOWNLOAD_ENABLED != 'true') || (!$stock_values['products_attributes_filename'])) {
  146.           $stock_left = $stock_values['products_quantity'] - $order->products[$i]['qty'];
  147.         } else {
  148.           $stock_left = $stock_values['products_quantity'];
  149.         }
  150.         tep_db_query("update " . TABLE_PRODUCTS . " set products_quantity = '" . $stock_left . "' where products_id = '" . tep_get_prid($order->products[$i]['id']) . "'");
  151.         if ( ($stock_left < 1) && (STOCK_ALLOW_CHECKOUT == 'false') ) {
  152.           tep_db_query("update " . TABLE_PRODUCTS . " set products_status = '0' where products_id = '" . tep_get_prid($order->products[$i]['id']) . "'");
  153.         }
  154.       }
  155.     }
  156.  
  157. // Update products_ordered (for bestsellers list)
  158.     tep_db_query("update " . TABLE_PRODUCTS . " set products_ordered = products_ordered + " . sprintf('%d', $order->products[$i]['qty']) . " where products_id = '" . tep_get_prid($order->products[$i]['id']) . "'");
  159.  
  160.     $sql_data_array = array('orders_id' => $insert_id, 
  161.                             'products_id' => tep_get_prid($order->products[$i]['id']), 
  162.                             'products_model' => $order->products[$i]['model'], 
  163.                             'products_name' => $order->products[$i]['name'], 
  164.                             'products_price' => $order->products[$i]['price'], 
  165.                             'final_price' => $order->products[$i]['final_price'], 
  166.                             'products_tax' => $order->products[$i]['tax'], 
  167.                             'products_quantity' => $order->products[$i]['qty']);
  168.     tep_db_perform(TABLE_ORDERS_PRODUCTS, $sql_data_array);
  169.     $order_products_id = tep_db_insert_id();
  170.  
  171. //------insert customer choosen option to order--------
  172.     $attributes_exist = '0';
  173.     $products_ordered_attributes = '';
  174.     if (isset($order->products[$i]['attributes'])) {
  175.       $attributes_exist = '1';
  176.       for ($j=0, $n2=sizeof($order->products[$i]['attributes']); $j<$n2; $j++) {
  177.         if (DOWNLOAD_ENABLED == 'true') {
  178.           $attributes_query = "select popt.products_options_name, poval.products_options_values_name, pa.options_values_price, pa.price_prefix, pad.products_attributes_maxdays, pad.products_attributes_maxcount , pad.products_attributes_filename 
  179.                                from " . TABLE_PRODUCTS_OPTIONS . " popt, " . TABLE_PRODUCTS_OPTIONS_VALUES . " poval, " . TABLE_PRODUCTS_ATTRIBUTES . " pa 
  180.                                left join " . TABLE_PRODUCTS_ATTRIBUTES_DOWNLOAD . " pad
  181.                                 on pa.products_attributes_id=pad.products_attributes_id
  182.                                where pa.products_id = '" . $order->products[$i]['id'] . "' 
  183.                                 and pa.options_id = '" . $order->products[$i]['attributes'][$j]['option_id'] . "' 
  184.                                 and pa.options_id = popt.products_options_id 
  185.                                 and pa.options_values_id = '" . $order->products[$i]['attributes'][$j]['value_id'] . "' 
  186.                                 and pa.options_values_id = poval.products_options_values_id 
  187.                                 and popt.language_id = '" . $languages_id . "' 
  188.                                 and poval.language_id = '" . $languages_id . "'";
  189.           $attributes = tep_db_query($attributes_query);
  190.         } else {
  191.           $attributes = tep_db_query("select popt.products_options_name, poval.products_options_values_name, pa.options_values_price, pa.price_prefix from " . TABLE_PRODUCTS_OPTIONS . " popt, " . TABLE_PRODUCTS_OPTIONS_VALUES . " poval, " . TABLE_PRODUCTS_ATTRIBUTES . " pa where pa.products_id = '" . $order->products[$i]['id'] . "' and pa.options_id = '" . $order->products[$i]['attributes'][$j]['option_id'] . "' and pa.options_id = popt.products_options_id and pa.options_values_id = '" . $order->products[$i]['attributes'][$j]['value_id'] . "' and pa.options_values_id = poval.products_options_values_id and popt.language_id = '" . $languages_id . "' and poval.language_id = '" . $languages_id . "'");
  192.         }
  193.         $attributes_values = tep_db_fetch_array($attributes);
  194.  
  195.         $sql_data_array = array('orders_id' => $insert_id, 
  196.                                 'orders_products_id' => $order_products_id, 
  197.                                 'products_options' => $attributes_values['products_options_name'],
  198.                                 'products_options_values' => $attributes_values['products_options_values_name'], 
  199.                                 'options_values_price' => $attributes_values['options_values_price'], 
  200.                                 'price_prefix' => $attributes_values['price_prefix']);
  201.         tep_db_perform(TABLE_ORDERS_PRODUCTS_ATTRIBUTES, $sql_data_array);
  202.  
  203.         if ((DOWNLOAD_ENABLED == 'true') && isset($attributes_values['products_attributes_filename']) && tep_not_null($attributes_values['products_attributes_filename'])) {
  204.           $sql_data_array = array('orders_id' => $insert_id, 
  205.                                   'orders_products_id' => $order_products_id, 
  206.                                   'orders_products_filename' => $attributes_values['products_attributes_filename'], 
  207.                                   'download_maxdays' => $attributes_values['products_attributes_maxdays'], 
  208.                                   'download_count' => $attributes_values['products_attributes_maxcount']);
  209.           tep_db_perform(TABLE_ORDERS_PRODUCTS_DOWNLOAD, $sql_data_array);
  210.         }
  211.         $products_ordered_attributes .= "\n\t" . $attributes_values['products_options_name'] . ' ' . $attributes_values['products_options_values_name'];
  212.       }
  213.     }
  214. //------insert customer choosen option eof ----
  215.     $total_weight += ($order->products[$i]['qty'] * $order->products[$i]['weight']);
  216.     $total_tax += tep_calculate_tax($total_products_price, $products_tax) * $order->products[$i]['qty'];
  217.     $total_cost += $total_products_price;
  218.  
  219.     $products_ordered .= $order->products[$i]['qty'] . ' x ' . $order->products[$i]['name'] . ' (' . $order->products[$i]['model'] . ') = ' . $currencies->display_price($order->products[$i]['final_price'], $order->products[$i]['tax'], $order->products[$i]['qty']) . $products_ordered_attributes . "\n";
  220.   }
  221.  
  222. // lets start with the email confirmation
  223.   $email_order = STORE_NAME . "\n" . 
  224.                  EMAIL_SEPARATOR . "\n" . 
  225.                  EMAIL_TEXT_ORDER_NUMBER . ' ' . $insert_id . "\n" .
  226.                  EMAIL_TEXT_INVOICE_URL . ' ' . tep_href_link(FILENAME_ACCOUNT_HISTORY_INFO, 'order_id=' . $insert_id, 'SSL', false) . "\n" .
  227.                  EMAIL_TEXT_DATE_ORDERED . ' ' . strftime(DATE_FORMAT_LONG) . "\n\n";
  228.   if ($order->info['comments']) {
  229.     $email_order .= tep_db_output($order->info['comments']) . "\n\n";
  230.   }
  231.   $email_order .= EMAIL_TEXT_PRODUCTS . "\n" . 
  232.                   EMAIL_SEPARATOR . "\n" . 
  233.                   $products_ordered . 
  234.                   EMAIL_SEPARATOR . "\n";
  235.  
  236.   for ($i=0, $n=sizeof($order_totals); $i<$n; $i++) {
  237.     $email_order .= strip_tags($order_totals[$i]['title']) . ' ' . strip_tags($order_totals[$i]['text']) . "\n";
  238.   }
  239.  
  240.   if ($order->content_type != 'virtual') {
  241.     $email_order .= "\n" . EMAIL_TEXT_DELIVERY_ADDRESS . "\n" . 
  242.                     EMAIL_SEPARATOR . "\n" .
  243.                     tep_address_label($customer_id, $sendto, 0, '', "\n") . "\n";
  244.   }
  245.  
  246.   $email_order .= "\n" . EMAIL_TEXT_BILLING_ADDRESS . "\n" .
  247.                   EMAIL_SEPARATOR . "\n" .
  248.                   tep_address_label($customer_id, $billto, 0, '', "\n") . "\n\n";
  249.   if (is_object($$payment)) {
  250.     $email_order .= EMAIL_TEXT_PAYMENT_METHOD . "\n" . 
  251.                     EMAIL_SEPARATOR . "\n";
  252.     $payment_class = $$payment;
  253.     $email_order .= $payment_class->title . "\n\n";
  254.     if ($payment_class->email_footer) { 
  255.       $email_order .= $payment_class->email_footer . "\n\n";
  256.     }
  257.   }
  258.   tep_mail($order->customer['firstname'] . ' ' . $order->customer['lastname'], $order->customer['email_address'], EMAIL_TEXT_SUBJECT, $email_order, STORE_OWNER, STORE_OWNER_EMAIL_ADDRESS);
  259.  
  260. // send emails to other people
  261.   if (SEND_EXTRA_ORDER_EMAILS_TO != '') {
  262.     tep_mail('', SEND_EXTRA_ORDER_EMAILS_TO, EMAIL_TEXT_SUBJECT, $email_order, STORE_OWNER, STORE_OWNER_EMAIL_ADDRESS);
  263.   }
  264.  
  265. // load the after_process function from the payment modules
  266.   $payment_modules->after_process();
  267.  
  268.   $cart->reset(true);
  269.  
  270. // unregister session variables used during checkout
  271.   tep_session_unregister('sendto');
  272.   tep_session_unregister('billto');
  273.   tep_session_unregister('shipping');
  274.   tep_session_unregister('payment');
  275.   tep_session_unregister('comments');
  276.  
  277.   tep_redirect(tep_href_link(FILENAME_CHECKOUT_SUCCESS, '', 'SSL'));
  278.  
  279.   require(DIR_WS_INCLUDES . 'application_bottom.php');
  280. ?>
  281.