Stripeを使う機会が増えてきましたので、メモも兼ねてまとめていきたいと思います。

今回は、Stripeで継続課金をつくった注文の継続課金をキャンセルする方法です。

継続課金をキャンセルする前に理解しておくこと

StripeのAPIのドキュメントを見るといろいろ書かれていますが

Cancels a customer’s subscription immediately. The customer will not be charged again for the subscription.
Note, however, that any pending invoice items that you’ve created will still be charged for at the end of the period, unless manually deleted. If you’ve set the subscription to cancel at the end of the period, any pending prorations will also be left in place and collected at the end of the period. But if the subscription is set to cancel immediately, pending prorations will be removed.

By default, upon subscription cancellation, Stripe will stop automatic collection of all finalized invoices for the customer. This is intended to prevent unexpected payment attempts after the customer has canceled a subscription. However, you can resume automatic collection of the invoices manually after subscription cancellation to have us proceed. Or, you could check for unpaid invoices before allowing the customer to cancel the subscription at all.
https://stripe.com/docs/api/subscriptions/cancel

英語のマニュアルなのですが、
ここで注意しておかなければならないことは、
API経由で継続課金を行えばすぐにキャンセルになりますが、以下のような点があるので注意が必要です。
「確定のされてない請求は、Stripeのダッシュボード経由で手動で削除しない限り、期間終了時に請求される。」
「継続課金を期間終了時にキャンセルするように設定した場合、保留中の比例配分もそのままにされ、期間終了時に回収される。」
「すぐにキャンセルされるように設定されている場合、保留中の比例支払いは削除される。」

という点がありますので、継続課金を作成するときに注意しておきましょう。

継続課金をキャンセルする

$stripe->subscriptions->cancel('キャンセルしたいサブスクリプションのID',[]);

これでサブスクリプションをキャンセルできます。

複数継続課金がある場合のうち一つだけを次回の請求からキャンセルする

例として継続課金を一つの注文で4件行っていた場合、サブスクリプションのIDは4つとも同じになりますが、StripeはサブスクリプションIDに紐づくitem idを持っています。

注文番号 サブスクリプションID Item ID
1234 subscription_id_01 item_01
1234 subscription_id_01 item_02
1234 subscription_id_01 item_03
1234 subscription_id_01 item_04

注文番号1234のItemが「item_03」だけを次回の請求からキャンセルする場合

$stripe->subscriptionItems->delete(item_03,['proration_behavior'=>'none']);

これで次回からItem_03の請求がなくなります。

投稿者 White

デジタル機器で面白いものや役立つものを紹介しています。 よろしくお願いします。