DataFlex
DataFlex
Payeezy HMAC Computation
See more HTTP Misc Examples
Demonstrates how to calculate the HMAC for a Payeezy REST request.Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoCrypt
Handle hoPrng
String sApiKey
String sApiSecret
String sToken
String sNonce
Handle hoDtNow
Handle hoSbTimestamp
String sTimestamp
Handle hoJson
Handle hoSbHmacData
String sHexHash
Handle hoSbBase64Hash
String sTemp1
Move False To iSuccess
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
Get Create (RefClass(cComChilkatCrypt2)) To hoCrypt
If (Not(IsComObjectCreated(hoCrypt))) Begin
Send CreateComObject of hoCrypt
End
Get Create (RefClass(cComChilkatPrng)) To hoPrng
If (Not(IsComObjectCreated(hoPrng))) Begin
Send CreateComObject of hoPrng
End
// An API key such as y6pWAJNyJyjGv66IsVuWnklkKUPFbb0a
Move "my_api_key" To sApiKey
// An API secret such as 86fbae7030253af3cd15faef2a1f4b67353e41fb6799f576b5093ae52901e6f7
Move "my_api_secret" To sApiSecret
// A token such as fdoa-a480ce8951daa73262734cf102641994c1e55e7cdf4c02b6
Move "my_merchant_token" To sToken
// The nonce is a random number (bytes), something like "6057786719490086000"
Get ComGenRandom Of hoPrng 8 "decimal" To sNonce
Showln "nonce = " sNonce
Get Create (RefClass(cComCkDateTime)) To hoDtNow
If (Not(IsComObjectCreated(hoDtNow))) Begin
Send CreateComObject of hoDtNow
End
Get ComSetFromCurrentSystemTime Of hoDtNow To iSuccess
Get Create (RefClass(cComChilkatStringBuilder)) To hoSbTimestamp
If (Not(IsComObjectCreated(hoSbTimestamp))) Begin
Send CreateComObject of hoSbTimestamp
End
// Get the epoch timestamp in seconds
Get ComGetAsUnixTimeStr Of hoDtNow False To sTemp1
Get ComAppend Of hoSbTimestamp sTemp1 To iSuccess
// Change it to milliseconds
Get ComAppend Of hoSbTimestamp "000" To iSuccess
// The timestamp is a number similar to this: 1546011905000 (which is a timestamp taken on 28-Dec-2018).
Get ComGetAsString Of hoSbTimestamp To sTimestamp
Showln "timestamp = " sTimestamp
// 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"
// }
// }
// }
Get Create (RefClass(cComChilkatJsonObject)) To hoJson
If (Not(IsComObjectCreated(hoJson))) Begin
Send CreateComObject of hoJson
End
Get ComUpdateString Of hoJson "merchant_ref" "Astonishing-Sale" To iSuccess
Get ComUpdateString Of hoJson "transaction_type" "authorize" To iSuccess
Get ComUpdateString Of hoJson "method" "token" To iSuccess
Get ComUpdateString Of hoJson "amount" "200" To iSuccess
Get ComUpdateString Of hoJson "currency_code" "USD" To iSuccess
Get ComUpdateString Of hoJson "token.token_type" "FDToken" To iSuccess
Get ComUpdateString Of hoJson "token.token_data.type" "visa" To iSuccess
Get ComUpdateString Of hoJson "token.token_data.value" "2537446225198291" To iSuccess
Get ComUpdateString Of hoJson "token.token_data.cardholder_name" "JohnSmith" To iSuccess
Get ComUpdateString Of hoJson "token.token_data.exp_date" "1030" To iSuccess
Get ComUpdateString Of hoJson "token.token_data.special_payment" "B" To iSuccess
// string hashData = apiKey + nonce + timestamp + token + jsonString;
Get Create (RefClass(cComChilkatStringBuilder)) To hoSbHmacData
If (Not(IsComObjectCreated(hoSbHmacData))) Begin
Send CreateComObject of hoSbHmacData
End
Get ComAppend Of hoSbHmacData sApiKey To iSuccess
Get ComAppend Of hoSbHmacData sNonce To iSuccess
Get ComAppend Of hoSbHmacData sTimestamp To iSuccess
Get ComAppend Of hoSbHmacData sToken To iSuccess
Get ComEmit Of hoJson To sTemp1
Get ComAppend Of hoSbHmacData sTemp1 To iSuccess
// HMAC the data to produce a hex string.
Set ComEncodingMode Of hoCrypt To "hexlower"
Set ComMacAlgorithm Of hoCrypt To "hmac"
Get ComSetMacKeyString Of hoCrypt sApiSecret To iSuccess
Set ComHashAlgorithm Of hoCrypt To "sha256"
Set ComCharset Of hoCrypt To "utf-8"
Get ComGetAsString Of hoSbHmacData To sTemp1
Get ComMacStringENC Of hoCrypt sTemp1 To sHexHash
// Now base64 encode the hex string:
Get Create (RefClass(cComChilkatStringBuilder)) To hoSbBase64Hash
If (Not(IsComObjectCreated(hoSbBase64Hash))) Begin
Send CreateComObject of hoSbBase64Hash
End
Get ComAppend Of hoSbBase64Hash sHexHash To iSuccess
Get ComEncode Of hoSbBase64Hash "base64" "utf-8" To iSuccess
Showln "This is the Authorization header to be sent with the payeezy request:"
Get ComGetAsString Of hoSbBase64Hash To sTemp1
Showln "Authorization: " sTemp1
End_Procedure