=== Tropratik e-Facture ===
Contributors: tropratik
Tags: factur-x, chorus pro, facturation electronique, woocommerce, e-invoicing
Requires at least: 6.2
Tested up to: 7.1
Requires PHP: 8.1
Stable tag: 1.1.8
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html

Factur-X (EN 16931) invoices for WooCommerce and transmission to Chorus Pro or Super PDP, designed for the French e-invoicing reform.

== Description ==

**Tropratik e-Facture** generates a **PDF invoice** for every WooCommerce order, which can be **sent automatically by email** to your customer. Beyond a standard PDF, the plugin turns your orders into genuine **Factur-X (EN 16931) electronic invoices**: a human-readable PDF embedding the structured XML data required by the authorities.

Transmission is optional. Invoices for the **public sector (B2G)** can be submitted to **Chorus Pro** through the PISTE API. **B2B and B2C** invoices can be submitted to the approved **Super PDP** platform. Each connection provides a test environment and a production mode.

= Key features =

* Automatic generation of **Factur-X / ZUGFeRD** invoices (PDF/A-3 with embedded EN 16931 XML).
* Manual or automatic transmission to **Chorus Pro** for the public sector.
* Manual or automatic transmission to **Super PDP** for B2B and B2C flows.
* Automatic tracking of submission, acceptance, and rejection statuses.
* Configurable numbering: prefix, dynamic tags, periodic reset, and starting number.
* Customization of the logo, colors, font, and invoice template.
* Direct connection to PISTE or connection through a self-hosted VPS proxy.
* Encryption of PISTE, Chorus Pro, Super PDP, and proxy secrets in the database.
* Compatibility with WooCommerce **HPOS** and classic order storage.
* Read-only `tropratik_ef_invoice` field in the WooCommerce REST API.

= Requirements =

The plugin requires WooCommerce, WordPress 6.2 or later, and PHP 8.1 or later. Transmission to a platform requires the corresponding accounts and credentials:

1. For Chorus Pro: a PISTE account, OAuth2 credentials, a Chorus Pro technical account, and a registered API connection.
2. For Super PDP: a Super PDP account and an OAuth2 application associated with the company.

= Learn more =

