Paypal Recurring Gem - 暂停付款

2024-01-20

我正在寻找为贝宝经常性宝石(跟随 Rails Cast)设置付款暂停。我不确定是否需要设置 IPN,因为 gem 的文档中没有提及它。我目前拥有的代码不执行任何操作。

我在模型中定义了取消重复,但我不确定如何完成代码,因为我很难理解这一切是如何工作的。这个问题已经被其他人问过,但没有答案。

如果有人有时间来帮助我,那就太好了!

问题是如何暂停/取消用户的定期付款。

Paypal_ payment.rb:

   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 suspend
        process :suspend, :profile_id => @subscription.paypal_recurring_profile_token
    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.price,
        currency: "USD"
      )
      response = PayPal::Recurring.new(options).send(action)
      raise response.errors.inspect if response.errors.present?
      response
    end
  end

订阅控制器:

  def new
    plan = Plan.find(params[:plan_id])
    @subscription = plan.subscriptions.build
    if params[:PayerID]
      @subscription.paypal_customer_token = params[:PayerID]
      @subscription.paypal_payment_token = params[:token]
      @subscription.email = @subscription.paypal.checkout_details.email
    end
  end

  def create
    @subscription = Subscription.new(params[:subscription])
    if @subscription.save_with_payment
      redirect_to @subscription, :notice => "Thank you for subscribing!"
    else
      render :new
    end
  end

  def show
    @subscription = Subscription.find(params[:id])
  end

  def paypal_checkout
    plan = Plan.find(params[:plan_id])
    subscription = plan.subscriptions.build
    redirect_to subscription.paypal.checkout_url(
      return_url: new_subscription_url(:plan_id => plan.id),
      cancel_url: root_url
    )
  end

    def updatesubscription
      @user = current_user
      @customer = Stripe::Customer.retrieve(@user.subscription.stripe_customer_token)
      @customer.update_subscription(:plan => "1", :prorate => true)
     current_user.save!
      flash.alert = 'Your subscription has been updated!'
      redirect_to root_url
     end

     def cancelsubscription
       @user = current_user
         @customer = Stripe::Customer.retrieve(@user.subscription.stripe_customer_token)
         @customer.cancel_subscription()
         current_user.save!
         flash.alert = 'Your subscription has been cancelled successfully!'
         redirect_to root_url
       end

       def showcard
         @user = current_user
         Stripe::Customer.retrieve(@user.subscription.stripe_customer_token).cards.all()
       end

       def changecard
           @user = current_user       
           @customer = Stripe::Customer.retrieve(@user.subscription.stripe_customer_token)

             card = @customer.cards.create({
               :card => @user.subscription.stripe_customer_token
             })

             @customer.default_card = card
             @customer.save
           end

           def suspend
             @user = current_user
             @user.subscription.suspend_paypal
           end


         def updatebilling
             @user = current_user
             customer = Stripe::Customer.retrieve(@user.subscription.stripe_customer_token)
             customer.cards.retrieve("#{@user.subscription.stripe_card_id}").delete()
             customer.cards.create({
                   card: {
                   number: params[:user][:scardnumber],
                   exp_month: params[:user][:sexp_month],
                   exp_year: params[:user][:sexp_year],
                   cvc: params[:user][:scvc],
                   name: params[:user][:sname],
                   address_line1: params[:user][:sbilling_address1],
                   address_line2: params[:user][:sbilling_address2],
                   address_city: params[:user][:saddress_city],
                   address_zip: params[:user][:saddress_zip],
                   address_state: params[:user][:saddress_state],
                   address_country: params[:user][:saddress_country]
                   }
                 })
                 if customer.save!
                   @user.stripe_card_id = customer.active_card.id
                   @user.save!
                   flash.alert = 'Billing information updated successfully!'
                   redirect_to root_url
                 else
                   flash.alert = 'Stripe error'
                   redirect_to root_url
                 end
               end
end

订阅模式:

  belongs_to :plan
  belongs_to :subscription
  belongs_to :user

  validates_presence_of :plan_id
  validates_presence_of :email

  attr_accessor :stripe_card_token, :paypal_payment_token

  def save_with_payment
    if valid?
      if paypal_payment_token.present?
        save_with_paypal_payment
      else
        save_with_stripe_payment
      end
    end
  end

  def paypal
    PaypalPayment.new(self)
  end

  def save_with_paypal_payment
    response = paypal.make_recurring
    self.paypal_recurring_profile_token = response.profile_id
    save!
  end

  def save_with_stripe_payment
    customer = Stripe::Customer.create(description: email, plan: plan_id, card: stripe_card_token)
    self.stripe_customer_token = customer.id
    save!
  rescue Stripe::InvalidRequestError => e
    logger.error "Stripe error while creating customer: #{e.message}"
    errors.add :base, "There was a problem with your credit card."
    false
  end

  def payment_provided?
    stripe_card_token.present? || paypal_payment_token.present?
  end

  def suspend_paypal
    paypal.suspend
    self.status = "canceled"
    save
  end
