Skip to content

Instantly share code, notes, and snippets.

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/aced3081f924651891a44c474625976d to your computer and use it in GitHub Desktop.
Save iloveitaly/aced3081f924651891a44c474625976d to your computer and use it in GitHub Desktop.
/*
Author: <mike@suitesync.io>
Description: Specifies which Stripe account should be used on a transaction based on custom business logic
Link: https://gist.github.com/iloveitaly/aced3081f924651891a44c474625976d
Installation:
Create Custom Field:
1. [Customization > Transaction Body Fields > New](https://system.sandbox.netsuite.com/app/common/custom/bodycustfields.nl)
2. Create a new field to appear on SalesOrders and Invoices.
* Label: "Stripe Account ID"
* Description: "Field to contain the Stripe account ID to use for this transaction"
* ID: `_stripe_account_id`
* Type: free form text
* Store Value: true
* Applies to: Sale
3. Save. Generated ID: `custrecord_stripe_account_id`
Create Script Deployment:
1. https://system.sandbox.netsuite.com/app/common/scripting/uploadScriptFile.nl
2. User Event
3. Name: Custom Stripe Account Mapping
4. ID: _stripe_account_mapping
5. After Submit: afterSubmit
6. Deployments: Invoice & SalesOrder. Event Type: Create. Ensure accessible to all roles and executes as admin.
* Note you cannot create a deployment for the SO & Invoice when creating the script record. NetSuite will throw an error.
7. Update STRIPE_ACCOUNT_MAPPING and `determineMappingKey` to match your business logic
*/
function log(msg) {
nlapiLogExecution('DEBUG', msg);
}
function isEmpty(obj) {
return obj === undefined || obj === null || obj === "";
}
var STRIPE_ACCOUNT_MAPPING = {
'USD': 'acct_1',
'EUR': 'acct_2'
};
function afterSubmit(type) {
if(type != 'create' && type != 'edit') {
return
}
var recordType = nlapiGetRecordType();
var recordId = nlapiGetRecordId();
processAfterSubmit(recordType, recordId);
}
function processAfterSubmit(recordType, recordId) {
var accountMappingKey = determineMappingKey(recordType, recordId);
if(isEmpty(accountMappingKey)) {
log("stripe account mapping key is empty");
return
}
nlapiSubmitField(recordType, recordId, 'custbody_stripe_account_id', STRIPE_ACCOUNT_MAPPING[accountMappingKey]);
}
// NOTE this method should be customized to fit the unique business logic you are using to determine which Stripe account should be used
// in this example, the currency ISO of the transaction is used as the mapping key
function determineMappingKey(recordType, recordId) {
var currencyId = nlapiLookupField(recordType, recordId, 'currency', false);
if(isEmpty(currencyId)) {
return
}
var currencyISO = nlapiLookupField('currency', currencyId, 'symbol');
if(isEmpty(currencyISO)) {
return
}
return currencyISO.toUpperCase();
}
if(nlapiGetContext().getExecutionContext() == 'debugger') {
processAfterSubmit('invoice', 5127407)
}
'debug';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment