3

After the user registration page, web clients will come to this page, which contains only a proceed button. But I want to skip this page and proceed to the payment page. Is there any way to do this action completely in the controller?

<form method="post" action="https://www.ccavenue.com/shopzone/cc_details.jsp">
    <input type=hidden name=Merchant_Id value="<%= @Merchant_id %>">
    <input type=hidden name=Amount value="<%= @Amount %>">
    <input type=hidden name=Order_Id value="<%= @user_id %>">
    <input type=hidden name=Redirect_Url value="<%= @redirect_url %>">
    <input type=hidden name=Checksum value="<%= @checksum %>">
    <input type="submit" class="Register_button" value="Proceed to payment">
</form>
thejartender
  • 9,339
  • 6
  • 34
  • 51
Aahlad
  • 141
  • 5
  • 14

4 Answers4

3

You should do POST request from controller.

uri = URI.parse("https://www.ccavenue.com/shopzone/cc_details.jsp")
http = Net::HTTP.new(uri.host, uri.port)

form_data = {
  "Merchant_Id" => @Merchant_id,
  "Amount" => @Amount,
  "Order_Id" => @user_id,
  "Redirect_Url" => @redirect_url,
  "Checksum" => @checksum
}

request = Net::HTTP::Post.new(uri.request_uri)
request.set_form_data(form_data)

response = http.request(request)

You can use httparty gem to do post request.

Lütfi Demirci
  • 478
  • 1
  • 3
  • 9
  • 1
    Hey Demirici!! Thanks for your response, i want the page to redirect with parameters to specified URL!! by using POST request i can only send parameters and have response.But, the redirection is not happening. – Aahlad May 17 '12 at 06:39
  • Hi @Aahlad ! Were you able to solve the issue? I am trying to solve the same problem. Thanks! – Hans Sep 18 '14 at 15:02
1

No, not the way you're wanting.

As others have said, you can POST from within your controller, and receive the response, but if you want to have the browser post to the remote page, it has to be through a form. A common solution is to submit the form on load via javascript. e.g.

<body onload="document.forms['myform'].submit();">
    <form id="myform">
        …
    </form>
</body>
Colin
  • 784
  • 2
  • 8
  • 21
1

This might be years late response, but in case someone is still looking, I found this Repost plugin that could achieve this behaviour.

Could be used just like the normal redirect_to method in controller.

redirect_post('https://external-url.io/endpoint', params: {...})
Kok A.
  • 167
  • 1
  • 15
  • I really appreciate a newer answer to this question as RoR has changed much over the years. I was able to use the repost gem as you suggested and it worked perfectly for my needs. Thank you @Odeth A. – CWarrington Aug 04 '21 at 18:49
0

There's no reason you couldn't post from a controller using uri and net/http in the Ruby standard library and handle the response from the server there.

Some good examples for using net/http can be found here. https://github.com/augustl/net-http-cheat-sheet/blob/master/post_form.rb

Still that's probably not the best way to handle this.

sprysoft
  • 1,468
  • 2
  • 11
  • 7