zatca-xml-ts
Version:
An implementation of Saudi Arabia ZATCA's E-Invoicing requirements, processes, and standards.
75 lines (74 loc) • 3.42 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ZATCAInvoice = exports.ZATCAPaymentMethods = exports.ZATCAInvoiceTypes = void 0;
const parser_1 = require("../parser");
const calc_js_1 = require("./calc.js");
const signing_1 = require("./signing");
const simplified_tax_invoice_template_1 = __importStar(require("./templates/simplified_tax_invoice_template"));
Object.defineProperty(exports, "ZATCAInvoiceTypes", { enumerable: true, get: function () { return simplified_tax_invoice_template_1.ZATCAInvoiceTypes; } });
Object.defineProperty(exports, "ZATCAPaymentMethods", { enumerable: true, get: function () { return simplified_tax_invoice_template_1.ZATCAPaymentMethods; } });
class ZATCAInvoice {
invoice_xml;
/**
* Parses a ZATCA Simplified Tax Invoice XML string. Or creates a new one based on given props.
* @param invoice_xml_str Invoice XML string to parse.
* @param props ZATCASimplifiedInvoiceProps props to create a new unsigned invoice.
*/
constructor({ invoice_xml_str, props, acceptWarning, }) {
if (invoice_xml_str) {
this.invoice_xml = new parser_1.XMLDocument(invoice_xml_str);
if (!this.invoice_xml)
throw new Error("Error parsing invoice XML string.");
}
else {
if (!props)
throw new Error("Unable to create new XML invoice.");
this.invoice_xml = new parser_1.XMLDocument((0, simplified_tax_invoice_template_1.default)(props));
// Parsing
this.parseLineItems(props.line_items ?? [], props, acceptWarning);
}
}
parseLineItems(line_items, props, acceptWarning = false) {
(0, calc_js_1.Calc)(line_items, props, this.invoice_xml, acceptWarning);
}
getXML() {
return this.invoice_xml;
}
/**
* Signs the invoice.
* @param certificate_string String signed EC certificate.
* @param private_key_string String ec-secp256k1 private key;
* @returns String signed invoice xml, includes QR generation.
*/
sign(certificate_string, private_key_string) {
return (0, signing_1.generateSignedXMLString)({
invoice_xml: this.invoice_xml,
certificate_string: certificate_string,
private_key_string: private_key_string,
});
}
}
exports.ZATCAInvoice = ZATCAInvoice;