0

I have a rails application that calls an external API for payments. The service providing this API sends back response through a callback that i passed in the initial request. However, i want this response to be reflected back on the users front-end (browser) which I'm using react. Problem is i don't know how to connect the callback to the user front-end. Example code is as shown.

class PaymentsController < ActionController::API
  def pay
    # Payment call that includes my callback 
  end

  def callback
    # When payment is successful, this route is hit successfully
    result = params[:Body]

    # What i want to do is inform the user that payment is successful
  end
end
Melvin Otieno
  • 79
  • 2
  • 2
  • 17
  • You either need to use polling from the client (use a timeout to repeatedly ask the server if its done yet) or [websockets](https://guides.rubyonrails.org/action_cable_overview.html) which allows a two way connection so that the server can send data to client without the client first requesting it. – max Apr 18 '20 at 12:13
  • I wan't to use the websockets route as rails already provides a way of doing it. Problem is, i cannot use the connection on the callback, so how will the callback notify the connection to send the data to the client – Melvin Otieno Apr 18 '20 at 12:24
  • https://stackoverflow.com/questions/42679739/how-can-i-call-a-channel-method-in-a-rails-controller – max Apr 18 '20 at 12:27
  • Thanks for the link, however, i am not quite sure that it has helped me that much – Melvin Otieno Apr 18 '20 at 13:12
  • One solution can be you can redirect the user to the new page saying `successful payment` – Avinash Kumar Singh Apr 18 '20 at 20:12
  • yes but i'm using a react front end – Melvin Otieno Apr 18 '20 at 20:25

1 Answers1

0

Figured that I could use server sent events to solve this issue so that my front end would be listening to the events.

Melvin Otieno
  • 79
  • 2
  • 2
  • 17