Skip to content

Invoice Service

@nx/invoice issues Vietnamese electronic invoices. It reacts to sale-payment events and merchant CDC, builds provider payloads from order + tax data, and drives issuance through two provider-adapter layers (@nx/iiapi, @nx/t-van) over a 3-partition BullMQ pipeline with scheduled batch issuance and buyer-claim self-service.

1. Quick Reference

PropertyValue
Package@nx/invoice
CodeSVC-00150-INVOICE (registered in core ServiceCodes; Kafka client id defaults to SVC-00150-INVOICE_CONSUMER)
TypeMicroservice
RuntimeBun
Base ClassVerifierApplication
Locationpackages/invoice
Base Path/v1/api (no per-service segment)
Dev Port1190 ⚠️ (others use 310x0)
Local nginx upstreaminvoice_upstream → 127.0.0.1:31140 (packages/gateway/local/nginx.conf)
Snowflake IDunset ⚠️ (APP_ENV_..._WORKER_ID not configured)
DB Schemainvoice (8 tables) + tax (TaxInfo, shared)
Binding Namespace@nx/invoice
Ownerinvoice-team

⚠️ Service identity is drifted/unset. Reconcile a unique SVC code, snowflake id, and a 310x0 port before production deploy. See Operations.

2. Purpose & Scope

IncludedExcluded
Issue VN e-invoices (VAT, sale, POS, ticket, internal-delivery)Payment capture (owned by @nx/payment / @nx/mq-pay)
Multi-provider via iiapi (VNIS, VNPAY) + T-VANOrder lifecycle (owned by @nx/sale)
Payment-driven + scheduled + manual + buyer-self-service issuanceMerchant tax-info input (entered in @nx/commerce metadata.tax)
Adjustment / replacement / cancellation of issued invoicesAuthoritative tax-code registry (issued by GDT)
Buyer self-service claim (token + QR + expiry)PDF rendering pipeline (provider-side)
Merchant invoice profile + provider credential vaulting

3. Tech Stack

External:

LibraryPurpose
@venizia/ignisIoC container, DI, base classes, ControllerFactory
honoHTTP server (via IGNIS)
drizzle-ormDB access via PostgresCoreDataSource
@platformatic/kafkaKafka consumer (PAYMENT_SUCCESS, Merchant CDC)
bullmqPartitioned async issuance + claim-expiry queues
ioredisBullMQ backing store + WebSocket emitter
zodRequest/response validation

Internal:

PackagePurpose
@nx/coreAll Invoice/TaxInfo schemas, repositories, KafkaTopics/CDCKafkaTopics, Debezium types, VerifierApplication, createAppConfig
@nx/iiapiE-invoice adapter layer (VNIS + VNPAY); IIAPIProviders
@nx/t-vanT-VAN gateway adapter layer; TVanProviders
@nx/mq-payPayment-success message contract

4. Project Structure

packages/invoice/
├── src/
│   ├── application.ts          # VerifierApplication subclass
│   ├── index.ts                # Entry → bootstrapApplication()
│   ├── migrate.ts              # Migration entry → bootstrapMigration()
│   ├── common/                 # RestPaths, queues, websocket, providers (iiapi mapper), constants
│   ├── components/             # kafka-consumer, invoice-queue, websocket, redis, cron,
│   │                           #   invoice-provider-connection (iiapi), tvan-connection (t-van)
│   ├── controllers/            # 11 REST controllers + definitions
│   ├── datasources/            # PostgresCoreDataSource binding
│   ├── migrations/             # Seeds: VN admin data, configs, webhook configs, permissions
│   ├── models/                 # Request/response schemas (zod)
│   ├── repositories/           # Re-exports from @nx/core (+ TransactionRepository)
│   ├── services/               # 22 services (issuance, worker, queue, provider, claim, tax-info)
│   └── utilities/              # Encryption, tax-code validation, VN number-to-words
├── package.json
└── tsconfig.json

5. Architecture

Detail: see Architecture.

6. Domain Snapshot

Full ERD + per-entity tables: see Domain Model.

7. Surface Summary

