Mike Dorsey
Mike Dorsey

Reputation: 21

Rails, How to submit PayPal requests from controller, instead of a form, URL encoding

We are trying to submit requests to PayPal from a controller instead of from a form. When we use the form version everything works correctly, but when we use redirect in the controller, we get errors no matter what we do. It appears that there is some sort of URL encoding problem.

Form:

<% form_tag Paypal_URL do %> <%= hidden_field_tag :cmd, "_s-xclick" %> <%= hidden_field_tag :encrypted, @paypal_link %> <%= submit_tag "Complete Purchase" %> <% end %>

Controller: We've tried all the following, but all result in errors.

redirect_to Paypal_URL + "?cmd=_s-xclick:encrypted=" + @paypal_link and return => Rails give us URI errors

redirect_to URI.encode(Paypal_URL + "?cmd=_s-xclick:encrypted=" + @paypal_link) and return => We get a 500 error from paypal. Looks like the link is encoding wrong

Upvotes: 2

Views: 822

Answers (1)

Carlos A. Cabrera
Carlos A. Cabrera

Reputation: 1984

Mike,

This might not be the best solution to the problem but I also needed to redirect to a Paypal payment url after from a controller. The following worked fine for me:

if @project.save!

  # Send to PayPal
  redirect_to URI.encode("https://www.paypal.com/cgi-bin/webscr" + "?cmd=_xclick&[email protected]&currenct_code=USD&item_name#{@project.name}&amount=#{@project.price}")

else
  # not created
  redirect_to :back, :notice => "Didn't work"
end

Upvotes: 3

Related Questions