繁体   English   中英

如何将Paypal与Ruby on Rails集成

[英]How to integrate Paypal with Ruby on Rails

我试图使用rest-api-sdk-ruby gem( https://github.com/paypal/rest-api-sdk-ruby )将paypal与我的ruby集成在rails应用程序上,但无法找到足够的信息或者支持我的好教程。 上面提供的描述虽然提供了必要的代码,但没有说明如何处理每种方法应该使用的方法或文件。

谁能给我一个起点或指向一个好的教程?

我正在使用rails版本4。

非常感谢。

标准PayPal与Rails应用程序Active Merchant gem集成

步骤1

  • 在Gemfile中添加gem'activemerchant gem 'activemerchant'

  • 运行bundle install

第2步

  • 转到“ developer.paypal.com ”并创建一个包含美国地址详细信息的帐户(也称为商家帐户)。

    它将在“sandbox.paypal.com”中创建两个虚拟测试帐户,分别为买方和卖方(也称为协调人)。 查看测试帐户详细信息单击“仪表板 - >帐户”

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

第3步

  • 转到卖家帐户(即协调人)个人资料详细信息并复制API凭据,即用户名,密码和签名。 例如:

     Username:  naveengoud-facilitator_api1.gamil.com Password:  VSPALJ5ALA5YY9YJ Signature: AVLslxW5UGzEpaDPEK4Oril7Xo4IAYjdWHD25HhS8a8kqPYO4FjFhd6A 
  • 在“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 

第4步

我参加派对有点晚了,但我在PayPal文档中发现了这一点

PayPal付款涉及以下3个步骤:

  • 指定付款信息以创建付款。
  • 获得付款批准。
  • 执行PayPal用户帐户的付款。

1)将意图设置为sale ,将payment_method设置为paypal

包含重定向网址。 用户在批准或取消付款时会被重定向到这些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

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

注意:付款完成后,称为销售。 然后,您可以查找销售并退款。

希望能帮助到你!

深入分步过程在这里给出

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

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

如果您想接受定期付款:
经常性付款

您可以克隆此应用并在本地计算机中进行测试

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