Created
March 30, 2018 23:36
-
-
Save iloveitaly/3044a736efc9ff749dbfd70417844dfd 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: Generates the Stripe Payment Link for Estimates, or other records in NetSuite | |
Link: | |
Installation: | |
Create Custom Field: | |
1. https://dashboard.suitesync.io/docs/netsuite-configuration#creating-the-stripe-quote-payment-link-custom-field | |
2. Make sure the field has `Store Value` checked and is a free-form text field | |
Create Script Deployment: | |
1. https://system.sandbox.netsuite.com/app/common/scripting/uploadScriptFile.nl | |
2. User Event | |
3. Name: Stripe Payment Link Creator | |
4. ID: _create_custom_payment_link | |
5. After Submit: afterSubmit | |
6. Deployments: Estimate, or other records. Event Type: Create. Ensure accessible to all roles and executes as admin. | |
* Note you cannot create a deployment for the SO & Invoice at the same time when creating the script record. | |
NetSuite will throw an error. You must create the deployments for these separately | |
7. Update `STRIPE_ACCOUNT_ID` in this script to match your Stripe account ID | |
*/ | |
var STRIPE_ACCOUNT_ID = 'acct_'; | |
var BASE_URL = 'https://app.suitesync.io/customers/'; | |
var STRIPE_PAYMENT_LINK_FIELD = 'custbody_stripe_quote_payment_link'; | |
function log(msg) { | |
nlapiLogExecution('DEBUG', msg); | |
} | |
function afterSubmit() { | |
if(type != 'create' && type != 'edit') { | |
return; | |
} | |
var recordType = nlapiGetRecordType(); | |
var recordId = nlapiGetRecordId(); | |
var paymentLink = generateStripePayment(recordType, recordId); | |
nlapiSubmitField(recordType, recordId, STRIPE_PAYMENT_LINK_FIELD, paymentLink); | |
} | |
function generateStripePaymentLinkFromRecord(nsRecord) { | |
return generateStripePayment(nsRecord.getRecordType(), nsRecord.getId()); | |
} | |
function generateStripePayment(recordType, recordId) { | |
if(recordType == 'estimate') { | |
return generateStripeEstimatePaymentLink(recordId) | |
} else { | |
// NOTE you can add | |
return null; | |
} | |
} | |
function generateStripeEstimatePaymentLink(estimateId) { | |
var customerId = nlapiLookupField('estimate', estimateId, 'entity', false) | |
var baseLink = generateCustomerAccountLink(customerId); | |
var amount = Math.round(parseFloat(nlapiLookupField('estimate', estimateId, 'tranid', false)) * 100.0); | |
var memo = nlapiLookupField('estimate', estimateId, 'tranid', false); | |
return baseLink + '?memo=' + memo + '&amount=' + amount; | |
} | |
function generateCustomerAccountLink(customerId) { | |
var isSandboxEnvironment = nlapiGetContext().getEnvironment() != 'PRODUCTION'; | |
var customerSecret = nlapiStringToDate(nlapiLookupField('customer', customerId, 'datecreated', false)).getTime() / 1000 | |
return BASE_URL + (isSandboxEnvironment ? 'test/' : '') + STRIPE_ACCOUNT_ID + '/' + customerId + '/' + customerSecret; | |
} | |
if(nlapiGetContext().getExecutionContext() == 'debugger') { | |
log(generateStripePaymentLinkFromRecord(nlapiLoadRecord('estimate', 15465))); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment