I'm implementing a new payment method for magento2

here is my js code :

function (
    url) {
        'use strict';

        return Component.extend({
            defaults: {
                // template: 'YOUR_NAME_SPACE_Testpayment/payment/testpayment'
                template: 'Magento_SamplePaymentGateway/payment/form'

            placeOrder: function (data, event) {
                if (event) {

                placeOrder = placeOrderAction(this.getData(), false, this.messageContainer);

                    $.when(placeOrder).fail(function () {
                    return true;
                return false;

            afterPlaceOrder: function () {


First question ... how can I fill that ORDER_ID (how to get the order_id after order placed)

2nd question : am I doing it right (logically) ...?

I want to redirect to my module then redirect to bank page for doing payment then redirect back to my shop for saving transaction data is it logically correct way in Magento 2 when payment should be done on an external site?

2 Answers 2


In your payment method renderer you can overwrite the afterPlaceOrder function, which gets called directly after successfully placing the order.

Basically you should be able to get away with the following JS code:

], function (Component, redirectOnSuccessAction, url) {
    'use strict';

     return Component.extend({
     defaults: {
         template: 'Magento_SamplePaymentGateway/payment/form'

     afterPlaceOrder: function () {
            redirectOnSuccessAction.redirectUrl = url.build('mymodule/startpayment/');
            this.redirectAfterPlaceOrder = true;

Then in you StartPayment controller you rely on the \Magento\Checkout\Model\Session and can retrieve the order object via \Magento\Checkout\Model\Session::getLastRealOrder() and then redirect to the bank page with the relevant data from the order.

namespace MyVendor\MyModule\Controller\StartPayment;

use Magento\Checkout\Model\Session;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\Controller\ResultFactory;
use Magento\Sales\Model\Order;

class Index extends Action
    /** @var Session */
    private $checkoutSession;

     * @param Context $context
     * @param CreateHostedCheckout $hostedCheckout
     * @param Session $checkoutSession
    public function __construct(
        Context $context,
        Session $checkoutSession
    ) {
        $this->checkoutSession = $checkoutSession;

     * Initialize redirect to bank
     * @return \Magento\Framework\Controller\ResultInterface
    public function execute()
        /** @var Order $order */
        $order = $this->checkoutSession->getLastRealOrder();

        /** do stuff here */

        /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
        $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);

        return $resultRedirect;

are you redirected to this url mymodule/startpayment/ORDER_ID if yes I think such as Magento 1 you can get last order and use for sending customer to bank page tell me if you test my idea i am not sure but i think this link can help you https://stackoverflow.com/questions/42989047/magento-2-place-order-before-sending-to-payment-gateway

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.