I want to implement custom payment gateway with redirect. I found a lot of questions about it here.
(Like Payment gateway with redirects in magento2. )
From which I think I have two options.
- After placing order, redirect customer to my custom controller, there have some logic and then redirect him to gw.
- Make AJAX req to my custom controller, that returns gw url, and redirect user there with JS.
But, in my custom controller, I somehow need to get the newly created order data, or at least id. Presumably, I should send them in my redirect/ajax req. But I don't know how.
Currently, my code in method-render looks like this:
return Component.extend({
defaults: {
template: 'Vendor_Module/payment/template',
redirectAfterPlaceOrder: false
},
afterPlaceOrder: function () {
$.mage.redirect(
url.build('route/controller/action')
);
}
});
Is there a way I can add order data to the request? Or is there any other way I can retrieve order data in controller?
Note: In controller tried this
/**
* @var \Magento\Checkout\Model\Type\Onepage\Interceptor $onepage
*/
$onepage = $this>_objectManager>get('Magento\Checkout\Model\Type\Onepage');
$orderId = $onepage->getLastOrderId();
But I don't think it's transaction save. I get an id even when I do random get request on my controller. Also I don't know how this behave when two orders are placed at the same time.
Best Answer
magento\app\code\Custom\Module\etc\frontend\events.xml
magento\app\code\Custom\Module\Observer\OrderPlaceAfter.php