I am developing a payment module. I am trying to redirect to an external url after calling an API with curl. The api returns a redirect url (https://.....com/..../..), which I would like to follow and continue with payment on the page it should open.

I am trying to do that from an Observer (before_save_order_payment) I tried every possible way (resultRedirectFactory, _redirect(), header("Location:"), etc) but none of them redirects to that external url.

Sometimes it happens to redirect to the cart page... o_O

This is my first Magento 2 development, so I am not sure if doing that from the observer is the correct way.

1 Answer 1


You can write your redirect logic in your custom method-renderer js file not in Observer. I assume you already created a js file that extending the Magento_Checkout/js/view/payment/default . You can extend the placeOrder or afterPlaceOrder method. You can create and api call within this two methods and redirect client to given url from API call.

You can check Adyen Payment extension. They are making an API call while placing order and getting external URL and showing it in iframe. https://github.com/Adyen/adyen-magento2/blob/develop/view/frontend/web/js/view/payment/method-renderer/adyen-cc-method.js

  • Thank you for the fast answer! I have extended with a -method.js file, but how to pass the value of the redirectUrl from the Observer.php file to this js file? Commented Feb 21, 2021 at 16:26
  • Creating an web API to retrieve data from its better to use instead of Observer. Adyen using some custom API methods to get data during checkout on js side. check here Commented Feb 21, 2021 at 16:33
  • Actually from what I tested right now, I can see that my -method.js is triggered when the page with payment options is shown. In Ayden there is a method in the js file called "validateThreeDS2OrPlaceOrder" it is used to redirect but I don't understand how it receives its responseJSON. Commented Feb 21, 2021 at 17:37

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.