Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to receive POST request in an AngularJS page?

We made a AngularJS app where user opens a URL (xyz.com/booking) fills the forms and then selects some items for purchase.

After that user clicks on BUY button and leaves the site for the payment gateway site. On successful payment, Payment Gateway sends back the user by sending a POST request on a Callback URL(xyz.com/booking-success).

Now problem is that my corresponding Angular Page that I configured for that Callback URL is not opening. I configured xyz.com/booking-success page in RouteProvider, but that seems not to be working.

However, if I open the page xyz.com/booking-success directly via browser, it gets opened.

How this scenario can be handled? Any help would be greatly appreciated.

like image 934
sachinaero Avatar asked Jun 28 '15 21:06

sachinaero


1 Answers

  1. ngRoute can't handle URLs accessed via POST , if you have rewrite on index.html you are just posting to index.html not to you route, it does not work when you have configured /booking-success and you POST on that url via cURL you are actually posting to index.html. Routing in angular is just "virtual" not real. Systems like cURL or any other HTTP client cant see it and therefore they are posting to index.html.

  2. Never handle callback URLs from payment/booking/any gateway inside angular. Angular is dangerous because you must be sure that callback process will be executed. Angular is not fault tolerant , if there will be any javascript error, any error inside your angular call you will lost all callback data and you are literally screwed.

Callbacks must be part of your REST API and must be implemented to return HTTP status code 200, otherwise in case on any other HTTP code, gateway should know that they must retry call. (For example, skrill.com is repeating a callback 10 times in case of no HTTP 200 code). Actually, AngularJS will respond every time with a 200 because your web server will serve the index.html, it doesn't care what happens inside javascript code.

  1. Only 1 part that can be handled via angular is redirect_url, usually URL that user is redirected after successful payment/action on broker side. But in the meantime the callback URL is still called on a lower level of your application. The redirect URL is called via GET inside browser redirect so user will see it as normal.

Every "normal" gateway has callback_url (low level notification for your API that action is successful and redirect_url, that is redirecting back to your site xyz.com/booking-success). But they should be separated. If your gateway does not have redirection, you don't have a choice but to open whole booking in new window. And somehow notify your front end after callback is called. That can be done via polling with intervals. For example, 10 seconds, which is retrieving actual status of your booking_id (column is_processed (true|false) which you will set inside callback code. When status changes from false to true you will notify your front end (redirect user or show popup) . Also you can implement socket.io library which you can handle polling and your notification will be instant when callback URL is called.

like image 168
Milos Mosovsky Avatar answered Oct 07 '22 00:10

Milos Mosovsky