getConfig('type') == self::TYPE_CONFIRMATION && !Qs_Request::getRequestValue('action')) {
$this->_action = 'previewOrder';
}
if ($this->getConfig('type') == self::TYPE_BILLING_INFORMATION ) {
$this->_action = 'viewBillingInfo';
}
return parent::_initAction();
}
protected function _callAction()
{
if (!Qs_Request::isXmlHttpRequest()) {
if ($this->_action !== 'verifyOrder' && $this->_action !== 'cancelOrder' &&
$this->_action !== 'completeOrder')
{
if (!(bool)$this->_getCart()->getItemsCount()) {
Qs_Http::redirect($this->_getCartUrl());
}
if (true !== ($errors = $this->_getCart()->validateCartItems())) {
$this->_setBackUrl($this->_getCartUrl());
$this->_setBackError($this->_errorMap[self::ERROR_VALIDATE_ITEMS] . "
\n" . implode("
\n", $errors));
$this->_doBack();
}
}
}
return parent::_callAction();
}
protected function _doInitOrder()
{
$form = $this->_getNewForm();
$form->setDefaults();
if ($form->validate()) {
$data = $form->getValues();
$data['billing']['countryTitle'] = App_User_Obj::getFullCountry($data['billing']['countryId']);
$data['billing']['state'] = App_User_Obj::getStateId($data['billing']);
$this->_saveUserDataToSession($data);
Qs_Http::redirect($this->_getCartUrl('preview'));
} else {
$this->_addFormItem($form);
}
return $this;
}
protected function _doInitOrderAjax()
{
$form = $this->_getNewForm();
$this->_displayJson($form->validateAjax());
}
protected function _doViewBillingInfo()
{
$this->_setBackUrl($this->_getCartUrl());
$data = $this->_getUserDataFromSession();
if (empty($data)) {
$this->_doBack();
}
$data['tpl'] = $this->getTemplate('billingInfo.tpl');
$this->_addItem($data);
return $this;
}
protected function _doPreviewOrder()
{
$this->_setBackUrl($this->_getCartUrl());
$userData = $this->_getUserDataFromSession();
if (empty($userData)) {
$this->_doBack();
}
$cartData = $this->_getCart()->getData();
$cartData = array_merge($cartData, $this->_prepareOrderData($userData));
$cartData['userData'] = $userData;
$submitForm = $this->_getFormInstance('process');
$submitForm->setAttrib('id', $submitForm->getAttrib('id') . '-proccess');
$cartData['submitOrderForm'] = $submitForm->setDefaults()->render();
$cartData['tpl'] = $this->getTemplate('confirmation.tpl');
$this->_addItem($cartData);
return $this;
}
protected function _getNewForm(array $options = array())
{
if ($this->_getCart()->isSponsorAtCart()) {
$options['isSponsorAtCart'] = true;
if (!empty($options['defaults'])) {
try {
$defaultPayment = reset(array_keys($this->getAvailablePaymentTypes()));
} catch (Exception $e) {
$defaultPayment = App_ECommerce_Order_Obj::PAYMENT_TYPE_PAYPAL_EXPRESS;
}
$options['defaults']['servicePayment']['type'] = $defaultPayment;
}
}
return parent::_getNewForm($options);
}
protected function _doNew()
{
$options = array();
$formDefaults = array();
$this->_clearSessionPaymentData();
if (!$this->_doc->getAuth()->getIdentity()) {
App_ECommerce_Checkout_LoginBlock_View::setSessionValue('skipLogin', true);
}
if (null !== ($sessionUserData = $this->_getUserDataFromSession())) {
$formDefaults = $sessionUserData;
} else if ($this->_doc->getAuthIdentity()) {
$userObj = new App_User_Obj();
$formDefaults = $userObj->setPrimaryKey($this->_doc->getAuth()->getData('id'))->getData();
} else {
$formDefaults = $this->_getFirstAttendeeData();
}
if (!empty($formDefaults)) {
// $formDefaults['state'] = App_User_Obj::getStateTitle($formDefaults);
$options['defaults'] = $formDefaults;
}
$form = $this->_getNewForm($options);
$form->setDefaults();
$this->_addFormItem($form);
$this->_postNew();
return $this;
}
protected function _getFirstAttendeeData()
{
foreach ($this->_getCart()->getList() as $cartItem) {
if (\App_ECommerce_Cart_View::ITEM_TYPE_EVENT == $cartItem['cartItemType']) {
if (!empty($cartItem['attendees'])) {
return array_shift($cartItem['attendees']);
}
}
}
return array();
}
/**
* Cancel started payment
*/
protected function _doCancelOrder()
{
$id = Qs_Request::getRequestValue('id');
$cart = new App_ECommerce_Cart_Obj();
$cart->setPrimaryKey($id);
$cartTransactionId = $cart->getData('transactionId');
$transactionId = $this->_getDataObj()->getTransactionId();
$this->_getDataObj()->saveTransactionId($transactionId);
if ((!empty($cartTransactionId) && $cartTransactionId == $transactionId)
|| (!empty($id) && $this->_getCart()->getData('id') == $id)
) {
$cart->update(array('transactionId' => null));
$cart->getSessionCartObject()->id = $id;
$this->_getDataObj()->cancelTransaction();
}
$this->_setBackUrl($this->_getCartUrl());
$this->_doBack();
}
protected function _doCompleteOrder()
{
$type = Qs_Request::getRequestValue('type');
$paymentAdapter = $this->_getPaymentAdapter($type);
if (!$paymentAdapter->verifyOrder()) {
$this->_setBackUrl($this->_getCartUrl());
$this->_setBackError($paymentAdapter->getErrors());
$this->_getDataObj()->cancelTransaction();
$this->_clearSessionPaymentData();
} else {
$this->_sendEventSignupNotification();
$this->completeOrder();
}
$this->_doBack();
}
protected function _doCreateOrder()
{
$orderData = $this->_getOrderData();
if (false === $this->_getDataObj()->insert($orderData)) {
$this->_setBackErrors($this->_getDataObj()->getErrors());
} else {
/** @var Qs_Form $form */
$form = $this->_getFormInstance('process')->setDefaults();
if ($form->validate()) {
$this->_initFromForm($form);
$this->_getDataObj()->update();
} else {
$this->_setBackUrl($this->_getCartUrl());
$this->_setBackError($this->_errorMap[self::ERROR_OTHER]);
$this->_doBack();
}
$this->_getDataObj()->startTransaction();
if ($this->getAvailablePaymentTypes()) {
$this->_processPayment($orderData);
}
$this->_sendEventSignupNotification();
$this->completeOrder();
}
$this->_doBack();
}
protected function _sendEventSignupNotification()
{
$cartItems = $this->_getCart()->getList();
foreach ($cartItems as $item) {
if (App_ECommerce_Cart_View::ITEM_TYPE_EVENT === $item['cartItemType']) {
$parentAttendeeId = $item['productId'];
$event = new \App\Event\Obj();
$event->sendSignupNotification($parentAttendeeId);
}
}
return $this;
}
protected function _getLog()
{
if (null === $this->_log) {
parent::_getLog();
$this->_log->setAction('new', 'Headed to %itemName% Checkout');
$this->_log->setAction('previewOrder', 'Headed to %itemName% Confirmation');
$this->_log->setAction('createOrder', 'Placed new %itemName% #%id%');
$this->_log->setAction('initOrder', 'Edited Payment Information');
}
return $this->_log;
}
}