Business Generated Links: Ruby Code

When it comes to creating Business Generated Links (BGLs), there are a wide range of programming languages that you can use. You can check out the general requirements outlined here, and we provide a few code repos in popular languages like Node.js, Python, and PHP in our official support document here.

In this article, let's take a look at how we might create BGLs with Ruby:

require "base64"
require "json"
require "openssl"
require "uri"

encryption_key_base64 = ""
authentication_key_base64 = ""
domain = ""

order = {
  "email": "michael@example.com",
  "name": "Michael",
    "ref": "ABC-1234"
}

json_order = JSON.generate(order)

encryption_key = Base64.decode64(encryption_key_base64)
authentication_key = Base64.decode64(authentication_key_base64)

cipher = OpenSSL::Cipher::AES.new(256, :CBC)                                                                                                                                                          
cipher.encrypt
cipher.key = encryption_key
iv = cipher.random_iv

encrypted = cipher.update(json_order) + cipher.final

hmac = OpenSSL::HMAC.digest('sha256', authentication_key, iv + encrypted)

base64_payload = Base64.encode64(iv + encrypted + hmac)

final_payload = URI.encode_www_form_component(base64_payload)

puts "https://www.trustpilot.com/evaluate-bgl/#{domain}?p=#{final_payload}"

Was this post helpful?

0 comments

Sort by
Please sign in to leave a comment.