示例#1
0
 /**
  * Create the Invoice Record and send user to checkout	 
  */
 function checkoutnow($VAR)
 {
     global $C_translate, $C_list, $smarty;
     $db =& DB();
     // Validate user is logged in:
     if (!SESS_LOGGED) {
         echo '<script language="JavaScript">alert("You must be logged in to complete this purchase! Please refresh this page in your browser to login now...");</script>';
         return false;
     }
     // check for admin
     if (!$this->admin_checkout && !empty($VAR['account_id'])) {
         global $C_auth;
         if (!empty($VAR['account_id']) && $C_auth->auth_method_by_name('checkout', 'admin_checkoutnow')) {
             $this->account_id = $VAR['account_id'];
             $this->admin_checkout = true;
         } else {
             $this->account_id = SESS_ACCOUNT;
         }
     }
     if (empty($this->session_id)) {
         $this->session_id = SESS;
     }
     if (empty($this->account_id)) {
         $this->account_id = SESS_ACCOUNT;
     }
     include_once PATH_MODULES . '/cart/cart.inc.php';
     $cartObj = new cart();
     $cartObj->account_id = $this->account_id;
     $cartObj->session_id = $this->session_id;
     $result = $cartObj->get_contents($db);
     if ($result->RecordCount() == 0) {
         return false;
     }
     // load invoice object
     include_once PATH_MODULES . 'invoice/invoice.inc.php';
     $invoice = new invoice();
     $invoice->account_id = $this->account_id;
     $invoice->initNew(0);
     // Get the account details:
     $account = $db->Execute(sqlSelect($db, "account", "*", "id=::{$this->account_id}::"));
     $invoice->country_id = $account->fields['country_id'];
     $invoice->state = $account->fields['state'];
     // load tax object for tax calculation
     include_once PATH_MODULES . 'tax/tax.inc.php';
     $taxObj = new tax();
     // load discount object for discount calculation
     include_once PATH_MODULES . 'discount/discount.inc.php';
     $discountObj = new discount();
     $discountObj->available_discounts($invoice->account_id);
     // put cart contents into invoice format
     $cartObj->put_contents_invoice($db, $result, $invoice, $smart, $taxObj, $discountObj);
     // Validate and init a checkout plugin
     $checkout = false;
     if ($this->admin_checkout_option) {
         // admin checkout option specified
         include_once PATH_MODULES . 'checkout/checkout_admin.inc.php';
         $PLG = new checkout_admin();
         $checkout = true;
         $invoice->checkout_plugin_id = false;
     } else {
         // get available checkout options and check against the one provided
         $invoice->checkout_plugin_id = $VAR['option'];
         foreach ($invoice->invoice_item as $item) {
             if (!empty($item['product_id'])) {
                 $product_arr[] = $item['product_id'];
             }
         }
         $checkout_options = $this->get_checkout_options($this->account_id, $invoice->total_amt, @$product_arr, $invoice->country_id, $invoice->any_new, $invoice->any_trial, $invoice->any_recurring);
         if ($checkout_options) {
             foreach ($checkout_options as $a) {
                 if ($a['fields']['id'] == $invoice->checkout_plugin_id) {
                     // load the selected checkout plugin and run pre-validation
                     $checkout_plugin = $a['fields']['checkout_plugin'];
                     $plugin_file = PATH_PLUGINS . 'checkout/' . $checkout_plugin . '.php';
                     include_once $plugin_file;
                     eval('$PLG = new plg_chout_' . $checkout_plugin . '("' . $invoice->checkout_plugin_id . '");');
                     $plugin_validate = $PLG->validate($VAR, $this);
                     if ($plugin_validate != true) {
                         echo $plugin_validate;
                         return false;
                     }
                     $checkout = true;
                     break;
                 }
             }
         }
     }
     if (!$checkout) {
         echo '<script language=Javascript> alert("Unable to checkout with the selected method, please select another."); </script> ';
         return false;
     }
     // validate credit card on file details
     global $VAR;
     if (!empty($VAR['account_billing_id']) && @$VAR['new_card'] == 2) {
         $invoice->account_billing_id = $VAR['account_billing_id'];
         /* validate credit card on file details */
         if (!$PLG->setBillingFromDB($this->account_id, $invoice->account_billing_id, $invoice->checkout_plugin_id)) {
             global $C_debug;
             $C_debug->alert("Sorry, we cannot use that billing record for this purchase.");
             return false;
         }
     } else {
         /* use passed in vars */
         $PLG->setBillingFromParams($VAR);
     }
     // validate recurring processing options
     if ($PLG->recurr_only) {
         if ($invoice->recur_amt <= 0) {
             echo '<script language=Javascript> alert("Cannot process non-recurring charges with this payment option, please select another payment option."); </script> ';
             return false;
         }
         if (is_array($invoice->recur_arr) && count($invoice->recur_arr) > 1) {
             $recurring = true;
             // validate recur day and recurring schedule are the same for both products
             foreach ($invoice->recur_arr as $a) {
                 foreach ($invoice->recur_arr as $b) {
                     foreach ($b as $key => $val) {
                         if ($key != 'price' && $key != 'recurr_week' && $a[$key] != $val) {
                             $recurring = false;
                             break;
                         }
                     }
                 }
             }
             if (!$recurring) {
                 echo '<script language=Javascript> alert("This payment option cannot be used when ordering both prorated and non-prorated subscriptions, or when ordering two or more subscriptions with different billing schedules selected. Please make sure all your subscriptions have the same billing schedule selected, try another payment option, or order one subscription at a time. We apologize for any inconvenience."); </script> ';
                 return false;
             }
         }
     }
     # Affiliate
     if (empty($this->affiliate_id)) {
         if (!empty($account->fields['affiliate_id'])) {
             $invoice->affiliate_id = $account->fields['affiliate_id'];
         } else {
             $invoice->affiliate_id = SESS_AFFILIATE;
         }
     }
     # Campaign
     if (empty($this->campaign_id)) {
         if (!empty($account->fields['campaign_id'])) {
             $invoice->campaign_id = $account->fields['campaign_id'];
         } else {
             $invoice->campaign_id = SESS_CAMPAIGN;
         }
     }
     $invoice->record_id = sqlGenID($db, "invoice");
     $invoice->actual_billed_currency_id = SESS_CURRENCY;
     $invoice->billed_currency_id = DEFAULT_CURRENCY;
     $invoice->checkout_type = $PLG->type;
     // initial invoice status
     if ($invoice->total_amt == 0 || $PLG->type == 'gateway') {
         $invoice->billing_status = 1;
         $invoice->actual_billed_amt = $C_list->format_currency_decimal($invoice->total_amt, SESS_CURRENCY);
         $invoice->billed_amt = $invoice->total_amt;
     }
     // Currency conversion:
     if (SESS_CURRENCY != DEFAULT_CURRENCY) {
         $bill_amt = $C_list->format_currency_decimal($invoice->total_amt, SESS_CURRENCY);
         $recur_amt = $C_list->format_currency_decimal($invoice->recur_amt, SESS_CURRENCY);
     } else {
         $bill_amt = round($invoice->total_amt, 2);
         $recur_amt = round($invoice->recur_amt, 2);
     }
     // Get currency ISO (three_digit) for checkout plugin
     $currrs = $db->Execute(sqlSelect($db, "currency", "three_digit", "id=" . SESS_CURRENCY));
     if ($currrs && $currrs->RecordCount()) {
         $currency_iso = $currrs->fields['three_digit'];
     }
     // Run the plugin bill_checkout() method:
     $currency_iso = $C_list->currency_iso(SESS_CURRENCY);
     $invoice->checkout_plugin_data = $PLG->bill_checkout($bill_amt, $invoice->record_id, $currency_iso, $account->fields, $recur_amt, $invoice->recur_arr);
     if ($invoice->checkout_plugin_data === false || $invoice->checkout_plugin_data == '') {
         if (!empty($PLG->redirect)) {
             echo $PLG->redirect;
         }
         return false;
     } elseif ($PLG->type == "gateway" || empty($PLG->redirect)) {
         $VAR['id'] = $invoice->record_id;
         if (!$this->admin_checkout) {
             $VAR['_page'] = "invoice:thankyou";
         }
         $invoice->checkout_plugin_data = false;
     } elseif (!$this->admin_checkout) {
         echo "<html><head></head><body><center>\n\t\t\t\tPlease wait while we redirect you to the secure payment site....\n\t\t\t\t{$PLG->redirect}</center></body></html>";
     }
     // Call the Plugin method for storing the checkout data:
     $invoice->account_billing_id = $PLG->store_billing($VAR, $invoice->account_id);
     // clear user discounts
     $fields = array('discounts' => "");
     $db->Execute(sqlUpdate($db, "session", $fields, "id = ::" . SESS . "::"));
     // admin options
     $email = true;
     if ($this->admin_checkout) {
         if (empty($VAR['send_email']) || $VAR['send_email'] == 'false') {
             $email = false;
         } else {
             $email = true;
         }
         if (!empty($VAR['due_date'])) {
             $invoice->due_date = $this->getInputDate($VAR['due_date']);
         }
         if (!empty($VAR['grace_period'])) {
             $invoice->grace_period = $VAR['grace_period'];
         }
         if (!empty($VAR['notice_max'])) {
             $invoice->notice_max = $VAR['notice_max'];
         }
     }
     if ($invoice->commitNew($taxObj, $discountObj, $email)) {
         // delete all cart items
         $db->Execute(sqlDelete($db, "cart", "(session_id=::" . SESS . ":: OR account_id={$invoice->account_id})"));
         // admin redirect
         if ($this->admin_checkout) {
             $url = URL . 'admin.php?_page=invoice:view&id=' . $invoice->record_id;
             echo '<script language="javascript"> parent.location.href=\'' . $url . '\';</script>';
         }
     }
     return false;
 }