// Nashorn Script (engine type: 1)
//
// Notes:
// Token is generated by JavaScript on the browser.
// - "Checkout" (forms hosted on Stripe) https://stripe.com/docs/payments/checkout
// - "Elements" (custom payment forms) https://stripe.com/docs/stripe-js
// - If M213-decoration "jQuery.getScript('https://js.stripe.com/v3/', function(){..."
// -- https://questetra.zendesk.com/hc/en-us/articles/360002245252-M213
// The card number etc is deposited at Stripe, and only the token flows in the workflow.
// Token is converted to Customer object and continuous billing becomes possible.
// There is no way to incorporate sensitive information into workflow data.
// - PAN: Primary Account Number
// - PIN: Personal Identification Number
// - CVC: Card Verification Code/Value (3 digit number, to be exact CVC2, CID)
// * also on the Stripe Dashboard (Non-retention of card information)
// Refer to the following for the error codes (ResponseCode other than 200).
// - https://stripe.com/docs/api/errors
// Notes(ja):
// Tokenはブラウザ上のJavaScriptによって生成されます。
// - "Checkout" (forms hosted on Stripe) https://stripe.com/docs/payments/checkout
// - "Elements" (custom payment forms) https://stripe.com/docs/stripe-js
// - M213デコの場合 "jQuery.getScript('https://js.stripe.com/v3/', function(){..."
// -- https://questetra.zendesk.com/hc/ja/articles/360002245252-M213
// カード番号等はStripe社に預けられ、フローには「預かり番号」としてのTokenを流します。
// TokenはCustomerオブジェクト(Customer ID)に変換され、継続課金が可能な状態となります。
// センシティブ情報をワークフローデータに取り込む方法はありません。(カード情報の非保持)
// - PAN:Primary Account Number (カード番号)
// - PIN:Personal Identification Number (暗証番号)
// - CVC: Card Verification Code/Value (3桁数字/正確にはCVC2/CIDとも)
// ※Stripe管理画面からも取得できません。(カード情報の非保持)
// エラーコード(200以外のResponseCode)の内容については以下を参照してください。
// - https://stripe.com/docs/api/errors
/*
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 strTokenId = configs.get( "strSetConfB" ); // required
const strCustomerEmail = configs.get( "strSetConfC1" ); // required
const strCustomerDescr = configs.get( "strSetConfC2" ); // required
const strCustomerName = configs.get( "strSetConfC3" ); // not
const pocketCustomerId = configs.getObject( "SelectConfD1" ); // required
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( strTokenId === "" ){
throw new Error( "\n AutomatedTask ConfigError:" +
" Config {Token ID B} is empty \n" );
}
if( strCustomerEmail === "" ){
throw new Error( "\n AutomatedTask ConfigError:" +
" Config {CustomerEmail C1} is empty \n" );
}
if( strCustomerDescr === "" ){
throw new Error( "\n AutomatedTask ConfigError:" +
" Config {CustomerDescription C2} is empty \n" );
}
//// == Data Retrieving / ワークフローデータの参照 ==
// (nothing, except Expression Language config)
//// == Calculating / 演算 ==
/// POST /v1/customers
// https://stripe.com/docs/api/customers/create
// curl https://api.stripe.com/v1/customers \
// -u sk_test_4eC39HqLyjWDarjtT1zdp7dc: \
// -d description="My First Test Customer (created for API docs)"
// preparing for API Request
let apiUri = "https://api.stripe.com/v1/customers";
let apiRequest = httpClient.begin(); // HttpRequestWrapper
apiRequest = apiRequest.basic( strSecretKey, "" );
apiRequest = apiRequest.formParam( "source", strTokenId );
apiRequest = apiRequest.formParam( "email", strCustomerEmail );
apiRequest = apiRequest.formParam( "description", strCustomerDescr );
apiRequest = apiRequest.formParam( "name", strCustomerName );
// 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 CustomerObject ID: " + responseObj.id );
engine.log( " AutomatedTask ApiResponse:" +
" Card Brand: " + responseObj.sources.data[0].brand );
//// == Data Updating / ワークフローデータへの代入 ==
engine.setData( pocketCustomerId, responseObj.id );
if( pocketCardBrand !== null ){ // STRING
engine.setData( pocketCardBrand, responseObj.sources.data[0].brand );
}
if( pocketCardLast4 !== null ){ // STRING
engine.setData( pocketCardLast4, responseObj.sources.data[0].last4 );
}
if( pocketCardExp !== null ){ // STRING or YMDATE
if( pocketCardExp.matchDataType( "STRING" ) ){
engine.setData( pocketCardExp,
("0" + responseObj.sources.data[0].exp_month).slice(-2) + "/" +
responseObj.sources.data[0].exp_year );
}else{
engine.setData( pocketCardExp,
java.sql.Date.valueOf(
responseObj.sources.data[0].exp_year + "-" +
("0" + responseObj.sources.data[0].exp_month).slice(-2) + "-01"
)
);
}
}
} //////// END "main()" /////////////////////////////////////////////////////////////////
Pingback: Stripe Customer ID Create from Token – Questetra Support
Pingback: Card Info to Stripe (MM/YY edition) – Questetra Support
Pingback: Card Info to Stripe – Questetra Support
Pingback: Stripe: Charge Object, Create – Questetra Support
Pingback: Stripe: Customer, Create – Questetra Support