Metadata-Version: 2.5
Name: cemacqr
Version: 0.1.1
Summary: Build, read and check the CEMAC common QR code (EMV QRCPS v1.1), with no dependencies
Project-URL: Homepage, https://github.com/bryand410/cemacqr
Project-URL: Issues, https://github.com/bryand410/cemacqr/issues
Project-URL: Changelog, https://github.com/bryand410/cemacqr/blob/main/CHANGELOG.md
Author: Ulrich Bryand Tamouffe Teyo
License: 
                                         Apache License
                                   Version 2.0, January 2004
                                http://www.apache.org/licenses/
        
           TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
        
           1. Definitions.
        
              "License" shall mean the terms and conditions for use, reproduction,
              and distribution as defined by Sections 1 through 9 of this document.
        
              "Licensor" shall mean the copyright owner or entity authorized by
              the copyright owner that is granting the License.
        
              "Legal Entity" shall mean the union of the acting entity and all
              other entities that control, are controlled by, or are under common
              control with that entity. For the purposes of this definition,
              "control" means (i) the power, direct or indirect, to cause the
              direction or management of such entity, whether by contract or
              otherwise, or (ii) ownership of fifty percent (50%) or more of the
              outstanding shares, or (iii) beneficial ownership of such entity.
        
              "You" (or "Your") shall mean an individual or Legal Entity
              exercising permissions granted by this License.
        
              "Source" form shall mean the preferred form for making modifications,
              including but not limited to software source code, documentation
              source, and configuration files.
        
              "Object" form shall mean any form resulting from mechanical
              transformation or translation of a Source form, including but
              not limited to compiled object code, generated documentation,
              and conversions to other media types.
        
              "Work" shall mean the work of authorship, whether in Source or
              Object form, made available under the License, as indicated by a
              copyright notice that is included in or attached to the work
              (an example is provided in the Appendix below).
        
              "Derivative Works" shall mean any work, whether in Source or Object
              form, that is based on (or derived from) the Work and for which the
              editorial revisions, annotations, elaborations, or other modifications
              represent, as a whole, an original work of authorship. For the purposes
              of this License, Derivative Works shall not include works that remain
              separable from, or merely link (or bind by name) to the interfaces of,
              the Work and Derivative Works thereof.
        
              "Contribution" shall mean any work of authorship, including
              the original version of the Work and any modifications or additions
              to that Work or Derivative Works thereof, that is intentionally
              submitted to Licensor for inclusion in the Work by the copyright owner
              or by an individual or Legal Entity authorized to submit on behalf of
              the copyright owner. For the purposes of this definition, "submitted"
              means any form of electronic, verbal, or written communication sent
              to the Licensor or its representatives, including but not limited to
              communication on electronic mailing lists, source code control systems,
              and issue tracking systems that are managed by, or on behalf of, the
              Licensor for the purpose of discussing and improving the Work, but
              excluding communication that is conspicuously marked or otherwise
              designated in writing by the copyright owner as "Not a Contribution."
        
              "Contributor" shall mean Licensor and any individual or Legal Entity
              on behalf of whom a Contribution has been received by Licensor and
              subsequently incorporated within the Work.
        
           2. Grant of Copyright License. Subject to the terms and conditions of
              this License, each Contributor hereby grants to You a perpetual,
              worldwide, non-exclusive, no-charge, royalty-free, irrevocable
              copyright license to reproduce, prepare Derivative Works of,
              publicly display, publicly perform, sublicense, and distribute the
              Work and such Derivative Works in Source or Object form.
        
           3. Grant of Patent License. Subject to the terms and conditions of
              this License, each Contributor hereby grants to You a perpetual,
              worldwide, non-exclusive, no-charge, royalty-free, irrevocable
              (except as stated in this section) patent license to make, have made,
              use, offer to sell, sell, import, and otherwise transfer the Work,
              where such license applies only to those patent claims licensable
              by such Contributor that are necessarily infringed by their
              Contribution(s) alone or by combination of their Contribution(s)
              with the Work to which such Contribution(s) was submitted. If You
              institute patent litigation against any entity (including a
              cross-claim or counterclaim in a lawsuit) alleging that the Work
              or a Contribution incorporated within the Work constitutes direct
              or contributory patent infringement, then any patent licenses
              granted to You under this License for that Work shall terminate
              as of the date such litigation is filed.
        
           4. Redistribution. You may reproduce and distribute copies of the
              Work or Derivative Works thereof in any medium, with or without
              modifications, and in Source or Object form, provided that You
              meet the following conditions:
        
              (a) You must give any other recipients of the Work or
                  Derivative Works a copy of this License; and
        
              (b) You must cause any modified files to carry prominent notices
                  stating that You changed the files; and
        
              (c) You must retain, in the Source form of any Derivative Works
                  that You distribute, all copyright, patent, trademark, and
                  attribution notices from the Source form of the Work,
                  excluding those notices that do not pertain to any part of
                  the Derivative Works; and
        
              (d) If the Work includes a "NOTICE" text file as part of its
                  distribution, then any Derivative Works that You distribute must
                  include a readable copy of the attribution notices contained
                  within such NOTICE file, excluding those notices that do not
                  pertain to any part of the Derivative Works, in at least one
                  of the following places: within a NOTICE text file distributed
                  as part of the Derivative Works; within the Source form or
                  documentation, if provided along with the Derivative Works; or,
                  within a display generated by the Derivative Works, if and
                  wherever such third-party notices normally appear. The contents
                  of the NOTICE file are for informational purposes only and
                  do not modify the License. You may add Your own attribution
                  notices within Derivative Works that You distribute, alongside
                  or as an addendum to the NOTICE text from the Work, provided
                  that such additional attribution notices cannot be construed
                  as modifying the License.
        
              You may add Your own copyright statement to Your modifications and
              may provide additional or different license terms and conditions
              for use, reproduction, or distribution of Your modifications, or
              for any such Derivative Works as a whole, provided Your use,
              reproduction, and distribution of the Work otherwise complies with
              the conditions stated in this License.
        
           5. Submission of Contributions. Unless You explicitly state otherwise,
              any Contribution intentionally submitted for inclusion in the Work
              by You to the Licensor shall be under the terms and conditions of
              this License, without any additional terms or conditions.
              Notwithstanding the above, nothing herein shall supersede or modify
              the terms of any separate license agreement you may have executed
              with Licensor regarding such Contributions.
        
           6. Trademarks. This License does not grant permission to use the trade
              names, trademarks, service marks, or product names of the Licensor,
              except as required for reasonable and customary use in describing the
              origin of the Work and reproducing the content of the NOTICE file.
        
           7. Disclaimer of Warranty. Unless required by applicable law or
              agreed to in writing, Licensor provides the Work (and each
              Contributor provides its Contributions) on an "AS IS" BASIS,
              WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
              implied, including, without limitation, any warranties or conditions
              of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
              PARTICULAR PURPOSE. You are solely responsible for determining the
              appropriateness of using or redistributing the Work and assume any
              risks associated with Your exercise of permissions under this License.
        
           8. Limitation of Liability. In no event and under no legal theory,
              whether in tort (including negligence), contract, or otherwise,
              unless required by applicable law (such as deliberate and grossly
              negligent acts) or agreed to in writing, shall any Contributor be
              liable to You for damages, including any direct, indirect, special,
              incidental, or consequential damages of any character arising as a
              result of this License or out of the use or inability to use the
              Work (including but not limited to damages for loss of goodwill,
              work stoppage, computer failure or malfunction, or any and all
              other commercial damages or losses), even if such Contributor
              has been advised of the possibility of such damages.
        
           9. Accepting Warranty or Additional Liability. While redistributing
              the Work or Derivative Works thereof, You may choose to offer,
              and charge a fee for, acceptance of support, warranty, indemnity,
              or other liability obligations and/or rights consistent with this
              License. However, in accepting such obligations, You may act only
              on Your own behalf and on Your sole responsibility, not on behalf
              of any other Contributor, and only if You agree to indemnify,
              defend, and hold each Contributor harmless for any liability
              incurred by, or claims asserted against, such Contributor by reason
              of your accepting any such warranty or additional liability.
        
           END OF TERMS AND CONDITIONS
        
           APPENDIX: How to apply the Apache License to your work.
        
              To apply the Apache License to your work, attach the following
              boilerplate notice, with the fields enclosed by brackets "[]"
              replaced with your own identifying information. (Don't include
              the brackets!)  The text should be enclosed in the appropriate
              comment syntax for the file format. We also recommend that a
              file or class name and description of purpose be included on the
              same "printed page" as the copyright notice for easier
              identification within third-party archives.
        
           Copyright [yyyy] [name of copyright owner]
        
           Licensed under the Apache License, Version 2.0 (the "License");
           you may not use this file except in compliance with the License.
           You may obtain a copy of the License at
        
               http://www.apache.org/licenses/LICENSE-2.0
        
           Unless required by applicable law or agreed to in writing, software
           distributed under the License is distributed on an "AS IS" BASIS,
           WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
           See the License for the specific language governing permissions and
           limitations under the License.