A detailed overview is available on our website: [Invoice your WooCommerce orders in Factur-X format](https://tropratik.fr/facturez-vos-commandes-woocommerce-au-format-factur-x).

= Trademarks and affiliation =

Tropratik e-Facture is an **independent** product and is not affiliated with or endorsed by the organizations or vendors mentioned. The names **Chorus Pro**, **PISTE**, **Super PDP**, **Factur-X**, and **WooCommerce** are used solely for descriptive purposes and belong to their respective owners.

= Published by =

[Tropratik.fr](https://www.tropratik.fr)

== External services ==

These connections are **optional**. No data is transmitted until you configure your own credentials and trigger a manual or automatic transmission.

**PISTE — French State API gateway**
PISTE authenticates access to Chorus Pro through OAuth2. Data transmitted: PISTE Client ID and Client Secret. Endpoints: https://oauth.piste.gouv.fr and https://sandbox-oauth.piste.gouv.fr.
Terms of use and GDPR: https://piste.gouv.fr/cgu — Legal notice: https://piste.gouv.fr/mentions-legales

**Chorus Pro — public-sector e-invoicing (AIFE)**
Chorus Pro receives invoices intended for the public sector and provides their status. Data transmitted: the Factur-X PDF and its invoice data, the technical account identifier, and the flow identifier for tracking. Endpoints: https://api.piste.gouv.fr/cpro/ and https://sandbox-api.piste.gouv.fr/cpro/.
Terms of use: https://portail.chorus-pro.gouv.fr/aife_csm?id=aife_cgu — Personal data: https://portail.chorus-pro.gouv.fr/aife_csm?id=aife_security — Legal notice: https://portail.chorus-pro.gouv.fr/aife_csm?id=aife_legal

**Super PDP — approved e-invoicing platform**
Super PDP authenticates the plugin through OAuth2, receives B2B/B2C invoices, and provides their lifecycle events. Data transmitted: Super PDP Client ID and Client Secret, the Factur-X PDF and its invoice data, external number, processing rule, and status or payment events. Endpoints: https://api.superpdp.tech/oauth2/token and https://api.superpdp.tech/v1.beta/.
Documentation: https://www.superpdp.tech/documentation

**Optional self-hosted VPS proxy**
To work around a hosting firewall that blocks PISTE, requests can be routed through a proxy that you install and control yourself. This is not a third-party service operated by Tropratik.

== Installation ==

1. Make sure **WooCommerce** is installed and active, and that the server runs **PHP 8.1 or later**.
2. Install the plugin from the WordPress “Plugins” screen or upload its folder to `/wp-content/plugins/`.
3. Activate the plugin from the “Plugins” menu.
4. Open **WooCommerce → Tropratik e-Facture** to configure the issuer, numbering, and invoice appearance.
5. Configure the **Chorus Pro** or **Super PDP** tab as needed, then use the test buttons before the first transmission.

== Frequently Asked Questions ==

= Does the plugin send my invoices automatically? =

Only if you enable automatic transmission for the relevant platform. Chorus Pro handles public-sector invoices; Super PDP handles B2B and B2C invoices. Manual transmission remains available from the orders list.

= What invoice format is generated? =

Invoices are generated in Factur-X PDF/A-3 format with XML compliant with the EN 16931 profile. The issuer remains responsible for tax rules and the information entered in WooCommerce.

= My hosting provider is blocked by PISTE with a 403 error. What can I do? =

The Chorus Pro tab provides a “Via VPS proxy” connection. Install the proxy on a server you control, enter its settings, and then use the test button. This test does not depend on the PISTE credentials or the technical account.

= Can I customize the invoice layout? =

Yes. The “Invoice appearance” tab lets you set the logo, colors, and font. You can also place a custom `invoice.php` template in `wp-content/themes/your-theme/tropratik-e-facture/` to preserve the customization during updates.

= Is the plugin compatible with HPOS? =

Yes. The plugin is compatible with High-Performance Order Storage (HPOS) and classic order storage.

= Can I retrieve invoices through the WooCommerce REST API? =

Yes. The plugin adds a read-only `tropratik_ef_invoice` field to order responses. It includes the number, dates, customer type, Factur-X profile, PDF hash, and Chorus Pro and Super PDP statuses. It never exposes the file path on the server.

= How are sensitive credentials and passwords stored? =

PISTE, Chorus Pro, Super PDP, and proxy secrets are encrypted in the database with libsodium, or with AES-256-GCM through OpenSSL as a fallback. They are decrypted only during API calls.

The key is derived from the security *salts* in `wp-config.php`. If these *salts* are regenerated, the secrets must be entered again. To use an independent key during a migration or restoration, define the following in `wp-config.php`:

`define( 'TROPRATIK_EF_ENCRYPTION_KEY', 'a-long-random-and-secret-string' );`

Credentials stored in plain text by an earlier version are encrypted the next time the form is saved.

== Screenshots ==

1. Example of a generated Factur-X invoice in PDF/A-3 format with embedded EN 16931 XML.
2. WooCommerce orders list with transmission statuses and actions for each platform.
3. “Invoice appearance” tab: logo, colors, and template override from the theme.
4. Chorus Pro and Super PDP tabs: credential configuration, connection tests, and automatic transmission.

== Changelog ==

= 1.1.8 =
* New: OAuth2 connection to Super PDP in sandbox and production, with credential testing and secret encryption.
* New: manual or automatic transmission of B2B/B2C invoices to Super PDP, status tracking, and reporting of received payments.
* New: twice-daily synchronization of Chorus Pro and Super PDP statuses, with order updates.
* New: Super PDP statuses added to the WooCommerce REST API `tropratik_ef_invoice` field.
* New: BAR/B2C marker added to consumer invoices and checked before submission.
* Fix: VAT categories and breakdowns now use the actual WooCommerce rates, including decimal reduced rates, the zero rate, shipping, and fees.
* Fix: the buyer type no longer forces an incorrect VAT exemption; EN 16931 inconsistencies now block generation.
* Fix: automatic transmission is attempted again when an invoice already exists, particularly for orders created in the admin area.
* Fix: the company name entered at checkout is explicitly saved, and the customer type is preserved during regeneration.

= 1.1.7 =
* New: configurable starting number for continuing an existing sequence. It applies only to the next invoice, after which automatic numbering resumes.
* New: server-side validation prevents a starting number lower than the current sequence maximum.
* New: live preview of the formatted invoice number in the settings.

= 1.1.6 =
* Fix: the logo width setting from 10 to 80 mm is correctly applied to the generated PDF.
* Change: default logo width reduced from 60 to 30 mm and centralized in a constant.
* Fix: BT-23 billing mode labels aligned with the DGFiP v3.2 specifications; codes absent from the specification removed.
* Fix: PHP parse error that prevented access to the appearance settings.
* Fix: compatibility declared with WordPress 7.1.

= 1.1.5 =
* New: configurable BT-23 billing mode based on the French reform process codes.
* New: mandatory French legal notices added to the Factur-X XML in accordance with BR-FR-05.
* New: handling of prepaid amounts, net amount due, and due date for already-paid modes.
* Fix: BT-30 and BT-47 use the 9-digit SIREN in accordance with BR-FR-10 and BR-FR-32.
* Fix: BT-32 tax identifier added for sellers under the French VAT exemption scheme.
* Fix: prohibited exemption amounts and reasons removed from line-level VAT breakdowns.

= 1.1.4 =
* New: PEPPOL `0225` addressing for BT-34 and BT-49 with SIREN, SIREN+SIRET, suffix, or routing code formats.
* New: seller BT-29 and buyer BT-46 `GlobalID` identifiers, buyer reference BT-10, date BT-72, and delivery address BG-15.
* New: required SIRET and PEPPOL fields for B2B customers, with a preview of the calculated address.
* Fix: BT-2 uses the invoice generation date, and BT-95 uses `E` for a seller under the French VAT exemption scheme.
* Fix: minimum WordPress version raised to 6.2.

= 1.1.3 =
* Fix: BT-34 and BT-49 electronic addresses now use a valid French EAS identifier.
* Fix: IBAN and BIC added for bank transfers in accordance with BR-61.
* Fix: VATEX codes added to BT-121 according to the exemption reason.
* Fix: WooCommerce payment methods mapped to the appropriate UNTDID 4461 codes.
* Fix: secured migration of the numbering table from versions 1.0.x.

= 1.1.2 =
* Fix: WooCommerce REST API HTTP 500 error caused by a method missing from the 1.1.1 package.

= 1.1.1 =
* Change: the formatted REST API number moves from `number` to `formatted_number`; `number` now contains the integer position in the legal sequence.
* New: `file_slug`, `file_name`, and `facturx_profile` added to the REST API invoice field.

= 1.1.0 =
* New: read-only `tropratik_ef_invoice` field in the WooCommerce orders REST API.
* New: customer type exposed with the values `individual`, `business`, or `public_sector`.
* Fix: issuer name position on the PDF when no logo is set.

= 1.0.0 =
* Initial release.
* Generation of Factur-X EN 16931 invoices.
* Manual or automatic transmission to Chorus Pro through PISTE.
* Configurable numbering and appearance, optional VPS proxy, and HPOS compatibility.

== Upgrade Notice ==

= 1.1.8 =
Adds Super PDP for B2B/B2C invoices, automatic status tracking, and several VAT and Factur-X fixes. Database migrations are automatic.

= 1.1.7 =
Adds a configurable starting number to continue an existing invoice sequence, with duplicate prevention.

= 1.1.6 =
Adds BT-23, legal notices, and prepaid amounts. Fixes SIREN identifiers and adds BT-32 for the French VAT exemption scheme.

= 1.1.4 =
Adds PEPPOL `0225` addressing and fixes the invoice date and VAT category. SIRET becomes mandatory for B2B. Requires WordPress 6.2 or later.

= 1.1.3 =
Fixes electronic addressing, bank transfer IBANs, exemption codes, and payment methods. Also secures the database migration.

= 1.1.1 =
The formatted REST API number moves to `formatted_number`, while `number` contains the integer sequence. Also adds `facturx_profile`.

= 1.1.0 =
Adds a read-only invoice field to the WooCommerce REST API and fixes the issuer position on PDFs without a logo.

= 1.0.0 =
Initial plugin release.
