Suitelet for creating SIgnature, nonce, timestamp for SOAP request

/**

 * @NApiVersion 2.x

 * @NScriptType Suitelet

 */

define([‘N/https’, ‘N/xml’, ‘N/record’, ‘N/log’, ‘N/crypto’],

    function (https, xml, record, log,crypto) {

        function generateNonce() {

            var nonceLength = 20; // Length of the nonce string

            var nonce = ;

            var chars = ‘ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789’;

            for (var i = 0; i < nonceLength; i++) {

                nonce += chars.charAt(Math.floor(Math.random() * chars.length));

            }

            return nonce;

        }

        function generateTimestamp() {

            return Math.floor(new Date().getTime() / 1000); // Convert milliseconds to seconds

        }

        /**

         * Generate a HMAC-SHA256 signature for the NetSuite SOAP request.

         *

         * @param {string} consumerKey – Your NetSuite consumer key.

         * @param {string} token – Your NetSuite token.

         * @param {string} nonce – The generated nonce for the request.

         * @param {string} timestamp – The generated timestamp.

         * @param {string} account – Your NetSuite account ID.

         * @param {string} consumerSecret – Your NetSuite consumer secret.

         * @param {string} tokenSecret – Your NetSuite token secret.

         * @returns {string} – The generated signature in base64 format.

         */

        function generateSignature(consumerKey, token, nonce, timestamp, account, consumerSecret, tokenSecret) {

            log.debug(“inputData”,{consumerKey:consumerKey,token:token,nonce:nonce,timestamp:timestamp,account:account,consumerSecret:consumerSecret,tokenSecret:tokenSecret});

            // Step 1: Create the signature base string

            var baseString = consumerKey + ‘&’ + token + ‘&’ + nonce + ‘&’ + timestamp + ‘&’ + account;

            // Step 2: Create the key by concatenating consumerSecret and tokenSecret with an “&”

            var key = consumerSecret + ‘&’ + tokenSecret;

            log.debug(“key”,key);

            // Step 3: Generate the HMAC-SHA256 signature

            // Step 3: Generate the HMAC-SHA256 signature using N/crypto

            var hmac = crypto.createHmac({

                algorithm: crypto.HashAlg.SHA256,

                key: key

            });

            log.debug(“hmachash”,hmac);

            hmac.update({

                input: baseString

            });

            // Step 4: Return the signature in base64 format

            var signature = hmac.digest({

                outputEncoding: crypto.Encoding.BASE_64

            });

            return signature;

        }

        function onRequest(context) {

            if (context.request.method === ‘GET’) {

                var consumerKey = ‘39958934127f030908411e2f522080280ba15cdf01c6c5998b4bf3b92dbf1e3a’;

                var token = ‘8a9f50d522c5e618d43f82a30f8974d54c4c636961d084cd9dee757a9eae0bb9’;

                var account = ‘6714807_SB1’; // Example account ID

                var consumerSecret = ‘5cff7c7d4aea8f1355cd4be0c17cc44cded806aae0e5f52b3a2e1922548c83b2’;

                var tokenSecret = ‘e3f0e04b3d14bad872b0cb5f965c1ade77eb39b01b92e5a24353e194c4a705b3’;

                // Generated nonce and timestamp

                var nonce = generateNonce();

                var timestamp = generateTimestamp();

                // Generate the signature

                var signature =generateSignature(consumerKey, token, nonce, timestamp, account, consumerSecret, tokenSecret);

                log.debug(‘Generated Signature’, signature);

                var url = ‘https://6714807-sb1.suitetalk.api.netsuite.com/services/NetSuitePort_2024_1’;

                var headers = {

                    ‘Content-Type’: ‘text/xml; charset=utf-8’,

                    ‘SOAPAction’: ‘search’

                };

                try {

                    var response = https.post({

                        url: url,

                        headers: headers,

                        body: soapRequest

                    });

                    log.debug(‘SOAP Response’, response.body);

                    // Respond with the SOAP response in the Suitelet

                    context.response.write({

                        output: response.body

                    });

                } catch (e) {

                    log.error(‘SOAP Request Error’, e.message);

                    context.response.write({

                        output: ‘Error: ‘ + e.message

                    });

                }

            }

        }

        return {

            onRequest: onRequest

        };

    });

Leave a comment

Your email address will not be published. Required fields are marked *