![](/img/trans.png)
[英]Get first_name (and other info) from Paypal and create Rails Devise account using paypal-recurring gem
[英]Rails - handling the PayPal IPN callback using the paypal-recurring gem
我正在使用paypal-recurring gem來處理Rails應用程序中的定期付款。 我的大部分代碼來自這個優秀的Railscast,但我還想添加一個payment_notification模型來接受IPN回調並存儲任何相關數據。 這個Railscast討論了如何設置通知。 但是,我很難弄清楚如何將paypal重復出現的寶石IPN回調發送到我的PaymentNotification模型。
如何設置:ipn_url以正確地將IPN回調寫入我的PaymentNotification模型。 到目前為止我嘗試了以下內容:
1)將ipn_url: "http://my-app-name.com/payment_notifications"
添加到流程方法(在選項下)或payment_notifications_url
2)嘗試在此GitHub問題頁面底部建議的解決方案
3)使用Paypal的即時付款通知(IPN)模擬器發送到“http://my-app-name.com/payment_notifications”,但我收到一個錯誤: IPN傳遞失敗。 HTTP錯誤代碼401:未經授權
編輯
我已經能夠成功模擬IPN到我的payments_notifications_url的交付。 現在我只需要弄清楚如何指出定期發送的gem將ipn發送到那里。
任何指針都將非常感激。 以下是我目前的一些代碼。 如果我忘記了任何相關部分,請告訴我。
PaypalPayment模型
class PaypalPayment
def initialize(subscription)
@subscription = subscription
end
def checkout_details
process :checkout_details
end
def checkout_url(options)
process(:checkout, options).checkout_url
end
def make_recurring
process :request_payment
process :create_recurring_profile, period: :monthly, frequency: 1, start_at: Time.zone.now
end
def cancel_recurring
process :cancel
end
private
def process(action, options = {})
options = options.reverse_merge(
token: @subscription.paypal_payment_token,
payer_id: @subscription.paypal_customer_token,
description: @subscription.plan.name,
amount: @subscription.plan.monthly_price,
currency: "JPY"
)
response = PayPal::Recurring.new(options).send(action)
raise response.errors.inspect if response.errors.present?
response
end
end
PaymentNotifications控制器
class PaymentNotificationsController < ApplicationController
protect_from_forgery :except => [:create]
def create
PaymentNotification.create!(:params => params, :status => params[:payment_status], :transaction_id => params[:txn_id])
render :nothing => true
end
end
我搞定了。 如果將來有人遇到PayPal IPN的麻煩,這里有一些我錯了:
1)在我的訂閱控制器中,我調用if @subscription.save
而不是if @subscription.save_with_payment
因此save_with_payment方法實際上從未被調用過。
2)在流程方法中我添加了ipn_url: "https://my-app-name.com/payment_notifications",
def process(action, options = {})
options = options.reverse_merge(
token: @subscription.paypal_payment_token,
payer_id: @subscription.paypal_customer_token,
description: @subscription.plan.name,
amount: @subscription.plan.monthly_price,
ipn_url: "https://my-app-name.com/payment_notifications",
currency: "JPY"
)
response = PayPal::Recurring.new(options).send(action)
raise response.errors.inspect if response.errors.present?
response
end
3)在PayPal的開發人員沙箱中,單擊“測試帳戶”,然后單擊“輸入沙箱測試站點”橙色按鈕。 在那里,使用您的沙盒賣家憑據登錄。 進入“我的帳戶”和“個人資料”后,在“銷售偏好設置”下點擊“即時付款通知首選項”。 設置通知網址以匹配您為接收IPN POST設置的網址,並將郵件傳遞設置為已啟用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.