Stripe: Charge Object, Create with Transfer Amount
Stripe: Charge Object, Create with Transfer Amount

Creates the Charge object with Transfer Amount to charge the Customer ID for any amount in any currency. Any Connected Account as the Transfer Destination, any Transfer Amount within the Charge amount are available. If fails, the object not created.

2020-06-05 (C) Questetra, Inc. (MIT License)
https://support.questetra.com/addons/stripe-charge-object-create-with-transfer-amount/

Configs
  • A: Set SecretKey of Stripe (sk_xxx: 32 letters) * #{EL}
  • B: Set Cusomer ID * #{EL}
  • C1: Set Charge Amount in positive integer * #{EL}
  • C2: Set Currency Code (e.g. “USD” “JPY” “EUR” etc) * #{EL}
  • C3: Set Charge Description (Registration Num, Corp Name, etc) * #{EL}
  • C4: Set Transfer Destination (e.g. “acct_1234567890abcdef”) * #{EL}
  • C5: Set Transfer Amount in positive integer * #{EL}
  • D1: Select STRING DATA for Stripe Charge ID (update)
  • D2: Select STRING DATA for Card Brand (update)
  • D3: Select STRING DATA for Card Last4 (update)
  • D4: Select STRING/YMDATE for Card Exp (update)
Script
// Nashorn Script (engine type: 1)
// 
// Notes:
// A positive integer representing how much to charge in the smallest currency unit.
// - e.g., "100" cents to charge $1.00
// - e.g., "100" to charge \100 (a zero-decimal currency)
// "#{#q_numUsdWithoutCent}00" is also possible if numerical data without auxiliary info
// If present, commas "," and periods "." are removed in advance.
// - When using numeric data, be careful of the number of digits after the decimal point
// Numeric parsing depends on JavaScript parseInt(x,10). (The prefix gives an error)
// See below for more information on available currency codes (JPY, USD, EUR, ...)
// - https://stripe.com/docs/currencies
// In case of USD, the minimum amount is $0.50 or equivalent.
// The amount value supports up to eight digits (e.g., for a USD charge of $999,999.99).
// Refer to the following for the error codes (ResponseCode other than 200).
// - https://stripe.com/docs/api/errors
// Transfer Amount is also expressed in the smallest currency unit. (Positive integer)
// - Exceeding the Charge amount will result in an error
// - `destination[amount]` must be less than or equal to the charge amount
// You will be charged by the Connect Destination method (for Express/Custom type).
// https://stripe.com/docs/connect/destination-charges#transfer-amount
// Connected Accout (child account) must be registered in advance on Stripe.
// 
// Notes(ja):
// 課金額の設定は、最小の通貨単位で表現します。(正の整数)
// - "1.00ドル" の場合は "100" セント (JPY)
// - 小数のない円通貨の場合は "100円" は "100" 円 (JPY)
// 補助通貨情報のない数値データを参照したい場合 "#{#q_numUsdWithoutCent}00" も可能です
// もしカンマ "," やピリオド "." が存在する場合は課金処理前に除去されます。
// - 数値型データの小数を使う場合、小数点以下の桁数設定に注意
// - "100.00円" は「1万円」の課金になります
// 数値判定は JavaScript parseInt(x,10) に依存します (接頭辞はエラーになります)
// 利用できる通貨コードの詳細は以下を参照してください (JPY, USD, EUR, ...)
// - https://stripe.com/docs/currencies
// 日本円の場合、最小金額は50円です。
// 課金額は最大桁数は8桁です。(日本円の場合、99,999,999円)
// エラーコード(200以外のResponseCode)の内容については以下を参照してください。
// - https://stripe.com/docs/api/errors
// 分配金額の設定も、最小の通貨単位で表現します。(正の整数)
// - 課金額を超える設定はエラーとなります
// - `destination[amount]` must be less than or equal to the charge amount
// Connect Destination 方式(Express/Customタイプ向け)で課金されます。
// https://stripe.com/docs/connect/destination-charges#transfer-amount
// Connected Accout(子アカウント)は、予め Stripe 上で登録されている必要があります。

