Save 36% for Black Friday! Learn more

Ask A Question

Notifications

You’re not receiving notifications from this thread.

How do I resolve this error with Stripe checkout on wizard page via wicked gem?

Robert Hopman asked in Rails

Right now I get this error message when the submit.html.erb loads: Cannot charge a customer that has no active card.

I want to understand how I can improve my controller. The goal is to not have the error message when the submit page loads. But only if there's an actual user submitting false information.

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 ...

Here's the 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 %>

If I can provide more context, please advise.

Reply

Added a couple of spaces in the submit.html.erb template, otherwise it wouldn't be visible.

Reply

Hey Robert,

When Stripe tells you that the customer doesn't have a card to charge, that means that your form probably hasn't submitted the stripeToken param to attach to the user. You'll want to make sure that's getting passed over.

Reply

Hey Chris, thanks.

Reply

Refactored into:

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
Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 86,946+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.