Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
Enhamce Readme
  • Loading branch information
Salah Alkhwlani committed Oct 11, 2021
0 parents commit f3d99bd
Show file tree
Hide file tree
Showing 17 changed files with 546 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
root = true

[*]
charset = utf-8
end_of_line = crlf
indent_size = 4
indent_style = space
insert_final_newline = true
tab_width = 8
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
composer.phar
composer.lock
vendor/
.idea
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Changelog

All notable changes to `salla/zatca` will be documented in this file

## 1.0.0 - 2021-10-11

- initial release
55 changes: 55 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Contributing

Contributions are **welcome** and will be fully **credited**.

Please read and understand the contribution guide before creating an issue or pull request.

## Etiquette

This project is open source, and as such, the maintainers give their free time to build and maintain the source code
held within. They make the code freely available in the hope that it will be of use to other developers. It would be
extremely unfair for them to suffer abuse or anger for their hard work.

Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the
world that developers are civilized and selfless people.

It's the duty of the maintainer to ensure that all submissions to the project are of sufficient
quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used.

## Viability

When requesting or submitting new features, first consider whether it might be useful to others. Open
source projects are used by many developers, who may have entirely different needs to your own. Think about
whether or not your feature is likely to be used by other users of the project.

## Procedure

Before filing an issue:

- Attempt to replicate the problem, to ensure that it wasn't a coincidental incident.
- Check to make sure your feature suggestion isn't already present within the project.
- Check the pull requests tab to ensure that the bug doesn't have a fix in progress.
- Check the pull requests tab to ensure that the feature isn't already in progress.

Before submitting a pull request:

- Check the codebase to ensure that your feature doesn't already exist.
- Check the pull requests to ensure that another person hasn't already submitted the feature or fix.

## Requirements

If the project maintainer has any additional requirements, you will find them listed here.

