infinity infinity - 2 years ago 97
Ruby Question

Stripe charge and Subscription

I have a rails 4 app that I'm using Stripe checkout. I've followed their tutorial, and my controller looks like:

def create

s =
s.user_id =
s.plan_id = params[:plan_id]
s.stripe_token = params[:stripeToken]

# Amount in cents
@amount = 699

customer = Stripe::Customer.create(
:email =>,
:card => params[:stripeToken]

charge = Stripe::Charge.create(
:customer =>,
:amount => @amount,
:description => 'Sitekite Pro',
:currency => 'usd'

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

I looked at a couple other tutorials, looking for help with creating a subscription with Stripe checkout. Some of them dont have the Stripe::Charge part. Is the Stripe::Charge part only for single charges? How do I sign the user up for a monthly subscription with the same

Answer Source

The Stripe::Charge is indeed for single charges. The Customer create is what you need, but when you create the customer you specify a plan (plans are defined in your Stripe dashboard). The plan will specify the amount to charge and how often to charge it.

When the charge is actually made, which might be the same day or might be several days later depending on whether your plan provides, say, some free access time... the Stripe service can send the charge to a webhook... which is to say a route in your project for the dedicated use of the Stripe service.

Stripe will post charges (and failures) to your webhook, and you can handle them appropriately (logging the payments and maybe restricting the user if his card becomes expired or a regular payment fails for some other reason)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download