Stripe: 顧客を作成

Stripe: 顧客を作成

Stripe: Create Customer

この工程は、Stripe 上に顧客オブジェクトを作成します。

Auto Step icon
Basic Configs
工程名
メモ
Configs for this Auto Step
conf_Auth
C1: API シークレットキーを設定した認証設定 *
conf_Name
C2: 顧客の名前 *#{EL}
conf_Language
C3: 請求書の言語 *
conf_Email
C4: 顧客のメールアドレス *#{EL}
conf_Description
C5: 顧客に関するメモ#{EL}
conf_CustomerId
C6: 顧客の ID を保存するデータ項目
conf_CustomerUrl
C7: 顧客詳細ページの URL を保存するデータ項目

Notes

Capture

See also

Script (click to open)
  • 次のスクリプトが記述されている XML ファイルをダウンロードできます
    • stripe-customer-create.xml (C) Questetra, Inc. (MIT License)
    • Professional のワークフロー基盤では、ファイル内容を改変しオリジナルのアドオン自動工程として活用できます


const STRIPE_API_VERSION = '2022-08-01';

function main(){
    //// == Config Retrieving / 工程コンフィグの参照 ==
    const auth = configs.getObject('conf_Auth');
    const name = retrieveConfig('conf_Name', 'Customer Name', true, 256);
    const language = configs.get('conf_Language');
    const email = retrieveConfig('conf_Email', 'Customer Email', true, 512);
    const description = retrieveConfig('conf_Description', 'Customer Description', false, 350);
    const customerIdDef = configs.getObject('conf_CustomerId');
    const customerUrlDef = configs.getObject('conf_CustomerUrl');

    //// == Calculating / 演算 ==
    const customerId = createCustomer(auth, name, language, email, description);

    //// == Data Updating / ワークフローデータへの代入 ==
    if (customerIdDef !== null) {
        engine.setData(customerIdDef, customerId);
    }
    if (customerUrlDef !== null) {
        engine.setData(customerUrlDef, `https://dashboard.stripe.com/customers/${customerId}`);
    }
}

/**
  * config から設定値を読み出す。以下の場合はエラー
  * - required が true で、空文字列の場合
  * - 長さが maxLength を超える場合
  * @param {String} confName config 名
  * @param {String} label エラー出力用ラベル
  * @param {boolean} required 必須項目かどうか
  * @param {Number} maxLength 最大文字数
  * @return {String} value 設定値
  */
function retrieveConfig(confName, label, required, maxLength) {
    const value = configs.get(confName);
    if (required && value === '') {
        throw new Error(`${label} is blank.`);
    }
    if (value.length > maxLength) {
        throw new Error(`${label} must be at most ${maxLength} characters.`);
    }
    return value;
}

/**
  * 顧客を作成する
  * @param {AuthSettingWrapper} auth  HTTP 認証設定 トークン直接指定
  * @param {String} name 名前
  * @param {String} language 言語
  * @param {String} email メールアドレス
  * @param {String} description 説明
  * @return {String} customerId 顧客 ID
  */
function createCustomer(auth, name, language, email, description) {
    const apiUri = 'https://api.stripe.com/v1/customers';
    const response = httpClient.begin()
        .authSetting(auth) // with "Authorization: Bearer XX"
        .header('Stripe-Version', STRIPE_API_VERSION)
        .formParam('name', name)
        .formParam('preferred_locales[0]', language)
        .formParam('email', email)
        .formParam('description', description)
        .post(apiUri);
    const status = response.getStatusCode();
    const responseStr = response.getResponseAsString();
    if (status !== 200) {
        engine.log(responseStr);
        throw new Error(`Failed to create customer. status: ${status}`);
    }
    const customerObj = JSON.parse(responseStr);
    return customerObj.id;
}

    
上部へスクロール

Questetra Supportをもっと見る

今すぐ購読し、続きを読んで、すべてのアーカイブにアクセスしましょう。

続きを読む