2017-10-28 87 views
0

現在,當submit.html.erb加載時出現此錯誤消息:Cannot charge a customer that has no active card通過邪惡的寶石在嚮導頁面上的條紋簽出提供加載錯誤,如何解決?

我想了解如何改進我的控制器。目標是在提交頁面加載時不會收到錯誤消息。但只有當真實用戶提交虛假信息時纔會這樣做。

class SubmissionsController < ApplicationController 
include Wicked::Wizard 
steps :name, :details, :comments, :submit, :thankyou 

def show 
    @company = Company.find(params[:company_id]) 
    render_wizard 
end 

def update 
    @company = Company.find(params[:company_id]) 

    # one time checkout 
    @amount = 50 
    @description = 'Premium Package' 

    customer = Stripe::Customer.create(
    :email => params[:stripeEmail], 
    :source => params[:stripeToken] 
) 

    Stripe::Charge.create(
    :customer => customer.id, 
    :amount  => @amount, 
    :description => @description, 
    :currency => 'eur' 
) 

    rescue Stripe::CardError => e 
    flash[:error] = e.message 

    params[:company][:status] = step.to_s 
    params[:company][:status] = 'active' if step == steps.last 
    @company.update_attributes(company_params) 
    render_wizard @company 
end 

private ... 

這裏的submit.html.erb

<%= form_for @company, url: wizard_path, method: :put do |f| %> 
    <article> 
    <% if flash[:error].present? %> 
     <div id="error_explanation"> 
     <p><%= flash[:error] %></p> 
     </div> 
    <% end %> 
    <label class="amount"> 
     <span>Amount: €0.50</span> 
    </label> 
    </article> 

    <script src="https://checkout.stripe.com/checkout.js" class="stripe-button" 
      data-key="<%= Rails.configuration.stripe[:publishable_key] %>" 
      data-description=<% @description %> 
      data-amount=<% @amount %> 
      data-locale="auto"></script> 
<% end %> 

如果我可以提供更多的背景,請告知。

回答

0

重構到這一點,現在,它的工作原理:

class SubmissionsController < ApplicationController 
    include Wicked::Wizard 
    steps :name, :details, :comments, :submit, :thankyou 

    def show 
    get_company 
    set_package 
    render_wizard 
    end 

    def update 
    case step 
    when :thankyou 
     get_company 
     render_wizard @company 
    when :submit 
     get_company 
     set_package 
     if request.put? 
     create_customer 
     do_payment 
     render_wizard @company 
     end 
    else 
     get_company 
     params[:company][:status] = 'active' if step == steps.last 
     @company.update_attributes(company_params) 
     render_wizard @company 
    end 
    end 

    def get_company 
    @company = Company.find(params[:company_id]) 
    end 

    def create_customer 
    @customer = Stripe::Customer.create(
     :email => params[:stripeEmail], 
     :source => params[:stripeToken] 
    ) 
    end 

    def set_package 
    @amount = 50 
    @amount_in_decimals = @amount/100.00 
    @description = "Premium Package" 
    end 

    def do_payment 
    Stripe::Charge.create(
     :customer => @customer.id, 
     :amount  => @amount, 
     :description => @description, 
     :currency => "eur" 
    ) 

    rescue Stripe::CardError => e 
     flash[:error] = e.message 
    end 

和視圖

<label class="amount"> 
     <span>Amount: <%= number_to_currency(@amount_in_decimals, :precision => 2, :locale => :nl) %></span> 
    </label> 
    </article> 

    <script src="https://checkout.stripe.com/checkout.js" class="stripe-button" 
      data-key="<%= Rails.configuration.stripe[:publishable_key] %>" 
      data-name="RailsApp!" 
      data-description='<%= @description %>' 
      data-amount=<%= @amount %> 
      data-locale="auto" 
      data-currency="eur"></script> 
<% end %>