Ejemplo n.º 1
0
 public function onPackageTransactionReturn(Ynmember_Model_Order $order, array $params = array())
 {
     $viewer = Engine_Api::_()->user()->getViewer();
     $view = Zend_Registry::get('Zend_View');
     // Check that gateways match
     if ($order->gateway_id != $this->_gatewayInfo->gateway_id) {
         throw new Engine_Payment_Plugin_Exception('Gateways do not match');
     }
     //Get created orders
     if (!$order->isOrderPending()) {
         throw new Engine_Payment_Plugin_Exception('CREDIT_No orders found');
     }
     $user = $order->getUser();
     // Check order states
     if ($order->status == 'completed') {
         return 'completed';
     }
     // Let's log it
     $this->getGateway()->getLog()->log('Return: ' . print_r($params, true), Zend_Log::INFO);
     // Check for cancel state - the user cancelled the transaction
     if ($params['state'] == 'cancel') {
         $order->onCancel();
         // Error
         throw new Payment_Model_Exception('Your payment has been cancelled and ' . 'not been purchased. If this is not correct, please try again later.');
     }
     // Check for processed
     if (empty($params['credit_card_processed'])) {
         // This is a sanity error and cannot produce information a user could use
         // to correct the problem.
         throw new Payment_Model_Exception('There was an error processing your ' . 'transaction. Please try again later.');
     }
     // Ensure product ids match
     if ($params['merchant_product_id'] != $order->getGatewayIdentity($order->user_id, $order->price)) {
         // This is a sanity error and cannot produce information a user could use
         // to correct the problem.
         throw new Payment_Model_Exception('There was an error processing your ' . 'transaction. Please try again later.');
     }
     // Ensure order ids match
     if ($params['order_id'] != $order->order_id && $params['merchant_order_id'] != $order->order_id) {
         // This is a sanity error and cannot produce information a user could use
         // to correct the problem.
         throw new Payment_Model_Exception('There was an error processing your ' . 'transaction. Please try again later.');
     }
     // Ensure vendor ids match
     if ($params['sid'] != $this->getGateway()->getVendorIdentity()) {
         // This is a sanity error and cannot produce information a user could use
         // to correct the problem.
         throw new Payment_Model_Exception('There was an error processing your ' . 'transaction. Please try again later.');
     }
     // Validate return
     try {
         $this->getGateway()->validateReturn($params);
     } catch (Exception $e) {
         /*if (!$this->getGateway()->getTestMode()) {
             // This is a sanity error and cannot produce information a user could use
             // to correct the problem.
             throw new Payment_Model_Exception('There was an error processing your ' .
               'transaction. Please try again later.');
           } else {
             echo $e; // For test mode
           }*/
     }
     // Update order with profile info and complete status?
     $order->gateway_transaction_id = $params['order_number'];
     $order->save();
     // Insert member transaction
     $transactionsTable = Engine_Api::_()->getDbtable('transactions', 'ynmember');
     $db = $transactionsTable->getAdapter();
     $db->beginTransaction();
     try {
         //active feature
         $featureTable = Engine_Api::_()->getItemTable('ynmember_feature');
         $featureRow = $featureTable->getFeatureRowByUserId($order->user_id);
         if ($order->number_day == 1) {
             $type = 'day';
         } else {
             $type = 'days';
         }
         $now = date("Y-m-d H:i:s");
         $expiration_date = date_add(date_create($now), date_interval_create_from_date_string($order->number_day . " " . $type));
         if (!empty($featureRow)) {
             if ($featureRow->active == 1) {
                 $expiration_date = date_add(date_create($featureRow->expiration_date), date_interval_create_from_date_string($order->number_day . " " . $type));
             }
             $featureRow->modified_date = $now;
             $featureRow->expiration_date = date_format($expiration_date, "Y-m-d H:i:s");
             $featureRow->active = 1;
             $featureRow->save();
         } else {
             $featureRow = $featureTable->createRow();
             $featureRow->user_id = $viewer->getIdentity();
             $featureRow->creation_date = $now;
             $featureRow->modified_date = $now;
             $featureRow->expiration_date = date_format($expiration_date, "Y-m-d H:i:s");
             $featureRow->active = 1;
             $featureRow->save();
         }
         $description = $view->translate(array('Feature in %s day', 'Feature in %s days', $order->number_day), $order->number_day);
         //save transaction
         $transactionsTable->insert(array('status' => 'completed', 'gateway_id' => $this->_gatewayInfo->gateway_id, 'amount' => $order->price, 'currency' => $params['currency_code'], 'user_id' => $order->user_id, 'creation_date' => date("Y-m-d"), 'payment_transaction_id' => $params['order_number'], 'description' => $description));
         $db->commit();
     } catch (Exception $e) {
         $db->rollBack();
         throw $e;
     }
     // Insert transaction
     $transactionsTable = Engine_Api::_()->getDbtable('transactions', 'payment');
     $db = $transactionsTable->getAdapter();
     $db->beginTransaction();
     try {
         $transactionsTable->insert(array('user_id' => $order->user_id, 'gateway_id' => $this->_gatewayInfo->gateway_id, 'timestamp' => new Zend_Db_Expr('NOW()'), 'order_id' => $order->getIdentity(), 'type' => 'Feature Member', 'state' => 'okay', 'gateway_transaction_id' => $params['order_number'], 'amount' => $order->price, 'currency' => $params['currency_code']));
         $db->commit();
     } catch (Exception $e) {
         $db->rollBack();
         throw $e;
     }
     $order->onPaymentSuccess();
     return 'completed';
 }