加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Rails – 使用paypal-recurring gem处理PayPal

发布时间:2020-12-16 19:05:15 所属栏目:百科 来源:网络整理
导读:我使用 paypal-recurring gem来处理Rails应用程序中的定期付款.我的大多数代码来自这个优秀的 Railscast,但我还想添加一个payment_notification模型来接受IPN回调并存储任何相关数据.这个 Railscast讨论了如何设置通知.但是,我很难搞清楚如何将paypal-recurr
我使用 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到我的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)在我的订阅控制器中,我调用@ subscription.save而不是if @ subscription.save_with_payment,因此save_with_payment方法实际上从未被调用过.

2)在流程方法中我添加了ipn_url:“https://my-app-name.com/payment_notifications”,

def process(action,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而设置的网址,并将邮件传递设置为已启用.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读