PayPal Invoicing Status
Stores the Paypal Invoice ID status specified by a String-type Data Item, and once paid, stores the payment date, time and amount in a Data Item.
2017 (C) Questetra, Inc. (MIT License)
Configs
  • A: Set CLIENT-ID and SECRET for PayPal-REST-API (2-lines) *
  • B: Select STRING DATA for Paypal Invoice ID *
  • C: Select STRING DATA for Paypal Invoice Status (update) *
  • D: Select STRING DATA for Payment Timestamp (update)
  • E: Select NUMBER DATA for Payment Amount (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 dataIdB = configs.get( "conf_DataIdB" );
var dataIdC = configs.get( "conf_DataIdC" ) + "";
var dataIdD = configs.get( "conf_DataIdD" ) + "";
var dataIdE = configs.get( "conf_DataIdE" ) + "";


//// == ワークフローデータの参照 / Data Retrieving ==
var paypalId         = data.get( dataIdB ) + "";


//// == 演算 / Calculating ==
// 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 oauthTokenObj = JSON.parse( response.getResponseAsString() );
var oauthToken = oauthTokenObj.access_token;

// Show Invoice Details
var uriDetail = "https://api.paypal.com/v1/invoicing/invoices/" + paypalId;
var responseDetail = httpClient.begin()
  .bearer( oauthToken )
  .get( uriDetail );
var paypalInvoiceObj = JSON.parse( responseDetail.getResponseAsString() );
var paypalInvoiceStatus = paypalInvoiceObj.status;


//// == ワークフローデータへの代入 / Data Updating ==
retVal.put( dataIdC, paypalInvoiceStatus ); // DRAFT, SENT, PAID, etc
if( dataIdD !== "" ){
  if ( paypalInvoiceStatus == "PAID" ){
    retVal.put( dataIdD, paypalInvoiceObj.payments[0].date );
  }
}
if( dataIdE !== "" ){
  if ( paypalInvoiceStatus == "PAID" ){
    var amountValue = parseFloat( paypalInvoiceObj.payments[0].amount.value );
    retVal.put( dataIdE, java.math.BigDecimal( amountValue ) );
  }
}
///retVal.put( "q_accessLog", responseDetail.getResponseAsString() ); // for debug

Download

  • Paypal-status.xml
    • Since Rhino (deprecated) is specified as the script engine, a setting error will occur even if you install this in a workflow App
    • To use this Add-on, you need to change the script engine and modify the script accordingly
    • A modified version is in preparation

Capture

Notes

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

See also

%d bloggers like this: