Support > Documentation > PayPlans 2.x > Developers > Create payment gateway App

Create payment gateway App

A payment gateway is an e-commerce application service provider service that automates the payment transaction between the buyer and merchant.

In PayPlans, after choosing a desired plan, when the user logins, he/she needs to select the payment method through which payment has to be made. After payment selection when user clicks on checkout,the work of payment gateway app starts.Following events can be triggered.

1.function isApplicable($refObject = null, $eventName='')

       isApplicable function is used to find whether the current app-instance should be triggered for given event and reference object. If you want to decide to trigger app as per event name then return true from here.

  1. function isApplicable($refObject = null, $eventName='')
  2. {
  3. // return true for event onPayplansControllerCreation
  4. if($eventName == 'onPayplansControllerCreation'){
  5. return true;
  6. }
  7. return parent::isApplicable($refObject, $eventName);
  8. }

2. function onPayplansControllerCreation($view, $controller, $task, $format)

     onPayplansControllerCreation,this event is triggered just before creation of controller instance. In payment gateways, we work only when view is payment and task is notify. Developers can process notification and can extract values from notification and get details, for example, for which invoice the notification arrives. They can extract invoice key or payment key from the payment notification.

  1. function onPayplansControllerCreation($view, $controller, $task, $format)
  2. {
  3. if($view == 'payment' && $task == 'notify')
  4. {
  5. $paymentKey = JRequest::getVar('invoice_num', null);
  6. if($paymentKey){
  7. $prefix = JString::substr($paymentKey, 0,3);
  8. if($prefix !== 'PK_')
  9. {
  10. //get payment key from order key
  11. $orderId = XiHelperUtils::getIdFromKey($paymentKey);
  12. $order = PayplansOrder::getInstance($orderId);
  13. $paymentId = $order->getParam('payment_id');
  14. $paymentKey = XiHelperUtils::getKeyFromId($paymentId);
  15. }
  16. else
  17. {
  18. $paymentKey = JString::substr($paymentKey, 3);
  19. }
  20. JRequest::setVar('payment_key', $paymentKey, 'POST');
  21. return true;
  22. }
  23.  

3. function onPayplansPaymentForm(PayplansPayment $payment, $data = null)

       When user selects payment gateway to pay and clicks on checkout button. He will be redirected to new page. Developers of app can decide what HTML should render using onPayplansPaymentForm event. HTML code can be a form or just a simple button. There are some payment gateways that allow to render the form at our end only. In that case you need to render the form HTML (You may take help of Authorize.net App for this case) and there are some, which redirect to there own site, for further processing, after check out (You may take help of Paypal App for this case).

  1. function onPayplansPaymentForm(PayplansPayment $payment, $data = null)
  2. {
  3. $invoice = $payment->getInvoice(PAYPLANS_INSTANCE_REQUIRE);
  4. $amount = $invoice->getTotal();
  5. $this->assign('post_url', XiRoute::_("index.php?option=com_payplans&view=payment&task=complete&payment_key=".$payment->getKey()));
  6. $this->assign('payment', $payment);
  7. $this->assign('invoice', $invoice);
  8. $this->assign('amount', $amount);
  9. return $this->_render('form');<br>}<br>


4. public function onPayplansPaymentAfter(PayplansPayment $payment, $action, $data, $controller)

This event can be use to post the data(as filled in the form) to their respective payment gateways. Recurring profiles can be created, if plan is of recurring type.If any error is there during transaction, then it will be redirected to the error page by calling the parent event.

  1. function onPayplansPaymentAfter(PayplansPayment $payment, $action, $data, $controller)
  2. {
  3. if($action == 'cancel'){
  4. return true;
  5. }
  6. if($invoice->isRecurring()){
  7.  
  8. //Will create recurring profile and then post the data to the payment gateway.
  9. $this->_processRecurringRequest($payment, $data);
  10. }
  11. else {
  12. //Will post the the data to the desired payment gateway.
  13. $this->_processNonRecurringRequest($payment, $data);
  14. }
  15. $payment->save();
  16.  
  17. //calling of parent event is required to check whether
  18. // there is any error during the above process.
  19. return parent::onPayplansPaymentAfter($payment, $action, $data, $controller);<br>} <br>


5. function onPayplansPaymentNotify($payment, $data, $controller)

Whenever payment notification arrives, onPayplansPaymentNotify event can be triggered. Processing of notification is done, means find out what type of notification is there whether it is for Sign-u(R), Payment, Refund, Hold or expire and verify whether transaction has been successfully done or not.Notification also contain information regarding the amount.So,if the amount is positive or negative,the set the transaction amount and message accordingly else make the taransaction for zero amount. If any error is present, then redirect to error page.

  1. function onPayplansPaymentNotify($payment, $data, $controller)
  2. {
  3. $invoice = $payment->getInvoice(PAYPLANS_INSTANCE_REQUIRE);
  4.  
  5. // if its a recurring subscription
  6. if(isset($data['x_subscription_id']) && $data['x_subscription_id'] ){
  7.  
  8. // get the transaction instace of lib
  9. $transaction = PayplansTransaction::getInstance();
  10. $transaction->set('user_id', $payment->getBuyer())
  11. ->set('invoice_id', $invoice->getId())
  12. ->set('payment_id', $payment->getId())
  13. ->set('gateway_txn_id', isset($data['x_trans_id']) ? $data['x_trans_id'] : 0)
  14. ->set('gateway_subscr_id', isset($data['x_subscription_id']) ? $data['x_subscription_id'] : 0)
  15. ->set('gateway_parent_txn', isset($data['parent_txn_id']) ? $data['parent_txn_id'] : 0)
  16. ->set('params', PayplansHelperParam::arrayToIni($data));
  17.  
  18.  
  19. $errors = $this->_processNotification($transaction, $data, $payment);
  20. $transaction->save();
  21. return count($errors) ? implode("\n", $errors) : ' No Errors';
  22. }
  23.  
  24. }

6. When you want to terminate any payment, then onPayplansPaymentTerminate event is triggered. This event is triggered when the plan is of recurring type.
For example, if recurrence count is set to 5 but, after 2 recurrence if user want to unsubscribe it, then this event can be used to terminate the order.

6. When you want to terminate any payment, then onPayplansPaymentTerminate event is triggered. This event is triggered when the plan is of recurring type.
   For example, if recurrence count is set to 5 but, after 2 recurrence if user want to unsubscribe it, then this event can be used.
  1. public function onPayplansPaymentTerminate(PayplansPayment $payment, $controller)
  2. {
  3. $transactions = $payment->getTransactions();
  4. foreach($transactions as $transaction){
  5. $subscriptionId = $transaction->get('gateway_subscr_id', 0);
  6. if(!empty($subscriptionId)){
  7. break;
  8. }
  9. }
  10. $arbInstance->setRefId($payment->getKey());
  11. $response = $arbInstance->cancelSubscription($subscriptionId);
  12.  
  13. $invoice = $payment->getInvoice(PAYPLANS_INSTANCE_REQUIRE);
  14. $txn = PayplansTransaction::getInstance();
  15. $txn->set('user_id', $payment->getBuyer())
  16. ->set('invoice_id', $invoice->getId())
  17. ->set('payment_id', $payment->getId())
  18. ->set('gateway_txn_id', isset($data['x_trans_id']) ? $data['x_trans_id'] : 0)
  19. ->set('gateway_subscr_id', $subscriptionId)
  20. ->set('gateway_parent_txn', isset($data['parent_txn_id']) ? $data['parent_txn_id'] : 0);
  21.  
  22. $txn->set('message', 'COM_PAYPLANS_PAYMENT_FOR_CANCEL_ORDER')->save();
  23. return $this->_render('cancel_success');
  24. }
  25.  
  • Saturday, 12 May 2012
  • Posted in: Developers

Disqus Comments

blog comments powered by Disqus

PayPlans + Stackideas Pack!