如何将Paypal与Ruby on Rails集成

21
我正在尝试使用rest-api-sdk-ruby宝石(https://github.com/paypal/rest-api-sdk-ruby)将PayPal整合到我的Ruby on Rails应用程序中,但无法找到足够的相关信息或好的教程来支持我。上面提供的描述虽然提供了必要的代码,但没有展示如何处理每个方法或每个文件中应该放置哪些方法。
请问是否有人能在这里给我一个起点或指向一个好的教程?
我正在使用Rails 4版本。谢谢。
3个回答

28

使用Active Merchant gem在Rails应用中标准集成PayPal

步骤1

  • 在Gemfile中添加gem 'activemerchant'

  • 运行bundle install

步骤2

  • 前往"developer.paypal.com"并创建一个带有美国地址信息的账户(也称为商家账户)。

    这将在"sandbox.paypal.com"中为买家和卖家(也称为方便器)各创建两个测试账户。要查看测试账户详细信息,请单击“仪表板 -> 账户”

  • 现在通过单击配置文件链接为两个测试账户设置密码。

步骤3

  • 进入卖家账户(即方便器)的配置文件详细信息,复制API凭据,例如用户名、密码和签名。

    Username:  naveengoud-facilitator_api1.gamil.com
    Password:  VSPALJ5ALA5YY9YJ
    Signature:AVLslxW5UGzEpaDPEK4Oril7Xo4IAYjdWHD25HhS8a8kqPYO4FjFhd6A
    
    将这些API凭据设置在“config/environments/development.rb”中,如下所示:
  • 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
    

第四步


那个视频是2009年的,无法加载 :( - pguardiario

2
我有点晚了,但我在PayPal文档中找到了这个。
PayPal支付包含以下3个步骤:
1. 指定付款信息以创建付款。 2. 获取付款批准。 3. 执行付款到PayPal用户账户。
1)将意图设置为“销售”,并将payment_method设置为“paypal”。
包括重定向URL。当用户批准或取消付款时,他们将被重定向到这些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."
      }
    ]
  }

回复:

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

请注意以上示例中的HATEOAS链接。将用户直接引导至PayPal网站上的approval_url以便用户可以批准付款。用户必须在您执行和完成销售之前批准付款。

3)执行付款

当用户批准付款后,PayPal会将用户重定向至创建付款时指定的return_url。支付人ID和支付ID将作为PayerIDpaymentId附加到返回URL中:

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

在执行付款时,不需要将令牌值附加到返回的URL中。

在用户批准后执行付款,请调用/payment/execute/接口。在请求正文中,使用附加到返回URL中的payer_id值。在标题中,使用创建付款时使用的访问令牌。

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" }'

注意:支付完成后,它被称为销售。然后,您可以查找销售并退款。
希望这有所帮助!

1

这里提供了深入的逐步过程:

使用基本结账方法将Paypal集成到您的Rails应用程序中:
基本结账

如果您想接受信用卡付款:
收费信用卡

如果您想接受循环付款:
循环付款

您可以克隆此应用并在本地机器上进行测试

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

请提供一些上下文和解释,而不是仅仅提供链接作为答案! - Robert Moskal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接