Skip to content

Instantly share code, notes, and snippets.

@iloveitaly
Last active February 5, 2018 14:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iloveitaly/4a632d62424d093bcda9e7a6da916658 to your computer and use it in GitHub Desktop.
Save iloveitaly/4a632d62424d093bcda9e7a6da916658 to your computer and use it in GitHub Desktop.
/*
Author: <mike@suitesync.io>
Description: Sets the paymenet method to blank if a Stripe transaction ID exists
Link: https://gist.github.com/iloveitaly/4a632d62424d093bcda9e7a6da916658
Installation:
1. https://system.sandbox.netsuite.com/app/common/scripting/uploadScriptFile.nl
2. User Event
3. Name: Eliminate Stripe Payment Method if a Stripe Transaction ID exists
4. ID: _suitesync_salesorder_set_pm
5. Before Submit: beforeSubmit
6. Deployments: SalesOrder. Event Type: Create. Ensure accessible to all roles and executes as admin.
*/
// Utils
// https://gist.github.com/iloveitaly/db7d532e772b67f5b81d0199d094301f
function isUserInterfaceContext() {
var context = nlapiGetContext();
var executionContext = context.getExecutionContext();
return executionContext == 'userinterface';
}
// we are in the dark ages folks: no native startsWith
function startsWith(str, searchString) {
return str.indexOf(searchString) === 0;
}
function isZero(obj) {
return parseFloat(obj) == 0.0
}
function log(msg) {
nlapiLogExecution('DEBUG', msg);
}
function error(msg) {
nlapiLogExecution('ERROR', msg);
}
function isEmpty(obj) {
return obj === undefined || obj === null || obj === "";
}
// End Utils
var STRIPE_TRANSACTION_ID_FIELD = 'custbody_suitesync_authorization_code'
// NOTE beforeSubmit is used to ensure
function beforeSubmit(type) {
if(type == 'create') {
if(nlapiGetRecordType() == 'salesorder') {
var stripeTransactionId = nlapiGetFieldValue(STRIPE_TRANSACTION_ID_FIELD);
if(!isEmpty(stripeTransactionId)) {
// NOTE it's possible (but not probable) that Stripe could add more transaction ID prefixes in the future
// if they do, they will be associated with new payment method types. This approach should only break
// if you use additional payment method types AND Stripe charges the prefix used
if(startsWith(stripeTransactionId, 'ch_') || startsWith(stripeTransactionId, 'py_')) {
nlapiSetFieldValue('paymentmethod', null, false);
} else {
log("non-stripe transaction ID detected for SO " + nlapiGetRecordId() + ", removing: " + stripeTransactionId)
nlapiSetFieldValue(STRIPE_TRANSACTION_ID_FIELD, null, false);
}
}
} else {
log("record type is not sales order, this script should only be run ");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment