Skip to main content

Blog

Stripe Billingで”しきい値決済”が可能になった様子

Posted over 2 years ago
この記事をシェア:

Stripeのダッシュボードを見ていると、突然謎の機能が増えていたので調べました。

問題の機能

定期支払の設定・更新画面に以下のような項目が増えています。

中の人に聞いてみたところ、「ある一定の金額やUsageに達すると自動的にInvoiceを発行する機能」ということだそうです。

使い方

ダッシュボードでは、以下のようにしきい値と請求期間の再計算が設定できます。

APIレスポンスでは以下のようにbilling_threshouldsにデータが入ります。

{
  id: 'sub_xxxxxx',
  object: 'subscription',
  application_fee_percent: null,
  billing: 'charge_automatically',
  billing_cycle_anchor: 1550457151,
  billing_thresholds: {
    amount_gte: 500, 
    reset_billing_cycle_anchor: true
  },

Nodejsで実装

パラメータ名がわかれば、あとはAPI / SDKからも投入できますね。ということでサンプルです。

const stripe = require('stripe')('sk_xxxx_xxxxx')


const test = async () => {
  try {
    const sub = await stripe.subscriptions.create(
      {
        customer: 'cus_XXXXX,
        items: [
          {
            plan: 'plan_ID'
          }
        ],
        billing_thresholds: {
          amount_gte: 500, 
          reset_billing_cycle_anchor: true
        }
      }
    )
    console.log(sub)
  } catch (e) {
    console.log(e)
  }
}
test()

動作確認していない&Dashboardに出てきませんが、subscription item毎に設定できそうな気配もします。だれか人柱なってください。

const sub = await stripe.subscriptions.create(
      {
        customer: customerId,
        items: [
          {
            plan: 'plan_XXX',
            billing_thresholds: {
              usage_gte: 600
            }
          }
        ]
      }
    )

Tools to Support Stripe Development

We provide helpful tools to extend the Stripe Dashboard and streamline development and testing.

View All Tools

Support This Project

If you find this content helpful, consider supporting the project through GitHub Sponsors. Your support helps maintain and improve these tools.