简体   繁体   中英

How to integrate Paypal with Ruby on Rails

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.

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 (aka 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 (ie facilitator) profile details and copy the API Credentials, ie 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

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!

In depth Step-by step procedure is given here

Integrating Paypal to your Rails application with a basic Checkout method:
Basic Checkout

If you want to accept credit cards for your payments:
Charge Credit Cards

If you want to accept recurring payments:
Recurring Payments

You can clone this app and test in your Local Machine

git clone https://github.com/gotealeaf/paypal-basics
cd paypal-basics
rake db:create
rake db:migrate
rake db:seed
rails s

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM