/** * POST request from PayPal * * @throws Zend_Controller_Action_Exception */ public function callbackAction() { //check request if ($this->getRequest()->isPost()) { $txnId = $this->getRequest()->getParam('txn_id'); $txnType = $this->getRequest()->getParam('txn_type'); if ($txnId || $txnType && $txnType === 'subscr_cancel') { $params = $this->getRequest()->getParams(); $orderManager = new Payments_Model_Order_Manager(); try { $orderManager->handlePaypalRequest($params); if (Zend_Registry::isRegistered('Log')) { $log = Zend_Registry::get('Log'); $log->log('Payment with params ' . var_export($params, true) . ' is successful.', Zend_Log::INFO); } return; //ok } catch (Exception $e) { if (Zend_Registry::isRegistered('Log')) { $log = Zend_Registry::get('Log'); $log->log($e->getMessage() . ' at ' . $e->getFile() . '#' . $e->getLine(), Zend_Log::CRIT); } } } } else { if (Zend_Registry::isRegistered('Log')) { $log = Zend_Registry::get('Log'); $log->log('GET request to Payments_PaypalController->callbackAction()', Zend_Log::WARN); } } throw new Zend_Controller_Action_Exception('Page not found'); }
public function testCreateOrder() { //Fake data $userId = '123456789'; $amount = '50'; $txnId = 'test-txn-id'; //Create user $account = new Users_Model_User(); $account->avatar = null; $account->login = '******' . date('YmdHis'); $account->email = 'autotest3' . time() . '@example.org'; $account->password = md5('password'); $account->role = Users_Model_User::ROLE_USER; $account->status = Users_Model_User::STATUS_ACTIVE; $account->id = $userId; $account->save(); $orderManager = new Payments_Model_Order_Manager(); $order = $orderManager->createOrder($userId, $amount); //Is created order $this->assertNotEmpty($order); $response = $orderManager->payOrder($order->id, $txnId, Payments_Model_Order::PAYMENT_SYSTEM_PAYPAL); //Is payed order $this->assertTrue($response); //Incorrect orderId $response = $orderManager->payOrder('123456789', $txnId, Payments_Model_Order::PAYMENT_SYSTEM_PAYPAL); $this->assertFalse($response); }
public function testCreateAndCancelSubscriptionByPaypalCustomParam() { //Fake data $orderType = Subscriptions_Model_Subscription::ORDER_TYPE_SUBSCRIPTION; $planId = '3'; $price = '50'; $payPalSubscriptionId = 'paypal'; //Create user $account = new Users_Model_User(); $account->avatar = null; $account->login = '******' . date('YmdHis'); $account->email = 'autotest2' . time() . '@example.org'; $account->password = md5('password'); $account->role = Users_Model_User::ROLE_USER; $account->status = Users_Model_User::STATUS_ACTIVE; $account->save(); //Create order $orderManager = new Payments_Model_Order_Manager(); $order = $orderManager->createOrder($account->id, $price); $subscriptionManager = new Subscriptions_Model_Subscription_Manager(); $this->assertNotNull($subscriptionManager->createPaidSubscription($account->id, $planId, $order->id)); //Test paySubscription incorrect orderId $this->assertFalse($subscriptionManager->paySubscription('123456789')); //Test paySubscription $this->assertTrue($subscriptionManager->paySubscription($order->id)); //Test canceled subscription $this->assertTrue($subscriptionManager->cancelSubscription($order->id)); //Test canceled subscription incorrect orderId $this->assertFalse($subscriptionManager->cancelSubscription('123456789')); }
/** * Create subscription page * * If selected free or trial plan - changes immediately * If selected paid plan user allow select payment method (PayPAl or another payment system) and pay subscription */ public function createAction() { $planId = $this->_getParam('id'); if ($this->_request->isPost() && $planId) { //Get user $identity = Zend_Auth::getInstance()->getIdentity(); $userId = $identity->id; //Get plan $subscriptionPlansTable = new Subscriptions_Model_SubscriptionPlans_Table(); $subscriptionPlan = $subscriptionPlansTable->getById($planId); //Not free subscription if ($subscriptionPlan->price > 0) { //Create order $orderManager = new Payments_Model_Order_Manager(); $order = $orderManager->createOrder($userId, $subscriptionPlan->price); $this->view->orderId = $order->id; $this->view->subscriptionPlan = $subscriptionPlan; $subscriptionManger = new Subscriptions_Model_Subscription_Manager(); //Create paid subscription $subscriptionManger->createPaidSubscription($userId, $subscriptionPlan->id, $order->id); //'type'-orderId-userId-planId $this->view->paypalCustom = Subscriptions_Model_Subscription::ORDER_TYPE_SUBSCRIPTION . '-' . $order->id; } else { //Free subscription try { //Not allow renewal Trial subscription if ($subscriptionPlan->type === Subscriptions_Model_SubscriptionPlan::PLAN_TYPE_TRIAL) { $subscriptionTable = new Subscriptions_Model_Subscription_Table(); $select = $subscriptionTable->select()->from(array('subscriptions'))->where('userId =?', $userId)->where('subscriptionPlanId = ?', $subscriptionPlan->id); if ($subscriptionTable->fetchRow($select)) { //Found old trial plan throw new Zend_Controller_Action_Exception('You can not change to this plan'); } } $subscriptionManger = new Subscriptions_Model_Subscription_Manager(); //Create subscription $subscriptionManger->createFreeSubscription($userId, $planId); //Redirect to Thank you page $this->redirect('/subscriptions/index/complete'); } catch (Zend_Controller_Action_Exception $ex) { $this->_flashMessenger->addMessage($ex->getMessage()); $this->redirect('/subscriptions'); } } } else { $this->redirect('/subscriptions'); } }
public function testIndexActionByUserCorrectOrder() { //Fake data $price = '50'; //Create user $account = new Users_Model_User(); $account->avatar = null; $account->login = '******' . date('YmdHis'); $account->email = 'testIndexActionByUserCorrectOrder' . time() . '@example.org'; $account->password = md5('password'); $account->role = Users_Model_User::ROLE_USER; $account->status = Users_Model_User::STATUS_ACTIVE; $account->save(); //Create order $orderManager = new Payments_Model_Order_Manager(); $order = $orderManager->createOrder($account->id, $price); //Test correct order $this->resetResponse(); $this->request->setMethod('GET')->setPost(array('orderId' => $order->id, 'callFrom' => 'view')); $this->dispatch('/payments/index/create'); $this->assertModule('payments'); $this->assertController('index'); $this->assertAction('create'); }
public function testCallbackActionCorrectTxnId() { //Fake data $orderType = Subscriptions_Model_Subscription::ORDER_TYPE_SUBSCRIPTION; $planId = '3'; $price = '50'; //Create user $account = new Users_Model_User(); $account->avatar = null; $account->login = '******' . date('YmdHis'); $account->email = 'testCallbackActionCorrectTxnId' . time() . '@example.org'; $account->password = md5('password'); $account->role = Users_Model_User::ROLE_USER; $account->status = Users_Model_User::STATUS_ACTIVE; $account->save(); //Create order $orderManager = new Payments_Model_Order_Manager(); $order = $orderManager->createOrder($account->id, $price); $mock = $this->getMock('Payments_Model_Order_Manager', array('checkPaypalPostParams')); $mock->expects($this->any())->method('checkPaypalPostParams')->will($this->returnValue(true)); $mock->setDbTable('Payments_Model_Order_Table'); //Create custom param $customParam = implode('-', array($orderType, $order->id)); $params = array('custom' => '111', 'subscr_id' => 111, 'mc_gross' => 0, 'txn_type' => 111, 'txn_id' => 111); $subscriptionManager = new Subscriptions_Model_Subscription_Manager(); $subscriptionManager->createPaidSubscription($account->id, $planId, $order->id); //Test incorrect custom param try { $mock->handlePaypalRequest($params); $this->fail('An expected exception has not been raised.'); } catch (Exception $ex) { // Test ok. } //Test incorrect custom param $params['custom'] = '0-0'; try { $mock->handlePaypalRequest($params); $this->fail('An expected exception has not been raised.'); } catch (Exception $ex) { // Test ok. } $params['custom'] = $customParam; try { $mock->handlePaypalRequest($params); $this->fail('An expected exception has not been raised.'); } catch (Exception $ex) { // Test empty $amount. } $params['mc_gross'] = $price; $this->assertTrue($mock->handlePaypalRequest($params)); //Test payOrder() incorrect orderType $params['custom'] = implode('-', array('incorrect', $order->id)); try { $mock->handlePaypalRequest($params); $this->fail('An expected exception has not been raised.'); } catch (Exception $ex) { // Test ok. } //Test payOrder() incorrect orderId $params['custom'] = implode('-', array('incorrect', '12345678')); try { $mock->handlePaypalRequest($params); $this->fail('An expected exception has not been raised.'); } catch (Exception $ex) { // Test ok. } //Test Cancel Subscription $params['txn_type'] = 'subscr_cancel'; $params['custom'] = $customParam; $this->assertTrue($mock->handlePaypalRequest($params)); //Test Cancel Subscription incorrect orderType $params['custom'] = implode('-', array('incorrect', $order->id)); try { $mock->handlePaypalRequest($params); $this->fail('An expected exception has not been raised.'); } catch (Exception $ex) { // Test ok. } }