@vicacha-devs/medusa-plugin-einvoicing 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.medusa/server/src/admin/index.cjs +107 -0
- package/.medusa/server/src/admin/index.js +107 -0
- package/.medusa/server/src/api/admin/einvoicing/configs/route.d.ts +3 -0
- package/.medusa/server/src/api/admin/einvoicing/configs/route.js +23 -0
- package/.medusa/server/src/api/admin/einvoicing/configs/route.js.map +1 -0
- package/.medusa/server/src/api/admin/einvoicing/drivers/route.d.ts +2 -0
- package/.medusa/server/src/api/admin/einvoicing/drivers/route.js +6 -0
- package/.medusa/server/src/api/admin/einvoicing/drivers/route.js.map +1 -0
- package/.medusa/server/src/api/admin/einvoicing/route.d.ts +3 -0
- package/.medusa/server/src/api/admin/einvoicing/route.js +13 -0
- package/.medusa/server/src/api/admin/einvoicing/route.js.map +1 -0
- package/.medusa/server/src/api/middlewares.d.ts +2 -0
- package/.medusa/server/src/api/middlewares.js +21 -0
- package/.medusa/server/src/api/middlewares.js.map +1 -0
- package/.medusa/server/src/api/validators.d.ts +34 -0
- package/.medusa/server/src/api/validators.js +29 -0
- package/.medusa/server/src/api/validators.js.map +1 -0
- package/.medusa/server/src/index.d.ts +6 -0
- package/.medusa/server/src/index.js +7 -0
- package/.medusa/server/src/index.js.map +1 -0
- package/.medusa/server/src/modules/einvoicing/drivers/country-drivers.d.ts +34 -0
- package/.medusa/server/src/modules/einvoicing/drivers/country-drivers.js +221 -0
- package/.medusa/server/src/modules/einvoicing/drivers/country-drivers.js.map +1 -0
- package/.medusa/server/src/modules/einvoicing/index.d.ts +35 -0
- package/.medusa/server/src/modules/einvoicing/index.js +7 -0
- package/.medusa/server/src/modules/einvoicing/index.js.map +1 -0
- package/.medusa/server/src/modules/einvoicing/integrations/optional-bridges.d.ts +21 -0
- package/.medusa/server/src/modules/einvoicing/integrations/optional-bridges.js +52 -0
- package/.medusa/server/src/modules/einvoicing/integrations/optional-bridges.js.map +1 -0
- package/.medusa/server/src/modules/einvoicing/models/fiscal-config.d.ts +12 -0
- package/.medusa/server/src/modules/einvoicing/models/fiscal-config.js +14 -0
- package/.medusa/server/src/modules/einvoicing/models/fiscal-config.js.map +1 -0
- package/.medusa/server/src/modules/einvoicing/models/fiscal-document.d.ts +18 -0
- package/.medusa/server/src/modules/einvoicing/models/fiscal-document.js +24 -0
- package/.medusa/server/src/modules/einvoicing/models/fiscal-document.js.map +1 -0
- package/.medusa/server/src/modules/einvoicing/service.d.ts +70 -0
- package/.medusa/server/src/modules/einvoicing/service.js +109 -0
- package/.medusa/server/src/modules/einvoicing/service.js.map +1 -0
- package/.medusa/server/src/sdk/index.d.ts +4 -0
- package/.medusa/server/src/sdk/index.js +5 -0
- package/.medusa/server/src/sdk/index.js.map +1 -0
- package/.medusa/server/src/types/index.d.ts +66 -0
- package/.medusa/server/src/types/index.js +2 -0
- package/.medusa/server/src/types/index.js.map +1 -0
- package/.medusa/server/src/workflows/index.d.ts +43 -0
- package/.medusa/server/src/workflows/index.js +12 -0
- package/.medusa/server/src/workflows/index.js.map +1 -0
- package/README.md +29 -2
- package/package.json +88 -3
- package/src/__tests__/einvoicing.spec.ts +107 -0
- package/src/admin/i18n/en.json +4 -0
- package/src/admin/i18n/es.json +4 -0
- package/src/admin/i18n/index.ts +9 -0
- package/src/admin/routes/einvoicing/page.tsx +92 -0
- package/src/api/admin/einvoicing/configs/route.ts +39 -0
- package/src/api/admin/einvoicing/drivers/route.ts +16 -0
- package/src/api/admin/einvoicing/route.ts +31 -0
- package/src/api/middlewares.ts +28 -0
- package/src/api/validators.ts +30 -0
- package/src/index.ts +9 -0
- package/src/modules/einvoicing/drivers/country-drivers.ts +266 -0
- package/src/modules/einvoicing/index.ts +8 -0
- package/src/modules/einvoicing/integrations/optional-bridges.ts +65 -0
- package/src/modules/einvoicing/models/fiscal-config.ts +14 -0
- package/src/modules/einvoicing/models/fiscal-document.ts +24 -0
- package/src/modules/einvoicing/service.ts +149 -0
- package/src/sdk/index.ts +4 -0
- package/src/types/index.ts +82 -0
- package/src/workflows/index.ts +28 -0
- package/tsconfig.json +17 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/sdk/index.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,mBAAmB,GAAG;IACjC,SAAS,EAAE,mBAAmB;IAC9B,SAAS,EAAE,mBAAmB;CACtB,CAAA"}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
export type FiscalDocumentType = "invoice" | "credit_note" | "debit_note" | "pos_receipt";
|
|
2
|
+
export type FiscalDocumentStatus = "draft" | "submitted" | "accepted" | "rejected" | "cancelled";
|
|
3
|
+
export interface FiscalTaxIdValidation {
|
|
4
|
+
valid: boolean;
|
|
5
|
+
normalized?: string;
|
|
6
|
+
reason?: string;
|
|
7
|
+
}
|
|
8
|
+
export interface FiscalDriverContext {
|
|
9
|
+
order_id: string;
|
|
10
|
+
country_code: string;
|
|
11
|
+
document_type: FiscalDocumentType;
|
|
12
|
+
document_number: string;
|
|
13
|
+
currency_code: string;
|
|
14
|
+
subtotal: number;
|
|
15
|
+
tax_total: number;
|
|
16
|
+
total: number;
|
|
17
|
+
issuer_tax_id: string;
|
|
18
|
+
issuer_name: string;
|
|
19
|
+
buyer_tax_id?: string | null;
|
|
20
|
+
buyer_name?: string | null;
|
|
21
|
+
buyer_email?: string | null;
|
|
22
|
+
is_b2b?: boolean;
|
|
23
|
+
company_id?: string | null;
|
|
24
|
+
items?: Array<{
|
|
25
|
+
title: string;
|
|
26
|
+
quantity: number;
|
|
27
|
+
unit_price: number;
|
|
28
|
+
tax_rate?: number;
|
|
29
|
+
}>;
|
|
30
|
+
settings?: Record<string, unknown> | null;
|
|
31
|
+
}
|
|
32
|
+
export interface FiscalSubmissionResult {
|
|
33
|
+
success: boolean;
|
|
34
|
+
status: FiscalDocumentStatus;
|
|
35
|
+
external_id?: string;
|
|
36
|
+
payload_xml?: string;
|
|
37
|
+
response_payload?: Record<string, unknown>;
|
|
38
|
+
error_message?: string;
|
|
39
|
+
}
|
|
40
|
+
export interface FiscalCountryDriver {
|
|
41
|
+
id: string;
|
|
42
|
+
label: string;
|
|
43
|
+
countryCodes: string[];
|
|
44
|
+
validateTaxId: (taxId: string) => FiscalTaxIdValidation;
|
|
45
|
+
buildPayload: (ctx: FiscalDriverContext) => string;
|
|
46
|
+
submitDocument: (ctx: FiscalDriverContext) => Promise<FiscalSubmissionResult>;
|
|
47
|
+
}
|
|
48
|
+
export interface IssueFiscalDocumentInput {
|
|
49
|
+
order_id: string;
|
|
50
|
+
country_code: string;
|
|
51
|
+
driver_id?: string;
|
|
52
|
+
document_type?: FiscalDocumentType;
|
|
53
|
+
currency_code?: string;
|
|
54
|
+
subtotal?: number;
|
|
55
|
+
tax_total?: number;
|
|
56
|
+
total?: number;
|
|
57
|
+
buyer_tax_id?: string | null;
|
|
58
|
+
buyer_name?: string | null;
|
|
59
|
+
buyer_email?: string | null;
|
|
60
|
+
company_id?: string | null;
|
|
61
|
+
items?: FiscalDriverContext["items"];
|
|
62
|
+
}
|
|
63
|
+
export interface CreateEinvoicingInput {
|
|
64
|
+
label: string;
|
|
65
|
+
active?: boolean;
|
|
66
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/types/index.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { IssueFiscalDocumentInput } from "../types";
|
|
2
|
+
export declare const issueFiscalDocumentStep: import("@medusajs/framework/workflows-sdk").StepFunction<IssueFiscalDocumentInput, {
|
|
3
|
+
id: string;
|
|
4
|
+
order_id: string;
|
|
5
|
+
country_code: string;
|
|
6
|
+
driver_id: string;
|
|
7
|
+
document_type: "invoice" | "credit_note" | "debit_note" | "pos_receipt";
|
|
8
|
+
document_number: string;
|
|
9
|
+
external_id: string | null;
|
|
10
|
+
status: "draft" | "submitted" | "accepted" | "rejected" | "cancelled";
|
|
11
|
+
buyer_tax_id: string | null;
|
|
12
|
+
is_b2b: boolean;
|
|
13
|
+
company_id: string | null;
|
|
14
|
+
pdf_document_id: string | null;
|
|
15
|
+
payload_xml: string | null;
|
|
16
|
+
response_payload: NoInfer<Record<string, unknown>> | null;
|
|
17
|
+
error_message: string | null;
|
|
18
|
+
issued_at: Date | null;
|
|
19
|
+
created_at: Date;
|
|
20
|
+
updated_at: Date;
|
|
21
|
+
deleted_at: Date | null;
|
|
22
|
+
}>;
|
|
23
|
+
export declare const issueFiscalDocumentWorkflow: import("@medusajs/framework/workflows-sdk").ReturnWorkflow<IssueFiscalDocumentInput, {
|
|
24
|
+
id: string;
|
|
25
|
+
order_id: string;
|
|
26
|
+
country_code: string;
|
|
27
|
+
driver_id: string;
|
|
28
|
+
document_type: "invoice" | "credit_note" | "debit_note" | "pos_receipt";
|
|
29
|
+
document_number: string;
|
|
30
|
+
external_id: string | null;
|
|
31
|
+
status: "draft" | "submitted" | "accepted" | "rejected" | "cancelled";
|
|
32
|
+
buyer_tax_id: string | null;
|
|
33
|
+
is_b2b: boolean;
|
|
34
|
+
company_id: string | null;
|
|
35
|
+
pdf_document_id: string | null;
|
|
36
|
+
payload_xml: string | null;
|
|
37
|
+
response_payload: NoInfer<Record<string, unknown>> | null;
|
|
38
|
+
error_message: string | null;
|
|
39
|
+
issued_at: Date | null;
|
|
40
|
+
created_at: Date;
|
|
41
|
+
updated_at: Date;
|
|
42
|
+
deleted_at: Date | null;
|
|
43
|
+
}, []>;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { createStep, createWorkflow, StepResponse, WorkflowResponse, } from "@medusajs/framework/workflows-sdk";
|
|
2
|
+
import { EINVOICING_MODULE } from "../modules/einvoicing";
|
|
3
|
+
export const issueFiscalDocumentStep = createStep("issue-fiscal-document", async (input, { container }) => {
|
|
4
|
+
const service = container.resolve(EINVOICING_MODULE);
|
|
5
|
+
const doc = await service.issueDocument(input, container);
|
|
6
|
+
return new StepResponse(doc);
|
|
7
|
+
});
|
|
8
|
+
export const issueFiscalDocumentWorkflow = createWorkflow("issue-fiscal-document", (input) => {
|
|
9
|
+
const document = issueFiscalDocumentStep(input);
|
|
10
|
+
return new WorkflowResponse(document);
|
|
11
|
+
});
|
|
12
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/workflows/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,EACV,cAAc,EACd,YAAY,EACZ,gBAAgB,GACjB,MAAM,mCAAmC,CAAA;AAC1C,OAAO,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAA;AAIzD,MAAM,CAAC,MAAM,uBAAuB,GAAG,UAAU,CAC/C,uBAAuB,EACvB,KAAK,EAAE,KAA+B,EAAE,EAAE,SAAS,EAAE,EAAE,EAAE;IACvD,MAAM,OAAO,GAAG,SAAS,CAAC,OAAO,CAC/B,iBAAiB,CACS,CAAA;IAC5B,MAAM,GAAG,GAAG,MAAM,OAAO,CAAC,aAAa,CAAC,KAAK,EAAE,SAAS,CAAC,CAAA;IACzD,OAAO,IAAI,YAAY,CAAC,GAAG,CAAC,CAAA;AAC9B,CAAC,CACF,CAAA;AAED,MAAM,CAAC,MAAM,2BAA2B,GAAG,cAAc,CACvD,uBAAuB,EACvB,CAAC,KAA+B,EAAE,EAAE;IAClC,MAAM,QAAQ,GAAG,uBAAuB,CAAC,KAAK,CAAC,CAAA;IAC/C,OAAO,IAAI,gBAAgB,CAAC,QAAQ,CAAC,CAAA;AACvC,CAAC,CACF,CAAA"}
|
package/README.md
CHANGED
|
@@ -1,3 +1,30 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `@vicacha-devs/medusa-plugin-einvoicing`
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Country-agnostic **Electronic Invoicing & Fiscal Compliance** plugin for **Medusa v2** (`>= 2.23.0`).
|
|
4
|
+
|
|
5
|
+
- **Pluggable Country Drivers**:
|
|
6
|
+
- `eu-peppol`: EU Peppol BIS 3.0 (EN 16931 UBL 2.1 + EU VAT ID validation across DE, NL, BE, FR, ES, IT, PL, PT, AT, SE)
|
|
7
|
+
- `co-dian`: Colombia DIAN UBL 2.1 (Resolución 000165 + NIT/CC validation + SHA-384 CUFE)
|
|
8
|
+
- `mx-cfdi`: Mexico SAT CFDI 4.0 (RFC validation + XML/UUID)
|
|
9
|
+
- `generic-webhook`: Country-agnostic UBL 2.1 / SSRF-guarded HTTPS webhook driver
|
|
10
|
+
- **Optional Zero-Hard-Dependency Bridges**:
|
|
11
|
+
- `@vicacha-devs/medusa-plugin-b2b`: Automatically resolves B2B company tax IDs and legal names.
|
|
12
|
+
- `@vicacha-devs/medusa-plugin-documents`: Links generated commercial PDFs (`pdf_document_id`).
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
pnpm add @vicacha-devs/medusa-plugin-einvoicing
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
// medusa-config.ts
|
|
22
|
+
export default defineConfig({
|
|
23
|
+
plugins: [
|
|
24
|
+
{
|
|
25
|
+
resolve: "@vicacha-devs/medusa-plugin-einvoicing",
|
|
26
|
+
options: {},
|
|
27
|
+
},
|
|
28
|
+
],
|
|
29
|
+
})
|
|
30
|
+
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,91 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vicacha-devs/medusa-plugin-einvoicing",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "E-Invoicing plugin for Medusa v2 by Vicacha Devs.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"cfdi",
|
|
7
|
+
"dian",
|
|
8
|
+
"einvoicing",
|
|
9
|
+
"fiscal",
|
|
10
|
+
"medusa",
|
|
11
|
+
"medusa-plugin",
|
|
12
|
+
"medusa-plugin-einvoicing",
|
|
13
|
+
"medusa-plugin-integration",
|
|
14
|
+
"medusa-v2",
|
|
15
|
+
"peppol",
|
|
16
|
+
"ubl-2.1"
|
|
17
|
+
],
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"author": "Vicacha Devs",
|
|
20
|
+
"files": [
|
|
21
|
+
".medusa/server",
|
|
22
|
+
"src",
|
|
23
|
+
"README.md",
|
|
24
|
+
"LICENSE",
|
|
25
|
+
"tsconfig.json"
|
|
26
|
+
],
|
|
27
|
+
"type": "module",
|
|
28
|
+
"exports": {
|
|
29
|
+
"./package.json": "./package.json",
|
|
30
|
+
".": {
|
|
31
|
+
"import": "./.medusa/server/src/index.js",
|
|
32
|
+
"default": "./.medusa/server/src/index.js"
|
|
33
|
+
},
|
|
34
|
+
"./admin": {
|
|
35
|
+
"import": "./.medusa/server/src/admin/index.js",
|
|
36
|
+
"default": "./.medusa/server/src/admin/index.js"
|
|
37
|
+
},
|
|
38
|
+
"./sdk": {
|
|
39
|
+
"import": "./src/sdk/index.ts",
|
|
40
|
+
"default": "./.medusa/server/src/sdk/index.js"
|
|
41
|
+
},
|
|
42
|
+
"./types": {
|
|
43
|
+
"import": "./src/types/index.ts",
|
|
44
|
+
"default": "./.medusa/server/src/types/index.js"
|
|
45
|
+
},
|
|
46
|
+
"./workflows": "./.medusa/server/src/workflows/index.js",
|
|
47
|
+
"./modules/*": "./.medusa/server/src/modules/*/index.js"
|
|
48
|
+
},
|
|
49
|
+
"publishConfig": {
|
|
50
|
+
"access": "public",
|
|
51
|
+
"provenance": true
|
|
52
|
+
},
|
|
53
|
+
"devDependencies": {
|
|
54
|
+
"@medusajs/admin-sdk": "2.23.0",
|
|
55
|
+
"@medusajs/admin-shared": "2.23.0",
|
|
56
|
+
"@medusajs/cli": "2.23.0",
|
|
57
|
+
"@medusajs/framework": "2.23.0",
|
|
58
|
+
"@medusajs/medusa": "2.23.0",
|
|
59
|
+
"@medusajs/types": "2.23.0",
|
|
60
|
+
"@medusajs/ui": "4.3.0",
|
|
61
|
+
"react": "^19.3.1",
|
|
62
|
+
"react-dom": "^19.3.1",
|
|
63
|
+
"tsc-alias": "^1.9.2",
|
|
64
|
+
"typescript": "5.6.2",
|
|
65
|
+
"vite": "^8.3.8",
|
|
66
|
+
"@vicacha-devs/medusa-eslint-config": "1.0.0",
|
|
67
|
+
"@vicacha-devs/medusa-typescript-config": "1.0.0",
|
|
68
|
+
"@vicacha-devs/medusa-plugin-testing": "1.0.0"
|
|
69
|
+
},
|
|
70
|
+
"peerDependencies": {
|
|
71
|
+
"@medusajs/admin-sdk": "2.23.0",
|
|
72
|
+
"@medusajs/admin-shared": "2.23.0",
|
|
73
|
+
"@medusajs/framework": "2.23.0",
|
|
74
|
+
"@medusajs/medusa": "2.23.0",
|
|
75
|
+
"@medusajs/ui": "4.3.0",
|
|
76
|
+
"react": "^19.3.1",
|
|
77
|
+
"react-dom": "^19.3.1",
|
|
78
|
+
"zod": "4.6.7"
|
|
79
|
+
},
|
|
80
|
+
"engines": {
|
|
81
|
+
"node": ">=22.22.0"
|
|
82
|
+
},
|
|
83
|
+
"scripts": {
|
|
84
|
+
"build": "medusa plugin:build && tsc-alias -p tsconfig.json --resolve-full-paths",
|
|
85
|
+
"clean": "rm -rf .medusa dist",
|
|
86
|
+
"dev": "medusa plugin:develop",
|
|
87
|
+
"lint": "medusa lint",
|
|
88
|
+
"test": "node --import ../plugin-testing/register-ts-resolver.mjs --import ../plugin-testing/register-js-extension.mjs --test src/__tests__/*.spec.ts",
|
|
89
|
+
"typecheck": "tsc --noEmit"
|
|
90
|
+
}
|
|
6
91
|
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import assert from "node:assert/strict"
|
|
2
|
+
import { describe, it } from "node:test"
|
|
3
|
+
import {
|
|
4
|
+
ColombiaDianDriver,
|
|
5
|
+
EuPeppolDriver,
|
|
6
|
+
GenericFiscalWebhookDriver,
|
|
7
|
+
MexicoCfdiDriver,
|
|
8
|
+
} from "../modules/einvoicing/drivers/country-drivers.js"
|
|
9
|
+
import {
|
|
10
|
+
resolveOptionalB2BCompany,
|
|
11
|
+
resolveOptionalDocumentsPdfId,
|
|
12
|
+
} from "../modules/einvoicing/integrations/optional-bridges.js"
|
|
13
|
+
|
|
14
|
+
describe("@vicacha-devs/medusa-plugin-einvoicing", () => {
|
|
15
|
+
it("validates country-specific tax IDs across EU Peppol, Colombia DIAN, and Mexico CFDI", () => {
|
|
16
|
+
const eu = new EuPeppolDriver()
|
|
17
|
+
assert.equal(eu.validateTaxId("NL123456789B01").valid, true)
|
|
18
|
+
assert.equal(eu.validateTaxId("INVALID").valid, false)
|
|
19
|
+
|
|
20
|
+
const co = new ColombiaDianDriver()
|
|
21
|
+
assert.equal(co.validateTaxId("900.123.456-7").valid, true)
|
|
22
|
+
assert.equal(co.validateTaxId("123").valid, false)
|
|
23
|
+
|
|
24
|
+
const mx = new MexicoCfdiDriver()
|
|
25
|
+
assert.equal(mx.validateTaxId("XAXX010101000").valid, true)
|
|
26
|
+
assert.equal(mx.validateTaxId("BAD").valid, false)
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
it("generates UBL 2.1 XML and external IDs for EU Peppol and DIAN", async () => {
|
|
30
|
+
const eu = new EuPeppolDriver()
|
|
31
|
+
const res = await eu.submitDocument({
|
|
32
|
+
order_id: "ord_1",
|
|
33
|
+
country_code: "NL",
|
|
34
|
+
document_type: "invoice",
|
|
35
|
+
document_number: "INV-NL-000001",
|
|
36
|
+
currency_code: "EUR",
|
|
37
|
+
subtotal: 100,
|
|
38
|
+
tax_total: 21,
|
|
39
|
+
total: 121,
|
|
40
|
+
issuer_tax_id: "NL123456789B01",
|
|
41
|
+
issuer_name: "Vicacha EU BV",
|
|
42
|
+
buyer_tax_id: "DE123456789",
|
|
43
|
+
buyer_name: "Berlin GmbH",
|
|
44
|
+
})
|
|
45
|
+
assert.equal(res.success, true)
|
|
46
|
+
assert.equal(res.status, "accepted")
|
|
47
|
+
assert.ok(res.external_id?.startsWith("peppol_"))
|
|
48
|
+
assert.ok(res.payload_xml?.includes("urn:cen.eu:en16931:2017"))
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
it("resolves optional B2B company and Documents PDF bridges gracefully when absent or present", async () => {
|
|
52
|
+
assert.equal(await resolveOptionalB2BCompany(null, "comp_1"), null)
|
|
53
|
+
assert.equal(await resolveOptionalDocumentsPdfId(null, "ord_1"), null)
|
|
54
|
+
|
|
55
|
+
const mockContainer = {
|
|
56
|
+
resolve(key: string) {
|
|
57
|
+
if (key === "company") {
|
|
58
|
+
return {
|
|
59
|
+
async retrieveCompany(id: string) {
|
|
60
|
+
return { id, tax_id: "NL999888777B01", name: "Acme B2B BV" }
|
|
61
|
+
},
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
if (key === "documentsModuleService") {
|
|
65
|
+
return {
|
|
66
|
+
async listDocuments() {
|
|
67
|
+
return [{ id: "doc_pdf_1" }]
|
|
68
|
+
},
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return null
|
|
72
|
+
},
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const b2b = await resolveOptionalB2BCompany(mockContainer, "comp_1")
|
|
76
|
+
assert.equal(b2b?.is_b2b, true)
|
|
77
|
+
assert.equal(b2b?.tax_id, "NL999888777B01")
|
|
78
|
+
|
|
79
|
+
const pdfId = await resolveOptionalDocumentsPdfId(mockContainer, "ord_1")
|
|
80
|
+
assert.equal(pdfId, "doc_pdf_1")
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
it("rejects unsafe non-HTTPS fiscal webhook URLs in production", async () => {
|
|
84
|
+
const prev = process.env.NODE_ENV
|
|
85
|
+
process.env.NODE_ENV = "production"
|
|
86
|
+
try {
|
|
87
|
+
const driver = new GenericFiscalWebhookDriver()
|
|
88
|
+
const res = await driver.submitDocument({
|
|
89
|
+
order_id: "ord_2",
|
|
90
|
+
country_code: "US",
|
|
91
|
+
document_type: "invoice",
|
|
92
|
+
document_number: "INV-US-000001",
|
|
93
|
+
currency_code: "USD",
|
|
94
|
+
subtotal: 50,
|
|
95
|
+
tax_total: 0,
|
|
96
|
+
total: 50,
|
|
97
|
+
issuer_tax_id: "12-3456789",
|
|
98
|
+
issuer_name: "Vicacha US Inc",
|
|
99
|
+
settings: { webhook_url: "http://127.0.0.1:4000/fiscal" },
|
|
100
|
+
})
|
|
101
|
+
assert.equal(res.success, false)
|
|
102
|
+
assert.equal(res.status, "rejected")
|
|
103
|
+
} finally {
|
|
104
|
+
process.env.NODE_ENV = prev
|
|
105
|
+
}
|
|
106
|
+
})
|
|
107
|
+
})
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { defineRouteConfig } from "@medusajs/admin-sdk"
|
|
2
|
+
import { Badge, Container, Heading, Table, Text } from "@medusajs/ui"
|
|
3
|
+
import { useEffect, useState } from "react"
|
|
4
|
+
|
|
5
|
+
interface FiscalDocRow {
|
|
6
|
+
id: string
|
|
7
|
+
order_id: string
|
|
8
|
+
country_code: string
|
|
9
|
+
driver_id: string
|
|
10
|
+
document_number: string
|
|
11
|
+
status: string
|
|
12
|
+
is_b2b: boolean
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const EinvoicingPage = () => {
|
|
16
|
+
const [documents, setDocuments] = useState<FiscalDocRow[]>([])
|
|
17
|
+
const [loading, setLoading] = useState(true)
|
|
18
|
+
|
|
19
|
+
useEffect(() => {
|
|
20
|
+
fetch("/admin/einvoicing", { credentials: "include" })
|
|
21
|
+
.then((res) => res.json())
|
|
22
|
+
.then((data: { documents?: FiscalDocRow[] }) => {
|
|
23
|
+
setDocuments(data.documents ?? [])
|
|
24
|
+
})
|
|
25
|
+
.finally(() => setLoading(false))
|
|
26
|
+
}, [])
|
|
27
|
+
|
|
28
|
+
return (
|
|
29
|
+
<Container className="divide-y p-0">
|
|
30
|
+
<div className="px-6 py-4">
|
|
31
|
+
<Heading level="h1">E-Invoicing & Fiscal Compliance</Heading>
|
|
32
|
+
<Text size="small" className="text-ui-fg-subtle">
|
|
33
|
+
Multi-country electronic invoicing (EU Peppol, Colombia DIAN, Mexico
|
|
34
|
+
CFDI, Generic UBL 2.1).
|
|
35
|
+
</Text>
|
|
36
|
+
</div>
|
|
37
|
+
<div className="px-6 py-4">
|
|
38
|
+
{loading ? (
|
|
39
|
+
<Text size="small" className="text-ui-fg-subtle">
|
|
40
|
+
Loading fiscal documents...
|
|
41
|
+
</Text>
|
|
42
|
+
) : documents.length === 0 ? (
|
|
43
|
+
<Text size="small" className="text-ui-fg-subtle">
|
|
44
|
+
No fiscal documents issued yet.
|
|
45
|
+
</Text>
|
|
46
|
+
) : (
|
|
47
|
+
<Table>
|
|
48
|
+
<Table.Header>
|
|
49
|
+
<Table.Row>
|
|
50
|
+
<Table.HeaderCell>Number</Table.HeaderCell>
|
|
51
|
+
<Table.HeaderCell>Order</Table.HeaderCell>
|
|
52
|
+
<Table.HeaderCell>Country / Driver</Table.HeaderCell>
|
|
53
|
+
<Table.HeaderCell>Type</Table.HeaderCell>
|
|
54
|
+
<Table.HeaderCell>Status</Table.HeaderCell>
|
|
55
|
+
</Table.Row>
|
|
56
|
+
</Table.Header>
|
|
57
|
+
<Table.Body>
|
|
58
|
+
{documents.map((doc) => (
|
|
59
|
+
<Table.Row key={doc.id}>
|
|
60
|
+
<Table.Cell className="font-mono">
|
|
61
|
+
{doc.document_number}
|
|
62
|
+
</Table.Cell>
|
|
63
|
+
<Table.Cell className="font-mono">{doc.order_id}</Table.Cell>
|
|
64
|
+
<Table.Cell>
|
|
65
|
+
{doc.country_code} · {doc.driver_id}
|
|
66
|
+
</Table.Cell>
|
|
67
|
+
<Table.Cell>
|
|
68
|
+
<Badge size="2xsmall">{doc.is_b2b ? "B2B" : "B2C"}</Badge>
|
|
69
|
+
</Table.Cell>
|
|
70
|
+
<Table.Cell>
|
|
71
|
+
<Badge
|
|
72
|
+
size="2xsmall"
|
|
73
|
+
color={doc.status === "accepted" ? "green" : "grey"}
|
|
74
|
+
>
|
|
75
|
+
{doc.status}
|
|
76
|
+
</Badge>
|
|
77
|
+
</Table.Cell>
|
|
78
|
+
</Table.Row>
|
|
79
|
+
))}
|
|
80
|
+
</Table.Body>
|
|
81
|
+
</Table>
|
|
82
|
+
)}
|
|
83
|
+
</div>
|
|
84
|
+
</Container>
|
|
85
|
+
)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export const config = defineRouteConfig({
|
|
89
|
+
label: "E-Invoicing",
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
export default EinvoicingPage
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AuthenticatedMedusaRequest,
|
|
3
|
+
MedusaResponse,
|
|
4
|
+
} from "@medusajs/framework/http"
|
|
5
|
+
import { EINVOICING_MODULE } from "../../../../modules/einvoicing"
|
|
6
|
+
import type EinvoicingModuleService from "../../../../modules/einvoicing/service"
|
|
7
|
+
|
|
8
|
+
export async function GET(
|
|
9
|
+
req: AuthenticatedMedusaRequest,
|
|
10
|
+
res: MedusaResponse
|
|
11
|
+
) {
|
|
12
|
+
const service = req.scope.resolve(
|
|
13
|
+
EINVOICING_MODULE
|
|
14
|
+
) as EinvoicingModuleService
|
|
15
|
+
const configs = await service.listFiscalConfigs({})
|
|
16
|
+
res.json({ configs })
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export async function POST(
|
|
20
|
+
req: AuthenticatedMedusaRequest,
|
|
21
|
+
res: MedusaResponse
|
|
22
|
+
) {
|
|
23
|
+
const service = req.scope.resolve(
|
|
24
|
+
EINVOICING_MODULE
|
|
25
|
+
) as EinvoicingModuleService
|
|
26
|
+
const body = req.validatedBody as Record<string, any>
|
|
27
|
+
const config = await service.createFiscalConfigs({
|
|
28
|
+
country_code: String(body.country_code).toUpperCase(),
|
|
29
|
+
driver_id: String(body.driver_id),
|
|
30
|
+
is_active: body.is_active ?? true,
|
|
31
|
+
environment: body.environment ?? "sandbox",
|
|
32
|
+
issuer_tax_id: String(body.issuer_tax_id),
|
|
33
|
+
issuer_name: String(body.issuer_name),
|
|
34
|
+
prefix: body.prefix ?? "INV",
|
|
35
|
+
next_number: Number(body.next_number ?? 1),
|
|
36
|
+
settings: body.settings ?? null,
|
|
37
|
+
})
|
|
38
|
+
res.status(201).json({ config })
|
|
39
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AuthenticatedMedusaRequest,
|
|
3
|
+
MedusaResponse,
|
|
4
|
+
} from "@medusajs/framework/http"
|
|
5
|
+
import { EINVOICING_MODULE } from "../../../../modules/einvoicing"
|
|
6
|
+
import type EinvoicingModuleService from "../../../../modules/einvoicing/service"
|
|
7
|
+
|
|
8
|
+
export async function GET(
|
|
9
|
+
req: AuthenticatedMedusaRequest,
|
|
10
|
+
res: MedusaResponse
|
|
11
|
+
) {
|
|
12
|
+
const service = req.scope.resolve(
|
|
13
|
+
EINVOICING_MODULE
|
|
14
|
+
) as EinvoicingModuleService
|
|
15
|
+
res.json({ drivers: await service.listDrivers() })
|
|
16
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AuthenticatedMedusaRequest,
|
|
3
|
+
MedusaResponse,
|
|
4
|
+
} from "@medusajs/framework/http"
|
|
5
|
+
import { EINVOICING_MODULE } from "../../../modules/einvoicing"
|
|
6
|
+
import type EinvoicingModuleService from "../../../modules/einvoicing/service"
|
|
7
|
+
import type { IssueFiscalDocumentInput } from "../../../types"
|
|
8
|
+
import { issueFiscalDocumentWorkflow } from "../../../workflows"
|
|
9
|
+
|
|
10
|
+
export async function GET(
|
|
11
|
+
req: AuthenticatedMedusaRequest,
|
|
12
|
+
res: MedusaResponse
|
|
13
|
+
) {
|
|
14
|
+
const service = req.scope.resolve(
|
|
15
|
+
EINVOICING_MODULE
|
|
16
|
+
) as EinvoicingModuleService
|
|
17
|
+
const documents = await service.listFiscalDocuments(
|
|
18
|
+
{},
|
|
19
|
+
{ order: { created_at: "DESC" }, take: 50 }
|
|
20
|
+
)
|
|
21
|
+
res.json({ documents })
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export async function POST(
|
|
25
|
+
req: AuthenticatedMedusaRequest,
|
|
26
|
+
res: MedusaResponse
|
|
27
|
+
) {
|
|
28
|
+
const input = req.validatedBody as IssueFiscalDocumentInput
|
|
29
|
+
const { result } = await issueFiscalDocumentWorkflow(req.scope).run({ input })
|
|
30
|
+
res.status(201).json({ document: result })
|
|
31
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import {
|
|
2
|
+
authenticate,
|
|
3
|
+
defineMiddlewares,
|
|
4
|
+
validateAndTransformBody,
|
|
5
|
+
} from "@medusajs/framework/http"
|
|
6
|
+
import {
|
|
7
|
+
issueFiscalDocumentSchema,
|
|
8
|
+
upsertFiscalConfigSchema,
|
|
9
|
+
} from "./validators"
|
|
10
|
+
|
|
11
|
+
export default defineMiddlewares({
|
|
12
|
+
routes: [
|
|
13
|
+
{
|
|
14
|
+
matcher: "/admin/einvoicing*",
|
|
15
|
+
middlewares: [authenticate("user", ["session", "bearer", "api-key"])],
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
matcher: "/admin/einvoicing",
|
|
19
|
+
method: "POST",
|
|
20
|
+
middlewares: [validateAndTransformBody(issueFiscalDocumentSchema)],
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
matcher: "/admin/einvoicing/configs",
|
|
24
|
+
method: "POST",
|
|
25
|
+
middlewares: [validateAndTransformBody(upsertFiscalConfigSchema)],
|
|
26
|
+
},
|
|
27
|
+
],
|
|
28
|
+
})
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { z } from "@medusajs/framework/zod"
|
|
2
|
+
|
|
3
|
+
export const issueFiscalDocumentSchema = z.object({
|
|
4
|
+
order_id: z.string().min(1),
|
|
5
|
+
country_code: z.string().min(2).max(2),
|
|
6
|
+
driver_id: z.string().optional(),
|
|
7
|
+
document_type: z
|
|
8
|
+
.enum(["invoice", "credit_note", "debit_note", "pos_receipt"])
|
|
9
|
+
.optional(),
|
|
10
|
+
currency_code: z.string().min(3).max(3).optional(),
|
|
11
|
+
subtotal: z.number().nonnegative().optional(),
|
|
12
|
+
tax_total: z.number().nonnegative().optional(),
|
|
13
|
+
total: z.number().nonnegative().optional(),
|
|
14
|
+
buyer_tax_id: z.string().nullable().optional(),
|
|
15
|
+
buyer_name: z.string().nullable().optional(),
|
|
16
|
+
buyer_email: z.string().email().nullable().optional(),
|
|
17
|
+
company_id: z.string().nullable().optional(),
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
export const upsertFiscalConfigSchema = z.object({
|
|
21
|
+
country_code: z.string().min(2).max(2),
|
|
22
|
+
driver_id: z.string().min(1),
|
|
23
|
+
is_active: z.boolean().optional(),
|
|
24
|
+
environment: z.enum(["sandbox", "production"]).optional(),
|
|
25
|
+
issuer_tax_id: z.string().min(1),
|
|
26
|
+
issuer_name: z.string().min(1),
|
|
27
|
+
prefix: z.string().min(1).optional(),
|
|
28
|
+
next_number: z.number().int().positive().optional(),
|
|
29
|
+
settings: z.record(z.string(), z.unknown()).nullable().optional(),
|
|
30
|
+
})
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from "./types"
|
|
2
|
+
export {
|
|
3
|
+
EINVOICING_MODULE,
|
|
4
|
+
default as EinvoicingModule,
|
|
5
|
+
} from "./modules/einvoicing"
|
|
6
|
+
export { EinvoicingModuleService } from "./modules/einvoicing/service"
|
|
7
|
+
export * from "./modules/einvoicing/drivers/country-drivers"
|
|
8
|
+
export * from "./modules/einvoicing/integrations/optional-bridges"
|
|
9
|
+
export * from "./workflows"
|