infinity
infinity

Reputation: 719

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 =  Subscription.new
   s.user_id = current_user.id
   s.plan_id = params[:plan_id]
   s.stripe_token = params[:stripeToken]
   s.save

   # Amount in cents
   @amount = 699

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

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

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

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 @amount?

Upvotes: 1

Views: 427

Answers (1)

SteveTurczyn
SteveTurczyn

Reputation: 36860

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)

Upvotes: 2

Related Questions