I have a third party payment gateway that is up and running, but I could really use your help on sending information back to magento and redirecting to the success page after a payment is completed.
Magento redirects to our payment gateway on "place order" (via getOrderPlacedRedirectUrl
) in the checkout process and submits the order to the store dashboard. Our payment gateway has a customizable redirect url but when I try to route back to magento's success page I am getting a 403 forbidden error.
I would like to send the user back to a success page and update the order status/send a confirmation email from magento based on the response parameters from my payment gateway.
I have a PaymentConroller
with a redirectAction
, responseAction
and cancelAction
methods (though I don't think responseAction
is ever called).
Extra info: I have also tried directing to my payment gateway once it is selected as the payment method (via getCheckoutRedirecturl
before the "review order" step) the same way PayPal Express does, but again I am running into the problem of returning to magento afterwards with the 403 error. This would be my ideal setup and place the order after the payment has been made. Is this even possible?
Essentially my problem(s) revolve around getting back to magento and the current order after my payment is complete.
Thanks in advance for your help!
My code is below.
PaymentController (controllers/Paymentcontroller.php
):
<?php
/**
* @method redirectAction()
* @method responseAction()
* @method cancelAction()
*/
class Knox_KnoxGateway_PaymentController extends Mage_Core_Controller_Front_Action {
public function redirectAction() {
$this->loadLayout();
$block = $this->getLayout()->createBlock('Mage_Core_Block_Template','KnoxGateway',array('template' => 'KnoxGateway/redirect.phtml'));
$this->getLayout()->getBlock('content')->append($block);
$this->renderLayout();
}
/**
* @var $validated is initialized to true
* @var $orderId is set to 'default', might make it a number
*/
public function responseAction() {
if($this->getRequest()->isPost()) {
$validated = true;
$orderId = 'default';
if($validated) {
$order = Mage::getModel('sales/order');
$order->loadByIncrementId($orderId);
$order->setState(Mage_Sales_Model_Order::STATE_PROCESSING, true, 'Knox has authorized the payment.');
$order->sendNewOrderEmail();
$order->setEmailSent(true);
$order->save();
Mage::getSingleton('checkout/session')->unsQuoteId();
Mage_Core_Controller_Varien_Action::_redirect('checkout/onepage/success', array('_secure'=>true));
}
else {
$this->cancelAction();
Mage_Core_Controller_Varien_Action::_redirect('checkout/onepage/failure', array('_secure'=>true));
}
}
else
Mage_Core_Controller_Varien_Action::_redirect('');
}
public function cancelAction() {
if (Mage::getSingleton('checkout/session')->getLastRealOrderId()) {
$order = Mage::getModel('sales/order')->loadByIncrementId(Mage::getSingleton('checkout/session')->getLastRealOrderId());
if($order->getId()) {
$order->cancel()->setState(Mage_Sales_Model_Order::STATE_CANCELED, true, 'Knox has declined the payment.')->save();
}
}
}
}
Standard (Models/Standard.php
):
<?php
class Knox_KnoxGateway_Model_Standard extends Mage_Payment_Model_Method_Abstract {
/**
* @var $_code defines the name of our plugin when we register to magento
*/
protected $_code = 'KnoxGateway';
/**
* @var $_isInitializeNeeded is set to true to declare we need
* to initialize while the order is in place
*/
protected $_isInitializeNeeded = true;
/**
* @var $_canUseInternal is set to true to declare that people can pay
* with knox from the admin pages
*/
protected $_canUseInternal = true;
/**
* @var $_canUseForMultishipping is set to false so that we don't try
* to send to multiple shipping addresses
*/
protected $_canUseForMultishipping = false;
/**
* @var $_canUseCheckout is set to true due to the fact that we want to
* be used like any other normal payment gateway
*/
protected $_canUseCheckout = true;
/**
* @return getOrderPlacedRedirectUrl simply returns a redirect to Knox
*/
public function getOrderPlaceRedirectUrl() {
$key = Mage::getStoreConfig('payment/KnoxGateway/api_key');
$grandTotal = Mage::getSingleton('checkout/cart')->getQuote()->getGrandTotal();//'11.50';
$reccur = "ot";
$callback = Mage::getStoreConfig('payment/KnoxGateway/callback_url');//"https://www.knoxpayments.com";//
$info = Mage::getStoreConfig('payment/KnoxGateway/info_request');
$invoice = Mage::getStoreConfig('payment/KnoxGateway/invoice_detail');
return "https://knoxpayments.com/pay?api_key=".$key."&amount=".$grandTotal."&redirect_url=".$callback."&recurring=".$reccur."&information_request=".$info."&invoice_detail=".$invoice."";
// "https://knoxpayments.com/newflow/?api_key='{$this->$API_KEY}'&api_password='{$this->$API_PASSWORD}'&amount='{$this->$DATA_AMOUNT}'&redirect_url='{$this->$CALLBACK_URL}'&recurring=ot&information_request='{$this->$INFO_REQUEST}'&invoice_detail='{$this->$INVOICE_DETAIL}'&user_request";
}
}
?>
config (etc/config.xml
):
<?xml version="1.0"?>
<config>
<modules>
<Knox_KnoxGateway>
<version>0.1.0</version>
</Knox_KnoxGateway>
</modules>
<global>
<models>
<KnoxGateway>
<class>Knox_KnoxGateway_Model</class>
</KnoxGateway>
</models>
<helpers>
<KnoxGateway>
<class>Knox_KnoxGateway_Helper</class>
</KnoxGateway>
</helpers>
<blocks>
<KnoxGateway>
<class>Knox_KnoxGateway_Block</class>
</KnoxGateway>
</blocks>
</global>
<default>
<payment>
<KnoxGateway>
<model>KnoxGateway/standard</model>
<active>1</active>
<order_status>payment_review</order_status>
<title>Knox Gateway</title>
<payment_action>sale</payment_action>
<sort_order>1</sort_order>
</KnoxGateway>
</payment>
</default>
<frontend>
<routers>
<KnoxGateway>
<use>standard</use>
<args>
<module>Knox_KnoxGateway</module>
<frontName>KnoxGateway</frontName>
</args>
</KnoxGateway>
</routers>
</frontend>
</config>
system (etc/system.xml):
<?xml version="1.0"?>
<config>
<sections>
<payment>
<groups>
<KnoxGateway translate="label comment" module="paygate">
<label>Knox Gateway</label>
<frontend_type>text</frontend_type>
<sort_order>0</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<fields>
<active translate="label">
<label>Enabled</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_yesno</source_model>
<sort_order>10</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>0</show_in_store>
</active>
<title translate="label">
<label>Title</label>
<frontend_type>text</frontend_type>
<sort_order>20</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</title>
<callback_url translate="label">
<label>Knox Callback URL</label>
<frontend_type>text</frontend_type>
<sort_order>10</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</callback_url>
<info_request translate="label">
<label>Information request</label>
<frontend_type>text</frontend_type>
<sort_order>10</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</info_request>
<why_who translate="label">
<label>Why Who</label>
<frontend_type>text</frontend_type>
<sort_order>10</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</why_who>
<invoice_detail translate="label">
<label>Invoice Detail</label>
<frontend_type>text</frontend_type>
<sort_order>10</sort_order>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<show_in_default>1</show_in_default>
</invoice_detail>
<api_key translate="label">
<label>Knox Api Key</label>
<frontend_type>text</frontend_type>
<sort_order>10</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</api_key>
<api_password translate="label">
<label>Knox Api Password</label>
<frontend_type>text</frontend_type>
<sort_order>10</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</api_password>
</fields>
</KnoxGateway>
</groups>
</payment>
</sections>
</config>