License-File: LICENSE
Keywords: africa,beac,cemac,emvco,gimac,interoperability,payments,qr,qrcps
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Financial and Insurance Industry
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Office/Business :: Financial
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Typing :: Typed
Requires-Python: >=3.9
Provides-Extra: dev
Requires-Dist: pytest>=7.4; extra == 'dev'
Requires-Dist: ruff~=0.17.0; extra == 'dev'
Description-Content-Type: text/markdown

# cemacqr

**Build, read and check the CEMAC common QR code.** Zero dependencies, no
registration, no permission to ask for.

[![CI](https://github.com/bryand410/cemacqr/actions/workflows/ci.yml/badge.svg)](https://github.com/bryand410/cemacqr/actions/workflows/ci.yml)
[![PyPI](https://img.shields.io/pypi/v/cemacqr.svg)](https://pypi.org/project/cemacqr/)
[![Python](https://img.shields.io/pypi/pyversions/cemacqr.svg)](https://pypi.org/project/cemacqr/)
[![License](https://img.shields.io/badge/license-Apache--2.0-blue.svg)](LICENSE)

```bash
pip install cemacqr
```

---

## The one thing to know first

**The checksum is a 16-bit CRC, not CRC-32.**

EMV QRCPS v1.1, section 4.7.3.1, says it exactly:

> The checksum shall be calculated according to [ISO/IEC 13239] using the
> polynomial `'1021'` (hex) and initial value `'FFFF'` (hex).

Polynomial `0x1021`, initial value `0xFFFF`, four hexadecimal characters. That is
the variant catalogued as **CRC-16/CCITT-FALSE**.

Several published descriptions of the CEMAC common QR code announce a "CRC-32".
Building from those descriptions produces a payload whose checksum no reader in
the region will accept — and CRC-32 over the same bytes gives `7F7AE5AA` where
the specification's own worked example requires `A13A`. Eight characters where
four are permitted, and not even a truncation of the right value.

The test suite pins the difference against the specification's Annex B.7
example. See [docs/2026-10-emvco-crc.md](docs/2026-10-emvco-crc.md).

---

## Quick start

```python
>>> from cemacqr import parse_payload, validate, CEMAC

>>> qr = parse_payload(
...     "00020101021229300012D156000000000510A93FO3230Q31280012D15600000001030812"
...     "345678520441115802CN5914BEST TRANSPORT6007BEIJING64200002ZH0104最佳运输"
...     "0202北京540523.7253031565502016233030412340603***0708A60086670902ME"
...     "91320016A0112233449988770708123456786304A13A"
... )
>>> qr.merchant_name
'BEST TRANSPORT'
>>> qr.crc_ok
True
>>> qr.amount_decimal
Decimal('23.72')

>>> print(validate(qr, CEMAC))
INVALID (cemac)
  [PROFILE_CURRENCY] ID 53: cemac permits only currency 950; found '156'
  [PROFILE_COUNTRY] ID 58: cemac permits only country codes CM, CF, CG, GA, GQ, TD; found 'CN'
  [CEMAC_DATA_MISSING] ID 50: the CEMAC profile requires the CEMAC payment system
    data object (ID 50) inside the Additional Data Field Template (ID 62); it is absent
  [NOT CHECKED] the Globally Unique Identifier for the Merchant Account Information…
```

Building a merchant code:

```python
from cemacqr import MerchantQr, CEMAC_CURRENCY

qr = (
    MerchantQr()
    .merchant(name="BOULANGERIE AKWA", city="Douala", country="CM")
    .category("5462")
    .currency(CEMAC_CURRENCY)  # "950" — the numeric code, never "XAF"
    .reusable(True)  # omit .amount() and the consumer enters it
    .bill_number("F-2026-0001")
    .account("26", gui="…", merchant_pan="2376000000001")
    .cemac_data(member="31003", account_alias="10790001", notify="https://akwa.cm/pmt/t01")
    .build()
)
print(qr)  # the payload string
print(qr.crc_ok)  # True
```

## En français, en dix lignes

`cemacqr` construit, lit et vérifie le **QR Code communautaire de la CEMAC**
(homologué par le Règlement n° 02/26/CEMAC/UMAC/CORENOFI du 8 avril 2026, lancé
le 29 juillet 2026 à Douala). Aucune dépendance, aucune inscription.

```python
from cemacqr import MerchantQr, CEMAC_CURRENCY, validate, CEMAC

qr = (
    MerchantQr()
    .merchant(name="BOULANGERIE AKWA", city="Douala", country="CM")
    .category("5462")
    .currency(CEMAC_CURRENCY)  # « 950 » — le code numérique, jamais « XAF »
    .account("26", gui="…", merchant_pan="2376000000001")
    .cemac_data(member="31003", notify="https://akwa.cm/pmt/t01")
    .build()
)

print(validate(qr, CEMAC))  # ce qui est vérifié, et ce qui ne peut pas l'être
```

⚠️ **Le point à retenir : le contrôle d'intégrité est un CRC 16 bits**
(polynôme `0x1021`, valeur initiale `0xFFFF`), **pas un CRC-32**. Plusieurs
descriptions publiées du QR CEMAC annoncent un « CRC-32 » : un code construit
d'après elles sera rejeté par tous les lecteurs de la zone. La norme CORENOFI
elle-même tranche — « conformément à [ISO/CEI 13239] en utilisant le polynôme
'1021' (hex) et la valeur initiale 'FFFF' (hex) » — et son propre exemple
reproduit `3FD3` sous cet algorithme.

## Command line

```bash
cemacqr crc    "<payload>"          # recompute the checksum and compare  (exit 1 on mismatch)
cemacqr check  "<payload>"          # validate                              (exit 1 if invalid)
cemacqr check  "<payload>" --profile cemac --json
cemacqr parse  "<payload>" --expand # decode every data object, templates included
cemacqr build  --name "…" --city Douala --country CM --mcc 5462 --gui "…"
cemacqr profiles                    # list profiles and what they cannot check
cemacqr tags                        # the EMVCo data object registry
```

Exit codes are part of the contract: `0` valid, `1` invalid, `2` bad invocation.

## What is checked

Against the base EMVCo profile:

- Payload Format Indicator first and equal to `01`; CRC last and matching
- At least one Merchant Account Information object (IDs `02`–`51`)
- The five mandatory root objects: MCC, currency, country, merchant name, city
- Field formats and maximum lengths for every object in the registry
- Amount syntax — `"98"`, `"98."` and `"98.73"` are all legal; `"98,73"` and
  `"3 705"` are not; zero is not
- Tip and convenience-fee consistency: `55=02` requires `56`, `55=03` requires
  `57` in `00.01`–`99.99`, and either without the matching indicator is a violation
- Nested template rules for `62` and `64`, including the `"***"` prompt sentinel
  and the `A`/`M`/`E` characters of the Additional Consumer Data Request
- IDs `65`–`79` are Reserved for Future Use and shall not appear

Against the CEMAC profile, additionally, all read from the CORENOFI norm
*Spécifications du QR code pour l'acceptation des paiements interopérables dans la
CEMAC*, v2.1 (March 2025):

- currency `950` and one of the six member state codes
- the CEMAC payment system data object (ID `50`) inside template `62` is
  **mandatory**, and is itself a template — a payload without it is well-formed
  EMVCo and not a CEMAC QR code
- inside ID `50`, only IDs `00`–`03` are defined
- IDs `6251` and `6252`, present in the pre-homologation draft, were deleted

## What is **not** checked, and why that is stated out loud

The CORENOFI norm is readable; the text homologated on 8 April 2026 is not. Two
things therefore cannot be settled from the outside:

- the Globally Unique Identifier for the Merchant Account Information template —
  the norm's Annex 4 example carries `0012 A15600000000`, while its Tableau 17
  shows `0014 A0000006561010`. The document disagrees with itself, so neither is
  enforced
- whether the version homologated in April 2026 differs from the v2.1 text of
  March 2025 that is readable today

So `validate(..., CEMAC)` reports `VALID, SUBJECT TO UNCHECKED RULES` rather than
a bare `True`, and `.fully_checked` is `False`. A validator that returns success
for a payload it only partially examined is worse than no validator. The residual
unknowns are listed in `cemacqr.CEMAC.undefined`.

### The norm's own example fails the norm's own currency rule

Annex 4 of the norm ends with the clear-text value of a merchant-presented QR
code, checksum included. Its prose says *"Devise : XAF (FCFA)"*; its normative
table for Transaction Currency says *"La valeur applicable doit toujours être 950
(Valeur numérique à 3 chiffres du FCFA en Afrique Centrale -XAF-)"*; and the
example encodes `5303 952`. **952 is XOF**, the West African franc of the UEMOA.
The Central African franc is 950.

This library enforces the table, not the example, and the disagreement is pinned
in `tests/test_corenofi_example.py` rather than smoothed over. The checksum in
that same example *is* reproduced by this library, which is what makes the reading
trustworthy: `scripts/check_corenofi_example.py` recomputes `3FD3`.

## Design notes

**Zero runtime dependencies.** Not minimalism for its own sake. This is code that
sits in front of a payment: it parses attacker-controlled input, and every
dependency is attack surface that the team integrating it did not choose. It uses
`urllib`-free pure standard library and nothing else, verifiable with:

```bash
PYTHONPATH=src python -S -c "import cemacqr; print(cemacqr.__version__)"
```

`-S` removes `site-packages`, so that command can only succeed through
`PYTHONPATH`.

**Lengths count characters, bytes carry the checksum.** The specification counts
Alphanumeric Special characters; its own example is 248 characters but 260 UTF-8
bytes. A byte-counting parser rejects every merchant name outside the Latin
alphabet — which in a six-country union is the ordinary case, not the edge case.

**Nothing is repaired.** A truncated value, a non-numeric ID, a stray character:
each raises with the character offset. A parser that skips what it cannot read
turns a corrupt QR code into a payment.

**Context is not optional.** EMVCo reuses IDs across templates: ID `01` is the
Point of Initiation Method under the root and the Bill Number under `62`. Lookups
take the parent, and there is a test for exactly this.

## Test suite

272 tests, no network, no fixtures on disk. Two anchors, and they are the only
vectors here that are not something I constructed:

- EMV QRCPS v1.1 Annex B.7, transcribed from the hex dump in the specification
- the CORENOFI norm's Annex 4 example, whose published checksum this library
  recomputes

```bash
python -m pytest
python scripts/check_corenofi_example.py
```

## Status

Version 0.1.1. The EMVCo layer is complete and tested. The CEMAC layer covers
what the CORENOFI norm states and declares the two things it cannot settle.

## Sources

- *EMV QR Code Specification for Payment Systems — Merchant-Presented Mode*, v1.1
  (EMVCo, 27 November 2020) — public access at
  <https://www.emvco.com/emv-technologies/qr-codes/>
- *EMV QR Code Specification for Payment Systems — Consumer-Presented Mode*, v1.1
- *Spécifications du QR code pour l'acceptation des paiements interopérables dans
  la CEMAC*, CORENOFI, v2.1, March 2025 — the norm whose Annex 4 example is tested
  here, and whose Tableau 15 makes ID `6250` mandatory
- *Règlement n° 02/26/CEMAC/UMAC/CORENOFI du 8 avril 2026* homologating the norms
  for the QR code, the automated bill of exchange and the direct debit
- BEAC / GIMAC launch of the common QR code, Douala, 29 July 2026

## Licence

Apache-2.0. See [LICENSE](LICENSE).

The EMVCo specifications kept under `docs/reference/` are development references
only. EMVCo permits reproduction "only pursuant to the applicable agreement
between the user and EMVCo", so they are never redistributed — they are excluded
from both the wheel and the sdist.
