Skip to content

KSeF integration

The KSeF module sends electronic invoices to the National e-Invoice System (Ministry of Finance). Invoices are sent in the background, with automatic retries on errors.

The National e-Invoice System (KSeF) is a platform by the Ministry of Finance for issuing, storing and receiving structured invoices in XML format. The plugin provides tools to integrate WooCommerce with KSeF - it generates invoices in the required XML format and submits them to the system.

Go to WooCommerce > Settings > Polski > PRO Modules > KSeF.

SettingDescription
Enable KSeF integrationActivates the module
EnvironmentTest (sandbox) or Production
API key (token)Authorization token generated in the KSeF portal
Issuer VAT ID (NIP)VAT ID associated with the KSeF account

KSeF provides a test environment (sandbox) for verifying integration. The test environment:

  • does not require a real authorization key
  • accepts invoices in the same format as the production environment
  • does not transmit data to the Tax Office
  • is recommended for initial integration testing

After successful verification in the test environment, switch to the production environment and enter the proper API key.

  1. Log in to the KSeF portal: https://ksef.mf.gov.pl/
  2. Navigate to the token management section
  3. Generate a new token with invoice issuing permissions
  4. Copy the token and paste it in the plugin settings

After enabling the Automatic submission to KSeF option, the plugin sends the invoice to KSeF automatically when its status changes to “Issued”. Submission is handled asynchronously via Action Scheduler.

In the order panel, the “Invoices” meta box contains a Send to KSeF button. Clicking it adds a submission task to the Action Scheduler queue.

The plugin uses Action Scheduler (built into WooCommerce) for asynchronous invoice submission. This means that:

  • submission does not block order processing
  • invoices are sent in a queue, one after another
  • when there is a large number of invoices, the system processes them gradually

The plugin generates invoices in XML format compliant with the KSeF schema (FA(2)). The XML document contains:

  • header with date and invoice type
  • seller data (VAT ID, name, address)
  • buyer data (VAT ID, name, address)
  • invoice line items (name, quantity, net price, VAT rate, value)
  • summary with VAT rate breakdown
  • payment information

The XML is validated before submission. If validation detects errors, the invoice will not be sent, and a detailed message will appear in the log.

After submitting an invoice to KSeF, the plugin tracks its status:

StatusDescription
QueuedInvoice added to the submission queue
SubmittedInvoice submitted to KSeF, awaiting processing
AcceptedInvoice accepted by KSeF, KSeF number assigned
RejectedInvoice rejected - check the error message
ErrorCommunication error with the KSeF API

After an invoice is accepted, the plugin saves the KSeF reference number in the invoice metadata. This number is visible in the order panel and on the PDF printout.

The plugin automatically checks the status of submitted invoices. After submitting an invoice to KSeF, the plugin queries the API for status every few minutes (via Action Scheduler) until it receives an “Accepted” or “Rejected” response.

In case of a communication error with the KSeF API, the plugin applies an exponential backoff mechanism:

AttemptDelay
1st retry5 minutes
2nd retry25 minutes
3rd retry125 minutes

After three failed attempts, the invoice receives the “Error” status and requires manual intervention. The administrator receives an email notification about the failed submission.

Typical causes of errors:

  • invalid or expired API token
  • XML validation errors (e.g. missing buyer data)
  • temporary unavailability of the KSeF API
  • mismatch between issuer VAT ID and token

Action fired before submitting an invoice to KSeF.

/**
* @param int $invoice_id ID faktury
* @param string $xml Wygenerowany XML faktury
*/
do_action('polski_pro_ksef_submit', int $invoice_id, string $xml);

Example:

add_action('polski_pro_ksef_submit', function (int $invoice_id, string $xml): void {
// Zapisanie kopii XML przed wysyłką
$upload_dir = wp_upload_dir();
$xml_path = $upload_dir['basedir'] . '/polski-pro/ksef-xml/';
if (! is_dir($xml_path)) {
wp_mkdir_p($xml_path);
}
file_put_contents(
$xml_path . "invoice-{$invoice_id}.xml",
$xml
);
}, 10, 2);

Action fired after checking an invoice status in KSeF.

/**
* @param int $invoice_id ID faktury
* @param string $status Nowy status (accepted, rejected, error)
* @param string $ksef_number Numer referencyjny KSeF (tylko dla accepted)
*/
do_action('polski_pro_ksef_check_status', int $invoice_id, string $status, string $ksef_number);

Example:

add_action('polski_pro_ksef_check_status', function (int $invoice_id, string $status, string $ksef_number): void {
if ($status === 'accepted') {
// Powiadomienie zewnętrznego systemu o zaakceptowaniu faktury
wp_remote_post('https://erp.example.com/api/ksef-update', [
'body' => wp_json_encode([
'invoice_id' => $invoice_id,
'ksef_number' => $ksef_number,
]),
'headers' => ['Content-Type' => 'application/json'],
]);
}
}, 10, 3);

The plugin logs all KSeF operations in the WooCommerce log. Go to WooCommerce > Status > Logs and select the polski-pro-ksef source.

Logged events:

  • invoice submission (request/response)
  • status check
  • XML validation errors
  • API communication errors
  • submission retries

In the KSeF module settings, a Test Connection button is available. It sends a test request to the KSeF API and verifies:

  • token validity
  • connectivity with the KSeF server
  • VAT ID match with the token
  1. Check the error message in the WooCommerce log
  2. Most common causes: missing buyer VAT ID, invalid VAT rate, incomplete address data
  3. Fix the data and resubmit
  1. Make sure the token has not expired
  2. Check if the token has permissions for issuing invoices
  3. Verify that the VAT ID in the plugin settings matches the one associated with the token
  1. Check if WP-Cron is working properly
  2. Go to Tools > Scheduled Actions and check the queue status
  3. Verify that there are no stuck tasks
This page is for informational purposes only and does not constitute legal advice. Consult a lawyer before implementation. Polski for WooCommerce is open source software (GPLv2) provided without warranty.