Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to integrate Paypal with Ruby on Rails

Tags:

Im trying to integrate paypal with my ruby on rails application using the rest-api-sdk-ruby gem (https://github.com/paypal/rest-api-sdk-ruby), but could not find enough information around or a good tutorial to back me up. The description provided above, although providing the necessary code, does not show how to handle the methods around or in which files should each method go to.

Could anyone give me a starting point here or point me to a good tutorial?

I am using rails version 4.

Many thanks.

like image 322
user3462261 Avatar asked May 01 '14 15:05

user3462261


People also ask

Does PayPal have PHP?

Being an online store owner, you can integrate PayPal in PHP applications within minutes. You can accept payments via plugins, addons, extensions, or manually via the framework-based applications.

Does stripe have PayPal integration?

Does PayPal work with Stripe? No, you cannot integrate Stripe with PayPal. However, you can use both payment gateways on your online store to accept payments separately.


2 Answers

Standard PayPal Integration with Rails app Active Merchant gem

Step 1

  • Add gem 'activemerchant' in your Gemfile

  • Run bundle install

Step 2

  • Go to "developer.paypal.com" and create an account (also known as Merchant Account) with US address details.

    It will create two dummy test accounts, one each for the buyer and the seller (a.k.a. facilitator), in "sandbox.paypal.com". To see test accounts details Click on "Dashboard -> Accounts"

  • Now set the password for both test accounts by clicking on the profile link.

Step 3

  • Go to seller account (i.e. facilitator) profile details and copy the API Credentials, i.e. username, password and signature. For example:

    Username:  naveengoud-facilitator_api1.gamil.com Password:  VSPALJ5ALA5YY9YJ Signature: AVLslxW5UGzEpaDPEK4Oril7Xo4IAYjdWHD25HhS8a8kqPYO4FjFhd6A 
  • Set these API Credentials in "config/environments/development.rb" as follows:

    config.after_initialize do   ActiveMerchant::Billing::Base.mode = :test   ::GATEWAY = ActiveMerchant::Billing::PaypalGateway.new(     login: "merchant_api1.gotealeaf.com",     password: "2PWPEUKZXAYE7ZHR",     signature: "AFcWxV21C7fd0v3bYYYRCpSSRl31A-dRI5VpyF4A9emruhNYzlM8poc0"   ) end 

Step 4

  • From here onward follow Rails Cast episode 145 (http://railscasts.com/episodes/145-integrating-active-merchant).
like image 110
Naveen Thonpunoori Avatar answered Oct 20 '22 01:10

Naveen Thonpunoori


I'm a bit late to the party but I found this in the PayPal docs

PayPal payments involve these 3 steps:

  • Specify payment information to create a payment.
  • Get payment approval.
  • Execute the payment to the PayPal user's account.

1) Set the intent to sale, and the payment_method to paypal.

Include redirect URLs. The user is redirected to these URLs when they either approve or cancel the payment.

curl https://api.sandbox.paypal.com/v1/payments/payment \   -v \   -H 'Content-Type: application/json' \   -H 'Authorization: Bearer accessToken' \   -d '{     "intent":"sale",     "redirect_urls":{       "return_url":"http://return_URL_here",       "cancel_url":"http://cancel_URL_here"     },     "payer":{       "payment_method":"paypal"     },     "transactions":[       {         "amount":{           "total":"7.47",           "currency":"USD"         },         "description":"This is the payment transaction description."       }     ]   } 

Response:

{   "id":"PAY-6RV70583SB702805EKEYSZ6Y",   "create_time":"2013-03-01T22:34:35Z",   "update_time":"2013-03-01T22:34:36Z",   "state":"created",   "intent":"sale",   "payer":{     "payment_method":"paypal"   },   "transactions":[     {       "amount":{         "total":"7.47",         "currency":"USD",         "details":{           "subtotal":"7.47"         }       },       "description":"This is the payment transaction description."     }   ],   "links":[     {       "href":"https://api.sandbox.paypal.com/v1/payments/payment/PAY-6RV70583SB702805EKEYSZ6Y",       "rel":"self",       "method":"GET"     },     {       "href":"https://www.sandbox.paypal.com/webscr?cmd=_express-checkout&token=EC-60U79048BN7719609",       "rel":"approval_url",       "method":"REDIRECT"     },     {       "href":"https://api.sandbox.paypal.com/v1/payments/payment/PAY-6RV70583SB702805EKEYSZ6Y/execute",       "rel":"execute",       "method":"POST"     }   ] } 

2) Get payment approval

Please note the HATEOAS links in the example above. Direct the user to the approval_url on the PayPal site, so that the user can approve the payment. The user must approve the payment before you can execute and complete the sale.

3) Execute the payment

When the user approves the payment, PayPal redirects the user to the return_url that was specified

when the payment was created. A payer Id and payment Id are appended to the return URL, as PayerID and paymentId:

http://return_url?paymentId=PAY-6RV70583SB702805EKEYSZ6Y&token=EC-60U79048BN7719609&PayerID=7E7MGXCWTTKK2 

The token value appended to the return URL is not needed when you execute the payment.

To execute the payment after the user's approval, make a /payment/execute/ call. In the body of the request, use the payer_id value that was appended to the return URL. In the header, use the access token that you used when you created the payment.

curl https://api.sandbox.paypal.com/v1/payments/payment/PAY-6RV70583SB702805EKEYSZ6Y/execute/ \   -v \   -H 'Content-Type: application/json' \   -H 'Authorization: Bearer accessToken' \   -d '{ "payer_id" : "7E7MGXCWTTKK2" }' 

Note: Once a payment is complete, it is referred to as a sale. You can then look up the sale and refund it.

Hope it helps!

like image 31
Stef Hej Avatar answered Oct 19 '22 23:10

Stef Hej