public function index()
 {
     $site = SiteConfig::current_site_config();
     $order = $this->order;
     // Setup the paypal gateway URL
     if (Director::isDev()) {
         $gateway_url = "https://www.sandbox.paypal.com/cgi-bin/webscr";
     } else {
         $gateway_url = "https://www.paypal.com/cgi-bin/webscr";
     }
     $callback_url = Controller::join_links(Director::absoluteBaseURL(), Payment_Controller::config()->url_segment, "callback", $this->payment_gateway->ID);
     $success_url = Controller::join_links(Director::absoluteBaseURL(), Payment_Controller::config()->url_segment, 'complete');
     $error_url = Controller::join_links(Director::absoluteBaseURL(), Payment_Controller::config()->url_segment, 'complete', 'error');
     $back_url = Controller::join_links(Director::absoluteBaseURL(), Checkout_Controller::config()->url_segment, "finish");
     $fields = new FieldList(HiddenField::create('business', null, $this->payment_gateway->BusinessID), HiddenField::create('item_name', null, $site->Title), HiddenField::create('cmd', null, "_cart"), HiddenField::create('paymentaction', null, "sale"), HiddenField::create('invoice', null, $order->OrderNumber), HiddenField::create('custom', null, $order->OrderNumber), HiddenField::create('upload', null, 1), HiddenField::create('discount_amount_cart', null, $order->DiscountAmount), HiddenField::create('amount', null, $order->Total), HiddenField::create('currency_code', null, $site->Currency()->GatewayCode), HiddenField::create('first_name', null, $order->FirstName), HiddenField::create('last_name', null, $order->Surname), HiddenField::create('address1', null, $order->Address1), HiddenField::create('address2', null, $order->Address2), HiddenField::create('city', null, $order->City), HiddenField::create('zip', null, $order->PostCode), HiddenField::create('country', null, $order->Country), HiddenField::create('email', null, $order->Email), HiddenField::create('return', null, $success_url), HiddenField::create('notify_url', null, $callback_url), HiddenField::create('cancel_return', null, $error_url));
     $i = 1;
     foreach ($order->Items() as $item) {
         $fields->add(HiddenField::create('item_name_' . $i, null, $item->Title));
         $fields->add(HiddenField::create('amount_' . $i, null, number_format($item->Price + $item->Tax, 2)));
         $fields->add(HiddenField::create('quantity_' . $i, null, $item->Quantity));
         $i++;
     }
     // Add shipping as an extra product
     $fields->add(HiddenField::create('item_name_' . $i, null, _t("Commerce.Postage", "Postage")));
     $fields->add(HiddenField::create('amount_' . $i, null, number_format($order->PostageCost + $order->PostageTax, 2)));
     $fields->add(HiddenField::create('quantity_' . $i, null, "1"));
     $actions = FieldList::create(LiteralField::create('BackButton', '<a href="' . $back_url . '" class="btn btn-red commerce-action-back">' . _t('Commerce.Back', 'Back') . '</a>'), FormAction::create('Submit', _t('Commerce.ConfirmPay', 'Confirm and Pay'))->addExtraClass('btn')->addExtraClass('btn-green'));
     $form = Form::create($this, 'Form', $fields, $actions)->addExtraClass('forms')->setFormMethod('POST')->setFormAction($gateway_url);
     $this->extend('updateForm', $form);
     return array("Title" => _t('Commerce.CheckoutSummary', "Summary"), "MetaTitle" => _t('Commerce.CheckoutSummary', "Summary"), "Form" => $form);
 }
 /**
  * Default Action
  *
  */
 public function index()
 {
     // Setup payment gateway form
     $back_url = Controller::join_links(Director::absoluteBaseURL(), Checkout_Controller::config()->url_segment, "finish");
     $fields = FieldList::create(HiddenField::create('navigate'), HiddenField::create('VPSProtocol', null, $this->payment_gateway->ProtocolVersion), HiddenField::create('TxType', null, 'PAYMENT'), HiddenField::create('Vendor', null, $this->payment_gateway->VendorName), HiddenField::create('Crypt', null, $this->gateway_data()));
     $actions = FieldList::create(LiteralField::create('BackButton', '<a href="' . $back_url . '" class="btn btn-red commerce-action-back">' . _t('Commerce.Back', 'Back') . '</a>'), FormAction::create('Submit', _t('Commerce.ConfirmPay', 'Confirm and Pay'))->addExtraClass('btn')->addExtraClass('btn-green'));
     $form = Form::create($this, 'Form', $fields, $actions)->addExtraClass('forms')->setFormMethod('POST')->setFormAction($this->payment_gateway->GatewayURL());
     $this->extend('updateForm', $form);
     return array('Title' => _t('Commerce.CheckoutSummary', "Summary"), 'MetaTitle' => _t('Commerce.CheckoutSummary', "Summary"), "Form" => $form);
 }
 public function index($request)
 {
     $this->extend('onBeforeIndex');
     $site = SiteConfig::current_site_config();
     $order = $this->getOrderData();
     $cart = ShoppingCart::get();
     // Setup the paypal gateway URL
     if (Director::isDev()) {
         $gateway_url = "https://www.sandbox.paypal.com/cgi-bin/webscr";
     } else {
         $gateway_url = "https://www.paypal.com/cgi-bin/webscr";
     }
     $callback_url = Controller::join_links(Director::absoluteBaseURL(), Payment_Controller::config()->url_segment, "callback", $this->payment_gateway->ID);
     $success_url = Controller::join_links(Director::absoluteBaseURL(), Payment_Controller::config()->url_segment, 'complete');
     $error_url = Controller::join_links(Director::absoluteBaseURL(), Payment_Controller::config()->url_segment, 'complete', 'error');
     $back_url = Controller::join_links(Director::absoluteBaseURL(), Checkout_Controller::config()->url_segment, "finish");
     $fields = new FieldList(HiddenField::create('business', null, $this->payment_gateway->BusinessID), HiddenField::create('item_name', null, $site->Title), HiddenField::create('cmd', null, "_cart"), HiddenField::create('paymentaction', null, "sale"), HiddenField::create('invoice', null, $order->OrderNumber), HiddenField::create('custom', null, $order->OrderNumber), HiddenField::create('upload', null, 1), HiddenField::create('discount_amount_cart', null, number_format($cart->DiscountAmount, 2)), HiddenField::create('currency_code', null, Checkout::config()->currency_code), HiddenField::create('first_name', null, $order->FirstName), HiddenField::create('last_name', null, $order->Surname), HiddenField::create('address1', null, $order->Address1), HiddenField::create('address2', null, $order->Address2), HiddenField::create('city', null, $order->City), HiddenField::create('zip', null, $order->PostCode), HiddenField::create('country', null, $order->Country), HiddenField::create('email', null, $order->Email), HiddenField::create('return', null, $success_url), HiddenField::create('notify_url', null, $callback_url), HiddenField::create('cancel_return', null, $error_url));
     if (!Checkout::config()->simple_checkout && !$cart->isCollection()) {
         // Shipping Details
         $fields->add(HiddenField::create('shipping_addressee_name', null, $order->DeliveryFirstnames . " " . $order->DeliverySurname));
         $fields->add(HiddenField::create('shipping_address1', null, $order->DeliveryAddress1));
         $fields->add(HiddenField::create('shipping_address2', null, $order->DeliveryAddress2));
         $fields->add(HiddenField::create('shipping_city', null, $order->DeliveryCity));
         $fields->add(HiddenField::create('shipping_zip', null, $order->DeliveryPostCode));
         $fields->add(HiddenField::create('shipping_country', null, $order->DeliveryCountry));
     }
     $i = 1;
     foreach ($cart->getItems() as $item) {
         $fields->add(HiddenField::create('item_name_' . $i, null, $item->Title));
         $fields->add(HiddenField::create('amount_' . $i, null, number_format($item->Price, 2)));
         $fields->add(HiddenField::create('quantity_' . $i, null, $item->Quantity));
         $i++;
     }
     if (!Checkout::config()->simple_checkout && !$cart->isCollection()) {
         // Add shipping as an extra product
         $fields->add(HiddenField::create('item_name_' . $i, null, $order->PostageType));
         $fields->add(HiddenField::create('amount_' . $i, null, number_format($cart->PostageCost, 2)));
         $fields->add(HiddenField::create('quantity_' . $i, null, "1"));
     }
     // Add tax (if needed) else just total
     if ($cart->TaxCost) {
         $fields->add(HiddenField::create('tax_cart', null, number_format($cart->TaxCost, 2)));
     }
     $actions = FieldList::create(LiteralField::create('BackButton', '<a href="' . $back_url . '" class="btn btn-red checkout-action-back">' . _t('Checkout.Back', 'Back') . '</a>'), FormAction::create('Submit', _t('Checkout.ConfirmPay', 'Confirm and Pay'))->addExtraClass('btn')->addExtraClass('btn-green'));
     $form = Form::create($this, 'Form', $fields, $actions)->addExtraClass('forms')->setFormMethod('POST')->setFormAction($gateway_url);
     $this->customise(array("Title" => _t('Checkout.Summary', "Summary"), "MetaTitle" => _t('Checkout.Summary', "Summary"), "Form" => $form, "Order" => $order));
     $this->extend('onAfterIndex');
     return $this->renderWith(array("PayPal", "Payment", "Checkout", "Page"));
 }
 public function index($request)
 {
     $this->extend('onBeforeIndex');
     // Setup payment gateway form
     $order = $this->getOrderData();
     $cart = ShoppingCart::get();
     // Setup the gateway URL
     if (Director::isDev()) {
         $gateway_url = "https://secure-test.worldpay.com/wcc/purchase";
     } else {
         $gateway_url = "https://secure.worldpay.com/wcc/purchase ";
     }
     $callback_url = Controller::join_links(Director::absoluteBaseURL(), Payment_Controller::config()->url_segment, "callback", $this->payment_gateway->ID);
     $back_url = Controller::join_links(Director::absoluteBaseURL(), Checkout_Controller::config()->url_segment, "finish");
     $fields = FieldList::create(HiddenField::create('instId', null, $this->payment_gateway->InstallID), HiddenField::create('cartId', null, $order->OrderNumber), HiddenField::create('MC_callback', null, $callback_url), HiddenField::create('amount', null, number_format($cart->TotalCost, 2)), HiddenField::create('currency', null, Checkout::config()->currency_code), HiddenField::create('name', null, $order->FirstName . " " . $order->Surname), HiddenField::create('address1', null, $order->Address1), HiddenField::create('address2', null, $order->Address2), HiddenField::create('town', null, $order->City), HiddenField::create('region', null, $order->Country), HiddenField::create('postcode', null, $order->PostCode), HiddenField::create('country', null, $order->Country), HiddenField::create('email', null, $order->Email), HiddenField::create('tel', null, $order->PhoneNumber));
     // Create a string of items ordered (to manage the order via WorldPay)
     $desc_string = "";
     foreach ($cart->getItems() as $item) {
         $desc_string .= $item->Title . ' x ' . $item->Quantity . ', ';
     }
     if (!Checkout::config()->simple_checkout && !$cart->isCollection()) {
         // Add postage type to description
         $desc_string .= _t("Checkout.Postage", "Postage") . ': ' . $order->PostageType . '; ';
         // Add postage address to description
         $desc_string .= _t("Checkout.PostTo", "Post to") . ': ';
         $desc_string .= $order->DeliveryFirstnames . " " . $order->DeliverySurname . ', ';
         $desc_string .= $order->DeliveryAddress1 . ', ';
         $desc_string .= $order->DeliveryAddress2 ? $order->DeliveryAddress2 . ', ' : '';
         $desc_string .= $order->DeliveryCity . ', ';
         $desc_string .= $order->DeliveryCountry ? $order->DeliveryCountry . ', ' : '';
         $desc_string .= $order->DeliveryPostCode . ', ';
         $desc_string .= $order->DeliveryCountry;
     }
     $fields->add(HiddenField::create('desc', null, $desc_string));
     if (Director::isDev()) {
         $fields->add(HiddenField::create('testMode', null, '100'));
     }
     $actions = FieldList::create(LiteralField::create('BackButton', '<a href="' . $back_url . '" class="btn btn-red checkout-action-back">' . _t('Checkout.Back', 'Back') . '</a>'), FormAction::create('Submit', _t('Checkout.ConfirmPay', 'Confirm and Pay'))->addExtraClass('btn')->addExtraClass('btn-green'));
     $form = Form::create($this, 'Form', $fields, $actions)->addExtraClass('forms')->setFormMethod('POST')->setFormAction($gateway_url);
     $this->customise(array("Title" => _t('Checkout.Summary', "Summary"), "MetaTitle" => _t('Checkout.Summary', "Summary"), "Form" => $form, "Order" => $order));
     $this->extend("onAfterIndex");
     return $this->renderWith(array("Worldpay", "Payment", "Checkout", "Page"));
 }
 public function index($request)
 {
     $this->extend('onBeforeIndex');
     // Setup payment gateway form
     $site = SiteConfig::current_site_config();
     $order = $this->getOrderData();
     $cart = ShoppingCart::get();
     $pw = $this->payment_gateway->SHA;
     $sha_data = "";
     // Setup the gateway URL
     if (Director::isDev()) {
         $gateway_url = "https://mdepayments.epdq.co.uk/ncol/test/orderstandard.asp";
     } else {
         $gateway_url = "https://payments.epdq.co.uk/ncol/prod/orderstandard.asp";
     }
     $success_url = Controller::join_links(Director::absoluteBaseURL(), Payment_Controller::config()->url_segment, 'complete');
     $error_url = Controller::join_links(Director::absoluteBaseURL(), Payment_Controller::config()->url_segment, 'complete', 'error');
     $back_url = Controller::join_links(Director::absoluteBaseURL(), Checkout_Controller::config()->url_segment, "finish");
     $template_url = Controller::join_links(Director::absoluteBaseURL(), "BarclaycardEpdqDynamicTemplate");
     // Get an array of details, so we can generate a hash and convert
     // to hidden fields
     $data = array("PSPID" => $this->payment_gateway->PSPID, "ORDERID" => $order->OrderNumber, "AMOUNT" => round($cart->TotalCost * 100), "CURRENCY" => Checkout::config()->currency_code, "LANGUAGE" => i18n::get_locale(), "CN" => $order->FirstName . " " . $order->Surname, "EMAIL" => $order->Email, "OWNERADDRESS" => $order->Address1, "OWNERTOWN" => $order->City, "OWNERZIP" => $order->PostCode, "OWNERCTY" => $order->Country, "TITLE" => $site->Title, "BGCOLOR" => $this->payment_gateway->Background, "TXTCOLOR" => $this->payment_gateway->Text, "TBLBGCOLOR" => $this->payment_gateway->TableBackground, "TBLTXTCOLOR" => $this->payment_gateway->TableText, "BUTTONBGCOLOR" => $this->payment_gateway->ButtonBackground, "BUTTONTXTCOLOR" => $this->payment_gateway->ButtonText, "ACCEPTURL" => $success_url, "DECLINEURL" => $error_url, "EXCEPTIONURL" => $error_url, "CANCELURL" => $error_url, "TP" => $template_url);
     // Account for the fact the phone number might not be set
     if ($order->PhoneNumber) {
         $data["OWNERTELNO"] = $order->PhoneNumber;
     }
     $fields = FieldList::create();
     ksort($data);
     // Generate our SHA Key and add fields
     foreach ($data as $k => $v) {
         $fields->push(HiddenField::create($k, null, $v));
         $sha_data .= sprintf("%s=%s%s", $k, $v, $pw);
     }
     $hashed_data = strtoupper(hash("sha1", $sha_data));
     // Finally add out hashed data
     $fields->push(HiddenField::create("SHASign", null, $hashed_data));
     $actions = FieldList::create(LiteralField::create('BackButton', '<a href="' . $back_url . '" class="btn btn-red checkout-action-back">' . _t('Checkout.Back', 'Back') . '</a>'), FormAction::create('Submit', _t('Checkout.ConfirmPay', 'Confirm and Pay'))->addExtraClass('btn')->addExtraClass('btn-green'));
     $form = Form::create($this, 'Form', $fields, $actions)->addExtraClass('forms')->setFormMethod('POST')->setFormAction($gateway_url);
     $this->customise(array("Title" => _t('Checkout.Summary', "Summary"), "MetaTitle" => _t('Checkout.Summary', "Summary"), "Form" => $form, "Order" => $order));
     $this->extend("onAfterIndex");
     return $this->renderWith(array("BarclaysEpqd", "Payment", "Checkout", "Page"));
 }
 public function index($request)
 {
     $this->extend("onBeforeIndex");
     $site = SiteConfig::current_site_config();
     $order = $this->getOrderData();
     $cart = ShoppingCart::get();
     $key = $this->payment_gateway->ConfigKey;
     $merchant_id = Director::isDev() ? "TEST" : $this->payment_gateway->MerchantID;
     $callback_url = Controller::join_links(Director::absoluteBaseURL(), Payment_Controller::config()->url_segment, "callback", $this->payment_gateway->ID);
     $return_url = Controller::join_links(Director::absoluteBaseURL(), Payment_Controller::config()->url_segment, 'complete');
     $back_url = Controller::join_links(Director::absoluteBaseURL(), Checkout_Controller::config()->url_segment, "finish");
     $payment_details = array('username' => $this->payment_gateway->Username, 'password' => $this->payment_gateway->Password, 'biller_code' => $this->payment_gateway->BillerCode, 'merchant_id' => $merchant_id, 'receipt_address' => $order->Email, 'payment_amount' => number_format($cart->TotalCost, 2), 'payment_reference' => $order->OrderNumber, 'payment_reference_minimum_length' => 10, 'payment_reference_maximum_length' => 20, 'payment_reference_text' => _t("PayWay.PaymentReferenceText", "Order Number"), 'return_link_url' => $return_url, 'reply_link_url' => $callback_url, 'reply_link_email' => $this->payment_gateway->PaymentReplyEmail, 'reply_link_post_type' => 'extended');
     foreach ($cart->getItems() as $item) {
         $payment_details[$item->Title] = $item->Quantity . ',' . number_format($item->Price, 2);
     }
     if (!Checkout::config()->simple_checkout) {
         $payment_details[$order->PostageType] = number_format($cart->PostageCost, 2);
     }
     // Add tax (if needed) else just total
     if ($cart->TaxCost) {
         $payment_details[_t("PayWay.Tax", 'Tax')] = number_format($cart->TaxCost, 2);
     }
     // If we cannot get payway's token, generate a friendly error
     try {
         $token = $this->get_token($payment_details);
     } catch (Exception $e) {
         error_log("Exception caught: " . $e->getMessage());
         $content = "<p>";
         $content = _t("PayWay.UnableToPayContent", "Please return to the previous page and try again");
         $content = "</p>";
         $content = '<p><a href="' . $back_url . '" class="btn">Back</a></p>';
         $this->customise(array("Title" => _t("PayWay.UnableToPay", "Unable to take payment"), "MetaTitle" => _t("PayWay.UnableToPay", "Unable to take payment"), "Content" => $content));
         return $this->renderWith(array("Page"));
     }
     $hand_off_url = Controller::join_links($this->config()->gateway_url, "MakePayment");
     $hand_off_url .= "?biller_code=" . $this->payment_gateway->BillerCode;
     $hand_off_url .= "&token=" . urlencode($token);
     $this->extend('onAfterIndex');
     return $this->redirect($hand_off_url);
 }
 public function index($request)
 {
     $this->extend('onBeforeIndex');
     $site = SiteConfig::current_site_config();
     $order = $this->getOrderData();
     $cart = ShoppingCart::get();
     $key = $this->payment_gateway->ConfigKey;
     $sofort = new SofortMultipayPayment($key);
     $sofort->setAmount(number_format($cart->TotalCost, 2));
     $sofort->setCurrencyCode(Checkout::config()->currency_code);
     $callback_url = Controller::join_links(Director::absoluteBaseURL(), Payment_Controller::config()->url_segment, "callback", $this->payment_gateway->ID);
     $success_url = Controller::join_links(Director::absoluteBaseURL(), Payment_Controller::config()->url_segment, 'complete', $order->OrderNumber);
     $error_url = Controller::join_links(Director::absoluteBaseURL(), Payment_Controller::config()->url_segment, 'complete', 'error');
     $back_url = Controller::join_links(Director::absoluteBaseURL(), Checkout_Controller::config()->url_segment, "finish");
     $sofort->setSuccessUrl($success_url, true);
     $sofort->setAbortUrl($error_url);
     $sofort->setNotificationUrl($callback_url);
     $desc_string = "";
     foreach ($cart->getItems() as $item) {
         $desc_string .= $item->Title . ' x ' . $item->Quantity . ', ';
     }
     $sofort->setReason($desc_string);
     $sofort->sendRequest();
     $fields = new FieldList();
     $i = 1;
     $actions = FieldList::create(LiteralField::create('BackButton', '<a href="' . $back_url . '" class="btn btn-red checkout-action-back">' . _t('Checkout.Back', 'Back') . '</a>'));
     $form = Form::create($this, 'Form', $fields, $actions)->addExtraClass('forms')->setFormMethod('GET');
     if ($sofort->getPaymentUrl()) {
         $actions->add(FormAction::create('Submit', _t('Checkout.ConfirmPay', 'Confirm and Pay'))->addExtraClass('btn')->addExtraClass('btn-green'));
         $form->setFormAction($sofort->getPaymentUrl());
         // Set the Payment No to our order data (accessable by
         // onAfterIndex)
         $order->PaymentID = $sofort->getTransactionId();
     } else {
         $actions->add(LiteralField::create('BackButton', '<strong class="error">' . _t('Sofort.TransactionError', 'Error with transaction') . '</strong>'));
     }
     $this->customise(array("Title" => _t('Checkout.Summary', "Summary"), "MetaTitle" => _t('Checkout.Summary', "Summary"), "Form" => $form, "Order" => $order));
     $this->extend("onAfterIndex");
     return $this->renderWith(array("Sofort", "Payment", "Checkout", "Page"));
 }
 /**
  * Default action
  */
 public function index()
 {
     // Setup payment gateway form
     $site = SiteConfig::current_site_config();
     $order = $this->order;
     $callback_url = Controller::join_links(Director::absoluteBaseURL(), Payment_Controller::config()->url_segment, "callback", $this->payment_gateway->ID);
     $back_url = Controller::join_links(Director::absoluteBaseURL(), Checkout_Controller::config()->url_segment, "finish");
     $fields = FieldList::create(HiddenField::create('instId', null, $this->payment_gateway->InstallID), HiddenField::create('cartId', null, $order->OrderNumber), HiddenField::create('MC_callback', null, $callback_url), HiddenField::create('amount', null, $order->Total), HiddenField::create('currency', null, $site->Currency()->GatewayCode), HiddenField::create('name', null, $order->FirstName . " " . $order->Surname), HiddenField::create('address1', null, $order->Address1), HiddenField::create('address2', null, $order->Address2), HiddenField::create('town', null, $order->City), HiddenField::create('region', null, $order->State), HiddenField::create('postcode', null, $order->PostCode), HiddenField::create('country', null, $order->Country), HiddenField::create('email', null, $order->Email));
     if ($this->payment_gateway->GatewayMessage) {
         $fields->add(HiddenField::create('desc', null, $this->payment_gateway->GatewayMessage));
     }
     if ($curr_local = str_replace("_", "-", i18n::get_locale())) {
         $fields->add(HiddenField::create('lang', null, $curr_local));
     }
     if (Director::isDev()) {
         $fields->add(HiddenField::create('testMode', null, '100'));
     }
     $actions = FieldList::create(LiteralField::create('BackButton', '<a href="' . $back_url . '" class="btn btn-red commerce-action-back">' . _t('Commerce.Back', 'Back') . '</a>'), FormAction::create('Submit', _t('Commerce.ConfirmPay', 'Confirm and Pay'))->addExtraClass('btn')->addExtraClass('btn-green'));
     $form = Form::create($this, 'Form', $fields, $actions)->addExtraClass('forms')->setFormMethod('POST')->setFormAction($this->payment_gateway->GatewayURL());
     $this->extend('updateForm', $form);
     return array("Title" => _t('Commerce.CheckoutSummary', "Summary"), "MetaTitle" => _t('Commerce.CheckoutSummary', "Summary"), "Form" => $form);
 }
 /**
  * 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"));
 }
 /**
  * Default action
  */
 public function index()
 {
     $order = $this->order;
     $site = SiteConfig::current_site_config();
     // First send our intial data to sagepay to get our payment ID
     // and URL
     $callback_url = Controller::join_links(Director::absoluteBaseURL(), Payment_Controller::config()->url_segment, "callback", $this->payment_gateway->ID);
     $back_url = Controller::join_links(Director::absoluteBaseURL(), Checkout_Controller::config()->url_segment, "finish");
     $payload_data = array();
     $payload_data['VPSProtocol'] = $this->payment_gateway->ProtocolVersion;
     $payload_data['TxType'] = 'PAYMENT';
     $payload_data['Vendor'] = $this->payment_gateway->VendorName;
     // Order details
     $payload_data["VendorTxCode"] = $order->OrderNumber;
     $payload_data["Amount"] = $order->Total;
     $payload_data["Currency"] = $site->Currency()->GatewayCode;
     $payload_data["Description"] = $this->payment_gateway->GatewayMessage;
     $payload_data["NotificationURL"] = $callback_url;
     $payload_data["SuccessURL"] = $callback_url;
     $payload_data["FailureURL"] = $callback_url;
     $payload_data["CustomerName"] = $order->FirstName . " " . $order->Surname;
     $payload_data["SendEMail"] = $this->payment_gateway->SendEmail;
     $payload_data["CustomerEMail"] = $order->Email;
     $payload_data["VendorEMail"] = $this->payment_gateway->EmailRecipient;
     // Billing details
     $payload_data["BillingFirstnames"] = $order->FirstName;
     $payload_data["BillingSurname"] = $order->Surname;
     $payload_data["BillingAddress1"] = $order->Address1;
     $payload_data["BillingAddress2"] = $order->Address2;
     $payload_data["BillingCity"] = $order->City;
     $payload_data["BillingPostCode"] = $order->PostCode;
     $payload_data["BillingCountry"] = $order->Country;
     $payload_data["BillingState"] = $order->State;
     $payload_data["BillingPhone"] = $order->PhoneNumber;
     // Delivery details
     $payload_data["DeliveryFirstnames"] = $order->DeliveryFirstnames;
     $payload_data["DeliverySurname"] = $order->DeliverySurname;
     $payload_data["DeliveryAddress1"] = $order->DeliveryAddress1;
     $payload_data["DeliveryAddress2"] = $order->DeliveryAddress2;
     $payload_data["DeliveryCity"] = $order->DeliveryCity;
     $payload_data["DeliveryPostCode"] = $order->DeliveryPostCode;
     $payload_data["DeliveryCountry"] = $order->DeliveryCountry;
     $payload_data["DeliveryState"] = $order->DeliveryState;
     $payload_data["DeliveryPhone"] = $order->PhoneNumber;
     // For charities registered for Gift Aid
     $payload_data["AllowGiftAid"] = 0;
     // 3D secure
     $payload_data["Apply3DSecure"] = 0;
     // Generate a html payload from our settings
     $payload = "";
     $i = 0;
     foreach ($payload_data as $key => $value) {
         $payload .= $key . "=" . $value;
         $i++;
         if ($i < count($payload_data)) {
             $payload .= "&";
         }
     }
     // Write our connection and check result
     $parsed_url = parse_url($this->payment_gateway->GatewayURL());
     $host = $parsed_url['host'];
     $path = $parsed_url['path'];
     $port = 443;
     $response = "";
     $request = "POST {$path} HTTP/1.1\r\n";
     $request .= "Host:{$host}\r\n";
     $request .= "Content-Type: application/x-www-form-urlencoded\r\n";
     $request .= "Content-Length: " . strlen($payload) . "\r\n";
     $request .= "Connection: close\r\n\r\n";
     $request .= $payload;
     $socket = fsockopen("ssl://{$host}", $port, $errno, $errstr, 30);
     if (!$socket) {
         return null;
     }
     for ($written = 0; $written < strlen($request); $written += $fwrite) {
         $fwrite = fwrite($socket, substr($request, $written));
     }
     while (!feof($socket)) {
         $response .= fgets($socket, 1024);
     }
     fclose($socket);
     // Ready to deal with response data
     $response_data = array();
     foreach (explode("\n", $response) as $item) {
         if (!strpos($item, "=") === false) {
             $item_array = explode("=", $item, 2);
             $response_data[$item_array[0]] = $item_array[1];
         }
     }
     // Check our data was recieved ok
     if (strpos($response_data['Status'], 'OK') === false) {
         $form = null;
     } else {
         $order->PaymentID = $response_data['VPSTxId'];
         $order->write();
         Session::set('Commerce.Order', $order);
         // now setup our form
         $actions = FieldList::create(LiteralField::create('BackButton', '<a href="' . $back_url . '" class="btn btn-red commerce-action-back">' . _t('Commerce.Back', 'Back') . '</a>'), FormAction::create('Submit', _t('Commerce.ConfirmPay', 'Confirm and Pay'))->addExtraClass('btn')->addExtraClass('btn-green'));
         $form = Form::create($this, 'Form', FieldList::create(), $actions)->addExtraClass('forms')->setFormMethod('POST')->setFormAction($response_data['NextURL']);
         $this->extend('updateForm', $form);
     }
     return array('Title' => _t('Commerce.CheckoutSummary', "Summary"), 'MetaTitle' => _t('Commerce.CheckoutSummary', "Summary"), "Form" => $form);
 }