1

我有一个用户注册页面,我将在其中输入个人详细信息以及信用卡信息。用户 has_many 个帐户和帐户属于_to 用户。创建用户时,会创建帐户并将信用卡信息和地址存储在订阅表中。创建帐户后,它将转到 subscription.rb 模型中的 store_card 方法。当我在 Rails 3.2.13 中运行应用程序并将其迁移到 Rails 4.2.6 时,它工作正常,它没有存储订阅详细信息,也没有显示错误。我将 activemerchant gem 升级到 1.47.0,并将以下控制器中 load_billing 方法中的代码从

@creditcard = ActiveMerchant::Billing::CreditCard.new(params[:account].blank? ? nil : params[:account][:creditcard])

@creditcard = ActiveMerchant::Billing::CreditCard.new(params[:account].blank? ? {} : params[:account][:creditcard])

并以同样的方式@address。

没有特定的关系,只是在 account.rb 中添加了这一行:

has_subscription :user_limit => Proc.new {|a| a.users.count }

当我尝试从控制台运行时:

u = Account.find(1)
u.subscription 

它正在生成订阅列。

class AccountsController < ApplicationController

  before_filter :build_account, :only => [:new, :create]
  before_filter :build_user, :only => [:new, :create]
  before_filter :load_billing, :only => [:new, :create, :billing]
  def create
    @address.first_name = @creditcard.first_name
    @address.last_name = @creditcard.last_name
    @account.address = @address
    @account.creditcard = @creditcard
    if @account.new_record?
      if @account.save
        flash[:notice] = 'Account was created.'
        bypass_sign_in(@user)
        redirect_to session[:previous_url] || user_reports_path(@user)
      else
        render :action => 'new'
      end
    else
      @user.account_id = @account.id
      if @user.save
        flash[:notice] = 'User was created.'
        bypass_sign_in(@user)
        redirect_to session[:previous_url] || user_reports_path(@user)
      else
        render :action => 'new'
      end
    end
  end

  def billing
    @user = current_user
    @account = Account.find(params[:id])
    if request.put?
      @address.first_name = @creditcard.first_name
      @address.last_name = @creditcard.last_name
      puts @address.first_name
      if @creditcard.valid? & @address.valid?
       if @subscription.store_card(@creditcard, :billing_address => @address.to_activemerchant, :ip => request.remote_ip)
         flash[:notice] = "Your billing information has been updated."
         redirect_to settings_path(@user)
       end
      end
    end
  end
  protected

  def resource
    @account ||= current_account
  end

  def build_account
    @account = params[:account_name].blank? ? Account.new : Account.find_by_name(params[:account_name])
  end

  def build_user
    @account.user = @user = User.new(params[:account].blank? ? nil : params[:account][:user])
  end

  def load_billing
    @creditcard = ActiveMerchant::Billing::CreditCard.new(params[:account].blank? ? {} : params[:account][:creditcard])
    @address = SubscriptionAddress.new(params[:account].blank? ? {} : params[:account][:address])
  end

end

订阅.rb:

class Subscription < ActiveRecord::Base

  attr_accessor :creditcard, :address
  def store_card(creditcard, gw_options = {})
    # Clear out payment info if switching to CC from PayPal
    destroy_gateway_record(paypal) if paypal?

    @response = if billing_id.blank?
      gateway.store(creditcard, gw_options)
    else
      gateway.update(billing_id, creditcard, gw_options)
    end

    if @response.success?
      if active_card = @response.params['active_card']
        # Stripe token-based response
        self.card_number = "XXXX-XXXX-XXXX-#{active_card['last4']}"
        self.card_expiration = "%02d-%d" % [active_card['exp_month'], active_card['exp_year']]
      else
        self.card_number = creditcard.display_number
        self.card_expiration = "%02d-%d" % [creditcard.expiry_date.month, creditcard.expiry_date.year]
      end
      set_billing
    else
      errors.add(:base, @response.message)
      false
    end
  end

  def card_storage
      self.store_card(@creditcard, :billing_address => @address.to_activemerchant) if @creditcard && @address && card_number.blank?
    end

  def set_billing
      self.billing_id = @response.token
    end

end

帐户.rb:

class Account < ActiveRecord::Base

validate :valid_subscription?, :on => :create

  def valid_subscription?
    puts "valid_subscription**************************"
    return if errors.any? # Don't bother with a subscription if there are errors already
    self.build_subscription(:plan => @plan, :next_renewal_at => @plan_start, :creditcard => @creditcard, :address => @address)
    if !subscription.valid?
      errors.add(:base, "Error with payment: #{subscription.errors.full_messages.to_sentence}")
      return false
    end
  end
end

请帮助我是否需要使用强参数以及如何或任何其他方式?

4

1 回答 1

0

看看Account模型也会很好,但是根据您的代码,您似乎永远不会保存信用卡信息。方法store_card只是分配值,但从不将它们保存到数据库中。最简单的解决方案就是调用self.save你的store_card方法。

 if active_card = @response.params['active_card']
    # Stripe token-based response
    self.card_number = "XXXX-XXXX-XXXX-#{active_card['last4']}"
    self.card_expiration = "%02d-%d" % [active_card['exp_month'], active_card['exp_year']]
  else
    self.card_number = creditcard.display_number
    self.card_expiration = "%02d-%d" % [creditcard.expiry_date.month, creditcard.expiry_date.year]
  end
  self.save
于 2016-07-10T22:10:48.320 回答