Gurmukh Singh
Gurmukh Singh

Reputation: 2017

stripe cancel subscription after end period

I have a method to cancel subscriptions like this:

def cancel_subscription
  customer = Stripe::Customer.retrieve(current_user.stripe_customer_id)
  customer.subscriptions.retrieve(current_user.subscription_id).delete(:at_period_end => true)
  current_user.subscribed = false
  if current_user.save
    redirect_to root_path
  end
end

Im using (:at_period_end => true) to cancel at the end at end date from started.

But also in my DB i have a column which is set to false or true depending on if the customer is subscribed or not:

current_user.subscribed = false

I want to change this to false after the end period. For example the customer cancels the subscription but still has 10 days left. I want current_user.subscribed set to false after the actual period is over and the customer is now properly unsubscribed.

Here is web hook set up:

def webhook
  begin
    event_json = JSON.parse(request.body.read)
    event_object = event_json['data']['object']
    #refer event types here https://stripe.com/docs/api#event_types
    case event_json['type']
      when 'invoice.payment_succeeded'
        handle_success_invoice event_object
      when 'invoice.payment_failed'
        handle_failure_invoice event_object
      when 'charge.failed'
        handle_failure_charge event_object
      when 'customer.subscription.deleted'
      when 'customer.subscription.updated'
    end
  rescue Exception => ex
    render :json => {:status => 422, :error => "Webhook call failed"}
    return
  end
  render :json => {:status => 200}
end

In my routes file I have the following for stripe:

Rails.application.routes.draw do
  root to: 'welcome#index'
  devise_for :users, controllers: { sessions: 'users/sessions',
                                    :omniauth_callbacks => "callbacks",
                                    registrations: "users/registrations"}
  resources :profiles
  resources :subscriptions, only: [:new, :create]
  resources :topics, only: [:index, :edit, :create, :destroy, :update]
  resources :topic_profiles, only: [:create, :destroy]
  resources :chats, only: [:index]

  #resources :messages, only: %i[new edit]
  resources :messages, only: %i[new create index]
  post 'incoming_message/webhook'
  resources :test_inbound_messages, only: %i[new create]


  post 'subscription_checkout' => 'subscriptions#subscription_checkout'
  post 'webhook' => 'subscriptions#webhook'
  get 'plans' => 'subscriptions#plans'
  post 'cancel_subscription' => 'subscriptions#cancel_subscription'
  get 'get_cities' => 'profiles#get_cities'
  get 'turn_off_modal'=> "profiles#turn_off_modal"
end

I forgot to set up web hooks on the stripe dashboard, its asking me for a URL (URL to be called) to enter, according to these routes, what should the url be? im not sure what to use here?

Upvotes: 1

Views: 1953

Answers (1)

kiddorails
kiddorails

Reputation: 13014

Handle in case of customer.subscription.deleted, look below:

def webhook
  begin
    event_json = JSON.parse(request.body.read)
    event_object = event_json['data']['object']
    #refer event types here https://stripe.com/docs/api#event_types
    case event_json['type']
      when 'invoice.payment_succeeded'
        handle_success_invoice event_object
      when 'invoice.payment_failed'
        handle_failure_invoice event_object
      when 'charge.failed'
        handle_failure_charge event_object
      when 'customer.subscription.deleted'
        # NOTE HERE, this event will come when stripe actually marks the subscription as canceled, which in your example will be 10 days later.
        # so simply get the user here and mark `subscribed` as false, instead of doing it early
        user = Stripe::Customer.retrieve(event_object['customer'])
        user.update_attribute :subscribed, false
      when 'customer.subscription.updated'
    end
  rescue Exception => ex
    render :json => {:status => 422, :error => "Webhook call failed"}
    return
  end
  render :json => {:status => 200}
end

Upvotes: 1

Related Questions