end

Routes:

  get "subscriptions/cancelsubscription"
  get "subscriptions/updatesubscription"
  get "subscriptions/changecard"
  get "subscriptions/suspend"
  get "subscriptions/updatebilling"

  resources :charges
  resources :subscriptions
  resources :plans
  get 'paypal/checkout', to: 'subscriptions#paypal_checkout'

View:

<%= link_to "Suspend paypal", subscriptions_suspend_path, :data => { :confirm => "Are you sure?" } %>

这个 PaypalPayment 是 paypal-recurring gem 的一种包装。因此,此类中的所有方法都只是准备并委托给 PayPal::Recurring,这就是为什么所有方法都只调用实例化并传递操作的“process”方法。

因此,对于暂停/取消,您只需为每个操作添加一个方法。正如文件所述,您需要执行此操作才能取消

ppr = PayPal::Recurring.new(:profile_id => "I-VCEL6TRG35CU")
ppr.suspend

因此,对于您的 PaypalPayment 类,它看起来像这样:

def suspend
    process :suspend, :profile_id => @subscription.paypal_recurring_profile_token
end

所以你的模型subscription.rb

def suspend_paypal
  paypal.suspend
  self.status = "canceled"
  save
end

和控制器 susbcription_controller.rb

def suspend
  current_user.suspend_paypal
end

关于 IPN,如果用户通过您的网站暂停,我认为没有必要,但由于用户可能直接通过 paypal 取消它,您必须处理这种情况,以便用户不会停止付款,而是保持有效订阅。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Paypal Recurring Gem - 暂停付款 的相关文章

  • 我应该使用哪个 PayPal API 向关联公司发送付款

    我在一家网站公司工作 该公司向向我们网站发送流量的合作伙伴支付佣金 目前 我们跟踪从附属机构引向我们网站的流量 然后通过 PayPal 进行繁琐的手动付款过程 这是我们当前流程的要点 1 Review affiliate commissio
  • Rails 中的 PDF 导出

    我需要将包含一些图表的 HTML 页面导出为 PDF 有哪些好的 gem 可以做到这一点 PDFKit http railscasts com episodes 220 pdfkit http railscasts com episodes
  • 如何在 Rails 3.2.1 版本中注释 Rails 模型

    我正在尝试遵循一些在线教程来在 Rails 中注释我的模型 然而 似乎所有教程都在谈论过时的注释版本或不正确的安装 这真是一团糟 到目前为止我已经尝试过以下方法 1 在 Gemfile 中添加此内容 gem annotate 2 4 0 2
  • Rails 注释分段错误

    有一些问题围绕着这个问题 但没有什么真正能满足我的需求 After I bundle install下面列出了我的 Gemfile 我运行annotate并出现以下错误 Users nickcoelius rvm gems ruby 1 8
  • paymentId 和 TRANSACTIONID 之间的区别

    我正在从 REST 转向经典 API 而且我对两者都是新手 作为一名开发人员 我想记录付款的唯一标识符 以便将网站中的销售与 Paypal 付款 ID 相关联 例如我想要退款时 REST API 曾经给我付款 ID https stacko
  • 在rails中,如何将记录作为csv文件返回

    我有一个名为 Entries 的简单数据库表 class CreateEntries lt ActiveRecord Migration def self up create table entries do t t string firs
  • 将 Rails 变量传递给液体可以在控制台中工作,但不在视图中

    我想将哈希传递给渲染方法 当我这样做时 在我的控制台中一切正常 object Object find params id hash object object to liquid template Liquid Template parse
  • 在 ec2 上托管 Rails

    我想将 Rails 部署到亚马逊 ec2 上 我看过 poolparty 和 ec2onrails 但似乎都不再维护了 人们用什么来做到这一点 都是自制的木偶和卡皮斯特拉诺 还是有一个项目可以让我继续下去 我可以推荐两个项目 如果您有一个
  • Rubymine 6 更新/刷新 Rails 项目中可用的方法和路径?

    这是一个例子 假设我更新路线resources foo这给我带来了一些新的道路 例如 new foo session当我开始在 ERB 视图中输入路径时 我希望它向我显示 建议自动完成与路径匹配的名称 当我输入 new foo 我会得到所有
  • Rails 4:资产未在生产中加载

    我正在尝试将我的应用程序投入生产 但图像和 CSS 资源路径不起作用 这是我目前正在做的事情 图像资源位于 app assets images image jpg 样式表位于 app assets stylesheets style css
  • 多重要求和允许强参数rails 4

    在下面的情况下 我尝试使用强参数 我想要求email address password并允许remember me fields 但像下面这样使用它只允许最后一行在方法示例中 在下面的情况下 它只需要params permit rememb
  • Ruby on Rails:如何使用 TCP 套接字连接 GPS 设备

    ruby 2 3 0p0 2015 12 25 修订版 53290 x86 64 linux 轨道 4 2 4 我正在使用 cloud9 IDE 和 webrick 服务器 我的项目是实时跟踪GPS 我想使用TCP连接与GPS跟踪设备进行通
  • 浏览器关闭时 Omniauth 会话过期

    在我的 Rails 3 应用程序中 我使用 Omniauth 进行用户身份验证部分 fb twitter 实际上我遵循这个 https github com RailsApps rails3 mongoid omniauth https g
  • Rails 4 应用程序...在开发环境中,除非刷新页面,否则 javascript 不会触发

    所以我的第一个rails4应用程序遇到了一个奇怪的问题 除非我重新加载页面 否则我的页面javascript不会触发 对于我的 asset pipeline JS 和 content for JS 来说都是如此 在我的 assets jav
  • 用户未定义的方法 attr_accessible 错误

    我正在尝试创建某种登录 我创建了一个用户脚手架并将此代码放在我的 user rb 中 class User lt ActiveRecord Base attr accessible name password digest password
  • ruby从1.8.7升级到1.9.2(使用Rails 3.1.1)后本地服务器错误

    我刚刚安装了rvm并使用rvm将ruby从1 8 7升级到1 9 2 我在我的应用程序上运行了捆绑安装 它重新安装了我的 gems 当我在本地运行 Rails 服务器并将浏览器导航到 localhost 3000 时 服务器日志中显示以下错
  • 如何加载页面特定的rails 4 js文件?

    我正在阅读资产管道的 Rails 指南文档 它指出 CoffeeScript 页面特定生成的文件 如果清单上有 require tree 指令 则默认情况下可供用户使用 这对我不起作用我必须包括这个 在特定控制器上 我缺少什么 资产管道会将
  • Rails Windows Vagrant 响应时间非常慢

    我在跑 Vagrant 1 7 1 Rails 4 1 4 Thin 1 6 1 Windows 7 每个静态文件的发送时间都超过一秒 在我的 PC 上加载一个页面可能需要大约 20 秒 而在同事的 Linux 机器上则只需瞬间 有一些帖子
  • 资产管道:仅对一个控制器使用 javascript 文件

    在 Ruby on Rails v4 中 我希望仅为特定控制器加载一个 js 文件 或一组 js 文件 执行此操作的标准方法是什么 在 application js 中有 require tree 线 我假设这需要删除 所以我并不总是加载每
  • RoR - Rails 中的大文件上传

    我有一个 Rails Web 应用程序 允许用户上传视频 视频存储在 NFS 安装的目录中 当前的设置适用于较小的文件 但我也需要支持大文件上传 最多 4GB 当我尝试上传 4GB 文件时 它最终会发生 但从用户体验的角度来看很糟糕 上传开

