我有一个用户注册页面,我将在其中输入个人详细信息以及信用卡信息。用户has_many帐户和帐户belongs_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
subscription.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
account.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
请帮助我是否需要使用强制参数以及如何或任何其他方式?
答案 0 :(得分:0)
同样很高兴看到.*([0-9]{4}).*([0-9]{2}).*([0-9]{2}).*
模型,但根据您的代码,您似乎永远不会保存信用卡信息。方法Account
只是分配值,但从不将它们保存到数据库中。最简单的解决方案是在store_card
方法中调用self.save
。
store_card