Exemplo n.º 1
0
 function repeatAction()
 {
     if (!$this->invoice) {
         throw new Am_Exception_InputError('No invoice found, cannot repeat');
     }
     if ($this->invoice->isPaid()) {
         throw new Am_Exception_InputError("Invoice #{$id} is already paid");
     }
     $found = false;
     foreach ($this->view->paysystems as $ps) {
         if ($ps['paysys_id'] == $this->getFiltered('paysys_id')) {
             $found = true;
             break;
         }
     }
     if (!$found) {
         return $this->indexAction();
     }
     $this->invoice->updateQuick('paysys_id', $this->getFiltered('paysys_id'));
     if ($err = $this->invoice->validate()) {
         throw new Am_Exception_InputError($err[0]);
     }
     $payProcess = new Am_Paysystem_PayProcessMediator($this, $this->invoice);
     $result = $payProcess->process();
     if ($result->isFailure()) {
         $this->view->error = $result->getErrorMessages();
         return $this->indexAction();
     }
 }
Exemplo n.º 2
0
 public function indexAction()
 {
     /* @var $invoice Invoice */
     $invoice = $this->getDi()->invoiceTable->findBySecureId($this->getParam('secure_id'), 'payment-link');
     if (!$invoice || $invoice->status != Invoice::PENDING) {
         throw new Am_Exception_InternalError(sprintf('Unknow invoice [%s] or invoice is already processed', filterId($this->getParam('secure_id'))));
     }
     if (!$invoice->due_date && sqlDate($invoice->tm_added) < sqlDate("-" . Invoice::DEFAULT_DUE_PERIOD . " days")) {
         throw new Am_Exception_InputError(___('Invoice is expired'));
     } elseif ($invoice->due_date && $invoice->due_date < sqlDate('now')) {
         throw new Am_Exception_InputError(___('Invoice is expired'));
     }
     $form = new Am_Form();
     if (!$invoice->paysys_id) {
         $psOptions = array();
         foreach (Am_Di::getInstance()->paysystemList->getAllPublic() as $ps) {
             $psOptions[$ps->getId()] = $this->renderPaysys($ps);
         }
         $paysys = $form->addAdvRadio('paysys_id')->setLabel(___('Payment System'))->loadOptions($psOptions);
         $paysys->addRule('required', ___('Please choose a payment system'));
         if (count($psOptions) == 1) {
             $paysys->toggleFrozen(true);
         }
     }
     $form->addSaveButton(___('Pay'));
     $this->view->invoice = $invoice;
     $this->view->form = $form;
     $form->setDataSources(array($this->getRequest()));
     if ($form->isSubmitted() && $form->validate()) {
         $vars = $form->getValue();
         if (!$invoice->paysys_id) {
             $invoice->setPaysystem($vars['paysys_id']);
             $invoice->save();
         }
         $payProcess = new Am_Paysystem_PayProcessMediator($this, $invoice);
         $result = $payProcess->process();
         throw new Am_Exception_InternalError(sprintf('Error occurred while trying proccess invoice [%s]', filterId($invoice->public_id)));
     }
     $this->view->layoutNoMenu = true;
     $this->view->display('pay.phtml');
 }
