Stripe: Create Customer
This item creates a customer object on Stripe.
Configs: Common
  • Step Name
  • Note
Configs
  • C1: Authorization Setting in which API Secret Key is set *
  • C2: Customer’s Name *#{EL}
  • C3: Language of invoice *
  • C4: Customer’s Email Address *#{EL}
  • C5: Description on customer#{EL}
  • C6: Data item to save ID of the customer
  • C7: Data item to save URL of the customer detail page

Notes

Capture

See also

Script (click to open)
  • An XML file that contains the code below is available to download
    • stripe-customer-create.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 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 `${label} is blank.`;
    }
    if (value.length > maxLength) {
        throw `${label} must be at most ${maxLength} characters.`;
    }
    return value;
}

/**
  * 顧客を作成する
  * @param {String} oauth 認証設定
  * @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 `Failed to create customer. status: ${status}`;
    }
    const customerObj = JSON.parse(responseStr);
    return customerObj.id;
}

    
%d bloggers like this: