Blog
Stripe SDKでBillingの今月末期限の請求をリストアップする
Posted over 2 years ago
この記事をシェア:
今月請求される内容をリストアップしてSlackなどに投げたいので、ざっとピックアップ系の処理をあらいました。
コード
invoices.listの引数にdue_dateを渡すことで指定期間のinvoiceだけ引っ張れます。UNIXタイムスタンプを文字列で渡す必要がある点に要注意です。
import * as Stripe from 'stripe';
import * as moment from 'moment';
const stripe = new Stripe(YOUR_SK);
const listInvoices = async(): Promise<void> => {
const invoices = await stripe.invoices.list({
due_date: {
gte: moment().startOf('month').unix().toString(),
lte: moment().endOf('month').unix().toString()
}
})
invoices.data.forEach(invoice => {
console.log('=====\n')
console.log('請求書番号: %j', invoice.number)
if (invoice.due_date) console.log('請求日: %j', moment.unix(invoice.due_date).format('YYYY/MM/DD'))
console.log(`金額: ${invoice.total.toLocaleString()} ${invoice.currency.toUpperCase()}`)
})
}
listInvoices()
実行結果がこのようになります。
=====
請求書番号: "1A7499D-0011"
請求日: "2019/03/17"
金額: 16,200 JPY
=====
請求書番号: "1A7499D-0010"
請求日: "2019/03/16"
金額: 15,000 JPY
invoices.data.forEach以下の処理でデータを整形して、SlackにPOSTするなどすれば社内のリマインダーにできるでしょう。
請求書決済だけをピックアップする
メールでカード決済の請求をリクエストするタイプのSubscriptionも作れます。
手動でメール送信などをやらないといけないですが、Stripeの外での決済についてもStripeで管理できるようになります。
このタイプのSubscriptionだけをピックアップすることで、今月確認が必要な決済がどれかを絞ることができます。
const invoices = await stripe.invoices.list({
billing: "send_invoice",
due_date: {
gte: moment().startOf('month').unix().toString(),
lte: moment().endOf('month').unix().toString()
}
})
カード決済だけをピックアップする
ちなみにカード決済だけをピックアップもできます。
const invoices = await stripe.invoices.list({
billing: "charge_automatically",
due_date: {
gte: moment().startOf('month').unix().toString(),
lte: moment().endOf('month').unix().toString()
}
})
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.
