/**
  * Action that gets called before we interface with our payment
  * method.
  *
  * This action is responsible for setting up an order and
  * saving it into the database (as well as a session) and then hands
  * the current request over to the relevent payment handler
  * for final processing.
  *
  * @param $request Current request object
  */
 public function index($request)
 {
     $cart = ShoppingCart::get();
     $data = array();
     $payment_data = array();
     $handler = $this->payment_handler;
     // If shopping cart doesn't exist, redirect to base
     if (!$cart->getItems()->exists() || $this->getPaymentHandler() === null) {
         return $this->redirect($cart->Link());
     }
     // Get billing and delivery details and merge into an array
     $billing_data = Session::get("Checkout.BillingDetailsForm.data");
     $delivery_data = Session::get("Checkout.DeliveryDetailsForm.data");
     // If we have applied free shipping, set that up, else get
     if (Session::get('Checkout.PostageID') == -1) {
         $postage = Checkout::CreateFreePostageObject();
     } else {
         $postage = PostageArea::get()->byID(Session::get('Checkout.PostageID'));
     }
     // If we are using a complex checkout and do not have correct
     // details redirect
     if (!Checkout::config()->simple_checkout && !$cart->isCollection() && (!$postage || !$billing_data || !$delivery_data)) {
         return $this->redirect(Checkout_Controller::create()->Link());
     }
     if ($cart->isCollection() && !$billing_data) {
         return $this->redirect(Checkout_Controller::create()->Link());
     }
     // Create an order number
     $data["OrderNumber"] = substr(chunk_split(Checkout::getRandomNumber(), 4, '-'), 0, -1);
     // Setup holder for Payment ID
     $data["PaymentID"] = 0;
     // Set status
     $data['Status'] = 'incomplete';
     // Assign billing, delivery and postage data
     if (!Checkout::config()->simple_checkout) {
         $data = array_merge($data, $billing_data);
         $data = is_array($delivery_data) ? array_merge($data, $delivery_data) : $data;
         $checkout_data = Checkout::config()->checkout_data;
         if (!$cart->isCollection()) {
             $data['PostageType'] = $postage->Title;
             $data['PostageCost'] = $postage->Cost;
             $data['PostageTax'] = $postage->Tax ? $postage->Cost / 100 * $postage->Tax : 0;
         }
         if ($cart->getDiscount()) {
             $data['Discount'] = $cart->getDiscount()->Title;
             $data['DiscountAmount'] = $cart->DiscountAmount;
         }
         // Add full country names if needed
         if (in_array("CountryFull", $checkout_data)) {
             $data['CountryFull'] = Checkout::country_name_from_code($data["Country"]);
         }
         if (in_array("DeliveryCountryFull", $checkout_data) && array_key_exists("DeliveryCountry", $data)) {
             $data['DeliveryCountryFull'] = Checkout::country_name_from_code($data["DeliveryCountry"]);
         }
         foreach ($checkout_data as $key) {
             if (array_key_exists($key, $data)) {
                 $payment_data[$key] = $data[$key];
             }
         }
     }
     // Set our order data as a generic object
     $handler->setOrderData(ArrayData::array_to_object($payment_data));
     return $handler->handleRequest($request, $this->model);
 }
 /**
  * Action that gets called before we interface with our payment
  * method.
  *
  * This action is responsible for setting up an order and
  * saving it into the database (as well as a session) and also then
  * generating an order summary before the user performs any final
  * actions needed.
  *
  * This action is then mapped directly to the index action of the
  * Handler for the payment method that was selected by the user
  * in the "Postage and Payment" form.
  *
  */
 public function index()
 {
     $cart = ShoppingCart::get();
     // If shopping cart doesn't exist, redirect to base
     if (!$cart->getItems()->exists() || $this->getPaymentHandler() === null) {
         return $this->redirect(Director::BaseURL());
     }
     // Get billing and delivery details and merge into an array
     $billing_data = Session::get("Commerce.BillingDetailsForm.data");
     $delivery_data = Session::get("Commerce.DeliveryDetailsForm.data");
     $postage = PostageArea::get()->byID(Session::get('Commerce.PostageID'));
     if (!$postage || !$billing_data || !$delivery_data) {
         return $this->redirect(Checkout_Controller::create()->Link());
     }
     // Work out if an order prefix string has been set in siteconfig
     $config = SiteConfig::current_site_config();
     $order_prefix = $config->OrderPrefix ? $config->OrderPrefix . '-' : '';
     // Merge billand and delivery data into an array
     $data = array_merge((array) $billing_data, (array) $delivery_data);
     // Set discount info
     $data['DiscountAmount'] = $cart->DiscountAmount();
     // Get postage data
     $data['PostageType'] = $postage->Title;
     $data['PostageCost'] = $postage->Cost;
     $data['PostageTax'] = $config->TaxRate > 0 && $postage->Cost > 0 ? (double) $postage->Cost / 100 * $config->TaxRate : 0;
     // Set status
     $data['Status'] = 'incomplete';
     // Setup an order based on the data from the shopping cart and load data
     $order = new Order();
     $order->update($data);
     // If user logged in, track it against an order
     if (Member::currentUserID()) {
         $order->CustomerID = Member::currentUserID();
     }
     // Write so we can setup our foreign keys
     $order->write();
     // Loop through each session cart item and add that item to the order
     foreach ($cart->getItems() as $cart_item) {
         $order_item = new OrderItem();
         $order_item->Title = $cart_item->Title;
         $order_item->SKU = $cart_item->SKU;
         $order_item->Price = $cart_item->Price;
         $order_item->Tax = $cart_item->Tax;
         $order_item->Customisation = serialize($cart_item->Customised);
         $order_item->Quantity = $cart_item->Quantity;
         $order_item->write();
         $order->Items()->add($order_item);
     }
     $order->write();
     // Add order to session so our payment handler can process it
     Session::set("Commerce.Order", $order);
     $this->payment_handler->setOrder($order);
     // Get gateway data
     $return = $this->payment_handler->index();
     return $this->customise($return)->renderWith(array("Payment", "Commerce", "Page"));
 }