/*
 Questetra BPMS V12.0 (released 2020-05-11)
 - configs.getObject()
 - engine.findData()
 - engine.setData()
*/

//////// START "main()" /////////////////////////////////////////////////////////////////

main();
function main(){ 

//// == Config Retrieving / 工程コンフィグの参照 ==
const strSecretKey     = configs.get( "strSetConfA"  ); // required
const strCustomerId    = configs.get( "strSetConfB"  ); // required
let   strChargeAmount  = configs.get( "strSetConfC1" ); // required
const strCurrencyCode  = configs.get( "strSetConfC2" ); // required
const strChargeDescr   = configs.get( "strSetConfC3" ); // required
const strTransferDest   = configs.get( "strSetConfC4" ); // required
let   strTransferAmount = configs.get( "strSetConfC5" ); // required
const pocketChargeId   = configs.getObject( "SelectConfD1" ); // not
const pocketCardBrand  = configs.getObject( "SelectConfD2" ); // not
const pocketCardLast4  = configs.getObject( "SelectConfD3" ); // not
const pocketCardExp    = configs.getObject( "SelectConfD4" ); // not *STRING/YMDATE

if( strSecretKey === "" ){
  throw new Error( "\n AutomatedTask ConfigError:" +
                   " Config {SecretKey A} is empty \n" );
}
if( strCustomerId === "" ){
  throw new Error( "\n AutomatedTask ConfigError:" +
                   " Config {Customer ID B} is empty \n" );
}
if( strChargeAmount === "" ){
  throw new Error( "\n AutomatedTask ConfigError:" +
                   " Config {ChargeAmount C1} is empty \n" );
}
if( strCurrencyCode === "" ){
  throw new Error( "\n AutomatedTask ConfigError:" +
                   " Config {CurrencyCode C2} is empty \n" );
}
if( strChargeDescr === "" ){
  throw new Error( "\n AutomatedTask ConfigError:" +
                   " Config {ChargeDescription C3} is empty \n" );
}
if( strTransferDest === "" ){
  throw new Error( "\n AutomatedTask ConfigError:" +
                   " Config {TransferDestination C4} is empty \n" );
}
if( strTransferAmount === "" ){
  throw new Error( "\n AutomatedTask ConfigError:" +
                   " Config {TransferAmount C5} is empty \n" );
}

strChargeAmount = strChargeAmount.replace(/,/g, '').replace(/\./g, '');
let numChargeAmount = parseInt( strChargeAmount, 10 );
if( numChargeAmount <= 0 ){
  throw new Error( "\n AutomatedTask ConfigError:" +
                   " Config {ChargeAmount C1} must be positive \n" );
}
strChargeAmount = numChargeAmount + "";

strTransferAmount = strTransferAmount.replace(/,/g, '').replace(/\./g, '');
let numTransferAmount = parseInt( strTransferAmount, 10 );
if( numTransferAmount <= 0 ){
  throw new Error( "\n AutomatedTask ConfigError:" +
                   " Config {TransferAmount C5} must be positive \n" );
}
strTransferAmount = numTransferAmount + "";


//// == Data Retrieving / ワークフローデータの参照 ==
// (nothing, except Expression Language config)


//// == Calculating / 演算 ==
/// POST /v1/charges
// https://stripe.com/docs/api/charges/create

// preparing for API Request
let apiUri = "https://api.stripe.com/v1/charges";
let apiRequest = httpClient.begin(); // HttpRequestWrapper
    apiRequest = apiRequest.basic( strSecretKey, "" );
    apiRequest = apiRequest.formParam( "customer",    strCustomerId   );
    apiRequest = apiRequest.formParam( "amount",      strChargeAmount );
    apiRequest = apiRequest.formParam( "currency",    strCurrencyCode );
    apiRequest = apiRequest.formParam( "description", strChargeDescr  );
    apiRequest = apiRequest.formParam( "transfer_data[amount]", strTransferAmount );
    apiRequest = apiRequest.formParam( "transfer_data[destination]", strTransferDest );


// throwing Request to the API (POST, GET, PUT, etc)
engine.log( " AutomatedTask Trying: POST " + apiUri );
const response = apiRequest.post( apiUri );
const responseCode = response.getStatusCode() + "";
engine.log( " AutomatedTask ApiResponse: Status " + responseCode );
if( responseCode !== "200"){
  throw new Error( "\n AutomatedTask UnexpectedResponseError: " +
                    responseCode + "\n" + response.getResponseAsString() + "\n" );
} // C.F. https://stripe.com/docs/api/errors

// parsing Response Json
const responseStr = response.getResponseAsString() + "";
//engine.log( " DEBUG for api upgrade: \n" + responseStr );
const responseObj = JSON.parse( responseStr );
engine.log( " AutomatedTask ApiResponse:" +
            " New ChargeObject ID: " + responseObj.id );
engine.log( " AutomatedTask ApiResponse:" +
            " Card Brand: " + responseObj.source.brand );


//// == Data Updating / ワークフローデータへの代入 ==
if( pocketChargeId !== null ){ // STRING
  engine.setData( pocketChargeId, responseObj.id );
}
if( pocketCardBrand !== null ){ // STRING
  engine.setData( pocketCardBrand, responseObj.source.brand );
}
if( pocketCardLast4 !== null ){ // STRING
  engine.setData( pocketCardLast4, responseObj.source.last4 );
}
if( pocketCardExp !== null ){ // STRING or YMDATE
  if( pocketCardExp.matchDataType( "STRING" ) ){
    engine.setData( pocketCardExp, 
         ("0" + responseObj.source.exp_month).slice(-2) + "/" +
         responseObj.source.exp_year );
  }else{
    engine.setData( pocketCardExp, 
         java.sql.Date.valueOf(
           responseObj.source.exp_year + "-" +
           ("0" + responseObj.source.exp_month).slice(-2) + "-01"
         )
    );
  }
}

} //////// END "main()" /////////////////////////////////////////////////////////////////

