ruby-on-rails - 调用操作后将列更新为 NULL

标签 ruby-on-rails

我的应用程序上已设置 Stripe,用户可以取消或升级其订阅。它与 Stripe 完美沟通。我需要帮助弄清楚如何使更改与我的数据库进行通信。

如果用户取消订阅,则应在订阅表中的已取消列下进行标记。完成此操作后,如果数据库中显示用户的订阅已取消,则用户对网站的访问将受到限制。

我不确定如何将其添加到我设置的取消订阅操作中。

如果您能提供帮助,我们将不胜感激!

订阅 Controller :

  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 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
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
    save
  end


  def reactivate_paypal
    paypal.reactivate
    save
  end
end

最佳答案

最简单的方法是添加额外的行来更新所需的列。

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

但是,理想情况下,所有这些都应该在模型内发生。您应该从 Controller 告诉模型取消订阅。

所以,这个方法可能会变成这样:

#subscriptions_controller.rb
def cancelsubscription
  current_user.subscription.cancel_stripe_subscription
  flash.alert = 'Your subscription has been cancelled successfully!'
  redirect_to root_url
end

#subscription.rb model
def cancel_stripe_subscription
  customer = Stripe::Customer.retrieve(self.stripe_customer_token)
  customer.cancel_subscription()
  self.cancelled = 1
  self.save!
end

关于ruby-on-rails - 调用操作后将列更新为 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22413938/

相关文章:

ruby-on-rails - Rails 4 - 在 jsonb 列中动态添加嵌套 json 数据

ruby-on-rails - "Other"Rails 中单选按钮集合的文本字段

ruby-on-rails - 如何动态添加路由到 Rails3 中的作用域资源?

ruby-on-rails - 如何在 Rails 的开发模式下自动重新加载每个请求的 gem 代码?

ruby-on-rails - Ruby on Rails 显示 Action 在 Controller 中不起作用

ruby-on-rails - rails : Unit testing a before_create?

ruby-on-rails - RVM 找不到系统版本

ruby-on-rails - Rails 在 ajax 调用同一 Controller 的方法时更新实例变量

javascript - 使用 Rails 4,需要帮助制作更好的侧边栏导航菜单

ruby-on-rails - 在Ruby on Rails应用程序中搜索和嵌入youtube视频