I have integrated my magento cart with my payment gateway and the transactions are running through fine. But I need some help on redirecting the users to the success page and error page based on the payment gateway validation.
On returning from my payment gateway (say XYZPymnt) , my page controller method resultAction is called - code is as below
$session = $this->getCheckout(); $session->getMessages(true); if($paymentValid == 'Y') { //echo "<br>Thank you for shopping with us. Your credit card has been charged and your transaction is successful. We will be shipping your order to you soon.";
$this->successAction(); } else { //echo "<br>Thank you for shopping with us.However,the transaction has been declined."; //Here you need to put in the routines for a failed //transaction such as sending an email to customer //setting database status etc etc
$this->failureAction(); }
}
protected function successAction() { $session = $this->getCheckout();
$messages = $session->getMessages(); $errors = false; if ($messages) { foreach( $messages->getErrors() as $msg ) { $errors[] = $msg->toString(); } }
if (isset($request)) { $this->_order->addStatusToHistory( Mage_Sales_Model_Order::STATE_CANCELED, "Failure from CCAvenue<br/>". (is_array($errors)?implode("<br/>",$errors):'No extra information')); $this->_order->cancel(); }
My problem now is in the redirection once resultAction has been executed. I dont think my code for failureAction and successAction are correct. This is because right now with this code, I get the content of my failure.phtml and success.phtml on top of the Magento layout and am unable to get the contents for failure and success inside the magento content block.
<div class="page-head"> <h3><?php echo $this->__('Your order has been received') ?></h3> </div> <p><strong><?php echo $this->__('Thank you for your purchase!') ?></strong></p> <p> <?php echo $this->__('Your order # is: %s', $this->getOrderId()) ?>.<br/> <?php echo $this->__('You will receive an order confirmation email with details of your order and a link to track its progress.') ?><br/> <?php if ($this->canPrint()) :?> <?php echo $this->__('Click <a href="%s" target="_blank">here to print</a> a copy of your order confirmation.', $this->getPrintUrl()) ?> <?php endif;?> </p> <div class="button-set"> <button class="form-button" onclick="[removed]='<?php echo $this->getUrl() ?>'"><span><?php echo $this->__('Continue Shopping') ?></span></button> </div>
Also the image of the page when payment gateway returns with an error is attached herewith to see where the problem is -
The problem is you’re not redirecting properly, by example here:
if($paymentValid == 'Y') { //echo "<br>Thank you for shopping with us. Your credit card has been charged and your transaction is successful. We will be shipping your order to you soon.";
$this->successAction(); }
So, the website is still in the same url, there is not redirection.
Currently, when you call SuccessAction or FailureAction is rendering first one of then and after Magento renders the template specified in the layout file by the current “action” (this action seems be “result"). I’m 99% sure about I’m telling you
Here I paste some of code executed in the case of the screenshot that you posted:
You are rendering the error payment message directly, using the method setBody of the Response object.
But, you are rendering the template associated with the current action when you call renderLayout.
loadLayout and renderLayout uses the layout configuration and the block class specified there.
I suggest you setup the actions “success” and “failure” in the layout file in the same way than “result” action. Then, you can redirect the browser with this instruction:
// instead of $this->successAction(); $this->_redirect('*/*/sucess');
In theory, you can not call an Action method in the controller by hand. The method should be automatically invoked depending of the requested url (again, we need a confirmation of somebody of Magento team, but I’m 99% sure)
This is a great start to a tutorial, however, I’m still confused by the Controller implementation.
Can someone please please please explain how the controllers actually work.
I need to do an identical thing to the above but I don’t understand how the blocks, layouts and controllers are connected together.
Some modules override different controllers like Mage_Core_Controller_Front_Action
and have actions named: callback3dAction
However, when I grep for callback3dAction it comes up empty. How does does this get called? greping for this over the entire source tree turns up blank.
so for instance, to add a product to the cart, the URL is something like: http://www.yourstore.com/index.php/cart/add/somenumbers/product/3
(this will add product ID = 3 to the cart via the “addAction” method in a cart controller class, found in the Checkout module
Anyway, my point is the URL almost follows the general naming structure which points to the file structure of Magento code -
Hopefully I’m not pointing out what you already know (I have a feeling I am) -
I want to point out also that you might not override a controller to get this to work...but might need to make a new one (assuming you are using a payment module that did not come with Magento...[of course now that I mention this, I do not see any controllers being used for other payment gateways that come with magento such as authorize.net])
edit: Looks like the auth.net payment module creates an alert when there is an error as stock functionality. Kinda lame, but functional.
So what you’re saying is that if the URL is http://www.yourstore.com/checkout/onepage then the controller is the checkout controller.
Looking through the directories I notice that there is a Mage_Checkout_OnepageController located under app/code/core/Mage/Checkout/controllers/OnepageController.php.
It sounds like that would be the onepage checkout controller right?
Although now it gets confusing. By your definition onepage is the action, correct? or does that mean there should be an action called onepageAction?
which I can’t find. Or is onepage another controller beneath checkout?
You said that: the URL almost follows the general naming structure which points to the file structure of Magento code.
Is that just by convention or does Magento calculate the function names based on the URL retrieved or is there some mapping file that does this?
I guess I’m just a bit confused about where to even begin. It’s not clear to me how to override the controller correctly in order to provide a success & failure URL for my payment module and what actions get called. Or how to provide some calculated content on those URL’s and so on.
What am I doing wrong? none of the other payments modules that I’ve looked set up url rewrites like the examples do. Are they all wrong or do you sometimes not need to do that?
Hi, I’ve spent quite a few hours on this and still can’t manage to override the controller.
Even when I have the following config.xml file, it still doesn’t work.
<global> <rewrite> <!-- This is an identifier for your rewrite that should be unique --> <mage_pxpay_checkout_onepage> <from><![CDATA[#^/checkout/onepage/$#]]></from> <to>/pxpay/checkout_onepage</to> </mage_pxpay_checkout_onepage> </rewrite> </global>