Sample code for 30+ languages & platforms
Zig

hacienda.go.cr Recepción de comprobantes electrónicos y respuestas del receptor.

See more hacienda.go.cr Examples

Comprobantes Electrónicos API version v1 -- Recepción de comprobantes electrónicos y respuestas del receptor.

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

    // Implements the following CURL command:

    // curl -X POST -H "Content-Type: application / json" \
    //   -H 'authorization: Bearer {access_token}' \
    //   -d '{
    //   "clave": "50601011600310112345600100010100000000011999999999",
    //   "fecha": "2016-01-01T00:00:00-0600",
    //   "emisor": {
    //     "tipoIdentificacion": "02",
    //     "numeroIdentificacion": "003101123456"
    //   },
    //   "receptor": {
    //     "tipoIdentificacion": "02",
    //     "numeroIdentificacion": "003101123456"
    //   },
    //   "comprobanteXml": "PD94..."
    // }' https://api.comprobanteselectronicos.go.cr/recepcion/v1/recepcion

    // Use the following online tool to generate HTTP code from a CURL command
    // Convert a cURL Command to HTTP Source Code

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

    // The following JSON is sent in the request body.

    // {
    //   "clave": "50601011600310112345600100010100000000011999999999",
    //   "fecha": "2016-01-01T00:00:00-0600",
    //   "emisor": {
    //     "tipoIdentificacion": "02",
    //     "numeroIdentificacion": "003101123456"
    //   },
    //   "receptor": {
    //     "tipoIdentificacion": "02",
    //     "numeroIdentificacion": "003101123456"
    //   },
    //   "comprobanteXml": "PD94b..."
    // }

    // Load the previously signed XML.
    const bd_signed_xml = try chilkat.BinData.init();
    defer bd_signed_xml.deinit();
    bd_signed_xml.loadFile("someDir/signed.xml") catch {
        std.debug.print("Failed to load the XAdES signed XML.\n", .{});
        return;
    };

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.updateString("clave", "50601011600310112345600100010100000000011999999999") catch {};
    json.updateString("fecha", "2016-01-01T00:00:00-0600") catch {};
    json.updateString("emisor.tipoIdentificacion", "02") catch {};
    json.updateString("emisor.numeroIdentificacion", "003101123456") catch {};
    json.updateString("receptor.tipoIdentificacion", "02") catch {};
    json.updateString("receptor.numeroIdentificacion", "003101123456") catch {};
    // Add the base64 encoded representation of the signed XML.
    json.updateString("comprobanteXml", try bd_signed_xml.getEncoded(alloc, "base64")) catch {};

    // Load our previously obtained OAuth2 access token.
    const json_token = try chilkat.JsonObject.init();
    defer json_token.deinit();
    json_token.loadFile("qa_data/tokens/hacienda_cr.json") catch {
        std.debug.print("Failed to load constantContact.json\n", .{});
        return;
    };

    // Adds the "Authorization: Bearer {access_token}" header.
    http.setAuthToken(try json_token.stringOf(alloc, "access_token"));

    http.setRequestHeader("Content-Type", "application / json");

    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpJson("POST", "https://api.comprobanteselectronicos.go.cr/recepcion/v1/recepcion", json, "application/json", resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    // A 201 status code indicates success.
    std.debug.print("Response status code: {d}\n", .{resp.getStatusCode()});

    std.debug.print("Response body:\n", .{});
    std.debug.print("{s}\n", .{try resp.getBodyStr(alloc)});
}