Source of file Payment_Controller.php
Size: 9,620 Bytes - Last Modified: 2021-12-24T05:16:24+00:00
/var/www/docs.ssmods.com/process/src/code/control/Payment_Controller.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327 | <?php /** * Summary Controller is responsible for displaying all order data before posting * to the final payment gateway. * * @author i-lateral (http://www.i-lateral.com) * @package checkout */ class Payment_Controller extends Controller { /** * URL Used to generate links to this controller. * * NOTE If you alter routes.yml, you MUST alter this. * * @var string * @config */ private static $url_segment = "checkout/payment"; /** * Name of the current controller. Mostly used in templates for * targeted styling. * * @var string * @config */ private static $class_name = "Checkout"; private static $allowed_actions = array( "index", "callback", "complete" ); /** * Set up the "restful" URLs * * @config * @var array */ private static $url_handlers = array( '$Action/$ID' => 'handleAction', ); /** * Name of the payment handler we are using * * @var string */ protected $payment_handler; public function getPaymentHandler() { return $this->payment_handler; } public function setPaymentHandler($handler) { $this->payment_handler = $handler; return $this; } /** * Name of the payment method we are using * * @var string */ protected $payment_method; public function getPaymentMethod() { return $this->payment_method; } public function setPaymentMethod($method) { $this->payment_method = $method; return $this; } public function getClassName() { return self::config()->class_name; } /** * Shortcut to checkout config, to allow us to access it via * templates * * @return boolean */ public function ShowTax() { return Checkout::config()->show_tax; } /** * Get the link to this controller * * @return string */ public function Link($action = null) { return Controller::join_links( Director::BaseURL(), $this->config()->url_segment, $action ); } public function init() { parent::init(); // Check if payment ID set and corresponds if ($this->request->param('ID') && $method = PaymentMethod::get()->byID($this->request->param('ID'))) { $this->payment_method = $method; } // Then check session elseif ($method = PaymentMethod::get()->byID(Session::get('Checkout.PaymentMethodID'))) { $this->payment_method = $method; } // Setup payment handler if ($this->payment_method && $this->payment_method !== null) { $handler = $this->payment_method->ClassName; $handler = $handler::$handler; $this->payment_handler = $handler::create(); $this->payment_handler->setPaymentGateway($this->getPaymentMethod()); } } /** * 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 || !$cart->isDeliverable()) { $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() && $cart->isDeliverable() && (!$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); } /** * This method can be called by a payment gateway to provide * automated integration. * * This action performs some basic setup then hands control directly * to the payment handler's "callback" action. * * @param $request Current Request Object */ public function callback($request) { // If post data exists, process. Otherwise provide error if ($this->payment_handler === null) { // Redirect to error page return $this->redirect(Controller::join_links( Director::BaseURL(), $this->config()->url_segment, 'complete', 'error' )); } // Hand the request over to the payment handler return $this ->payment_handler ->handleRequest($request, $this->model); } /* * Deal with rendering a completion message to the end user * * @return String */ public function complete() { $site = SiteConfig::current_site_config(); $id = $this->request->param('ID'); if ($id == "error") { $return = $this->error_data(); } else { $return = $this->success_data(); } $this->customise($return); // Extend our completion process, to allow for custom completion // templates $this->extend("onBeforeComplete"); // Clear our session data if (isset($_SESSION)) { ShoppingCart::get()->clear(); unset($_SESSION['Checkout.PostageID']); unset($_SESSION['Checkout.PaymentMethod']); } return $this->renderWith(array( "Payment_complete_" . $this->payment_handler, "Payment_complete", "Checkout", "Page" )); } /* * Pull together data to be used in success templates * * @return array */ public function success_data() { $site = SiteConfig::current_site_config(); return array( 'Title' => _t('Checkout.ThankYouForOrder', 'Thank you for your order'), 'Content' => ($site->SuccessCopy) ? nl2br(Convert::raw2xml($site->SuccessCopy), true) : false ); } /* * Pull together data to be used in success templates * * @return array */ public function error_data() { $site = SiteConfig::current_site_config(); return array( 'Title' => _t('Checkout.OrderProblem', 'There was a problem with your order'), 'Content' => ($site->FailerCopy) ? nl2br(Convert::raw2xml($site->FailerCopy), true) : false ); } } |