/**
  * order()
  * Processes orders by passing transaction information to the active
  * payment gateway */
 function order($gateway = false)
 {
     global $Shopp;
     $Cart = $Shopp->Cart;
     $db = DB::get();
     do_action('shopp_order_preprocessing');
     $Order = $Shopp->Cart->data->Order;
     $Order->Totals = $Shopp->Cart->data->Totals;
     $Order->Items = $Shopp->Cart->contents;
     $Order->Cart = $Shopp->Cart->session;
     if ($Shopp->Gateway && !$Cart->orderisfree()) {
         // Use an external checkout payment gateway
         if (SHOPP_DEBUG) {
             new ShoppError('Processing order through a remote-payment gateway service.', false, SHOPP_DEBUG_ERR);
         }
         $Purchase = $Shopp->Gateway->process();
         if (!$Purchase) {
             if (SHOPP_DEBUG) {
                 new ShoppError('The remote-payment gateway encountered an error.', false, SHOPP_DEBUG_ERR);
             }
             $Shopp->Gateway->error();
             return false;
         }
         if (SHOPP_DEBUG) {
             new ShoppError('Transaction successfully processed by remote-payment gateway service.', false, SHOPP_DEBUG_ERR);
         }
     } else {
         // Use local payment gateway set in payment settings
         $gateway = $Shopp->Settings->get('payment_gateway');
         // Process a transaction if the order has a cost (is not free)
         if (!$Cart->orderisfree()) {
             if (!$Shopp->gateway($gateway)) {
                 return false;
             }
             // Process the transaction through the payment gateway
             if (SHOPP_DEBUG) {
                 new ShoppError('Processing order through local-payment gateway service.', false, SHOPP_DEBUG_ERR);
             }
             $processed = $Shopp->Gateway->process();
             // exit();
             // There was a problem processing the transaction,
             // grab the error response from the gateway so we can report it
             if (!$processed) {
                 if (SHOPP_DEBUG) {
                     new ShoppError('The local-payment gateway encountered an error.', false, SHOPP_DEBUG_ERR);
                 }
                 $Shopp->Gateway->error();
                 return false;
             }
             $gatewaymeta = $this->scan_gateway_meta(SHOPP_GATEWAYS . $gateway);
             $gatewayname = $gatewaymeta->name;
             $transactionid = $Shopp->Gateway->transactionid();
             if (SHOPP_DEBUG) {
                 new ShoppError('Transaction ' . $transactionid . ' successfully processed by local-payment gateway service ' . $gatewayname . '.', false, SHOPP_DEBUG_ERR);
             }
         } else {
             if (!$Cart->validorder()) {
                 new ShoppError(__('There is not enough customer information to process the order.', 'Shopp'), 'invalid_order', SHOPP_TRXN_ERR);
                 return false;
             }
             $gatewayname = __('N/A', 'Shopp');
             $transactionid = __('(Free Order)', 'Shopp');
         }
         $authentication = $Shopp->Settings->get('account_system');
         // Transaction successful, save the order
         if ($authentication == "wordpress") {
             // Check if they've logged in
             // If the shopper is already logged-in, save their updated customer info
             if ($Shopp->Cart->data->login) {
                 if (SHOPP_DEBUG) {
                     new ShoppError('Customer logged in, linking Shopp customer account to existing WordPress account.', false, SHOPP_DEBUG_ERR);
                 }
                 get_currentuserinfo();
                 global $user_ID;
                 $Order->Customer->wpuser = $user_ID;
             }
             // Create WordPress account (if necessary)
             if (!$Order->Customer->wpuser) {
                 if (SHOPP_DEBUG) {
                     new ShoppError('Creating a new WordPress account for this customer.', false, SHOPP_DEBUG_ERR);
                 }
                 if (!$Order->Customer->new_wpuser()) {
                     new ShoppError(__('Account creation failed on order for customer id:' . $Order->Customer->id, "Shopp"), false, SHOPP_TRXN_ERR);
                 }
             }
         }
         // Create a WP-compatible password hash to go in the db
         if (empty($Order->Customer->id)) {
             $Order->Customer->password = wp_hash_password($Order->Customer->password);
         }
         $Order->Customer->save();
         $Order->Billing->customer = $Order->Customer->id;
         $Order->Billing->card = substr($Order->Billing->card, -4);
         $Order->Billing->save();
         // Card data is truncated, switch the cart to normal mode
         if ($Shopp->Cart->secured() && is_shopp_secure()) {
             $Shopp->Cart->secured(false);
         }
         if (!empty($Order->Shipping->address)) {
             $Order->Shipping->customer = $Order->Customer->id;
             $Order->Shipping->save();
         }
         $Promos = array();
         foreach ($Shopp->Cart->data->PromosApplied as $promo) {
             $Promos[$promo->id] = $promo->name;
         }
         if ($Shopp->Cart->orderisfree()) {
             $orderisfree = true;
         } else {
             $orderisfree = false;
         }
         $Purchase = new Purchase();
         $Purchase->customer = $Order->Customer->id;
         $Purchase->billing = $Order->Billing->id;
         $Purchase->shipping = $Order->Shipping->id;
         $Purchase->copydata($Order->Customer);
         $Purchase->copydata($Order->Billing);
         $Purchase->copydata($Order->Shipping, 'ship');
         $Purchase->copydata($Shopp->Cart->data->Totals);
         $Purchase->data = $Order->data;
         $Purchase->promos = $Promos;
         $Purchase->freight = $Shopp->Cart->data->Totals->shipping;
         $Purchase->gateway = $gatewayname;
         $Purchase->transactionid = $transactionid;
         $Purchase->transtatus = "CHARGED";
         $Purchase->ip = $Shopp->Cart->ip;
         $Purchase->save();
         // echo "<pre>"; print_r($Purchase); echo "</pre>";
         foreach ($Shopp->Cart->contents as $Item) {
             $Purchased = new Purchased();
             $Purchased->copydata($Item);
             $Purchased->purchase = $Purchase->id;
             if (!empty($Purchased->download)) {
                 $Purchased->keygen();
             }
             $Purchased->save();
             if ($Item->inventory) {
                 $Item->unstock();
             }
         }
         if (SHOPP_DEBUG) {
             new ShoppError('Purchase ' . $Purchase->id . ' was successfully saved to the database.', false, SHOPP_DEBUG_ERR);
         }
     }
     // Skip post order if no Purchase ID exists
     if (empty($Purchase->id)) {
         return true;
     }
     // Empty cart on successful order
     $Shopp->Cart->unload();
     session_destroy();
     // Start new cart session
     $Shopp->Cart = new Cart();
     session_start();
     // Keep the user logged in or log them in if they are a new customer
     if ($Shopp->Cart->data->login || $authentication != "none") {
         $Shopp->Cart->loggedin($Order->Customer);
     }
     // Save the purchase ID for later lookup
     $Shopp->Cart->data->Purchase = new Purchase($Purchase->id);
     $Shopp->Cart->data->Purchase->load_purchased();
     // // $Shopp->Cart->save();
     // Allow other WordPress plugins access to Purchase data to extend
     // what Shopp does after a successful transaction
     do_action_ref_array('shopp_order_success', array(&$Shopp->Cart->data->Purchase));
     // Send email notifications
     // notification(addressee name, email, subject, email template, receipt template)
     $Purchase->notification("{$Purchase->firstname} {$Purchase->lastname}", $Purchase->email, __('Order Receipt', 'Shopp'));
     if ($Shopp->Settings->get('receipt_copy') == 1) {
         $Purchase->notification('', $Shopp->Settings->get('merchant_email'), __('New Order', 'Shopp'));
     }
     $ssl = true;
     // Test Mode will not require encrypted checkout
     if (strpos($gateway, "TestMode.php") !== false || isset($_GET['shopp_xco']) || $orderisfree || SHOPP_NOSSL) {
         $ssl = false;
     }
     shopp_redirect($Shopp->link('receipt', $ssl));
 }
 function order()
 {
     global $Shopp;
     $txnstatus = false;
     $ipnstatus = $this->verifyipn();
     // Validate the order notification
     if ($ipnstatus != "VERIFIED") {
         $txnstatus = $ipnstatus;
         new ShoppError('An unverifiable order notification was received from PayPal. Possible fraudulent order attempt! The order will be created, but the order payment status must be manually set to "Charged" when the payment can be verified.', 'paypal_txn_verification', SHOPP_TRXN_ERR);
     }
     if (!$txnstatus) {
         $txnstatus = $this->status[$_POST['payment_status']];
     }
     $Order = $Shopp->Cart->data->Order;
     $Order->Totals = $Shopp->Cart->data->Totals;
     $Order->Items = $Shopp->Cart->contents;
     $Order->Cart = $Shopp->Cart->session;
     if (SHOPP_DEBUG) {
         new ShoppError('IPN notification validated.', false, SHOPP_DEBUG_ERR);
     }
     // Transaction successful, save the order
     $authentication = $Shopp->Settings->get('account_system');
     if ($authentication == "wordpress") {
         // Check if they've logged in
         // If the shopper is already logged-in, save their updated customer info
         if ($Shopp->Cart->data->login) {
             $user = get_userdata($Order->Customer->wpuser);
             $Order->Customer->wpuser = $user->ID;
             if (SHOPP_DEBUG) {
                 new ShoppError('Customer logged in, linking Shopp customer account to existing WordPress account.', false, SHOPP_DEBUG_ERR);
             }
         }
         // Create WordPress account (if necessary)
         if (!$Order->Customer->wpuser) {
             if (SHOPP_DEBUG) {
                 new ShoppError('Creating a new WordPress account for this customer.', false, SHOPP_DEBUG_ERR);
             }
             if (!$Order->Customer->new_wpuser()) {
                 new ShoppError(__('Account creation failed on order for customer id:' . $Order->Customer->id, "Shopp"), false, SHOPP_TRXN_ERR);
             }
         }
     }
     // Create a WP-compatible password hash to go in the db
     if (empty($Order->Customer->id) && isset($Order->Customer->password)) {
         $Order->Customer->password = wp_hash_password($Order->Customer->password);
     }
     $Order->Customer->save();
     $Order->Billing->customer = $Order->Customer->id;
     $Order->Billing->cardtype = "PayPal";
     $Order->Billing->save();
     if (!empty($Order->Shipping->address)) {
         $Order->Shipping->customer = $Order->Customer->id;
         $Order->Shipping->save();
     }
     $Promos = array();
     foreach ($Shopp->Cart->data->PromosApplied as $promo) {
         $Promos[$promo->id] = $promo->name;
     }
     $Purchase = new Purchase();
     $Purchase->customer = $Order->Customer->id;
     $Purchase->billing = $Order->Billing->id;
     $Purchase->shipping = $Order->Shipping->id;
     $Purchase->data = $Order->data;
     $Purchase->promos = $Promos;
     $Purchase->copydata($Order->Customer);
     $Purchase->copydata($Order->Billing);
     $Purchase->copydata($Order->Shipping, 'ship');
     $Purchase->copydata($Shopp->Cart->data->Totals);
     $Purchase->freight = $Shopp->Cart->data->Totals->shipping;
     $Purchase->gateway = "PayPal" . (isset($_POST['test_ipn']) && $_POST['test_ipn'] == "1" ? " Sandbox" : "");
     $Purchase->transactionid = $_POST['txn_id'];
     $Purchase->transtatus = $txnstatus;
     $Purchase->fees = $_POST['mc_fee'];
     $Purchase->ip = $Shopp->Cart->ip;
     $Purchase->save();
     // echo "<pre>"; print_r($Purchase); echo "</pre>";
     foreach ($Shopp->Cart->contents as $Item) {
         $Purchased = new Purchased();
         $Purchased->copydata($Item);
         $Purchased->purchase = $Purchase->id;
         if (!empty($Purchased->download)) {
             $Purchased->keygen();
         }
         $Purchased->save();
         if ($Item->inventory) {
             $Item->unstock();
         }
     }
     // Empty cart on successful order
     $Shopp->Cart->unload();
     session_destroy();
     // Start new cart session
     $Shopp->Cart = new Cart();
     session_start();
     // Keep the user loggedin
     if ($Shopp->Cart->data->login) {
         $Shopp->Cart->loggedin($Order->Customer);
     }
     // Save the purchase ID for later lookup
     $Shopp->Cart->data->Purchase = new Purchase($Purchase->id);
     $Shopp->Cart->data->Purchase->load_purchased();
     // $Shopp->Cart->save();
     // Allow other WordPress plugins access to Purchase data to extend
     // what Shopp does after a successful transaction
     do_action_ref_array('shopp_order_success', array(&$Shopp->Cart->data->Purchase));
     // Send email notifications
     // notification(addressee name, email, subject, email template, receipt template)
     $Purchase->notification("{$Purchase->firstname} {$Purchase->lastname}", $Purchase->email, __('Order Receipt', 'Shopp'));
     if ($Shopp->Settings->get('receipt_copy') == 1) {
         $Purchase->notification('', $Shopp->Settings->get('merchant_email'), __('New Order', 'Shopp'));
     }
     shopp_redirect($Shopp->link('receipt', false));
 }