Sample code for 30+ languages & platforms
Zig Requires Chilkat v11.0.0+

CardConnect Void

See more CardConnect Examples

Demonstrates how to send a CardConnect void request.
The void service cancels a transaction that is in either "Authorized" or "Queued for Capture" status.. ...

See https://developer.cardconnect.com/cardconnect-api#void

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    const http = try chilkat.Http.init();
    defer http.deinit();

    http.setBasicAuth(true);
    http.setLogin("API_USERNAME");
    http.setPassword("API_PASSWORD");

    // Build and send the following JSON:

    // The "retref" is the value returned in the JSON response for the Authorization request.

    // {
    //     "retref":"112989260941",
    //     "merchid":"MERCHANT_ID"
    // }

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.updateString("retref", "112989260941") catch {};
    json.updateString("merchid", "MERCHANT_ID") catch {};

    const url = "https://<site>.cardconnect.com:<port>/cardconnect/rest/void";

    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpStr("PUT", url, try json.emit(alloc), "utf-8", "application/json", resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    // A response status of 200 indicates potential success.  The JSON response body
    // must be examined to determine if it was truly successful or an error.
    std.debug.print("response status code = {d}\n", .{resp.getStatusCode()});

    const json_resp = try chilkat.JsonObject.init();
    defer json_resp.deinit();
    json_resp.load(try resp.getBodyStr(alloc)) catch {};
    json_resp.setEmitCompact(false);

    std.debug.print("response JSON:\n", .{});
    std.debug.print("{s}\n", .{try json_resp.emit(alloc)});

    // A successful response looks like this:

    // {
    //   "authcode": "REVERS",
    //   "respproc": "FNOR",
    //   "amount": "0.00",
    //   "resptext": "Approval",
    //   "currency": "USD",
    //   "retref": "112989260941",
    //   "respstat": "A",
    //   "respcode": "00",
    //   "merchid": "496160873888"
    // }

    // Use this online tool to generate parsing code from sample JSON:
    // Generate Parsing Code from JSON

    _ = try json_resp.stringOf(alloc, "authcode");
    _ = try json_resp.stringOf(alloc, "respproc");
    _ = try json_resp.stringOf(alloc, "amount");
    _ = try json_resp.stringOf(alloc, "resptext");
    _ = try json_resp.stringOf(alloc, "currency");
    _ = try json_resp.stringOf(alloc, "retref");
    _ = try json_resp.stringOf(alloc, "respstat");
    _ = try json_resp.stringOf(alloc, "respcode");
    _ = try json_resp.stringOf(alloc, "merchid");
}