
Stripe: Update Customer
This item updates a customer object on Stripe. Fields with blank values remain unchanged.
Configs: Common
- Step Name
- Note
Configs
- C1: Authorization Setting in which API Secret Key is set *
- C2: Customer ID *
- C3: Customer’s New Name#{EL}
- C4: Language of invoice
- C5: Customer’s New Email Address#{EL}
- C6: New Description on customer#{EL}
Notes
- To get Stripe’s API Secret Key, visit https://dashboard.stripe.com/apikeys.
Capture

See also
Script (click to open)
- An XML file that contains the code below is available to download
- stripe-customer-update.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 customerId = retrieveCustomerId();
const name = retrieveConfig('conf_Name', 'Customer Name', 256);
const language = configs.get('conf_Language');
const email = retrieveConfig('conf_Email', 'Customer Email', 512);
const description = retrieveConfig('conf_Description', 'Customer Description', 350);
//// == Calculating / 演算 ==
updateCustomer(auth, customerId, name, language, email, description);
}
/**
* config から顧客 ID を読み出す。空文字列の場合はエラー
* @return {String} customerId 顧客 ID
*/
function retrieveCustomerId() {
let customerId = configs.get('conf_CustomerId'); // 固定値の場合
const customerIdDef = configs.getObject('conf_CustomerId');
if (customerIdDef !== null) {
if (customerIdDef.matchDataType('SELECT_SINGLE')) { // 選択型データ項目の場合
const select = engine.findData(customerIdDef);
if (select === null || select.size() === 0) { // 未選択
throw 'Customer ID is not selected.';
}
customerId = select.get(0).getValue();
} else { // 文字型データ項目の場合
customerId = engine.findData(customerIdDef);
}
}
if (customerId === null || customerId === '') {
throw 'Customer ID is blank.';
}
return customerId;
}
/**
* config から設定値を読み出す。長さが maxLength を超える場合、エラー
* @param {String} confName config 名
* @param {String} label エラー出力用ラベル
* @param {Number} maxLength 最大文字数
* @return {String} value 設定値
*/
function retrieveConfig(confName, label, maxLength) {
const value = configs.get(confName);
if (value.length > maxLength) {
throw `${label} must be at most ${maxLength} characters.`;
}
return value;
}
/**
* 顧客を更新する
* @param {String} oauth 認証設定
* @param {String} customerId 顧客 ID
* @param {String} name 名前
* @param {String} language 言語
* @param {String} email メールアドレス
* @param {String} description 説明
*/
function updateCustomer(auth, customerId, name, language, email, description) {
const formParams = prepareFormParams(name, language, email, description);
const apiUri = `https://api.stripe.com/v1/customers/${encodeURIComponent(customerId)}`;
let request = httpClient.begin()
.authSetting(auth) // with "Authorization: Bearer XX"
.header('Stripe-Version', STRIPE_API_VERSION);
Object.keys(formParams).forEach(key => {
request = request.formParam(key, formParams[key]);
});
const response = request.post(apiUri);
const status = response.getStatusCode();
const responseStr = response.getResponseAsString();
if (status !== 200) {
engine.log(responseStr);
throw `Failed to update customer. status: ${status}`;
}
}
/**
* 顧客更新のためのフォームパラメータを準備する。値がすべて空の場合はエラー
* @param {String} name 名前
* @param {String} language 言語
* @param {String} email メールアドレス
* @param {String} description 説明
* @return {Object} formParams フォームパラメータを格納したオブジェクト
*/
function prepareFormParams(name, language, email, description) {
const formParams = {};
addParamIfNotBlank(formParams, 'name', name);
addParamIfNotBlank(formParams, 'preferred_locales[0]', language);
addParamIfNotBlank(formParams, 'email', email);
addParamIfNotBlank(formParams, 'description', description);
if (Object.keys(formParams).length === 0) {
throw 'No fields to update.';
}
return formParams;
}
/**
* 値が空でない場合のみ、フォームパラメータを格納するオブジェクトに値を追加する
* @param {Object} formParams フォームパラメータを格納するオブジェクト
* @param {String} paramName パラメータ名
* @param {String} value パラメータにセットする値
*/
function addParamIfNotBlank(formParams, paramName, value) {
if (value !== '') {
formParams[paramName] = value;
}
}