PureBasic
PureBasic
Payeezy HMAC Computation
See more HTTP Misc Examples
Demonstrates how to calculate the HMAC for a Payeezy REST request.Chilkat PureBasic Downloads
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkPrng.pb"
IncludeFile "CkDateTime.pb"
IncludeFile "CkCrypt2.pb"
Procedure ChilkatExample()
success.i = 0
; This example requires the Chilkat API to have been previously unlocked.
; See Global Unlock Sample for sample code.
crypt.i = CkCrypt2::ckCreate()
If crypt.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
prng.i = CkPrng::ckCreate()
If prng.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; An API key such as y6pWAJNyJyjGv66IsVuWnklkKUPFbb0a
apiKey.s = "my_api_key"
; An API secret such as 86fbae7030253af3cd15faef2a1f4b67353e41fb6799f576b5093ae52901e6f7
apiSecret.s = "my_api_secret"
; A token such as fdoa-a480ce8951daa73262734cf102641994c1e55e7cdf4c02b6
token.s = "my_merchant_token"
; The nonce is a random number (bytes), something like "6057786719490086000"
nonce.s = CkPrng::ckGenRandom(prng,8,"decimal")
Debug "nonce = " + nonce
dtNow.i = CkDateTime::ckCreate()
If dtNow.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkDateTime::ckSetFromCurrentSystemTime(dtNow)
sbTimestamp.i = CkStringBuilder::ckCreate()
If sbTimestamp.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; Get the epoch timestamp in seconds
CkStringBuilder::ckAppend(sbTimestamp,CkDateTime::ckGetAsUnixTimeStr(dtNow,0))
; Change it to milliseconds
CkStringBuilder::ckAppend(sbTimestamp,"000")
; The timestamp is a number similar to this: 1546011905000 (which is a timestamp taken on 28-Dec-2018).
timestamp.s = CkStringBuilder::ckGetAsString(sbTimestamp)
Debug "timestamp = " + 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"
; }
; }
; }
json.i = CkJsonObject::ckCreate()
If json.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckUpdateString(json,"merchant_ref","Astonishing-Sale")
CkJsonObject::ckUpdateString(json,"transaction_type","authorize")
CkJsonObject::ckUpdateString(json,"method","token")
CkJsonObject::ckUpdateString(json,"amount","200")
CkJsonObject::ckUpdateString(json,"currency_code","USD")
CkJsonObject::ckUpdateString(json,"token.token_type","FDToken")
CkJsonObject::ckUpdateString(json,"token.token_data.type","visa")
CkJsonObject::ckUpdateString(json,"token.token_data.value","2537446225198291")
CkJsonObject::ckUpdateString(json,"token.token_data.cardholder_name","JohnSmith")
CkJsonObject::ckUpdateString(json,"token.token_data.exp_date","1030")
CkJsonObject::ckUpdateString(json,"token.token_data.special_payment","B")
; string hashData = apiKey + nonce + timestamp + token + jsonString;
sbHmacData.i = CkStringBuilder::ckCreate()
If sbHmacData.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkStringBuilder::ckAppend(sbHmacData,apiKey)
CkStringBuilder::ckAppend(sbHmacData,nonce)
CkStringBuilder::ckAppend(sbHmacData,timestamp)
CkStringBuilder::ckAppend(sbHmacData,token)
CkStringBuilder::ckAppend(sbHmacData,CkJsonObject::ckEmit(json))
; HMAC the data to produce a hex string.
CkCrypt2::setCkEncodingMode(crypt, "hexlower")
CkCrypt2::setCkMacAlgorithm(crypt, "hmac")
CkCrypt2::ckSetMacKeyString(crypt,apiSecret)
CkCrypt2::setCkHashAlgorithm(crypt, "sha256")
CkCrypt2::setCkCharset(crypt, "utf-8")
hexHash.s = CkCrypt2::ckMacStringENC(crypt,CkStringBuilder::ckGetAsString(sbHmacData))
; Now base64 encode the hex string:
sbBase64Hash.i = CkStringBuilder::ckCreate()
If sbBase64Hash.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkStringBuilder::ckAppend(sbBase64Hash,hexHash)
CkStringBuilder::ckEncode(sbBase64Hash,"base64","utf-8")
Debug "This is the Authorization header to be sent with the payeezy request:"
Debug "Authorization: " + CkStringBuilder::ckGetAsString(sbBase64Hash)
CkCrypt2::ckDispose(crypt)
CkPrng::ckDispose(prng)
CkDateTime::ckDispose(dtNow)
CkStringBuilder::ckDispose(sbTimestamp)
CkJsonObject::ckDispose(json)
CkStringBuilder::ckDispose(sbHmacData)
CkStringBuilder::ckDispose(sbBase64Hash)
ProcedureReturn
EndProcedure