Zig
Zig
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 Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const crypt = try chilkat.Crypt2.init();
defer crypt.deinit();
const prng = try chilkat.Prng.init();
defer prng.deinit();
// An API key such as y6pWAJNyJyjGv66IsVuWnklkKUPFbb0a
const api_key = "my_api_key";
// An API secret such as 86fbae7030253af3cd15faef2a1f4b67353e41fb6799f576b5093ae52901e6f7
const api_secret = "my_api_secret";
// A token such as fdoa-a480ce8951daa73262734cf102641994c1e55e7cdf4c02b6
const token = "my_merchant_token";
// The nonce is a random number (bytes), something like "6057786719490086000"
const nonce = try prng.genRandom(alloc, 8, "decimal");
std.debug.print("nonce = {s}\n", .{nonce});
const dt_now = try chilkat.DateTime.init();
defer dt_now.deinit();
dt_now.setFromCurrentSystemTime() catch {};
const sb_timestamp = try chilkat.StringBuilder.init();
defer sb_timestamp.deinit();
// Get the epoch timestamp in seconds
sb_timestamp.append(try dt_now.getAsUnixTimeStr(alloc, false)) catch {};
// Change it to milliseconds
sb_timestamp.append("000") catch {};
// The timestamp is a number similar to this: 1546011905000 (which is a timestamp taken on 28-Dec-2018).
const timestamp = try sb_timestamp.getAsString(alloc);
std.debug.print("timestamp = {s}\n", .{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"
// }
// }
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.updateString("merchant_ref", "Astonishing-Sale") catch {};
json.updateString("transaction_type", "authorize") catch {};
json.updateString("method", "credit_card") catch {};
json.updateString("amount", "1299") catch {};
json.updateString("currency_code", "USD") catch {};
json.updateString("credit_card.type", "visa") catch {};
json.updateString("credit_card.cardholder_name", "John Smith") catch {};
json.updateString("credit_card.card_number", "4788250000028291") catch {};
json.updateString("credit_card.exp_date", "1020") catch {};
json.updateString("credit_card.cvv", "123") catch {};
json.setEmitCompact(false);
std.debug.print("{s}\n", .{try json.emit(alloc)});
// string hashData = apiKey + nonce + timestamp + token + jsonString;
const sb_hmac_data = try chilkat.StringBuilder.init();
defer sb_hmac_data.deinit();
sb_hmac_data.append(api_key) catch {};
sb_hmac_data.append(nonce) catch {};
sb_hmac_data.append(timestamp) catch {};
sb_hmac_data.append(token) catch {};
sb_hmac_data.append(try json.emit(alloc)) catch {};
// HMAC the data to produce a hex string.
crypt.setEncodingMode("hexlower");
crypt.setMacAlgorithm("hmac");
crypt.setMacKeyString(api_secret) catch {};
crypt.setHashAlgorithm("sha256");
crypt.setCharset("utf-8");
const hex_hash = try crypt.macStringENC(alloc, try sb_hmac_data.getAsString(alloc));
std.debug.print("hexHash = {s}\n", .{hex_hash});
// Now base64 encode the hex string:
const sb_base64_hash = try chilkat.StringBuilder.init();
defer sb_base64_hash.deinit();
sb_base64_hash.append(hex_hash) catch {};
sb_base64_hash.encode("base64", "utf-8") catch {};
std.debug.print("This is the Authorization header to be sent with the payeezy request:\n", .{});
std.debug.print("Authorization: {s}\n", .{try sb_base64_hash.getAsString(alloc)});
// -----------------------------------------------------------
// Now that we have the value for the Authorization header, send the POST containing the JSON.
const http = try chilkat.Http.init();
defer http.deinit();
http.setAccept("*/*");
http.setUserAgent("");
http.setRequestHeader("Authorization", try sb_base64_hash.getAsString(alloc));
http.setRequestHeader("apikey", api_key);
http.setRequestHeader("nonce", nonce);
http.setRequestHeader("timestamp", timestamp);
http.setRequestHeader("token", token);
http.setSessionLogFilename("qa_output/payeezy.txt");
const url = "https://api-cert.payeezy.com/v1/transactions";
const resp = try chilkat.HttpResponse.init();
defer resp.deinit();
http.httpJson("POST", url, json, "application/json", resp) catch {
std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
return;
};
std.debug.print("response status code = {d}\n", .{resp.getStatusCode()});
json.load(try resp.getBodyStr(alloc)) catch {};
std.debug.print("{s}\n", .{try json.emit(alloc)});
// 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"
// }
//
std.debug.print("{s}\n", .{try json.stringOf(alloc, "correlation_id")});
std.debug.print("{s}\n", .{try json.stringOf(alloc, "transaction_status")});
std.debug.print("{s}\n", .{try json.stringOf(alloc, "validation_status")});
std.debug.print("{s}\n", .{try json.stringOf(alloc, "transaction_type")});
std.debug.print("{s}\n", .{try json.stringOf(alloc, "transaction_id")});
std.debug.print("{s}\n", .{try json.stringOf(alloc, "transaction_tag")});
std.debug.print("{s}\n", .{try json.stringOf(alloc, "method")});
std.debug.print("{s}\n", .{try json.stringOf(alloc, "amount")});
std.debug.print("{s}\n", .{try json.stringOf(alloc, "currency")});
std.debug.print("{s}\n", .{try json.stringOf(alloc, "cvv2")});
std.debug.print("{s}\n", .{try json.stringOf(alloc, "token.token_type")});
std.debug.print("{s}\n", .{try json.stringOf(alloc, "token.token_data.value")});
std.debug.print("{s}\n", .{try json.stringOf(alloc, "card.type")});
std.debug.print("{s}\n", .{try json.stringOf(alloc, "card.cardholder_name")});
std.debug.print("{s}\n", .{try json.stringOf(alloc, "card.card_number")});
std.debug.print("{s}\n", .{try json.stringOf(alloc, "card.exp_date")});
std.debug.print("{s}\n", .{try json.stringOf(alloc, "bank_resp_code")});
std.debug.print("{s}\n", .{try json.stringOf(alloc, "bank_message")});
std.debug.print("{s}\n", .{try json.stringOf(alloc, "gateway_resp_code")});
std.debug.print("{s}\n", .{try json.stringOf(alloc, "gateway_message")});
}