RailsApps / rails-stripe-membership-saas

An example Rails 4.2 app with Stripe and the Payola gem for a membership or subscription site.

Home Page:http://railsapps.github.io/rails-stripe-membership-saas

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to cancel subscription ?

ycourse opened this issue · comments

Hey guys,

I want to cancel automatically subscription for my customers after One year.

I heard that this object can help me....

  • cancel_at_period_end

How can i do this ?

Thank you.

I reread your question and it looks like you just pass the option in on subscription creation.

  def update_plan(role)
    self.role_ids = []
    self.add_role(role.name)
    unless customer_id.nil?
      customer = Stripe::Customer.retrieve(customer_id)
      customer.update_subscription(
          plan: role.name,
          cancel_at_period_end: true
      )
    end
    true
  rescue Stripe::StripeError => e
    logger.error "Stripe Error: " + e.message
    errors.add :base, "Unable to update your subscription. #{e.message}."
    false
  end

Using the stripe event engine, you can tie into a webhook from stripe.

The gem has been updated since this app was built, I believe, so double check the documentation for stripe event.

You can see the types of events you can hook into here:
https://stripe.com/docs/api?lang=ruby#event_types

Here is an example

StripeEvent.configure do |events|
  events.subscribe 'customer.subscription.deleted' do |event|
    # your logic here
  end
end