REST controllers (full reference rendered live from the service's /doc/openapi.json — see Operations):

ControllerBase pathNotes
InvoiceController/invoicesissue / adjust / cancel / list
InvoiceRequestController/invoice-requestsbuyer-info capture
InvoiceProviderController/invoice-providersper-merchant credentials
InvoiceProviderConfigController/invoice-provider-configsserial + issuance policy
InvoiceConfigMappingController/invoice-config-mappingsprincipal → config routing
MerchantInvoiceProfileController/merchant-invoice-profilesmerchant enrollment
TaxInfoController/tax-infosseller/buyer tax identity
InvoiceOnboardingController/invoice-onboardingguided enrollment flow
BuyerClaimController/invoice-claimbuyer self-service claim
VnAddressController/vn-addressVN administrative-unit lookup
WebhookController/webhooksVNPAY/iiapi + commerce callbacks

Async topics (full reference in API Events):

DirectionCount
Inbound (Kafka)2 — PAYMENT_SUCCESS, Merchant CDC
Outbound (Kafka)0
WebSocket out1 — invoice status updates
BullMQ jobs in2 — issuance (+adjustment sub-type), claim-expiry
BullMQ jobs outself-enqueued (issuance, retry, claim-expiry)

8. Components

ComponentFilePurpose
KafkaConsumerComponentsrc/components/kafka-consumer/component.tsSubscribes PAYMENT_SUCCESS + Merchant CDC → InvoiceWorkerService
InvoiceQueueComponentsrc/components/invoice-queue/component.tsBullMQ: 3 partitions ×
InvoiceWebSocketComponentsrc/components/websocket/component.tsLive issuance-status WS (Redis emitter)
InvoiceProviderConnectionComponentsrc/components/invoice-provider-connection/component.tsRegisters VNIS + per-merchant providers with @nx/iiapi
TVanConnectionComponentsrc/components/tvan-connection/component.tsRegisters T-VAN client with @nx/t-van
RedisComponentsrc/components/redis/component.tsShared Redis (queue + emitter)
CronComponentsrc/components/cron/component.tsScheduled batch issuance

9. Services

22 services. Highlights below; full layering in Architecture.

ServiceFileOne-liner
InvoiceIssuanceServicesrc/services/invoice-actions/invoice-issuance.service.tsBuild payload from order + TaxInfo, call adapter, record audit
InvoiceAdjustmentServicesrc/services/invoice-actions/invoice-adjustment.service.tsPost-issuance adjustment invoice
InvoiceCancellationServicesrc/services/invoice-actions/invoice-cancellation.service.tsCancel issued invoice
InvoiceResolveServicesrc/services/invoice-actions/invoice-resolve.service.tsReconcile provider/tax-authority status
InvoiceIssuanceQueueServicesrc/services/invoice-issuance-queue.service.tsEnqueue + process issuance/claim-expiry; retry policy
InvoiceWorkerServicesrc/services/invoice-worker.service.tsKafka handlers: handlePaymentSuccess, handleMerchantCDC
InvoiceScheduledIssuanceServicesrc/services/invoice-scheduled-issuance.service.tsCron batch issuance
InvoiceProviderServicesrc/services/invoice-provider.service.tsProvider credentials (encrypted)
InvoiceProviderConfigServicesrc/services/invoice-provider-config.service.tsSerial + issuance policy
InvoiceConfigMappingServicesrc/services/invoice-config-mapping.service.tsPrincipal → config routing
InvoiceRequestServicesrc/services/invoice-request.service.tsInvoice-request lifecycle
MerchantInvoiceProfileServicesrc/services/merchant-invoice-profile.service.tsMerchant enrollment
InvoiceOnboardingServicesrc/services/invoice-onboarding.service.tsGuided onboarding flow
TaxInfoServicesrc/services/tax-info.service.tsAuthoritative TaxInfo sync (CDC-sourced)
BuyerClaimService / BuyerClaimDeliveryServicesrc/services/buyer-claim*.service.tsClaim token, QR, delivery
CommerceWebhookServicesrc/services/commerce-webhook.service.tsInbound commerce webhook handler
VNPAYInvoiceService / VNPAYInvoiceWebhookServicesrc/services/third-parties/*.tsVNPAY issuance + inbound webhook
InvoiceSocketEventServicesrc/services/invoice-socket-event.service.tsEmit invoice-status WS events
VnAddressServicesrc/services/vn-address.service.tsVN administrative-unit lookup
AuthorizationServicesrc/services/authorization.service.tsMerchant-level access checks

10. Repositories

All schemas + repositories are owned by @nx/core; this service only re-exports and registers them (plus a local TransactionRepository).

RepositoryTableSource
InvoiceRepositoryinvoice.Invoice@nx/core
InvoiceRequestRepositoryinvoice.InvoiceRequest@nx/core
InvoiceIssuanceRepositoryinvoice.InvoiceIssuance@nx/core
InvoiceAuditTracingRepositoryinvoice.InvoiceAuditTracing@nx/core
InvoiceProviderRepositoryinvoice.InvoiceProvider@nx/core
InvoiceProviderConfigRepositoryinvoice.InvoiceProviderConfig@nx/core
InvoiceConfigMappingRepositoryinvoice.InvoiceConfigMapping@nx/core
MerchantInvoiceProfileRepositoryinvoice.MerchantInvoiceProfile@nx/core
TaxInfoRepositorytax.TaxInfo@nx/core
Vn*RepositoryVn{Province,Ward,AdministrativeUnit}@nx/core

Full field tables: see Domain Model.

11. Entry Points

FilePurpose
src/index.tsService entry → bootstrapApplication()
src/migrate.tsMigration entry → bootstrapMigration()
src/application.tsApplication extends VerifierApplication

Run modes: api, worker (BullMQ + claim-expiry), migrate.

12. Configuration

Env vars + feature flags + seeded data: see Configuration.

13. Operations

Deployment + observability + security + runbook: see Operations.

Proprietary and Confidential. Unauthorized copying, distribution, or use of this software is strictly prohibited.