1

I'm finding conflicting information on the standard way to integrate PayPal for what I'm trying to do. This is a somewhat higher level question.

My task: After a non-authenticated user of my app (a 'guest') pays for a booking on PayPal, I need them auto redirected back, where I create a record of this, send appropriate emails, and begin my account creation flow.

I'm leaning towards setting it up as described in this SO answer

It uses the 'paypal-sdk-rest' gem and sends a hash of data to a paypal endpoint like:

 values = {
   :business => business,
   :cmd => '_cart',
   :upload => 1,
   :return => return_url
 }
 values.merge!({
   "amount_1" => amount,
   "item_name_1" => name,
   "item_number_1" => id,
   "quantity_1" => '1'
 })                
 "https://www.sandbox.paypal.com/cgi-bin/webscr?" + values.to_query

But this appears based on a Railscast implementation from way back in 2008! I have doubts this is still an ideal way of doing it.

Auto return:

1) In the implementation above, I cannot get auto return to work, despite setting it up on my business profile.

2) A static URL is required for the auto return setup on the profile page, but I want to pass dynamic data to the auto return for triggering subsequent actions. Will the return_url specified above override the static URL set on the profile page, is it supposed to (since I can't get it to work I can't test and see)?

3) I've read that auto return won't work if users pay with a CC on PayPal?? If that's true, auto return isn't something I should depend on so I need to find a better implementation.

So maybe I should be using:

1) Instant Payment Notifications (IPNs)

or

2) Payment Data Transfer (PDTs).

Should I be using these, if so, which one? Do they accomplish the same goals?

I really just want the simplest, quickest implementation. PayPal is not the main payment method on my app, and I'm questioning whether I should bother supporting it now, given how dead straightforward other solutions (like Stripe) have been for me by comparison plus a growing task list that needs attention.

Thanks in advance for any guidance offered.

Community
  • 1
  • 1
rfish26535
  • 439
  • 3
  • 12

2 Answers2

1

I will recommend you use this gem paypal sdk rest, you can follow all the steps like installing the gem, you run the bundle install command and also the rails g paypal:sdk:install also need to login to developer.paypal.com and create a REST API, because you will need the client_id and client_secret in this section:

PayPal::SDK::REST.set_config(
  :mode => "sandbox", # "sandbox" or "live"
  :client_id => "EBWKjlELKMYqRNQ6sYvFo64FtaRLRR5BdHEESmha49TM",
  :client_secret => "EO422dn3gQLgDbuwqTjzrFgFtaRLRR5BdHEESmha49TM")

Also recommend you to go to the Execute Payment section of the link that i have show you above and click in

Only for Payment with payment_method as "paypal"

then you will see another code so copy and paste it in your app, at the end of that code you will see this

 # Create Payment and return status
if @payment.create
  # Redirect the user to given approval url
  @redirect_url = @payment.links.find{|v| v.method == "REDIRECT" }.href
  logger.info "Payment[#{@payment.id}]"
  logger.info "Redirect: #{@redirect_url}"
else
  logger.error @payment.error.inspect
end

There you cant add this line befose the else statement redirect_to @redirect_url That line of code will redirect you to Paypal, and when user accept the payment it will redirect you to the return_url you out in this part of the code

# ###Redirect URLs
  :redirect_urls => {
    :return_url => "http://localhost:3000/payment/execute",
    :cancel_url => "http://localhost:3000/" },
0

You definitely want to use IPN for this. Even with Auto-Return enabled there is no guarantee that the user will make it back to that page, so if you're attempting to handle post-payment processing tasks there you'll run into problems where sometimes the user doesn't make it there so the updates don't occur.

IPN will trigger regardless of whether the user makes it back to your site, so you can be sure it will always run the code as expected. Also, IPN will allow you to correctly handle things like e-checks, where the original IPN would show the payment as pending, and when it clears (or fails) you'd get another IPN with the same transaction ID but an updated status.

IPN also allows you to handle things like refunds, disputes, subscription payments, etc. where a checkout may not have happened, but a transaction of some sort did.

Drew Angell
  • 25,968
  • 5
  • 32
  • 51