PayPal Invoicing Create (USD)
Generates Paypal invoice automatically from the setting item information, and stores Paypal Invoice ID in a String type data item.
2018 (C) Questetra, Inc. (MIT License)
Configs
  • A: Set CLIENT-ID and SECRET for PayPal-REST-API (2-lines) *
  • B: Select STRING DATA for Access Log (update)
  • C: Set BisNAME/COUNTRY-CODE/POST/Pref/City for Merchant (5-lns) *
  • D: Set ADDRESS for Merchant (2-lines) *
  • E: Set PHONE-COUNTRY-CODE/NUMBER/EMAIL/URL for Merchant (4-lns) *
  • F: Select STRING DATA for Billing Business Name *
  • G: Select STRING DATA for Billing Name1 *
  • H: Select STRING DATA for Billing Name2 *
  • I: Select STRING DATA for Billing Email *
  • J: Select STRING DATA for Item Name *
  • K: Select STRING DATA for Item Description *
  • L: Select NUMERIC DATA for Item Quantity *
  • M: Select NUMERIC DATA for Unit Price *
  • N: Set ItemTax Percent (e.g. “8” “10”) *
  • O: Select STRING DATA for Invoice ID *
  • P: Select DATE DATA for Payment Due Date *
  • Q: Select STRING DATA for Invoice Note *
  • R: Select STRING DATA for Invoice Terms *
  • S: Set Logo Url (e.g. https://example.com/a.png) *
  • T: Select STRING DATA for Paypal Invoice ID (update) *
Script
//// == 工程コンフィグの参照 / Config Retrieving ==
var clientId_secret = configs.get( "conf_ClientId_Secret" ) + "";
var array_clientId_secret = clientId_secret.split("\n");
var clientId = array_clientId_secret[0];
var secret   = array_clientId_secret[1];

var merchantPostalCode   = configs.get( "conf_MerchantPostalCode" ) + "";
var array_merchantPostalCode = merchantPostalCode.split("\n");
var merchantBusinessName = array_merchantPostalCode[0];
var merchantAddressCC    = array_merchantPostalCode[1];
var merchantAddressPC    = array_merchantPostalCode[2];
var merchantAddressState = array_merchantPostalCode[3];
var merchantAddressCity  = array_merchantPostalCode[4];

var merchantAddress      = configs.get( "conf_MerchantAddress" ) + "";
var array_merchantAddress = merchantAddress.split("\n");
var merchantAddress1     = array_merchantAddress[0];
var merchantAddress2     = array_merchantAddress[1];

var merchantPhoneEmail   = configs.get( "conf_MerchantPhoneEmail" ) + "";
var array_merchantPhoneEmail  = merchantPhoneEmail.split("\n");
var merchantPhoneCC      = array_merchantPhoneEmail[0];
var merchantPhoneNum     = array_merchantPhoneEmail[1];
var merchantEmail        = array_merchantPhoneEmail[2];
var merchantWebsite      = array_merchantPhoneEmail[3];

var billingLanguage      = "en_US"; /// HARD-CODED
var itemUnitPriceCurrency = "USD"; /// HARD-CODED
var itemTaxName          = "TAX"; /// HARD-CODED
var itemTaxPercent       = configs.get( "conf_ItemTax" ) - 0;
var logoUrl              = configs.get( "conf_logoUrl" ) + "";

var dataIdB = configs.get( "conf_DataIdB" ) + "";
var dataIdF = configs.get( "conf_DataIdF" ) + "";
var dataIdG = configs.get( "conf_DataIdG" ) + "";
var dataIdH = configs.get( "conf_DataIdH" ) + "";
var dataIdI = configs.get( "conf_DataIdI" ) + "";
var dataIdJ = configs.get( "conf_DataIdJ" ) + "";
var dataIdK = configs.get( "conf_DataIdK" ) + "";
var dataIdL = configs.get( "conf_DataIdL" ) + "";
var dataIdM = configs.get( "conf_DataIdM" ) + "";

var dataIdO = configs.get( "conf_DataIdO" ) + "";
var dataIdP = configs.get( "conf_DataIdP" ) + "";
var dataIdQ = configs.get( "conf_DataIdQ" ) + "";
var dataIdR = configs.get( "conf_DataIdR" ) + "";
var dataIdT = configs.get( "conf_DataIdT" ) + "";


//// == ワークフローデータの参照 / Data Retrieving ==
var billingBusinessName  = engine.findDataByNumber( dataIdF ) + ""; // Max 100
var billingName1         = engine.findDataByNumber( dataIdG ) + ""; // Max 30
var billingName2         = engine.findDataByNumber( dataIdH ) + ""; // Max 30
var billingEmail         = engine.findDataByNumber( dataIdI ) + ""; // Max 260
var itemName             = engine.findDataByNumber( dataIdJ ) + ""; // Max 200
var itemDescription      = engine.findDataByNumber( dataIdK ) + ""; // Max 1000
var itemQuantity         = engine.findDataByNumber( dataIdL ) - 0;
var itemUnitPriceNum     = engine.findDataByNumber( dataIdM ) - 0;
var itemUnitPrice        = itemUnitPriceNum + ""; // STRING!! without Comma

var invoiceNumber        = engine.findDataByNumber( dataIdO ) + ""; // Max 25
var paymentTermDueDate   = engine.findDataByNumber( dataIdP ) + " PST";
var invoiceNote          = engine.findDataByNumber( dataIdQ ) + ""; // Max 4000
var invoiceTerms         = engine.findDataByNumber( dataIdR ) + ""; // Max 4000

var merchantMemo = "BPMS Process ID: " + processInstance.getProcessInstanceId();


//// == 演算 / Calculating ==
var accessLog = "";

// Get OAuth Token with Client Credentials
var uri = "https://api.paypal.com/v1/oauth2/token";
var response = httpClient.begin()
  .basic( clientId, secret )
  .formParam( "grant_type", "client_credentials" )
  .post( uri );
var httpStatus = response.getStatusCode() + "";
accessLog = "---POST request--- " + httpStatus + "\n";
accessLog += response.getResponseAsString() + "\n";
// Error Handling
if( httpStatus !== "200" ){
  throw new Error( accessLog );
}

var oauthTokenObj = JSON.parse( response.getResponseAsString() );
var oauthToken = oauthTokenObj.access_token;


// Create invoice
var invoiceObj = {};
  invoiceObj.number = invoiceNumber;
  invoiceObj.payment_term = {};
  invoiceObj.payment_term.due_date = paymentTermDueDate;
  invoiceObj.note = invoiceNote;
  invoiceObj.terms = invoiceTerms;
  invoiceObj.logo_url = logoUrl;
  invoiceObj.merchant_memo = merchantMemo;
  invoiceObj.merchant_info = {};
    invoiceObj.merchant_info.email = merchantEmail;
    invoiceObj.merchant_info.business_name = merchantBusinessName;
    invoiceObj.merchant_info.address = {};
      invoiceObj.merchant_info.address.line1 = merchantAddress1;
      invoiceObj.merchant_info.address.line2 = merchantAddress2;
      invoiceObj.merchant_info.address.city = merchantAddressCity;
      invoiceObj.merchant_info.address.state = merchantAddressState;
      invoiceObj.merchant_info.address.postal_code = merchantAddressPC;
      invoiceObj.merchant_info.address.country_code = merchantAddressCC;
      invoiceObj.merchant_info.address.phone = {};
        invoiceObj.merchant_info.address.phone.country_code = merchantPhoneCC;
        invoiceObj.merchant_info.address.phone.national_number = merchantPhoneNum;
    invoiceObj.merchant_info.website = merchantWebsite;
  invoiceObj.billing_info = [];
    invoiceObj.billing_info[0] = {};
      invoiceObj.billing_info[0].email = billingEmail;
      invoiceObj.billing_info[0].first_name = billingName1;
      invoiceObj.billing_info[0].last_name = billingName2;
      invoiceObj.billing_info[0].business_name = billingBusinessName;
      invoiceObj.billing_info[0].language = billingLanguage;
  invoiceObj.items = [];
    invoiceObj.items[0] = {};
      invoiceObj.items[0].name = itemName;
      invoiceObj.items[0].description =itemDescription;
      invoiceObj.items[0].quantity = itemQuantity;
      invoiceObj.items[0].unit_price = {};
        invoiceObj.items[0].unit_price.currency = itemUnitPriceCurrency;
        invoiceObj.items[0].unit_price.value = itemUnitPrice;
      if( itemTaxPercent !== 0 ){
      invoiceObj.items[0].tax = {};
        invoiceObj.items[0].tax.name = itemTaxName;
        invoiceObj.items[0].tax.percent = itemTaxPercent;
      }
var uriCreate = "https://api.paypal.com/v1/invoicing/invoices/";
var responseCreate = httpClient.begin()
  .bearer( oauthToken )
  .body( JSON.stringify( invoiceObj ), "application/json" )
  .post( uriCreate );

var httpStatusCreate = responseCreate.getStatusCode() + "";
accessLog = "---POST request--- " + httpStatusCreate + "\n";
accessLog += responseCreate.getResponseAsString() + "\n";
// Error Handling
if( httpStatusCreate !== "201" ){
  throw new Error( accessLog );
}

var paypalIdObj = JSON.parse( responseCreate.getResponseAsString() );
var paypalId    = paypalIdObj.id;


//// == ワークフローデータへの代入 / Data Updating ==
engine.setDataByNumber( dataIdT, paypalId );
if ( dataIdB !== "" ){ engine.setDataByNumber( dataIdB, accessLog ); }

Download

Capture

Notes

  • You need to create App at Paypal Dashboard beforehand and obtain Client ID/Secret in advance

Workflow-Sample

%d bloggers like this: