繁体   English   中英

结束时段后取消取消订阅

[英]stripe cancel subscription after end period

我有一种取消订阅的方法,例如:

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应该是什么? 我不确定在这里使用什么?

casecustomer.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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM