
Stripe: 顧客/商品/請求書のメタデータを更新
Stripe: Update Metadata of Customer/Product/Invoice
この工程は、Stripe 上の顧客、商品または請求書のメタデータを更新します。キーを指定していないメタデータフィールドは変更されません。
Basic Configs
- 工程名
- メモ
Configs for this Auto Step
- conf_Auth
- C1: API シークレットキーを設定した認証設定 *
- conf_Id
- C2: 顧客/商品/請求書の ID *
- conf_Key1
- C-K1: 追加/更新/削除するメタデータフィールド 1 のキー
- conf_Value1
- C-V1: フィールド 1 の更新後の値(空の場合、フィールドごと削除します)#{EL}
- conf_Key2
- C-K2: 追加/更新/削除するメタデータフィールド 2 のキー
- conf_Value2
- C-V2: フィールド 2 の更新後の値(空の場合、フィールドごと削除します)#{EL}
- conf_Key3
- C-K3: 追加/更新/削除するメタデータフィールド 3 のキー
- conf_Value3
- C-V3: フィールド 3 の更新後の値(空の場合、フィールドごと削除します)#{EL}
- conf_Key4
- C-K4: 追加/更新/削除するメタデータフィールド 4 のキー
- conf_Value4
- C-V4: フィールド 4 の更新後の値(空の場合、フィールドごと削除します)#{EL}
- conf_Key5
- C-K5: 追加/更新/削除するメタデータフィールド 5 のキー
- conf_Value5
- C-V5: フィールド 5 の更新後の値(空の場合、フィールドごと削除します)#{EL}
- conf_Key6
- C-K6: 追加/更新/削除するメタデータフィールド 6 のキー
- conf_Value6
- C-V6: フィールド 6 の更新後の値(空の場合、フィールドごと削除します)#{EL}
- conf_Key7
- C-K7: 追加/更新/削除するメタデータフィールド 7 のキー
- conf_Value7
- C-V7: フィールド 7 の更新後の値(空の場合、フィールドごと削除します)#{EL}
- conf_Key8
- C-K8: 追加/更新/削除するメタデータフィールド 8 のキー
- conf_Value8
- C-V8: フィールド 8 の更新後の値(空の場合、フィールドごと削除します)#{EL}
Notes
- Stripe の API シークレットキーを取得するには https://dashboard.stripe.com/apikeys を参照してください(要 Stripe ログイン)
Capture

See also
Script (click to open)
- 次のスクリプトが記述されている XML ファイルをダウンロードできます
- stripe-metadata-update.xml (C) Questetra, Inc. (MIT License)
- Professional のワークフロー基盤では、ファイル内容を改変しオリジナルのアドオン自動工程として活用できます
const STRIPE_API_VERSION = '2022-08-01';
const FIELD_NUM = 8; // 扱うフィールドの数
const MAX_KEY_LENGTH = 40; // メタデータの key の最大文字数(Stripe の制限)
const MAX_VALUE_LENGTH = 500; // メタデータの value の最大文字数(Stripe の制限)
function main(){
//// == Config Retrieving / 工程コンフィグの参照 ==
const auth = configs.getObject('conf_Auth');
const id = retrieveId();
const apiUri = decideApiUri(id);
const metadataMap = retrieveMetadataMap();
//// == Calculating / 演算 ==
updateMetadata(auth, apiUri, metadataMap);
}
/**
* config から顧客/商品/請求書 ID を読み出す。空文字列の場合はエラー
* @return {String} id 顧客/商品/請求書 ID
*/
function retrieveId() {
let id = configs.get('conf_Id'); // 固定値の場合
const idDef = configs.getObject('conf_Id');
if (idDef !== null) {
if (idDef.matchDataType('SELECT_SINGLE')) { // 選択型データ項目の場合
const select = engine.findData(idDef);
if (select === null || select.size() === 0) { // 未選択
throw new Error('Customer/Product/Invoice ID is not selected.');
}
id = select.get(0).getValue();
} else { // 文字型データ項目の場合
id = engine.findData(idDef);
}
}
if (id === null || id === '') {
throw new Error('Customer/Product/Invoice ID is blank.');
}
return id;
}
/**
* id から API の URI を決定する。顧客/商品/請求書 ID の形式でない場合、エラー
* @param {String} id 顧客/商品/請求書 ID
* @return {String} apiUri
*/
function decideApiUri(id) {
const prefix = id.substring(0, id.indexOf('_'));
const baseUrl = 'https://api.stripe.com/v1/';
switch (prefix) {
case 'cus':
return `${baseUrl}customers/${encodeURIComponent(id)}`;
case 'prod':
return `${baseUrl}products/${encodeURIComponent(id)}`;
case 'in':
return `${baseUrl}invoices/${encodeURIComponent(id)}`;
default:
throw new Error('Customer/Product/Invoice ID must start with "cus_", "prod_" or "in_".');
}
}
/**
* config からメタデータの key, value を読み出し、Map に格納して返す。以下の場合はエラー
* - value は設定されているのに key が空
* - key の指定が重複している
* - key の文字数が Stripe の制限を超える
* - value の文字数が Stripe の制限を超える
* - 更新すべきメタデータがない
* @return {Map<String, String>} metadataMap メタデータの Map
*/
function retrieveMetadataMap() {
const metadataMap = new Map();
for (let i = 0; i < FIELD_NUM; i++) {
const key = configs.get(`conf_Key${i+1}`);
const value = configs.get(`conf_Value${i+1}`);
if (key === '') {
if (value === '') {
// key, value がともに空白の組は無視
continue;
}
throw new Error(`C-V${i+1} is set but C-K${i+1} is blank.`);
}
if (metadataMap.has(key)) { // key の指定が重複
throw new Error(`Field Key "${key}" is set multiple times.`);
}
if (key.length > MAX_KEY_LENGTH) {
throw new Error(`C-K${i+1} exceeds the limit of ${MAX_KEY_LENGTH} characters.`);
}
if (value.length > MAX_VALUE_LENGTH) {
throw new Error(`C-V${i+1} exceeds the limit of ${MAX_VALUE_LENGTH} characters.`);
}
metadataMap.set(key, value);
}
if (metadataMap.size === 0) {
throw new Error('No metadata fields to update.');
}
return metadataMap;
}
/**
* メタデータを更新する
* @param {AuthSettingWrapper} auth HTTP 認証設定 トークン直接指定
* @param {String} apiUri リクエスト URI
* @param {Map<String, String>} metadataMap メタデータの Map
*/
function updateMetadata(auth, apiUri, metadataMap) {
let request = httpClient.begin()
.authSetting(auth) // with "Authorization: Bearer XX"
.header('Stripe-Version', STRIPE_API_VERSION);
metadataMap.forEach((value, key) => {
request = request.formParam(`metadata[${key}]`, value);
});
const response = request.post(apiUri);
const status = response.getStatusCode();
const responseStr = response.getResponseAsString();
if (status !== 200) {
engine.log(responseStr);
throw new Error(`Failed to update metadata. status: ${status}`);
}
}