简体   繁体   English

如何将Paypal与Ruby on Rails集成

[英]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. 我试图使用rest-api-sdk-ruby gem( https://github.com/paypal/rest-api-sdk-ruby )将paypal与我的ruby集成在rails应用程序上,但无法找到足够的信息或者支持我的好教程。 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. 我正在使用rails版本4。

Many thanks. 非常感谢。

Standard PayPal Integration with Rails app Active Merchant gem 标准PayPal与Rails应用程序Active Merchant gem集成

Step 1 步骤1

  • Add gem 'activemerchant' in your Gemfile 在Gemfile中添加gem'activemerchant gem 'activemerchant'

  • Run bundle install 运行bundle install

Step 2 第2步

  • Go to " developer.paypal.com " and create an account (also known as Merchant Account) with US address details. 转到“ developer.paypal.com ”并创建一个包含美国地址详细信息的帐户(也称为商家帐户)。

    It will create two dummy test accounts, one each for the buyer and the seller (aka facilitator), in "sandbox.paypal.com". 它将在“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 第3步

  • Go to seller account (ie facilitator) profile details and copy the API Credentials, ie username, password and signature. 转到卖家帐户(即协调人)个人资料详细信息并复制API凭据,即用户名,密码和签名。 For example: 例如:

     Username:  naveengoud-facilitator_api1.gamil.com Password:  VSPALJ5ALA5YY9YJ Signature: AVLslxW5UGzEpaDPEK4Oril7Xo4IAYjdWHD25HhS8a8kqPYO4FjFhd6A 
  • Set these API Credentials in "config/environments/development.rb" as follows: 在“config / environments / development.rb”中设置这些API凭据,如下所示:

     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 第4步

I'm a bit late to the party but I found this in the PayPal docs 我参加派对有点晚了,但我在PayPal文档中发现了这一点

PayPal payments involve these 3 steps: PayPal付款涉及以下3个步骤:

  • Specify payment information to create a payment. 指定付款信息以创建付款。
  • Get payment approval. 获得付款批准。
  • Execute the payment to the PayPal user's account. 执行PayPal用户帐户的付款。

1) Set the intent to sale , and the payment_method to paypal . 1)将意图设置为sale ,将payment_method设置为paypal

Include redirect URLs. 包含重定向网址。 The user is redirected to these URLs when they either approve or cancel the payment. 用户在批准或取消付款时会被重定向到这些URL。

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 2)获得付款批准

Please note the HATEOAS links in the example above. 请注意上面示例中的HATEOAS链接。 Direct the user to the approval_url on the PayPal site, so that the user can approve the payment. 将用户引导至PayPal网站上的approval_url ,以便用户可以批准付款。 The user must approve the payment before you can execute and complete the sale. 用户必须先批准付款才能执行和完成销售。

3) Execute the payment 3)执行付款

When the user approves the payment, PayPal redirects the user to the return_url that was specified 当用户批准付款时,PayPal会将用户重定向到指定的return_url

when the payment was created. 付款创建时。 A payer Id and payment Id are appended to the return URL, as PayerID and paymentId : 付款人ID和付款ID会附加到返回网址,如PayerIDpaymentId

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. 执行付款时,不需要附加到返回URL的令牌值。

To execute the payment after the user's approval, make a /payment/execute/ call. 要在用户批准后执行付款,请进行/payment/execute/致电。 In the body of the request, use the payer_id value that was appended to the return URL. 在请求正文中,使用附加到返回URL的payer_id值。 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: 使用基本的Checkout方法将Paypal集成到您的Rails应用程序:
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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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