Exemplo n.º 3
0
 function process(array $vars, $name, HTML_QuickForm2_Controller_Page $page)
 {
     $this->getDi()->hook->call(Am_Event::SIGNUP_PAGE_BEFORE_PROCESS, array('vars' => $vars, 'savedForm' => $this->record));
     $this->vars = $vars;
     // do actions here
     $this->user = $this->getDi()->auth->getUser();
     if ($this->getSession()->signup_member_id && $this->getSession()->signup_member_login) {
         $user = $this->getDi()->userTable->load((int) $this->getSession()->signup_member_id, false);
         if ($user && ($this->getDi()->time - strtotime($user->added) < 24 * 3600 && $user->status == User::STATUS_PENDING)) {
             // prevent attacks as if someone has got ability to set signup_member_id to session
             if ($this->getSession()->signup_member_login == $user->login) {
                 /// there is a potential problem
                 /// because user password is not updated second time - @todo
                 $this->user = $user;
                 $this->autoLoginIfNecessary();
             } else {
                 $this->getSession()->signup_member_id = null;
                 $this->getSession()->signup_member_login = null;
             }
         } else {
             $this->getSession()->signup_member_id = null;
         }
     }
     if (!$this->user) {
         $this->user = $this->getDi()->userRecord;
         $this->user->setForInsert($this->vars);
         // vars are filtered by the form !
         if (empty($this->user->login)) {
             $this->user->generateLogin();
         }
         if (empty($this->vars['pass'])) {
             $this->user->generatePassword();
         } else {
             $this->user->setPass($this->vars['pass']);
         }
         if (empty($this->user->lang)) {
             $this->user->lang = $this->getDi()->locale->getLanguage();
         }
         $this->user->insert();
         $this->getSession()->signup_member_id = $this->user->pk();
         $this->getSession()->signup_member_login = $this->user->login;
         $this->autoLoginIfNecessary();
         // user inserted
         $this->getDi()->hook->call(Am_Event::SIGNUP_USER_ADDED, array('vars' => $this->vars, 'user' => $this->user, 'form' => $this->form, 'savedForm' => $this->record));
         if ($this->getDi()->config->get('registration_mail')) {
             $this->user->sendRegistrationEmail();
         }
         if (!$this->user->isApproved()) {
             $this->user->sendNotApprovedEmail();
         }
     } else {
         if ($this->record->isCart()) {
             $url = $this->getSession()->redirectUrl;
             $this->getSession()->redirectUrl = '';
             $this->_redirect('cart/' . urldecode($url));
         }
         unset($this->vars['pass']);
         unset($this->vars['login']);
         unset($this->vars['email']);
         $this->user->setForUpdate($this->vars)->update();
         // user updated
         $this->getDi()->hook->call(Am_Event::SIGNUP_USER_UPDATED, array('vars' => $this->vars, 'user' => $this->user, 'form' => $this->form, 'savedForm' => $this->record));
     }
     // keep reference to e-mail confirmation link so it still working after signup
     if (!empty($this->vars['code'])) {
         $this->getDi()->store->setBlob(Am_Form_Signup_Action_SendEmailCode::STORE_PREFIX . $this->vars['code'], $this->user->pk(), '+7 days');
     }
     if ($this->record->isCart()) {
         $url = $this->getSession()->redirectUrl;
         $this->getSession()->redirectUrl = '';
         $this->_redirect('cart/' . urldecode($url));
         return true;
     }
     /// now the ordering process
     $invoice = $this->getDi()->invoiceRecord;
     $this->getDi()->hook->call(Am_Event::INVOICE_SIGNUP, array('vars' => $this->vars, 'user' => $this->user, 'form' => $this->form, 'invoice' => $invoice, 'savedForm' => $this->record));
     $invoice->setUser($this->user);
     foreach ($this->vars as $k => $v) {
         if (strpos($k, 'product_id') === 0) {
             foreach ((array) $this->vars[$k] as $product_id) {
                 @(list($product_id, $plan_id, $qty) = explode('-', $product_id, 3));
                 $product_id = (int) $product_id;
                 if (!$product_id) {
                     continue;
                 }
                 $p = $this->getDi()->productTable->load($product_id);
                 if ($plan_id > 0) {
                     $p->setBillingPlan(intval($plan_id));
                 }
                 $qty = (int) $qty;
                 if (!$p->getBillingPlan()->variable_qty || $qty <= 0) {
                     $qty = 1;
                 }
                 $invoice->add($p, $qty);
             }
         }
     }
     if (!$invoice->getItems()) {
         $this->form->getSessionContainer()->destroy();
         $this->_redirect('member');
         return true;
     }
     if (!empty($this->vars['coupon'])) {
         $invoice->setCouponCode($this->vars['coupon']);
         $invoice->validateCoupon();
     }
     $invoice->calculate();
     $invoice->setPaysystem(isset($this->vars['paysys_id']) ? $this->vars['paysys_id'] : 'free');
     $err = $invoice->validate();
     if ($err) {
         throw new Am_Exception_InputError($err[0]);
     }
     if (!empty($this->vars['coupon']) && !(double) $invoice->first_discount && !(double) $invoice->second_discount) {
         $coupon = $this->getDi()->couponTable->findFirstByCode($this->vars['coupon']);
         $batch = $coupon->getBatch();
         if ($batch->discount > 0) {
             $page = $this->form->findPageByElementName('coupon');
             if (!$page) {
                 throw new Am_Exception_InternalError('Coupon brick is not found but coupon code presents in request');
             }
             list($el) = $page->getForm()->getElementsByName('coupon');
             $el->setError(___('The coupon entered is not valid with any product(s) being purchased. No discount will be applied'));
             //now active datasource is datasource of current page
             //retrieve datasource for page with coupon element from
             //session and set it to form to populate it correctly
             $values = $page->getController()->getSessionContainer()->getValues($page->getForm()->getId());
             $page->getForm()->setDataSources(array(new HTML_QuickForm2_DataSource_Array($values)));
             $page->handle('display');
             return false;
         }
     }
     $invoice->insert();
     $this->getDi()->hook->call(Am_Event::INVOICE_BEFORE_PAYMENT_SIGNUP, array('vars' => $this->vars, 'form' => $this->form, 'invoice' => $invoice, 'savedForm' => $this->record));
     try {
         $payProcess = new Am_Paysystem_PayProcessMediator($this, $invoice);
         $result = $payProcess->process();
     } catch (Am_Exception_Redirect $e) {
         $this->form->getSessionContainer()->destroy();
         $invoice->refresh();
         if ($invoice->isCompleted()) {
             // relogin customer if free subscription was ok
             $this->autoLoginIfNecessary();
         }
         throw $e;
     }
     // if we got back here, there was an error in payment!
     /** @todo offer payment method if previous failed */
     $page = $this->form->findPageByElementName('paysys_id');
     if (!$page) {
         $page = $this->form->getFirstPage();
     }
     // just display first page
     foreach ($page->getForm()->getElementsByName('paysys_id') as $el) {
         $el->setValue(null)->setError(current($result->getErrorMessages()));
     }
     $page->handle('display');
     return false;
 }
