dia*_*ks2 7 paypal ruby-on-rails paypal-ipn ruby-on-rails-3
我正在使用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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
我搞定了.如果将来有人遇到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
Run Code Online (Sandbox Code Playgroud)
3)在PayPal的开发人员沙箱中,单击"测试帐户",然后单击"输入沙箱测试站点"橙色按钮.在那里,使用您的沙盒卖家凭据登录.进入"我的帐户"和"个人资料"后,在"销售偏好设置"下点击"即时付款通知首选项".设置通知网址以匹配您为接收IPN POST设置的网址,并将邮件传递设置为已启用.
| 归档时间: |
|
| 查看次数: |
2598 次 |
| 最近记录: |