2016-07-10 4 views
1

私はクレジットカード情報とともに個人情報を入力するユーザサインアップページを持っています。ユーザーhas_manyアカウントとアカウントbelongs_toユーザー。ユーザーが作成されると、アカウントが作成され、クレジットカード情報と住所がサブスクリプションテーブルに格納されます。アカウント作成後は、subscription.rbモデルのstore_cardメソッドを使用します。 Rails 3.2.13でアプリケーションを実行するとうまくいきました.Rails 4.2.6に移行すると、サブスクリプション の詳細が保存されず、エラーも表示されません。 Iは、1.47.0にactivemerchant宝石をアップグレードしバージョン変更後のカードの値をRails 4.2に保存できませんでした。

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

および@addressすぎ同様に

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

から以下コントローラ にload_billingメソッドのコードを変更しました。

そこには特定の関係ではありませんが、ちょうどこのラインは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 

私は強いのparamsを使用する必要があるかどうかを助けてくださいとどのようにまたは任意の他の方法はありますか?

答えて

0

Accountモデルも見ていただければ幸いですが、コードに基づいてクレジットカード情報を保存しないようです。メソッドstore_cardは単に値を割り当てますが、決してデータベースに保存しません。最も簡単な解決策は、store_cardメソッドでself.saveと呼ぶだけです。

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 
+0

@rk x:私の古いバージョンでは、このモデルとメソッドにヒットしますが、現在このモデルやメソッドには入っていません。それが問題だ。 – venkat

+0

アカウントモデルを追加しました。確認してください。 – venkat

+0

あなたのアカウントモデルで '' 'valid_subscription? '' 'の検証は行われますか? – rkx

関連する問題