2016-07-10 5 views
1

nicht speichern Ich habe eine Benutzer-Anmeldeseite, auf der ich persönliche Details zusammen mit Kreditkarteninformationen eingeben werde. Benutzer has_many Konten und Konto gehört zu Benutzer. Wenn ein Benutzer erstellt wird, wird ein Konto erstellt und die Kreditkarteninformationen und -adressen werden in einer Abonnementtabelle gespeichert. Nach der Kontoerstellung wird die Methode store_card im Modell "subscription.rb" verwendet. Es funktioniert einwandfrei, wenn ich die Anwendung in Rails 3.2.13 ausführe und wenn ich es zu Rails 4.2.6 migrierte, speichert es nicht die Subskription Details und kein Fehler angezeigt. Ich habe ein Upgrade activemerchant Juwel 1.47.0 und änderte den Code in load_billing Methode in dem unten stehenden Controller vonKonnte die Kartenwerte nach Versionsänderung in Rails 4.2

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

zu

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

und auf die gleiche Art und Weise @Adresse auch.

Es gibt keine spezifische Beziehung, sondern nur diese Zeile in account.rb hinzugefügt:

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

Wenn ich von der Konsole auszuführen versucht:

u = Account.find(1) 
u.subscription 

es Abonnement Spalten erzeugt.

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 

Bitte helfen Sie, ob ich stark params und wie oder jede andere Art und Weise verwenden?

Antwort

0

Wäre es gut zu sehen, Account Modell auch, aber basierend auf Ihrem Code sieht es aus wie Sie nie speichern Kreditkarteninformationen. Die Methode store_card weist nur Werte zu, speichert sie jedoch niemals in der Datenbank. Die einfachste Lösung wäre, rufen Sie self.save in Ihrer store_card Methode.

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: In meiner älteren Version trifft es dieses Modell und diese Methode, derzeit kommt es nicht in dieses Modell oder diese Methode. Das ist das Problem. – venkat

+0

Ich habe Account-Modell hinzugefügt, bitte überprüfen. – venkat

+0

so einfach klar sein, funktioniert Validation '' 'valid_subscription?' '' In Ihrem Account-Modell? – rkx

Verwandte Themen