随机推荐

  • 给 Java 桌面应用程序换肤?

    有谁知道一种 皮肤 Java 桌面应用程序的方法吗 例如 Winamp 皮肤或 MirandaIM 皮肤 我不想创造自己的外观和感觉 是的 有几种方法可以做到这一点 但我不知道有谁是真正简单的 看一下Nimbus Swing 的新面貌 ht
  • Math.pow(65,17) % 3233 的令人惊讶的结果

    由于某种原因 在处理大数时 模运算符没有给出正确的输出 请查看代码 double x Math pow 65 17 3233 输出应该是2790但输出是887 0 我确信这很愚蠢 但我无法绕过它 提前致谢 的结果Math pow 65 17
  • 部分代码覆盖率 C# - Nunit

    我有部分代码覆盖率 但我不知道为什么 对于在开始阅读之前喜欢这个问题的人 首先想说 第一篇文章 我的开发生涯还很初级 但我的学习速度相对较快 我认为 所以就这样吧 使用Junit进行测试 并基于MVP 待测试代码 void view Del
  • Express.js:如何获取远程客户端地址

    我不完全明白我应该如何获取远程用户IP地址 假设我有一个简单的请求路线 例如 app get function req res var forwardedIpsStr req header x forwarded for var IP if
  • 对象标签在 Chrome v41 之前显示 PDF,但在 v42(最新版本)中显示空白

    我有一个处于生产模式的应用程序 到目前为止运行得很好 我用过标签通过向对象标签提供base64编码数据来显示PDF 即使我没有 Adob e reader 插件 Chrome 也能正确显示它 因为它有内部 pdf 插件 但今天 我已将 Ch
  • ViewModel 订阅特定属性的 Model 的 PropertyChanged 事件

    我希望在模型中的属性更改时执行 methodToBeCalledWhenPropertyIsSet 我怎么能这样做呢 如果我理解正确的话 我可以添加MyModel PropertyChanged methodToBeCalledWhenPr
  • 从 Linux 为 Windows 交叉编译静态库

    我想在linux下为windows编译静态库 以下是我编译的过程 在linux中使用编译静态库的源代码i586 mingw32msvc cc c static lib c o static lib o 在linux中创建静态库ar rv s
  • UITextField 上的强密码叠加

    我面临着奇怪的覆盖UITextField 我正在使用类型字段textContentType password and isSecureTextEntry true 我还有眼睛按钮 可以通过更改来取消隐藏密码字符isSecureTextEnt
  • Kendo-Grid 列字段验证

    我正在努力使用 API 数据填充 kendo grid 但在一个字段上添加验证也会自动适用于所有其他字段 这是 kendo dataSource 内部的架构 schema model id id fields id editable fal
  • Android 上的 ORMLite 不调用 onCreate

    使用 ORMLite v 4 40 我尝试让我的应用程序运行 但它似乎忽略了 onCreate 函数 我的 DatabaseHelper 看起来像这样 片段样式 public class ORMLiteHelper extends OrmL
  • 动态创建函数/子程序的 AutoHotkey 热键

    自动热键命令Hotkey允许在运行时创建动态热键 但其语法和文档似乎将其限制为内置或现有标签 子例程 这使得much不太有用 热键 键名 标签 选项 有没有办法让它像常规的硬编码热键一样工作 例如 z MsgBox foobar Typic
  • SQL:显示标准差内的平均值和最小值/最大值

    我有以下 SQL 表 Date StoreNo Sales 23 4 34 4323 00 23 4 23 564 00 24 4 34 2345 00 etc 我正在运行一个查询 返回特定时间段内的平均销售额 最大销售额和最小销售额 se
  • Intellij IDEA:找不到“JdbcTemplate”类型的bean

    Intellij IDEA 15 0 3 表示 JdbcTemplate bean 不存在 这是一个 Spring Boot 项目 因此所有需要的 bean 都应该在 boot 内部创建 它工作正常 我可以与我的数据库交互 但 IDEA 将
  • 用于运行 DiskPart 的批处理文件

    我正在尝试开发一个批处理文件来运行并删除 Windows 7 中的隐藏分区 当尝试删除所有分区时 通常我在命令提示符窗口中逐行执行此操作 但试图找出如何创建批处理文件来运行并加快此过程 以下是我在命令提示符下键入的行 disk part R
  • 终结器在其对象仍在使用时启动

    Summary C NET 应该被垃圾收集 C 有一个析构函数 用于清理资源 当对象 A 在我尝试克隆其变量成员之一的同一行被垃圾回收时会发生什么 显然 在多处理器上 有时垃圾收集器会获胜 问题 今天 在 C 培训课程中 老师向我们展示了一
  • 程序化屏幕截图歪曲了最近邻居

    我遇到了屏幕截图 以编程方式使用下面的代码 与图像放大 在我的情况下非常远 并渲染最近邻以保留硬边缘时屏幕上实际显示的内容之间的不一致 我从这些论坛获得了以下屏幕截图代码 但保存的是图像的 双线性 渲染而不是最近邻 UIGraphicsBe
  • 如何在postgresql中将bigint(以毫秒为单位的时间戳)值写入为时间戳

    我试图将我的值存储在带有时区字段的时间戳中 从 1970 年开始以毫秒为单位 select TO CHAR TO TIMESTAMP 1401432881230 DD MM YYYY HH24 MI SS MS 预期的30 5 2014 1
  • 实体框架 - 只有 edmx,没有模板

    我们只想添加 EDMX 文件 而不添加上下文和实体的相应 tt 文件 造成这种情况的原因有多种 但长话短说 我们希望能够拥有 EDMX 并能够在需要时从数据库刷新它 目前 如果我每次 从数据库更新 时删除 tt 文件 它将重新生成我们不需要
  • C# 中的互斥量和信号量是什么?我们需要在哪里实施? [关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 C 中的互斥量和信号量是什么 我们需要在哪里实施 我们如何在多线程中使用它们 您应该从 MSDN 开始 系统 线程 互斥体 http
  • Paypal Recurring Gem - 暂停付款

    我正在寻找为贝宝经常性宝石 跟随 Rails Cast 设置付款暂停 我不确定是否需要设置 IPN 因为 gem 的文档中没有提及它 我目前拥有的代码不执行任何操作 我在模型中定义了取消重复 但我不确定如何完成代码 因为我很难理解这一切是如