public function requireDefaultRecords()
 {
     parent::requireDefaultRecords();
     // See if we need to create downloadable postage
     $records = PostageArea::get()->filter("Title", _t("DownloadableProduct.DownloadableGoods", "Downloadable Goods"));
     if (!$records->exists()) {
         $config = SiteConfig::current_site_config();
         $postage = PostageArea::create();
         $postage->Title = _t("DownloadableProduct.DownloadableGoods", "Downloadable Goods");
         $postage->Country = "*";
         $postage->ZipCode = "*";
         $postage->Calculation = "Weight";
         $postage->Unit = 0.0;
         $postage->Cost = 0.0;
         $postage->Tax = 0.0;
         $postage->SiteID = $config->ID;
         $postage->write();
         DB::alteration_message(_t("DownloadableProduct.AddedPostage", "Added downloadable postage"), 'created');
     }
 }
 /**
  * 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);
 }
 /**
  * Get the cost of postage
  *
  */
 public function PostageCost()
 {
     if ($postage = PostageArea::get()->byID(Session::get("Commerce.PostageID"))) {
         $cost = $postage->Cost;
     } else {
         $cost = 0;
     }
     return number_format($cost, 2);
 }
 /**
  * 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"));
 }
 public function __construct()
 {
     parent::__construct();
     // If items are stored in a session, get them now
     if (Session::get('Checkout.ShoppingCart.Items')) {
         $this->items = unserialize(Session::get('Checkout.ShoppingCart.Items'));
     } else {
         $this->items = ArrayList::create();
     }
     // If discounts stored in a session, get them, else create new list
     if (Session::get('Checkout.ShoppingCart.Discount')) {
         $this->discount = unserialize(Session::get('Checkout.ShoppingCart.Discount'));
     }
     // If we don't have any discounts, a user is logged in and he has
     // access to discounts through a group, add the discount here
     if (!$this->discount && Member::currentUserID()) {
         $member = Member::currentUser();
         $this->discount = $member->getDiscount();
         Session::set('Checkout.ShoppingCart.Discount', serialize($this->discount));
     }
     // Setup postage
     if ($postage = PostageArea::get()->byID(Session::get("Checkout.PostageID"))) {
         $this->postage = $postage;
     }
     // Allow extension of the shopping cart after initial setup
     $this->extend("augmentSetup");
 }