@axenda/zatca
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -16,5 +16,4 @@ import { QRCodeToDataURLOptions } from 'qrcode'; | ||
export declare class Invoice { | ||
private readonly _data; | ||
private readonly _tlv; | ||
constructor(invoice: IInvoice); | ||
private _getTags; | ||
/** | ||
@@ -21,0 +20,0 @@ * Returns the TLV representation of the invoice |
@@ -13,13 +13,10 @@ "use strict"; | ||
function Invoice(invoice) { | ||
this._data = invoice; | ||
this._tlv = (0, to_tlv_1.toTlv)([ | ||
new tag_1.Tag(1, invoice.sellerName), | ||
new tag_1.Tag(2, invoice.vatRegistrationNumber), | ||
new tag_1.Tag(3, invoice.invoiceTimestamp), | ||
new tag_1.Tag(4, invoice.invoiceTotal), | ||
new tag_1.Tag(5, invoice.invoiceVatTotal), | ||
]); | ||
} | ||
Invoice.prototype._getTags = function () { | ||
return [ | ||
new tag_1.Tag(1, this._data.sellerName), | ||
new tag_1.Tag(2, this._data.vatRegistrationNumber), | ||
new tag_1.Tag(3, this._data.invoiceTimestamp), | ||
new tag_1.Tag(4, this._data.invoiceTotal), | ||
new tag_1.Tag(5, this._data.invoiceVatTotal), | ||
]; | ||
}; | ||
/** | ||
@@ -30,3 +27,3 @@ * Returns the TLV representation of the invoice | ||
Invoice.prototype.toTlv = function () { | ||
return (0, to_tlv_1.toTlv)(this._getTags()); | ||
return this._tlv; | ||
}; | ||
@@ -38,3 +35,3 @@ /** | ||
Invoice.prototype.toBase64 = function () { | ||
return (0, to_base64_1.toBase64)(this.toTlv()); | ||
return (0, to_base64_1.toBase64)(this._tlv); | ||
}; | ||
@@ -41,0 +38,0 @@ /** |
{ | ||
"name": "@axenda/zatca", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "", | ||
"main": "lib/index.js", | ||
"types": "lib/index.d.ts", | ||
"type": "module", | ||
"files": [ | ||
@@ -30,3 +29,3 @@ "lib", | ||
"setup": "npm install", | ||
"dev": "nodemon src/index.ts", | ||
"dev": "nodemon src/internal/internal.ts", | ||
"build": "rm -rf lib && tsc", | ||
@@ -33,0 +32,0 @@ "start": "node lib/index.js", |
126
README.md
# ZATCA (Fatoorah) QR-Code Implementation | ||
[![npm version](https://badge.fury.io/js/recht.svg)](https://badge.fury.io/js/recht) | ||
[![build](https://travis-ci.org/user/project.svg?branch=master)](https://travis-ci.org/user/project) | ||
[![Coverage Status](https://coveralls.io/repos/github/user/project/badge.svg?branch=master)](https://coveralls.io/github/user/project?branch=master) | ||
[![dependencies Status](https://david-dm.org/user/project/status.svg)](https://david-dm.org/user/project) | ||
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/user/project/master/LICENSE) | ||
[![NPM](https://nodei.co/npm/@axenda/zatca.png?mini=true)](https://npmjs.org/package/@axenda/zatca) | ||
![GitHub Workflow Status (branch)](https://img.shields.io/github/workflow/status/axenda/zatca/Running%20Code%20Coverage/main) | ||
[![codecov](https://codecov.io/gh/axenda/zatca/branch/main/graph/badge.svg?token=T52NJXGE0O)](https://codecov.io/gh/axenda/zatca) | ||
[![GitHub License](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/user/project/master/LICENSE) | ||
An unofficial package to help developers implement ZATCA (Fatoora) QR code easily which is required for e-invoicing in | ||
Saudi Arabia. | ||
> ✅ Validated to have the same output as ZATCA's SDK as of 18 November 2021. | ||
## Installation | ||
To get started, install the package: | ||
```bash | ||
@@ -20,5 +24,44 @@ npm i --save @axenda/zatca | ||
### Import | ||
First, import Invoice class or Tag class to represent an invoice QR code: | ||
```typescript | ||
import { Invoice } from '@axenda/zatca'; | ||
// or | ||
import { Tag } from '@axenda/zatca'; | ||
``` | ||
### Representing an invoice QR code | ||
Second, create an instance of Invoice or an array of Tag class: | ||
```typescript | ||
const invoice = new Invoice({ | ||
sellerName: 'Axenda', | ||
vatRegistrationNumber: '1234567891', | ||
invoiceTimestamp: '2021-12-04T00:00:00Z', | ||
invoiceTotal: '100.00', | ||
invoiceVatTotal: '15.00', | ||
}); | ||
// or | ||
const tags: Tag[] = [ | ||
new Tag(1, 'Axenda'), | ||
new Tag(2, '1234567891'), | ||
new Tag(3, '2021-12-04T00:00:00Z'), | ||
new Tag(4, '100.00'), | ||
new Tag(5, '15.00'), | ||
]; | ||
``` | ||
### Generate TLV | ||
Now you can generate TLV string from the invoice or from the tags array: | ||
```typescript | ||
const tlv = invoice.toTlv(); | ||
// or | ||
import { toTlv } from '@axenda/zatca'; | ||
const tlv = toTlv(tags) | ||
``` | ||
@@ -28,39 +71,76 @@ | ||
You cloud generate Base64 string from the invoice or from the tags array: | ||
```typescript | ||
const base64 = invoice.toBase64(); | ||
// or | ||
import { tagsToBase64 } from '@axenda/zatca'; | ||
const base64 = tagsToBase64(tags); | ||
``` | ||
### Render QR Code | ||
### Render QR code | ||
You can generate image data (png) from base64 string and render it in browser. | ||
You can generate image data (png) from base64 string and render it in browser: | ||
```typescript | ||
const imageData = await invoice.render(); | ||
// or | ||
import { renderTags } from '@axenda/zatca'; | ||
const imageData = await renderTags(tags); | ||
``` | ||
## Documentation | ||
### Use QR code image data | ||
| Argument | Description | Mandatory | Type | Rules | | ||
|:-------------:|:---------------------------------------------------------------------------------|:--------------:|:-----------------------:|:----------------------------------------------------------------------------------------------------------:| | ||
| 1 | Description of argument 1 | True / False | Type | | | ||
| 2 | Description of argument 2 | True / False | Type | | | ||
| 3 | Description of argument 3 | True / False | Type | | | ||
Use the image data to display the QR code in browser: | ||
```html | ||
<img src='{imageData}' alt="Invoice QR Code"> | ||
``` | ||
### Summary | ||
Simple, all you need to generate a QR code is: | ||
```typescript | ||
import { Invoice } from '@axenda/zatca'; | ||
const invoice = new Invoice({ | ||
sellerName: 'Axenda', | ||
vatRegistrationNumber: '1234567891', | ||
invoiceTimestamp: '2021-12-04T00:00:00Z', | ||
invoiceTotal: '100.00', | ||
invoiceVatTotal: '15.00', | ||
}); | ||
const imageData = await invoice.render(); | ||
``` | ||
## Tests | ||
To run test suites, first install dependencies, then run `npm test`: | ||
```bash | ||
npm install | ||
npm test | ||
``` | ||
## Package roadmap | ||
- [x] Feature 1. | ||
- [ ] Feature 2. | ||
- [ ] Feature 3. | ||
- [ ] Feature 4. | ||
- [ ] Feature 5. | ||
- [ ] Feature 6. | ||
- [x] Support ZATCA QR code for phase 1 | ||
- [ ] Review package API consistency | ||
- [ ] Support ZATCA QR code for phase 2 | ||
## Contributing | ||
**https://github.com/user/project** | ||
We welcome [contributions](https://github.com/axenda/zatca/graphs/contributors) of all kinds from anyone. Please take a | ||
moment to review the [guidelines for contributing](CONTRIBUTING.md). | ||
## Bibliography | ||
* [Bug reports](https://github.com/axenda/zatca/wiki/Report-a-Bug) | ||
* [Feature requests](CONTRIBUTING.md#features) | ||
* [Pull requests](CONTRIBUTING.md#pull-requests) | ||
- It is always a good practise to support your work with scientific/ademic literature. | ||
## License | ||
The MIT License (MIT). Please see [License File](LICENSE.md) for more information. |
@@ -8,1 +8,3 @@ export { Invoice } from './models/invoice'; | ||
export { toTlv } from './utils/to-tlv'; | ||
@@ -21,18 +21,14 @@ import { Tag } from './tag'; | ||
export class Invoice { | ||
private readonly _data: IInvoice; | ||
private readonly _tlv: string; | ||
constructor(invoice: IInvoice) { | ||
this._data = invoice; | ||
this._tlv = toTlv([ | ||
new Tag(1, invoice.sellerName), | ||
new Tag(2, invoice.vatRegistrationNumber), | ||
new Tag(3, invoice.invoiceTimestamp), | ||
new Tag(4, invoice.invoiceTotal), | ||
new Tag(5, invoice.invoiceVatTotal), | ||
]); | ||
} | ||
private _getTags() { | ||
return [ | ||
new Tag(1, this._data.sellerName), | ||
new Tag(2, this._data.vatRegistrationNumber), | ||
new Tag(3, this._data.invoiceTimestamp), | ||
new Tag(4, this._data.invoiceTotal), | ||
new Tag(5, this._data.invoiceVatTotal), | ||
]; | ||
} | ||
/** | ||
@@ -43,3 +39,3 @@ * Returns the TLV representation of the invoice | ||
toTlv(): string { | ||
return toTlv(this._getTags()); | ||
return this._tlv; | ||
} | ||
@@ -52,3 +48,3 @@ | ||
toBase64(): string { | ||
return toBase64(this.toTlv()); | ||
return toBase64(this._tlv); | ||
} | ||
@@ -55,0 +51,0 @@ |
@@ -8,5 +8,7 @@ /** | ||
let hex = value.toString(16); | ||
if ((hex.length % 2) > 0) { | ||
hex = '0' + hex; | ||
} | ||
return Buffer | ||
@@ -13,0 +15,0 @@ .from(hex, 'hex') |
18840
439
145
No