Sample code for 30+ languages & platforms
PureBasic

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 PureBasic Downloads

PureBasic
IncludeFile "CkJsonObject.pb"
IncludeFile "CkDateTime.pb"
IncludeFile "CkPrng.pb"
IncludeFile "CkCrypt2.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkHttpResponse.pb"
IncludeFile "CkHttp.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": "credit_card",
    ;   "amount": "1299",
    ;   "currency_code": "USD",
    ;   "credit_card": {
    ;     "type": "visa",
    ;     "cardholder_name": "John Smith",
    ;     "card_number": "4788250000028291",
    ;     "exp_date": "1020",
    ;     "cvv": "123"
    ;   }
    ; }

    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","credit_card")
    CkJsonObject::ckUpdateString(json,"amount","1299")
    CkJsonObject::ckUpdateString(json,"currency_code","USD")
    CkJsonObject::ckUpdateString(json,"credit_card.type","visa")
    CkJsonObject::ckUpdateString(json,"credit_card.cardholder_name","John Smith")
    CkJsonObject::ckUpdateString(json,"credit_card.card_number","4788250000028291")
    CkJsonObject::ckUpdateString(json,"credit_card.exp_date","1020")
    CkJsonObject::ckUpdateString(json,"credit_card.cvv","123")
    CkJsonObject::setCkEmitCompact(json, 0)
    Debug CkJsonObject::ckEmit(json)

    ; 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))
    Debug "hexHash = " + hexHash

    ; 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)

    ; -----------------------------------------------------------
    ; Now that we have the value for the Authorization header, send the POST containing the JSON.

    http.i = CkHttp::ckCreate()
    If http.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkHttp::setCkAccept(http, "*/*")
    CkHttp::setCkUserAgent(http, "")
    CkHttp::ckSetRequestHeader(http,"Authorization",CkStringBuilder::ckGetAsString(sbBase64Hash))
    CkHttp::ckSetRequestHeader(http,"apikey",apiKey)
    CkHttp::ckSetRequestHeader(http,"nonce",nonce)
    CkHttp::ckSetRequestHeader(http,"timestamp",timestamp)
    CkHttp::ckSetRequestHeader(http,"token",token)

    CkHttp::setCkSessionLogFilename(http, "qa_output/payeezy.txt")

    url.s = "https://api-cert.payeezy.com/v1/transactions"
    resp.i = CkHttpResponse::ckCreate()
    If resp.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkHttp::ckHttpJson(http,"POST",url,json,"application/json",resp)
    If success = 0
        Debug CkHttp::ckLastErrorText(http)
        CkCrypt2::ckDispose(crypt)
        CkPrng::ckDispose(prng)
        CkDateTime::ckDispose(dtNow)
        CkStringBuilder::ckDispose(sbTimestamp)
        CkJsonObject::ckDispose(json)
        CkStringBuilder::ckDispose(sbHmacData)
        CkStringBuilder::ckDispose(sbBase64Hash)
        CkHttp::ckDispose(http)
        CkHttpResponse::ckDispose(resp)
        ProcedureReturn
    EndIf

    Debug "response status code = " + Str(CkHttpResponse::ckStatusCode(resp))
    CkJsonObject::ckLoad(json,CkHttpResponse::ckBodyStr(resp))
    Debug CkJsonObject::ckEmit(json)

    ; 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"
    ; }
    ; 

    Debug CkJsonObject::ckStringOf(json,"correlation_id")
    Debug CkJsonObject::ckStringOf(json,"transaction_status")
    Debug CkJsonObject::ckStringOf(json,"validation_status")
    Debug CkJsonObject::ckStringOf(json,"transaction_type")
    Debug CkJsonObject::ckStringOf(json,"transaction_id")
    Debug CkJsonObject::ckStringOf(json,"transaction_tag")
    Debug CkJsonObject::ckStringOf(json,"method")
    Debug CkJsonObject::ckStringOf(json,"amount")
    Debug CkJsonObject::ckStringOf(json,"currency")
    Debug CkJsonObject::ckStringOf(json,"cvv2")
    Debug CkJsonObject::ckStringOf(json,"token.token_type")
    Debug CkJsonObject::ckStringOf(json,"token.token_data.value")
    Debug CkJsonObject::ckStringOf(json,"card.type")
    Debug CkJsonObject::ckStringOf(json,"card.cardholder_name")
    Debug CkJsonObject::ckStringOf(json,"card.card_number")
    Debug CkJsonObject::ckStringOf(json,"card.exp_date")
    Debug CkJsonObject::ckStringOf(json,"bank_resp_code")
    Debug CkJsonObject::ckStringOf(json,"bank_message")
    Debug CkJsonObject::ckStringOf(json,"gateway_resp_code")
    Debug CkJsonObject::ckStringOf(json,"gateway_message")


    CkCrypt2::ckDispose(crypt)
    CkPrng::ckDispose(prng)
    CkDateTime::ckDispose(dtNow)
    CkStringBuilder::ckDispose(sbTimestamp)
    CkJsonObject::ckDispose(json)
    CkStringBuilder::ckDispose(sbHmacData)
    CkStringBuilder::ckDispose(sbBase64Hash)
    CkHttp::ckDispose(http)
    CkHttpResponse::ckDispose(resp)


    ProcedureReturn
EndProcedure