Exemplo n.º 4
0
 function yesOto(Am_Controller $controller, Invoice $invoice, Oto $oto)
 {
     $inv = $this->getDi()->invoiceTable->createRecord();
     /* @var $inv Invoice */
     $inv->data()->set('oto_parent', $invoice->pk());
     $inv->user_id = $invoice->user_id;
     $inv->add($oto->getProduct());
     $coupon = $oto->getCoupon();
     if ($coupon) {
         $inv->setCoupon($coupon);
     }
     $inv->calculate();
     if ($inv->isZero()) {
         // free invoice
         $inv->paysys_id = 'free';
     } elseif ($oto->getPaysysId()) {
         // configured
         $inv->paysys_id = $oto->getPaysysId();
     } elseif ($invoice->paysys_id != 'free') {
         // not free? take from invoice
         $inv->paysys_id = $invoice->paysys_id;
     } else {
         // was free, now paid, take first public
         $paysystems = Am_Di::getInstance()->paysystemList->getAllPublic();
         $inv->paysys_id = $paysystems[0]->paysys_id;
     }
     $inv->insert();
     $payProcess = new Am_Paysystem_PayProcessMediator($controller, $inv);
     $result = $payProcess->process();
     // we decided to ignore failures here...
 }
Exemplo n.º 5
0
 public function checkoutAction()
 {
     do {
         if (!$this->cart->getItems()) {
             $errors[] = ___("You have no items in your basket - please add something to your basket before checkout");
             return $this->view->display('cart/basket.phtml');
         }
         if (!$this->getDi()->auth->getUserId()) {
             return $this->loginAction();
         } else {
             $this->cart->setUser($this->getDi()->user);
         }
         if (empty($this->cart->getInvoice()->paysys_id)) {
             return $this->choosePaysysAction();
         }
         $invoice = $this->cart->getInvoice();
         $errors = $invoice->validate();
         if ($errors) {
             return $this->view->display('cart/basket.phtml');
         }
         // display confirmation
         if (!$this->getInt('confirm') && $this->getDi()->config->get('shop.confirmation')) {
             return $this->view->display('cart/confirm.phtml');
         }
         ///
         $invoice->save();
         $payProcess = new Am_Paysystem_PayProcessMediator($this, $invoice);
         $result = $payProcess->process();
         if ($result->isFailure()) {
             $this->view->error = ___("Checkout error: ") . current($result->getErrorMessages());
             $this->cart->getInvoice()->paysys_id = null;
             $this->_request->set('do-checkout', null);
             return $this->viewBasketAction();
         }
     } while (false);
 }
 function process(array $vars, $name, HTML_QuickForm2_Controller_Page $page)
 {
     $this->vars = $vars;
     // do actions here
     $this->user = $this->getDi()->auth->getUser();
     if ($this->getSession()->signup_member_id && $this->getSession()->signup_member_login) {
         $user = $this->getDi()->userTable->load((int) $this->getSession()->signup_member_id, false);
         if ($user && ($this->getDi()->time - strtotime($user->added) < 24 * 3600 && $user->status == User::STATUS_PENDING)) {
             // prevent attacks as if someone has got ability to set signup_member_id to session
             if ($this->getSession()->signup_member_login == $user->login) {
                 /// there is a potential problem
                 /// because user password is not updated second time - @todo
                 $this->user = $user;
             } else {
                 $this->getSession()->signup_member_id = null;
                 $this->getSession()->signup_member_login = null;
             }
         } else {
             $this->getSession()->signup_member_id = null;
         }
     }
     if (!$this->user) {
         $this->user = $this->getDi()->userRecord;
         $this->user->setForInsert($this->vars);
         // vars are filtered by the form !
         if (empty($this->user->login)) {
             $this->user->generateLogin();
         }
         if (empty($this->vars['pass'])) {
             $this->user->generatePassword();
         } else {
             $this->user->setPass($this->vars['pass']);
         }
         $this->user->insert();
         $this->getSession()->signup_member_id = $this->user->pk();
         $this->getSession()->signup_member_login = $this->user->login;
         $this->autoLoginIfNecessary();
         // user inserted
         $this->getDi()->hook->call(Am_Event::SIGNUP_USER_ADDED, array('vars' => $this->vars, 'user' => $this->user, 'form' => $this->form));
         if ($this->getDi()->config->get('registration_mail')) {
             $this->user->sendRegistrationEmail();
         }
     } else {
         if ($this->record->isCart()) {
             $this->_redirect('cart/');
         }
         unset($this->vars['pass']);
         unset($this->vars['login']);
         unset($this->vars['email']);
         unset($this->vars['name_f']);
         unset($this->vars['name_l']);
         $this->user->setForUpdate($this->vars)->update();
         // user updated
         $this->getDi()->hook->call(Am_Event::SIGNUP_USER_UPDATED, array('vars' => $this->vars, 'user' => $this->user, 'form' => $this->form));
     }
     // keep reference to e-mail confirmation link so it still working after signup
     if (!empty($this->vars['code'])) {
         $this->getDi()->store->setBlob(Am_Form_Signup_Action_SendEmailCode::STORE_PREFIX . $this->vars['code'], $this->user->pk(), '+7 days');
     }
     if ($this->record->isCart()) {
         $this->_redirect('cart/');
         return true;
     }
     /// now the ordering process
     $invoice = $this->getDi()->invoiceRecord;
     $invoice->setUser($this->user);
     foreach ($this->vars as $k => $v) {
         if (strpos($k, 'product_id') === 0) {
             foreach ((array) $this->vars[$k] as $product_id) {
                 @(list($product_id, $plan_id) = explode('-', $product_id, 2));
                 $p = $this->getDi()->productTable->load($product_id);
                 if ($plan_id > 0) {
                     $p->setBillingPlan(intval($plan_id));
                 }
                 $invoice->add($p, 1);
             }
         }
     }
     if (!empty($this->vars['coupon'])) {
         $invoice->setCouponCode($this->vars['coupon']);
         $invoice->validateCoupon();
     }
     $invoice->calculate();
     $invoice->setPaysystem($this->vars['paysys_id']);
     $payProcess = new Am_Paysystem_PayProcessMediator($this, $invoice);
     try {
         $result = $payProcess->process();
     } catch (Am_Exception_Redirect $e) {
         $invoice->refresh();
         if ($invoice->isCompleted()) {
             // relogin customer if free subscription was ok
             $this->autoLoginIfNecessary();
         }
         throw $e;
     }
     // if we got back here, there was an error in payment!
     /** @todo offer payment method if previous failed */
     $page = $this->form->findPageByElementName('paysys_id');
     if (!$page) {
         $page = $this->form->getFirstPage();
     }
     // just display first page
     foreach ($page->getForm()->getElementsByName('paysys_id') as $el) {
         $el->setValue(null)->setError(current($result->getErrorMessages()));
     }
     $page->handle('display');
     return false;
 }
Exemplo n.º 7
0
 function restoreRecurringAction()
 {
     // load invoice to work with
     $id = $this->getFiltered('invoice_id');
     if (!$id) {
         throw new Am_Exception_InputError("Wrong invoice# passed");
     }
     $invoice = $this->getDi()->invoiceTable->findFirstByPublicId($id);
     /* @var $invoice Invoice */
     if (!$invoice) {
         throw new Am_Exception_InputError(___("Invoice not found"));
     }
     if ($invoice->user_id != $this->user->user_id) {
         throw new Am_Exception_Security("Foreign invoice requested : [{$id}] for {$this->user->user_id}");
     }
     $newInvoice = $invoice->doRestoreRecurring();
     $newInvoice->setPaysystem($invoice->paysys_id);
     $err = $newInvoice->validate();
     if ($err) {
         throw new Am_Exception_InputError($err[0]);
     }
     $newInvoice->data()->set(Invoice::ORIG_ID, $invoice->pk());
     $newInvoice->insert();
     $payProcess = new Am_Paysystem_PayProcessMediator($this, $newInvoice);
     $result = $payProcess->process();
 }