簡體   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