PowerBuilder
PowerBuilder
Payeezy Place Temp Authorization Hold on Buyer’s Credit Card
See more HTTP Misc Examples
Demonstrates how to place a temporary authorization hold for the desired amount on the buyer’s credit card. You can Capture the authorized amount on completion of service or Void/Refund the transaction as required.Chilkat PowerBuilder Downloads
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
oleobject loo_Http
string ls_Url
oleobject loo_Resp
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": "credit_card",
// "amount": "1299",
// "currency_code": "USD",
// "credit_card": {
// "type": "visa",
// "cardholder_name": "John Smith",
// "card_number": "4788250000028291",
// "exp_date": "1020",
// "cvv": "123"
// }
// }
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","credit_card")
loo_Json.UpdateString("amount","1299")
loo_Json.UpdateString("currency_code","USD")
loo_Json.UpdateString("credit_card.type","visa")
loo_Json.UpdateString("credit_card.cardholder_name","John Smith")
loo_Json.UpdateString("credit_card.card_number","4788250000028291")
loo_Json.UpdateString("credit_card.exp_date","1020")
loo_Json.UpdateString("credit_card.cvv","123")
loo_Json.EmitCompact = 0
Write-Debug loo_Json.Emit()
// 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())
Write-Debug "hexHash = " + ls_HexHash
// 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()
// -----------------------------------------------------------
// Now that we have the value for the Authorization header, send the POST containing the JSON.
loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
loo_Http.Accept = "*/*"
loo_Http.UserAgent = ""
loo_Http.SetRequestHeader("Authorization",loo_SbBase64Hash.GetAsString())
loo_Http.SetRequestHeader("apikey",ls_ApiKey)
loo_Http.SetRequestHeader("nonce",ls_Nonce)
loo_Http.SetRequestHeader("timestamp",ls_Timestamp)
loo_Http.SetRequestHeader("token",ls_Token)
loo_Http.SessionLogFilename = "qa_output/payeezy.txt"
ls_Url = "https://api-cert.payeezy.com/v1/transactions"
loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")
li_Success = loo_Http.HttpJson("POST",ls_Url,loo_Json,"application/json",loo_Resp)
if li_Success = 0 then
Write-Debug loo_Http.LastErrorText
destroy loo_Crypt
destroy loo_Prng
destroy loo_DtNow
destroy loo_SbTimestamp
destroy loo_Json
destroy loo_SbHmacData
destroy loo_SbBase64Hash
destroy loo_Http
destroy loo_Resp
return
end if
Write-Debug "response status code = " + string(loo_Resp.StatusCode)
loo_Json.Load(loo_Resp.BodyStr)
Write-Debug loo_Json.Emit()
// Sample JSON response:
// {
// "correlation_id": "228.4604632998994",
// "transaction_status": "approved",
// "validation_status": "success",
// "transaction_type": "authorize",
// "transaction_id": "ET175628",
// "transaction_tag": "2313721985",
// "method": "credit_card",
// "amount": "1299",
// "currency": "USD",
// "cvv2": "M",
// "token": {
// "token_type": "FDToken",
// "token_data": {
// "value": "9732261336638291"
// }
// },
// "card": {
// "type": "visa",
// "cardholder_name": "John Smith",
// "card_number": "8291",
// "exp_date": "1020"
// },
// "bank_resp_code": "100",
// "bank_message": "Approved",
// "gateway_resp_code": "00",
// "gateway_message": "Transaction Normal"
// }
//
Write-Debug loo_Json.StringOf("correlation_id")
Write-Debug loo_Json.StringOf("transaction_status")
Write-Debug loo_Json.StringOf("validation_status")
Write-Debug loo_Json.StringOf("transaction_type")
Write-Debug loo_Json.StringOf("transaction_id")
Write-Debug loo_Json.StringOf("transaction_tag")
Write-Debug loo_Json.StringOf("method")
Write-Debug loo_Json.StringOf("amount")
Write-Debug loo_Json.StringOf("currency")
Write-Debug loo_Json.StringOf("cvv2")
Write-Debug loo_Json.StringOf("token.token_type")
Write-Debug loo_Json.StringOf("token.token_data.value")
Write-Debug loo_Json.StringOf("card.type")
Write-Debug loo_Json.StringOf("card.cardholder_name")
Write-Debug loo_Json.StringOf("card.card_number")
Write-Debug loo_Json.StringOf("card.exp_date")
Write-Debug loo_Json.StringOf("bank_resp_code")
Write-Debug loo_Json.StringOf("bank_message")
Write-Debug loo_Json.StringOf("gateway_resp_code")
Write-Debug loo_Json.StringOf("gateway_message")
destroy loo_Crypt
destroy loo_Prng
destroy loo_DtNow
destroy loo_SbTimestamp
destroy loo_Json
destroy loo_SbHmacData
destroy loo_SbBase64Hash
destroy loo_Http
destroy loo_Resp