Download

Capture

Creates the Charge object with Transfer Amount to charge the Customer ID for any amount in any currency. Any Connected Account as the Transfer Destination, any Transfer Amount within the Charge amount are available. If fails, the object not created.

Notes

  1. A positive integer representing how much to charge in the smallest currency unit.
    1. e.g., “100” cents to charge $1.00
    2. e.g., “100” to charge \100 (a zero-decimal currency)
  2. “#{#q_numUsdWithoutCent}00” is also possible if numerical data without auxiliary info
  3. If present, commas “,” and periods “.” are removed in advance.
    1. When using numeric data, be careful of the number of digits after the decimal point
  4. Numeric parsing depends on JavaScript parseInt(x,10). (The prefix gives an error)
  5. See below for more information on available currency codes (JPY, USD, EUR, …)
    1. https://stripe.com/docs/currencies
  6. In case of USD, the minimum amount is $0.50 or equivalent.
  7. The amount value supports up to eight digits (e.g., for a USD charge of $999,999.99).
  8. Refer to the following for the error codes (ResponseCode other than 200).
    1. https://stripe.com/docs/api/errors
  9. Transfer Amount is also expressed in the smallest currency unit. (Positive integer)
    1. Exceeding the Charge amount will result in an error
    2. destination[amount] must be less than or equal to the charge amount
  10. Charges by the Connect Destination method (for Express/Custom type)
    1. https://stripe.com/docs/connect/destination-charges#transfer-amount
    2. Connected Account (child account) must be registered in advance on Stripe.

See also

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: