结束时段后取消取消订阅

时间:2018-06-27 11:12:54

标签: ruby-on-rails ruby stripe-payments webhooks

我有一种取消订阅的方法,如下所示:

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

我使用(:at_period_end => true)在开始日期的结束日期结束时取消。

而且在我的数据库中,我还有一列设置为false或true,具体取决于客户是否订阅:

current_user.subscribed = false

我想在结束后将其更改为false。例如,客户取消了订阅,但还剩10天。我希望在实际期限结束后current_user.subscribed设置为false,然后就可以正确取消订阅该客户了。

在这里设置了网络挂钩:

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

在我的路由文件中,我具有以下条纹:

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

我忘记在条纹仪表板上设置Web挂钩,它要求我输入URL(URL to be called),根据这些路由,该URL应该是什么?我不确定在这里使用什么?

1 个答案:

答案 0 :(得分:0)

处理case中的customer.subscription.deleted,如下所示:

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