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
// Link: https://gist.github.com/iloveitaly/0e6d012d8dffa2444e0b36c0ec7b5781 | |
define([ 'N/search', 'N/record', 'N/runtime', 'N/error', 'N/https' ], | |
function(search, record, runtime, error, https) { | |
var STRIPE_KEY = 'sk_test_123'; | |
var STRIPE_ENDPOINT = 'https://api.stripe.com/v1/'; | |
function afterSubmit(context) { | |
if(context.type !== context.UserEventType.UPDATE) { | |
return | |
} | |
linkStripeCustomer(context.currentRecord.id); | |
} | |
function linkStripeCustomer(customerInternalId) { | |
// https://stoic.software/effective-suitescript/7-field-lookups-in-ss2/ | |
var stripeCustomerLookupKey = 'externalId'; | |
var entityData = search.lookupFields({ | |
type: s.Type.CUSTOMER, | |
id: customerInternalId, | |
columns: [ stripeCustomerLookupKey ] | |
}) | |
var stripeCustomerId = entityData[stripeCustomerLookupKey]; | |
if(isEmpty(stripeCustomerId) || startWith('cus_', stripeCustomerId)) { | |
log.debug("not a valid stripe customer ID"); | |
return | |
} | |
var headers = {}; | |
headers['Accept'] = 'application/json'; | |
headers['Authorization'] = STRIPE_KEY | |
var requestURL = STRIPE_ENDPOINT + 'customers/' + stripeCustomerId; | |
var params = ''; | |
// You'll want to assume that Stripe, NetSuite, or the network will fail. | |
// In other words, you'll want this function to be deterministic. The best way to do this | |
// https://stripe.com/docs/api#idempotent_requests | |
var idempotencyKey = recordType + "-" + customerInternalId; | |
params += "idempotency_key=" + encodeURIComponent(idempotencyKey) + "&"; | |
params += "metadata[netsuite_customer_id]=" + encodeURIComponent(stripeCustomerId) + "&"; | |
response = https.request({ | |
method : https.Method.POST, | |
url : requestURL, | |
body : params, | |
headers : headers | |
}); | |
var responseCode = response.code; | |
var responseBody = response.body; | |
// TODO check if successful response and update field on SO if processed successfully | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment