[英]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。
非常感謝。
在Gemfile中添加gem'activemerchant gem 'activemerchant'
運行bundle install
轉到“ developer.paypal.com ”並創建一個包含美國地址詳細信息的帳戶(也稱為商家帳戶)。
它將在“sandbox.paypal.com”中創建兩個虛擬測試帳戶,分別為買方和賣方(也稱為協調人)。 查看測試帳戶詳細信息單擊“儀表板 - >帳戶”
現在,通過單擊配置文件鏈接為兩個測試帳戶設置密碼。
轉到賣家帳戶(即協調人)個人資料詳細信息並復制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
我參加派對有點晚了,但我在PayPal文檔中發現了這一點
PayPal付款涉及以下3個步驟:
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會附加到返回網址,如PayerID
和paymentId
:
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" }'
注意:付款完成后,稱為銷售。 然后,您可以查找銷售並退款。
希望能幫助到你!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.