简体   繁体   English

Rails - 使用paypal-recurring gem处理PayPal IPN回调

[英]Rails - handling the PayPal IPN callback using the paypal-recurring gem

I am using the paypal-recurring gem to handle recurring payments in a Rails app. 我正在使用paypal-recurring gem来处理Rails应用程序中的定期付款。 The majority of my code is from this excellent Railscast but I also want to add a payment_notification model to accept the IPN callback and store any relevant data. 我的大部分代码来自这个优秀的Railscast,但我还想添加一个payment_notification模型来接受IPN回调并存储任何相关数据。 This Railscast goes over how to set up notifications. 这个Railscast讨论了如何设置通知。 However, I am having a hard time figuring out how to send the paypal-recurring gem IPN callback to my PaymentNotification model. 但是,我很难弄清楚如何将paypal重复出现的宝石IPN回调发送到我的PaymentNotification模型。

How can I set the :ipn_url to correctly write the IPN callback to my PaymentNotification model. 如何设置:ipn_url以正确地将IPN回调写入我的PaymentNotification模型。 I tried the following so far: 到目前为止我尝试了以下内容:

1) Adding ipn_url: "http://my-app-name.com/payment_notifications" to the process method (under options) or payment_notifications_url 1)将ipn_url: "http://my-app-name.com/payment_notifications"添加到流程方法(在选项下)或payment_notifications_url

2) Trying the solution suggested at the bottom of this GitHub issue page 2)尝试在此GitHub问题页面底部建议的解决方案

3) Using Paypal's Instant Payment Notification (IPN) simulator to send to "http://my-app-name.com/payment_notifications", but I get an error: IPN delivery failed. 3)使用Paypal的即时付款通知(IPN)模拟器发送到“http://my-app-name.com/payment_notifications”,但我收到一个错误: IPN传递失败。 HTTP error code 401: Unauthorized HTTP错误代码401:未经授权

EDIT 编辑

I have been able to successfully simulate the delivery of the IPN to my payments_notifications_url. 我已经能够成功模拟IPN到我的payments_notifications_url的交付。 Now I just need to figure out how to point the recurring gem to send the ipn to there. 现在我只需要弄清楚如何指出定期发送的gem将ipn发送到那里。

Any pointers would be greatly appreciated. 任何指针都将非常感激。 Below is some of my current code. 以下是我目前的一些代码。 If I am forgetting any relevant parts, please let me know. 如果我忘记了任何相关部分,请告诉我。

PaypalPayment Model 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 Controller 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

I got it working. 我搞定了。 In case anyone in the future runs into trouble with PayPal IPN, here are a few things I had wrong: 如果将来有人遇到PayPal IPN的麻烦,这里有一些我错了:

1) In my Subscriptions Controller I was calling if @subscription.save instead of if @subscription.save_with_payment so the save_with_payment method was actually never even being called. 1)在我的订阅控制器中,我调用if @subscription.save而不是if @subscription.save_with_payment因此save_with_payment方法实际上从未被调用过。

2) In the process method I added ipn_url: "https://my-app-name.com/payment_notifications", 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) In PayPal's developer sandbox, click on 'Test Accounts' and then click on the 'Enter Sandbox Test Site' orange button. 3)在PayPal的开发人员沙箱中,单击“测试帐户”,然后单击“输入沙箱测试站点”橙色按钮。 Once there, log in with your sandbox seller credentials. 在那里,使用您的沙盒卖家凭据登录。 Once inside go to 'My Account' and 'Profile' and under 'Selling Preferences' click on 'Instant Payment Notification Preferences'. 进入“我的帐户”和“个人资料”后,在“销售偏好设置”下点击“即时付款通知首选项”。 Set your notification url to match the url you set up to receive the IPN POST and set message delivery to enabled. 设置通知网址以匹配您为接收IPN POST设置的网址,并将邮件传递设置为已启用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM