Ad

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 ({{customer.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!

Ad

Answer

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="window.open('https://www.trustedshops/buyerrating/rate_XFD9974BBC558C007CD46431D056DF230.html&buyerEmail=' + encodeURIComponent(btoa({{ order.email | json }})) + '&shopOrderID=' + encodeURIComponent(btoa('{{ order.order_number }}')) + '&channel=cmF0ZW5vd2J1dHRvbg')">
        Open order on trustedshops.com
    </a>

And this code just explains the javascript in that onclick.

Javascript:

    // 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 window.open to open the url in a new tab
    window.open(url);
    // or use window.location.href = url if you'd like it 
    // to be the same tab
Ad
source: stackoverflow.com
Ad