- **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](https://pear.php.net/package/PHP_CodeSniffer).

- **Add tests!** - Your patch won't be accepted if it doesn't have tests.

- **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date.

- **Consider our release cycle** - We try to follow [SemVer v2.0.0](https://semver.org/). Randomly breaking public APIs is not an option.

- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests.

- **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](https://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting.

**Happy coding**!
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2014 Massimo Lombardo <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
123 changes: 123 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
# ZATCA (Fatoora) QR-Code Implementation

An unofficial package maintained by [Salla](https://salla.dev) to help developers to implement ZATCA (Fatoora) QR code easily which required for e-invoicing

## Requirements

* PHP >= 7.2
* An mbstring extension

## Installation

You can install the package via composer:

```bash
$ composer require salla/zatca
```

## Usage

### Generate Base64

```php
use Salla\ZATCA\GenerateQrCode;
use Salla\ZATCA\Tags\InvoiceDate;
use Salla\ZATCA\Tags\InvoiceTaxAmount;
use Salla\ZATCA\Tags\InvoiceTotalAmount;
use Salla\ZATCA\Tags\Seller;
use Salla\ZATCA\Tags\TaxNumber;

$generatedString = GenerateQrCode::fromArray([
new Seller('Salla'), // seller name
new TaxNumber('1234567891'), // seller tax number
new InvoiceDate('2021-07-12T14:25:09Z'), // invoice date as Zulu ISO8601 @see https://en.wikipedia.org/wiki/ISO_8601
new InvoiceTotalAmount('100.00'), // invoice total amount
new InvoiceTaxAmount('15.00') // invoice tax amount
// TODO :: Support others tags
])->toBase64();

// > Output
// AQVTYWxsYQIKMTIzNDU2Nzg5MQMUMjAyMS0wNy0xMlQxNDoyNTowOVoEBjEwMC4wMAUFMTUuMDA=
```

### Generate Plain

```php
use Salla\ZATCA\GenerateQrCode;
use Salla\ZATCA\Tags\InvoiceDate;
use Salla\ZATCA\Tags\InvoiceTaxAmount;
use Salla\ZATCA\Tags\InvoiceTotalAmount;
use Salla\ZATCA\Tags\Seller;
use Salla\ZATCA\Tags\TaxNumber;

$generatedString = GenerateQrCode::fromArray([
new Seller('Salla'), // seller name
new TaxNumber('1234567891'), // seller tax number
new InvoiceDate('2021-07-12T14:25:09Z'), // invoice date as Zulu ISO8601 @see https://en.wikipedia.org/wiki/ISO_8601
new InvoiceTotalAmount('100.00'), // invoice total amount
new InvoiceTaxAmount('15.00') // invoice tax amount
// TODO :: Support others tags
])->toTLV();
```

## Render A QR Code Image

You can render the tags as QR code image easily


```php
use Salla\ZATCA\GenerateQrCode;
use Salla\ZATCA\Tags\InvoiceDate;
use Salla\ZATCA\Tags\InvoiceTaxAmount;
use Salla\ZATCA\Tags\InvoiceTotalAmount;
use Salla\ZATCA\Tags\Seller;
use Salla\ZATCA\Tags\TaxNumber;

// data:image/png;base64, .........
$displayQRCodeAsBase64 = GenerateQrCode::fromArray([
new Seller('Salla'), // seller name
new TaxNumber('1234567891'), // seller tax number
new InvoiceDate('2021-07-12T14:25:09Z'), // invoice date as Zulu ISO8601 @see https://en.wikipedia.org/wiki/ISO_8601
new InvoiceTotalAmount('100.00'), // invoice total amount
new InvoiceTaxAmount('15.00') // invoice tax amount
// TODO :: Support others tags
])->render();

// now you can inject the output to src of html img tag :)
// <img src="$displayQRCodeAsBase64" alt="QR Code" />
```

## TODO

We'll continue work on this package until support the whole cycle of QR code implementation.

- [ ] Support the digital signature for the QR code.

## Testing

```bash
composer test
```

## Support

Please don't hesitate to contact us using the [</Salla Developers>](https://t.me/salladev) at telegram

- https://t.me/salladev

## Contributing

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

## Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

## Credits

- [Salla](https://github.com/sallaApp)
- [All Contributors](../../contributors)

## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
43 changes: 43 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"name": "salla/zatca",
"description": "A helper to generate the QR code and signed it for ZATCA e-invoicing",
"type": "library",
"keywords": [
"ZATCA",
"e-invocing",
"salla",
"qr-code"
],
"homepage": "https://github.com/salla/php-zatca",
"license": "MIT",
"authors": [
{
"name": "Salla Team",
"email": "[email protected]",
"homepage": "https://salla.dev"
}
],
"support": {
"issues": "https://github.com/salla/php-zatca/issues",
"source": "https://github.com/salla/php-zatca"
},
"minimum-stability": "stable",
"require": {
"php": ">=7.2",
"ext-mbstring": "*",
"chillerlan/php-qrcode": "^4.3"
},
"require-dev": {
"phpunit/phpunit": "~8.0"
},
"autoload": {
"psr-4": {
"Salla\\ZATCA\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Salla\\ZATCA\\Test\\": "tests/"
}
}
}
75 changes: 75 additions & 0 deletions src/GenerateQrCode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Salla\ZATCA;

use chillerlan\QRCode\QRCode;
use InvalidArgumentException;

class GenerateQrCode
{
/**
* @var Tag|Tag[] $data The data or list of tags
*/
protected $data = [];

/**
* @param Tag|Tag[] $data The data or list of tags
*
* @throws InvalidArgumentException If the TLV data structure
* contains other data than arrays and Tag instances.
*/
private function __construct($data)
{
$this->data = array_filter($data, function ($tag) {
return $tag instanceof Tag;
});

if (\count($this->data) === 0) {
throw new InvalidArgumentException('malformed data structure');
}
}

/**
* Initial the generator from list of tags.
*
* @param Tag[] $data The list of tags
*
* @return GenerateQrCode
*/
public static function fromArray(array $data): GenerateQrCode
{
return new self($data);
}

/**
* Encodes an TLV data structure.
*
* @return string Returns a string representing the encoded TLV data structure.
*/
public function toTLV(): string
{
return implode('', array_map(function ($tag) {
return (string) $tag;
}, $this->data));
}

/**
* Encodes an TLV as base64
*
* @return string Returns the TLV as base64 encode.
*/
public function toBase64(): string
{
return base64_encode($this->toTLV());
}

/**
* Render the QR code as base64 data image.
*
* @return string
*/
public function render(): string
{
return (new QRCode)->render($this->toBase64());
}
}
Loading

0 comments on commit f3d99bd

Please sign in to comment.