Sample code for 30+ languages & platforms
PowerBuilder

Payeezy HMAC Computation

See more HTTP Misc Examples

Demonstrates how to calculate the HMAC for a Payeezy REST request.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Crypt
oleobject loo_Prng
string ls_ApiKey
string ls_ApiSecret
string ls_Token
string ls_Nonce
oleobject loo_DtNow
oleobject loo_SbTimestamp
string ls_Timestamp
oleobject loo_Json
oleobject loo_SbHmacData
string ls_HexHash
oleobject loo_SbBase64Hash

li_Success = 0

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

loo_Crypt = create oleobject
li_rc = loo_Crypt.ConnectToNewObject("Chilkat.Crypt2")
if li_rc < 0 then
    destroy loo_Crypt
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_Prng = create oleobject
li_rc = loo_Prng.ConnectToNewObject("Chilkat.Prng")

// An API key such as y6pWAJNyJyjGv66IsVuWnklkKUPFbb0a
ls_ApiKey = "my_api_key"
// An API secret such as 86fbae7030253af3cd15faef2a1f4b67353e41fb6799f576b5093ae52901e6f7
ls_ApiSecret = "my_api_secret"
// A token such as fdoa-a480ce8951daa73262734cf102641994c1e55e7cdf4c02b6
ls_Token = "my_merchant_token"

// The nonce is a random number (bytes), something like "6057786719490086000"
ls_Nonce = loo_Prng.GenRandom(8,"decimal")
Write-Debug "nonce = " + ls_Nonce

loo_DtNow = create oleobject
li_rc = loo_DtNow.ConnectToNewObject("Chilkat.CkDateTime")

loo_DtNow.SetFromCurrentSystemTime()
loo_SbTimestamp = create oleobject
li_rc = loo_SbTimestamp.ConnectToNewObject("Chilkat.StringBuilder")

// Get the epoch timestamp in seconds
loo_SbTimestamp.Append(loo_DtNow.GetAsUnixTimeStr(0))
// Change it to milliseconds
loo_SbTimestamp.Append("000")
// The timestamp is a number similar to this: 1546011905000 (which is a timestamp taken on 28-Dec-2018).
ls_Timestamp = loo_SbTimestamp.GetAsString()
Write-Debug "timestamp = " + ls_Timestamp

// Generate the following JSON request body:
// 	{
// 	  "merchant_ref": "Astonishing-Sale",
// 	  "transaction_type": "authorize",
// 	  "method": "token",
// 	  "amount": "200",
// 	  "currency_code": "USD",
// 	  "token": {
// 	    "token_type": "FDToken",
// 	    "token_data": {
// 	      "type": "visa",
// 	      "value": "2537446225198291",
// 	      "cardholder_name": "JohnSmith",
// 	      "exp_date": "1030",
// 	      "special_payment": "B"
// 	    }
// 	  }
// 	}

loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

loo_Json.UpdateString("merchant_ref","Astonishing-Sale")
loo_Json.UpdateString("transaction_type","authorize")
loo_Json.UpdateString("method","token")
loo_Json.UpdateString("amount","200")
loo_Json.UpdateString("currency_code","USD")
loo_Json.UpdateString("token.token_type","FDToken")
loo_Json.UpdateString("token.token_data.type","visa")
loo_Json.UpdateString("token.token_data.value","2537446225198291")
loo_Json.UpdateString("token.token_data.cardholder_name","JohnSmith")
loo_Json.UpdateString("token.token_data.exp_date","1030")
loo_Json.UpdateString("token.token_data.special_payment","B")

// string hashData = apiKey + nonce + timestamp + token + jsonString;
loo_SbHmacData = create oleobject
li_rc = loo_SbHmacData.ConnectToNewObject("Chilkat.StringBuilder")

loo_SbHmacData.Append(ls_ApiKey)
loo_SbHmacData.Append(ls_Nonce)
loo_SbHmacData.Append(ls_Timestamp)
loo_SbHmacData.Append(ls_Token)
loo_SbHmacData.Append(loo_Json.Emit())

// HMAC the data to produce a hex string.
loo_Crypt.EncodingMode = "hexlower"
loo_Crypt.MacAlgorithm = "hmac"
loo_Crypt.SetMacKeyString(ls_ApiSecret)
loo_Crypt.HashAlgorithm = "sha256"
loo_Crypt.Charset = "utf-8"
ls_HexHash = loo_Crypt.MacStringENC(loo_SbHmacData.GetAsString())

// Now base64 encode the hex string:
loo_SbBase64Hash = create oleobject
li_rc = loo_SbBase64Hash.ConnectToNewObject("Chilkat.StringBuilder")

loo_SbBase64Hash.Append(ls_HexHash)
loo_SbBase64Hash.Encode("base64","utf-8")

Write-Debug "This is the Authorization header to be sent with the payeezy request:"
Write-Debug "Authorization: " + loo_SbBase64Hash.GetAsString()


destroy loo_Crypt
destroy loo_Prng
destroy loo_DtNow
destroy loo_SbTimestamp
destroy loo_Json
destroy loo_SbHmacData
destroy loo_SbBase64Hash