Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3/4TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

DataFlex Examples

Web API Categories

ASN.1
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
ECC
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
Geolocation
Google APIs
Google Calendar
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text

HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
Microsoft Graph
NTLM
OAuth1
OAuth2
OneDrive
OpenSSL
Outlook
PEM
PFX/P12
POP3
PRNG
REST
REST Misc
RSA
SCP
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
SharePoint
Socket/SSL/TLS
Spider
Stream
Tar Archive
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl

 

 

 

(DataFlex) Payeezy HMAC Computation

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

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

Use ChilkatAx-9.5.0-win32.pkg

Procedure Test
    Handle hoCrypt
    Handle hoPrng
    Boolean iSuccess
    String sApiKey
    String sApiSecret
    String sToken
    String sNonce
    Handle hoDtNow
    Handle hoSbTimestamp
    String sTimestamp
    Handle hoJson
    Handle hoSbHmacData
    String sHexHash
    Handle hoSbBase64Hash
    String sTemp1

    // 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

 

© 2000-2019 Chilkat Software, Inc. All Rights Reserved.