0

Used AMPScript and SSJS to generate an encrypted JSON message and pass this encryped JSON (payload) to the external system using a URL. However, the last line in the below code is not picking the URL. Cannot understand what's going wrong here -

%%[ const crypto, var @data, @contentType, @payload, @encryptkeybase64, @authkeybase64,  @domain,  @response, @req, @resp, @encryptionKey, @authenticationKey, @cipher, @cipherText, @hmac, @base64Payload, @url, @iv, @ssconsole]%%


<script runat="server">

Platform.Load("core","1.1.1");

var contentType = 'application/x-www-form-urlencoded';
var encryptkeybase64  = '3kXLV1rnimLRdsdfsdfsdf2Wdd+/hYVTB/Dt6nnIk=';
var authkeybase64 = 'BKulGe3CTrweSHkrwerwerwerdNpFAeuuWhfevc4A=';
var domain = 'hughes.com';
const crypto = require('crypto');

var order   = {
                name : "Nishanth M",
                email : "[email protected]",
                ref : "12345"
};

 var jsonSerializedOrder  = JSON.stringify(order);

// When you get the keys from Trustpilot, they are base64 encoded, so first we need to decode them
var encryptionKey = Buffer.from(encryptionKeyBase64, 'base64');
var authenticationKey = Buffer.from(authenticationKeyBase64, 'base64');

  // Generate a random initialization vector
var iv = crypto.randomBytes(16);

  // Encrypt our order
var cipher = crypto.createCipheriv('aes-256-cbc', encryptionKey, iv);
var cipherText = Buffer.concat([cipher.update(jsonSerializedOrder, 'utf8'), cipher.final()]);

// Compute the HMAC
var hmac = crypto.createHmac('sha256', authenticationKey).update(Buffer.concat([iv, cipherText])).digest();

// Base64 encode the IV + cipherText + HMAC
var base64Payload = Buffer.concat([iv, cipherText, hmac]).toString("base64");

// URL encode to get the final payload
var payload = encodeURIComponent(base64Payload);
var url = 'https://www.trustpilot.com/evaluate-bgl/' + domain + '?p=' + payload;  
  Write(url);
  console.log("Something happened");

  try {
  var req = HTTP.Post(url, contentType, payload);



    Write(req);

 if(req.StatusCode == 200) {
 var resp = Platform.Function.ParseJSON(String(req.Response[0]));
  Write(resp.Message);
  var accessToken = tokenResponse.accessToken;
   Write(accessToken);
 }
 } catch (ex) {
          Write("Exception Error: " + Stringify(ex));
          } 

  Variable.SetValue("@url",url);
</script>

<a href="%%=RedirectTo(@url)=%%">Test</a>

2 Answers 2

1

I hope the issue is NOT with SSJS or AMPscript! Please find the minified version of your code, where in I tested and it worked.

<script runat="server">
Platform.Load("core","1.1.1");

var url = 'https://www.trustpilot.com/evaluate-bgl/' + 'testDOMAIN' + '?p=' + 'testPayload';  
  Write(url);
  Variable.SetValue("@OutputToConsole",url); //you cannot directly output to console, as this will output to server console NOT browser console
  Variable.SetValue("@url",url);
</script>

<script runat="client">
console.log(`%%=v(@OutputToConsole)=%%`); //Here this outputs to Browser console
</script>

<a href="%%=RedirectTo(@url)=%%">Test</a>

Note: I added the technique to log to browser console from SSJS, use this technique to debug your code.

1
  • Appreciate for taking time to look into this and thank you so much Vishal .. this worked !! As you rightly pointed out, this was always hitting server console before.
    – Nishanth
    Commented Dec 18, 2019 at 23:06
1

Try it without the @:

Variable.SetValue("url",url);

Here’s the help article with an example: https://developer.salesforce.com/docs/atlas.en-us.mc-programmatic-content.meta/mc-programmatic-content/ssjs_utilitiesVariableSetValue.htm

1
  • I tried it but the URL never receives the value.
    – Nishanth
    Commented Dec 17, 2019 at 23:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .