/** * This method occurs before payment is attempted * and fires the onPrePayment plugin event * * @return unknown_type */ function preparePayment() { $this->current_step = 2; // verify that form was submitted by checking token JRequest::checkToken() or jexit('TiendaControllerCheckout::preparePayment - Invalid Token'); // 1. save the order to the table with a 'pre-payment' status // Get post values $values = JRequest::get('post'); $user = $this->user; //set to session to know that we are not doing POS order JFactory::getApplication()->setUserState('tienda.pos_order', false); $user_id = -1; // Guest Checkout if ($this->defines->get('guest_checkout_enabled', '1') && $values['guest'] == '1') { Tienda::load('TiendaHelperUser', 'helpers.user'); $userHelper = TiendaHelperUser::getInstance('User', 'TiendaHelper'); if ($userHelper->emailExists($values['email_address'])) { // TODO user already exists } else { $userHelper = new TiendaHelperUser(); // save the real user's info in the userinfo table JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_tienda/tables'); $userinfo = JTable::getInstance('UserInfo', 'TiendaTable'); $userinfo->user_id = $userHelper->getNextGuestUserId(); $userinfo->email = $values['email_address']; $userinfo->save(); $user_id = $userinfo->user_id; } } else { $user_id = $user->id; } // get // Save the order with a pending status if (!$this->saveOrder($values, $user_id)) { // Output error message and halt JError::raiseNotice('Error Saving Order', $this->getError()); return false; } // Get Order Object $order = $this->_order; // Update the addresses' user id! $shippingAddress = $order->getShippingAddress(); $billingAddress = $order->getBillingAddress(); $shippingAddress->user_id = $user_id; $billingAddress->user_id = $user_id; // Checking whether shipping is required $showShipping = false; $cartsModel = $this->getModel('carts'); if ($isShippingEnabled = $cartsModel->getShippingIsEnabled()) { $showShipping = true; } if ($showShipping && !$shippingAddress->save()) { // Output error message and halt JError::raiseNotice('Error Updating the Shipping Address', $shippingAddress->getError()); return false; } if (!$billingAddress->save()) { // Output error message and halt JError::raiseNotice('Error Updating the Billing Address', $billingAddress->getError()); return false; } $orderpayment_type = @$values['payment_plugin']; $transaction_status = JText::_('COM_TIENDA_INCOMPLETE'); // in the case of orders with a value of 0.00, use custom values if ($order->isRecurring()) { if ((double) $order->getRecurringItem()->recurring_price == (double) '0.00') { $orderpayment_type = 'free'; $transaction_status = JText::_('COM_TIENDA_COMPLETE'); } } else { if ((double) $order->order_total == (double) '0.00') { $orderpayment_type = 'free'; $transaction_status = JText::_('COM_TIENDA_COMPLETE'); } } // Save an orderpayment with an Incomplete status JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_tienda/tables'); $orderpayment = JTable::getInstance('OrderPayments', 'TiendaTable'); $orderpayment->order_id = $order->order_id; $orderpayment->orderpayment_type = $orderpayment_type; // this is the payment plugin selected $orderpayment->transaction_status = $transaction_status; // payment plugin updates this field onPostPayment $orderpayment->orderpayment_amount = $order->order_total; // this is the expected payment amount. payment plugin should verify actual payment amount against expected payment amount if (!$orderpayment->save()) { // Output error message and halt JError::raiseNotice('Error Saving Pending Payment Record', $orderpayment->getError()); return false; } // send the order_id and orderpayment_id to the payment plugin so it knows which DB record to update upon successful payment $values["order_id"] = $order->order_id; $values["orderinfo"] = $order->orderinfo; $values["orderpayment_id"] = $orderpayment->orderpayment_id; $values["orderpayment_amount"] = $orderpayment->orderpayment_amount; // IMPORTANT: Store the order_id in the user's session for the postPayment "View Invoice" link $mainframe = JFactory::getApplication(); $mainframe->setUserState('tienda.order_id', $order->order_id); $mainframe->setUserState('tienda.orderpayment_id', $orderpayment->orderpayment_id); // 2. perform payment process // this is the onPrePayment plugin event // in the case of offsite payment plugins (like Paypal), they will display an order summary (perhaps with ****** for CC number) // with a button that submits a form to the external site (button: "confirm order" or Paypal, MB, Alertpay, whatever) // the return url will point to the method that fires the onPostPayment plugin event: // target: index.php?option=com_tienda&view=checkout&task=confirmPayment&orderpayment_type=xxxxxx // in the case of onsite payment plugins, they will display an order summary (perhaps with ****** for CC number) // with a button that submits a form to the method that fires the onPostPayment plugin event ("confirm order") // target: index.php?option=com_tienda&view=checkout&task=confirmPayment&orderpayment_type=xxxxxx // onPostPayment, payment plugin to update order status with payment status // in the case of orders with a value of 0.00, we redirect to the confirmPayment page if ($order->isRecurring()) { if ((double) $order->getRecurringItem()->recurring_price == (double) '0.00') { JFactory::getApplication()->redirect('index.php?option=com_tienda&view=checkout&task=confirmPayment'); return; } } else { if ((double) $order->order_total == (double) '0.00') { JFactory::getApplication()->redirect('index.php?option=com_tienda&view=checkout&task=confirmPayment'); return; } } $dispatcher = JDispatcher::getInstance(); $results = $dispatcher->trigger("onPrePayment", array($values['payment_plugin'], $values)); // Display whatever comes back from Payment Plugin for the onPrePayment $html = ""; for ($i = 0; $i < count($results); $i++) { $html .= $results[$i]; } // get all coupons and add them to the order if (!empty($values['coupons'])) { foreach ($values['coupons'] as $coupon_id) { $coupon = JTable::getInstance('Coupons', 'TiendaTable'); $coupon->load(array('coupon_id' => $coupon_id)); $order->addCoupon($coupon); } } $this->addCoupons($values); // get the order totals $order->calculateTotals(); // get the order summary $summary = $this->getOrderSummary(); // Get Addresses $shipping_address = $order->getShippingAddress(); $billing_address = $order->getBillingAddress(); $shippingAddressArray = $showShipping ? $this->_shippingAddressArray : array(); $billingAddressArray = $this->_billingAddressArray; $shippingMethodName = $values['shipping_name']; $progress = $this->getProgress(); // Set display $view = $this->getView('checkout', 'html'); $view->setLayout('prepayment'); $view->set('_doTask', true); $view->assign('order', $order); $view->assign('plugin_html', $html); $view->assign('progress', $progress); $view->assign('orderSummary', $summary); $view->assign('shipping_info', $shippingAddressArray); $view->assign('billing_info', $billingAddressArray); $view->assign('shipping_method_name', $shippingMethodName); $view->assign('showShipping', $showShipping); // Get and Set Model $model = $this->getModel('checkout'); $view->setModel($model, true); ob_start(); $dispatcher->trigger('onBeforeDisplayPrePayment', array($order)); $view->assign('onBeforeDisplayPrePayment', ob_get_contents()); ob_end_clean(); ob_start(); $dispatcher->trigger('onAfterDisplayPrePayment', array($order)); $view->assign('onAfterDisplayPrePayment', ob_get_contents()); ob_end_clean(); $view->display(); return; }
public function submitOrder() { $this->setFormat(); $session = JFactory::getSession(); $response = $this->getResponseObject(); $values = JRequest::get('post'); // Prep the post if (!empty($this->user->id)) { $values["user_id"] = $this->user->id; $values["email_address"] = $this->user->email; $values["checkout_method"] = null; } else { $userHelper = new TiendaHelperUser(); $values["user_id"] = $userHelper->getNextGuestUserId(); } $values['ip_address'] = $_SERVER['REMOTE_ADDR']; $values["sameasbilling"] = !empty($values["shipping_input_same_as_billing"]) ? $values["shipping_input_same_as_billing"] : false; if ($shippingMethod = unserialize($session->get('tienda.opc.shippingMethod'))) { $values['shipping_plugin'] = $shippingMethod['element']; $values['shipping_price'] = $shippingMethod['price']; $values['shipping_extra'] = $shippingMethod['extra']; $values['shipping_name'] = $shippingMethod['name']; $values['shipping_tax'] = $shippingMethod['tax']; $values['shipping_code'] = $shippingMethod['code']; } $values['coupons'] = array(); if ($userCoupons = unserialize($session->get('tienda.opc.userCoupons'))) { foreach ($userCoupons as $coupon) { $values['coupons'][] = $coupon->coupon_id; } } if ($userCredit = unserialize($session->get('tienda.opc.userCredit'))) { $values['order_credit'] = $userCredit; } if (empty($values['currency_id'])) { Tienda::load('TiendaHelperCurrency', 'helpers.currency'); $values['currency_id'] = TiendaHelperCurrency::getCurrentCurrency(); } $model = $this->getModel('orders'); $errorMessage = ''; if (!($validate = $model->validate($values))) { $errorMessage = '<ul class="text-error">'; foreach ($model->getErrors() as $error) { $errorMessage .= "<li>" . $error . "</li>"; } $errorMessage .= '</ul>'; $response->goto_section = 'review'; $response->summary->html = $errorMessage; echo json_encode($response); return; } $options = array(); $options["save_addresses"] = true; if (!($order = $model->save($values, $options))) { $errorMessage = '<ul class="text-error">'; foreach ($model->getErrors() as $error) { $errorMessage .= "<li>" . $error . "</li>"; } $errorMessage .= '</ul>'; $response->goto_section = 'review'; $response->summary->html = $errorMessage; echo json_encode($response); return; } $this->_order = $order; if (!($html = $this->getPreparePaymentForm($values, $options, $order))) { $errorMessage = '<ul class="text-error">'; $errorMessage .= "<li>" . $this->getError() . "</li>"; $errorMessage .= '</ul>'; // get and return the error for display to the user $response->goto_section = 'review'; $response->summary->html = $errorMessage; echo json_encode($response); return; } if ($html == 'free') { $itemid = $this->router->findItemid(array('view' => 'opc')); if (empty($itemid)) { $itemid = $this->router->findItemid(array('view' => 'checkout')); if (empty($itemid)) { $itemid = JRequest::getInt('Itemid'); } } $response->redirect = JRoute::_('index.php?option=com_tienda&view=opc&task=confirmPayment&Itemid=' . $itemid); echo json_encode($response); return; } $response->summary->id = 'opc-payment-prepayment'; $response->summary->html = $html; $response->goto_section = 'prepare-payment'; echo json_encode($response); return; }