【问题标题】:Rails - handling the PayPal IPN callback using the paypal-recurring gemRails - 使用 paypal-recurring gem 处理 PayPal IPN 回调
【发布时间】:2012-09-21 12:06:19
【问题描述】:

我正在使用paypal-recurring gem 处理 Rails 应用程序中的定期付款。我的大部分代码来自这个优秀的Railscast,但我还想添加一个 payment_notification 模型来接受 IPN 回调并存储任何相关数据。这个Railscast 介绍了如何设置通知。但是,我很难弄清楚如何将 paypal-recurring gem IPN 回调发送到我的 PaymentNotification 模型。

如何设置 :ipn_url 以正确地将 IPN 回调写入我的 PaymentNotification 模型。到目前为止,我尝试了以下方法:

1)将ipn_url: "http://my-app-name.com/payment_notifications"添加到处理方法(选项下)或payment_notifications_url

2)尝试GitHub issue page底部建议的解决方案

3) 使用 Paypal 的即时付款通知 (IPN) 模拟器发送到“http://my-app-name.com/payment_notifications”,但我收到错误:IPN 交付失败。 HTTP 错误代码 401:未经授权

编辑

我已经能够成功模拟将 IPN 传送到我的 payment_notifications_url。现在我只需要弄清楚如何指向重复出现的 gem 以将 ipn 发送到那里。

任何指针将不胜感激。以下是我当前的一些代码。如果我忘记了任何相关部分,请告诉我。

Paypal 支付模式

 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

【问题讨论】:

标签: ruby-on-rails ruby-on-rails-3 paypal paypal-ipn


【解决方案1】:

我让它工作了。万一将来有人在使用 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 的开发人员沙箱中,单击“测试帐户”,然后单击“进入沙箱测试站点”橙色按钮。在那里,使用您的沙盒卖家凭据登录。进入“我的帐户”和“个人资料”后,在“销售偏好”下点击“即时付款通知偏好”。设置您的通知 url 以匹配您设置的接收 IPN POST 的 url 并将消息传递设置为启用。

【讨论】:

    猜你喜欢
    • 2014-04-18
    • 1970-01-01
    • 2013-07-17
    • 2014-01-08
    • 2011-11-24
    • 1970-01-01
    • 2012-12-13
    • 2017-12-15
    • 2015-08-17
    相关资源
    最近更新 更多