Blog
Stripe Billingsの解約まわりについての覚書
Billingで一番触りたくなくて、適当にすると一番もめる箇所。それが解約まわりですね。
通常のプランについて
もっとも簡単な方法は、ただsubscriptions.delのAPIを叩くことです。
await stripe.subscriptions.del(subscriptionId)
その場でsubscriptionが解約されます。
途中で解約されたユーザーに対して、比例配分を行いたい場合は、prorateという値をつけてやりましょう。
await stripe.subscriptions.del(subscriptionId, { prorate: true })
期末でのキャンセル方法
サービスによっては、「今終了する」のではなく「今月で利用終了する」という解約方法もあります。この場合は、subscriptions.delではなくsubscriptions.updateでcancel_at_period_endを設定してやります。
await stripe.subscriptions.update(subscriptionId, {
cancel_at_period_end: true
})
こちらではsubscription自体はdeleteされません。期末になったタイミングでcustomer.subscription.deletedイベントが発生し、subscriptionも削除されるという形になります。
キャンセルのキャンセル方法
即時終了の場合は、subscriptionがDELETEされてしまっています。もう一度契約してもらいましょう。
期末でのキャンセルを予定しているsubscriptionでは、subscription自体は存在しており、ステータスを更新するだけで「キャンセルのキャンセル」ができます。
await stripe.subscriptions.update(subscriptionId, {
cancel_at_period_end: false
})
UIの実装では、上記の実装をしたAPIを用意し、「キャンセルの取り消し」という画面を用意してやるとよいでしょう。
従量課金系について
従量課金系ではすこし事情が変わります。というのも、「解約時点までの利用料を請求する必要がある」からです。
素のままのsubscriptions.delを実行すると、請求されていない項目については破棄されてしまいます。
By default, the cancellation takes effect immediately. Once a customer’s subscription is canceled, no further invoices are generated for that subscription.
https://stripe.com/docs/billing/subscriptions/canceling-pausing#canceling
これでは請求前に解約されるととりっぱぐれが発生してしまいます。ですので従量課金系の解約は以下のように請求を行うパラメータを付与しましょう。
await stripe.subscriptions.del(subscriptionId, {
invoice_now: true
})
ちなみにsubscriptions.updateによる期末キャンセルでは、キャンセルされた時点で最終の請求が発生します。「解約日に使用量の最終請求を行います」という案内をユーザーにだしておけば混乱も少ないでしょう。
Note that when a subscription has been scheduled for cancellation using
cancel_at_period_end, it can be reactivated at any point up to the end of the period by updatingcancel_at_period_endtofalse. Any metered usage is charged for in a final invoice once the subscription cancels at the end of the billing period.https://stripe.com/docs/billing/subscriptions/canceling-pausing#canceling
期末キャンセルの場合は、usageRecord.createによる使用量の追加もキャンセルされるまで行うことができます。
SubscriptionItemsでの従量課金の解約について
subscriptionItems.del / updateには上記のような請求に役立つオプションが2019/06時点ではありません。「usageRecord.createで追加した値を消してdeleteしてね」という案内がされていますので、以下のような形で頑張るしかなさそうです。
- subscriptionItemsからautopay系を抜き出す
- 金額を拾う
- 別途charge / invoiceを出す
- subscriptionItems.delでitemを消す
なかなか大変そうな気配なので、従量課金プランは単体のsubscriptionで契約させる実装にしたほうが平和な予感がします。
Tools to Support Stripe Development
We provide helpful tools to extend the Stripe Dashboard and streamline development and testing.
View All ToolsSupport This Project
If you find this content helpful, consider supporting the project through GitHub Sponsors. Your support helps maintain and improve these tools.
