Encode String In Base64 Into Shopify

- 1 answer

How can I encode a string to base 64 in Shopify?

I need to encrypt into base64 the the buyer email ({{}}) and the order ({{order_name}})

The scope if for adding the trustedbadge*.de button into the email, and they are requesting the fallow url format:

www . trustedshops/buyerrating/rate_XFD9974BBC558C007CD46
431D056DF230.html&buyerEmail="[base64 buyerEmail]: "&
shopOrderID=" [base64 Order] "&channel=cmF0ZW5vd2J1dHRvbg"

Is there any way to convert those two values in base64?

Thank you for your time!



Base64 is not encryption -- it's known as an encoding.

There is no shopify liquid filter for base64 that I'm aware of, so you'll have to do this in javascript.

Liquid: HTML:

    <a target="_blank" rel="nofollow noreferrer" target="_blank" rel="nofollow noreferrer" href="#" onclick="'https://www.trustedshops/buyerrating/rate_XFD9974BBC558C007CD46431D056DF230.html&buyerEmail=' + encodeURIComponent(btoa({{ | json }})) + '&shopOrderID=' + encodeURIComponent(btoa('{{ order.order_number }}')) + '&channel=cmF0ZW5vd2J1dHRvbg')">
        Open order on

And this code just explains the javascript in that onclick.


    // The btoa function is the javascript function to encode a string as base64
    // Since base64 encoded strings can't go directly in a url, we then 
    // need to use encodeURIComponent to make it ok to use in a url.
    var encodedEmail = encodeURIComponent(btoa('[email protected]')); // aGVsbG9AZXhhbXBsZS5jb20%3D
    var encodedOrderId = encodeURIComponent(btoa('1234567890')); // MTIzNDU2Nzg5MA%3D%3D
    // we can now construct the full url
    var url = 'https://www.trustedshops/buyerrating/rate_XFD9974BBC558C007CD46431D056DF230.html&buyerEmail=' + encodedEmail + '&shopOrderID=' + encodedOrderId + '&channel=cmF0ZW5vd2J1dHRvbg';
    // use to open the url in a new tab;
    // or use window.location.href = url if you'd like it 
    // to be the same tab