SaaS / FaaSStripe

Stripe SDKでBillingの今月末期限の請求をリストアップする

今月請求される内容をリストアップしてSlackなどに投げたいので、ざっとピックアップ系の処理をあらいました。 コード invoices.listの引数にdue_dateを渡すことで指定期間のinvoiceだけ引っ張れます […]

広告ここから
広告ここまで

今月請求される内容をリストアップして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()
    }
  })

ブックマークや限定記事(予定)など

WP Kyotoサポーター募集中

WordPressやフロントエンドアプリのホスティング、Algolia・AWSなどのサービス利用料を支援する「WP Kyotoサポーター」を募集しています。
月額または年額の有料プランを契約すると、ブックマーク機能などのサポーター限定機能がご利用いただけます。

14日間のトライアルも用意しておりますので、「このサイトよく見るな」という方はぜひご検討ください。

広告ここから
広告ここまで

Related Category posts