Stripe: Finalize Draft Invoice
This item finalizes a draft invoice on Stripe, which allows you to send the invoice or charge the customer.
Configs: Common
  • Step Name
  • Note
Configs
  • C1: Authorization Setting in which API Secret Key is set *
  • C2: Draft Invoice ID *
  • C3: Due date for payment *

Notes

Capture

See also

Script (click to open)
  • An XML file that contains the code below is available to download
    • stripe-invoice-finalize.xml (C) Questetra, Inc. (MIT License)
    • If you are using Professional, you can modify the contents of this file and use it as your own add-on


const STRIPE_API_VERSION = '2022-08-01';

main();

function main(){
    //// == Config Retrieving / 工程コンフィグの参照 ==
    const auth = configs.get('conf_Auth');
    const invoiceId = retrieveInvoiceId();
    const dueDateTimestamp = retrieveDueDate();

    //// == Calculating / 演算 ==
    updateInvoice(auth, invoiceId, dueDateTimestamp);
    finalizeInvoice(auth, invoiceId);
}

/**
  * config から請求書ドラフトの ID を読み出す。空文字列の場合はエラー
  * @return {String} invoiceId 請求書ドラフトの ID
  */
function retrieveInvoiceId() {
    const invoiceId = engine.findData(configs.getObject('conf_InvoiceId'));
    if (invoiceId === null) {
        throw 'Invoice ID is blank.';
    }
    return invoiceId;
}

/**
  * config から支払い期日を読み出す。以下の場合はエラー
  * - データ項目の値が空
  * - 過去の日時
  * - 日時が遠すぎる
  * @return {String} dueDateTimestamp 支払い締め切り日時のタイムスタンプ
  */
function retrieveDueDate() {
    const maxDays = 1800; // Stripe の制限は「5 年後の 12 月 31 日まで」だが、単純化のため 1800 日後(約5年後)までとする
    const dueDatetime = engine.findData(configs.getObject('conf_DueDate')); // AddableTimestamp
    if (dueDatetime === null) {
        throw 'Due date is not set.';
    }
    const timestamp = Math.floor(dueDatetime.getTime() / 1000);
    const now = Math.floor(Date.now() / 1000);
    if (timestamp <= now) {
        throw 'Due date must be future.';
    }
    if (timestamp - now > maxDays * 24 * 60 * 60) {
        throw `Number of days until due date must be less than or equal to ${maxDays}.`;
    }
    return timestamp.toString();
}

/**
  * 請求書のドラフトを更新する
  * - collection_method を send_invoice に(メール送付、登録済み支払い方法への課金の両方をできるように)
  * - 支払い期日を設定(send_invoice の場合は必須)
  * @param {String} oauth 認証設定
  * @param {String} invoiceId 請求書ドラフトの ID
  * @param {String} dueDateTimestamp 支払い締め切り日時のタイムスタンプ
  */
function updateInvoice(auth, invoiceId, dueDateTimestamp) {
    const apiUri = `https://api.stripe.com/v1/invoices/${encodeURIComponent(invoiceId)}`;
    const response = httpClient.begin()
        .authSetting(auth) // with "Authorization: Bearer XX"
        .header('Stripe-Version', STRIPE_API_VERSION)
        .formParam('collection_method', 'send_invoice')
        .formParam('due_date', dueDateTimestamp)
        .post(apiUri);
    const status = response.getStatusCode();
    const responseStr = response.getResponseAsString();
    if (status !== 200) {
        engine.log(responseStr);
        throw `Failed to update invoice. status: ${status}`;
    }
}

/**
  * 請求書のドラフトを確定する
  * @param {String} oauth 認証設定
  * @param {String} invoiceId 請求書ドラフトの ID
  */
function finalizeInvoice(auth, invoiceId) {
    const apiUri = `https://api.stripe.com/v1/invoices/${encodeURIComponent(invoiceId)}/finalize`;
    const response = httpClient.begin()
        .authSetting(auth) // with "Authorization: Bearer XX"
        .header('Stripe-Version', STRIPE_API_VERSION)
        .formParam('auto_advance', 'false') // 確定後の処理が自動で進まないよう false に
        .post(apiUri);
    const status = response.getStatusCode();
    const responseStr = response.getResponseAsString();
    if (status !== 200) {
        engine.log(responseStr);
        throw `Failed to finalize invoice. status: ${status}`;
    }
}

    
%d