Example #1
0
 public function createPackageTransaction(Ynsocialads_Model_Order $order, array $params = array())
 {
     // Check that gateways match
     if ($order->gateway_id != $this->_gatewayInfo->gateway_id) {
         throw new Engine_Payment_Plugin_Exception('Gateways do not match');
     }
     if (!$order->isOrderPending()) {
         throw new Engine_Payment_Plugin_Exception('CREDIT_No orders found');
     }
     /**
      * Get related info
      *
      * @var $view Zend_View
      */
     $view = Zend_Registry::get('Zend_View');
     $currency = $this->getGateway()->getCurrency();
     $payment_requests = array();
     $payment_item = array();
     $payment_item = array_merge($payment_item, array('L_PAYMENTREQUEST_0_NAME0' => $view->translate('Buying Ad'), 'L_PAYMENTREQUEST_0_DESC0' => $view->translate('Buying Ad from %s', $view->layout()->siteinfo['title']), 'L_PAYMENTREQUEST_0_AMT0' => $order->price));
     $seller = $this->_gatewayInfo->config;
     $payment_requests = array_merge($payment_requests, array_merge(array('PAYMENTREQUEST_0_AMT' => $order->price, 'PAYMENTREQUEST_0_SELLERPAYPALACCOUNTID' => $seller, 'PAYMENTREQUEST_0_DESC' => $view->translate('Buying Ad'), 'PAYMENTREQUEST_0_PAYMENTACTION' => 'order', 'PAYMENTREQUEST_0_PAYMENTREQUESTID' => $order->getIdentity(), 'PAYMENTREQUEST_0_INVNUM' => $params['order_id'], 'PAYMENTREQUEST_0_CURRENCYCODE' => $currency, 'PAYMENTREQUEST_0_NOTIFYURL' => $params['ipn_url']), $payment_item));
     $params['driverSpecificParams']['PayPal'] = $payment_requests;
     $transaction = $this->createTransaction($params);
     return $transaction;
 }
Example #2
0
 public function onPackageTransactionReturn(Ynsocialads_Model_Order $order, array $params = array())
 {
     // 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();
     $item = $order->getSource();
     // Check order states
     if ($order->status == 'completed') {
         return 'completed';
     }
     // 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'] != $item->getGatewayIdentity($order->ad_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 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();
     $real_price = 0;
     if ($item instanceof Ynsocialads_Model_Package) {
         $real_price = (double) $order->price;
     }
     // Insert socialads transaction
     $transactionsTable = Engine_Api::_()->getDbtable('transactions', 'ynsocialads');
     $select = $transactionsTable->select()->where('ad_id = ?', $order->ad_id)->limit(1);
     $item = $transactionsTable->fetchRow($select);
     if (!isset($item)) {
         $db = $transactionsTable->getAdapter();
         $db->beginTransaction();
         try {
             $ad = Engine_Api::_()->getItem('ynsocialads_ad', $order->ad_id);
             $ad->status = 'pending';
             $ad->save();
             $transactionsTable->insert(array('start_date' => $ad->start_date, 'status' => 'completed', 'gateway_id' => $this->_gatewayInfo->gateway_id, 'amount' => $order->price, 'currency' => $params['currency_code'], 'ad_id' => $ad->getIdentity(), 'user_id' => $order->user_id, 'payment_transaction_id' => $params['order_number']));
             $db->commit();
         } catch (Exception $e) {
             $db->rollBack();
             throw $e;
         }
     } else {
         $ad = Engine_Api::_()->getItem('ynsocialads_ad', $order->ad_id);
         $ad->status = 'pending';
         $ad->save();
         $item->status = 'completed';
         $item->gateway_id = $this->_gatewayInfo->gateway_id;
         $item->payment_transaction_id = $params['order_number'];
         $item->save();
     }
     // 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' => 'Buy Ads', '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';
 }