Last active
February 5, 2018 14:34
-
-
Save iloveitaly/4a632d62424d093bcda9e7a6da916658 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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