Skip to content

Invoice Service

@nx/invoice phát hành hoá đơn điện tử Việt Nam. Nó phản ứng với sự kiện thanh toán từ sale và CDC merchant, dựng payload nhà cung cấp từ dữ liệu đơn hàng + thuế, và điều phối phát hành qua hai lớp adapter nhà cung cấp (@nx/iiapi, @nx/t-van) trên một pipeline BullMQ 3 phân vùng với phát hành theo lô định kỳ và tự khai báo từ người mua.

1. Tham chiếu nhanh

Thuộc tínhGiá trị
Package@nx/invoice
CodeSVC-00150-INVOICE (đã đăng ký trong ServiceCodes của core; Kafka client id mặc định SVC-00150-INVOICE_CONSUMER)
TypeMicroservice
RuntimeBun
Base ClassVerifierApplication
Locationpackages/invoice
Base Path/v1/api (không có segment riêng cho service)
Dev Port1190 ⚠️ (các service khác dùng 310x0)
Local nginx upstreaminvoice_upstream → 127.0.0.1:31140 (packages/gateway/local/nginx.conf)
Snowflake IDchưa đặt ⚠️ (APP_ENV_..._WORKER_ID chưa cấu hình)
DB Schemainvoice (8 bảng) + tax (TaxInfo, dùng chung)
Binding Namespace@nx/invoice
Ownerinvoice-team

⚠️ Định danh service bị lệch/chưa đặt. Cần đối chiếu một SVC code duy nhất, snowflake id, và một port 310x0 trước khi deploy production. Xem Operations.

2. Mục đích & Phạm vi

Bao gồmLoại trừ
Phát hành HĐĐT VN (VAT, sale, POS, vé, xuất nội bộ)Thu tiền thanh toán (thuộc @nx/payment / @nx/mq-pay)
Đa nhà cung cấp qua iiapi (VNIS, VNPAY) + T-VANVòng đời đơn hàng (thuộc @nx/sale)
Phát hành theo thanh toán + định kỳ + thủ công + tự khai báo từ người muaNhập thông tin thuế merchant (nhập trong @nx/commerce metadata.tax)
Điều chỉnh / thay thế / huỷ hoá đơn đã phát hànhCơ quan đăng ký mã số thuế (do GDT cấp)
Người mua tự khai báo (token + QR + hết hạn)Pipeline render PDF (phía nhà cung cấp)
Hồ sơ hoá đơn merchant + lưu trữ an toàn credential nhà cung cấp

3. Tech Stack

Bên ngoài:

Thư việnMục đích
@venizia/ignisIoC container, DI, base class, ControllerFactory
honoHTTP server (qua IGNIS)
drizzle-ormTruy cập DB qua PostgresCoreDataSource
@platformatic/kafkaKafka consumer (PAYMENT_SUCCESS, Merchant CDC)
bullmqQueue phát hành bất đồng bộ phân vùng + hết hạn claim
ioredisBacking store BullMQ + WebSocket emitter
zodValidate request/response

Nội bộ:

PackageMục đích
@nx/coreToàn bộ schema Invoice/TaxInfo, repository, KafkaTopics/CDCKafkaTopics, kiểu Debezium, VerifierApplication, createAppConfig
@nx/iiapiLớp adapter HĐĐT (VNIS + VNPAY); IIAPIProviders
@nx/t-vanLớp adapter cổng T-VAN; TVanProviders
@nx/mq-payHợp đồng thông điệp payment-success

4. Cấu trúc dự án

packages/invoice/
├── src/
│   ├── application.ts          # Lớp con VerifierApplication
│   ├── index.ts                # Entry → bootstrapApplication()
│   ├── migrate.ts              # Entry migration → 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 controller + definitions
│   ├── datasources/            # Binding PostgresCoreDataSource
│   ├── migrations/             # Seed: dữ liệu hành chính VN, configs, webhook configs, permissions
│   ├── models/                 # Schema request/response (zod)
│   ├── repositories/           # Re-export từ @nx/core (+ TransactionRepository)
│   ├── services/               # 22 service (issuance, worker, queue, provider, claim, tax-info)
│   └── utilities/              # Mã hoá, validate mã số thuế, chuyển số thành chữ VN
├── package.json
└── tsconfig.json

5. Kiến trúc

Chi tiết: xem Architecture.

6. Ảnh chụp Domain

ERD đầy đủ + bảng từng entity: xem Domain Model.

7. Tổng quan bề mặt

REST controller (tham chiếu đầy đủ render trực tiếp từ /doc/openapi.json của service — xem Operations):

ControllerBase pathGhi chú
InvoiceController/invoicesphát hành / điều chỉnh / huỷ / liệt kê
InvoiceRequestController/invoice-requeststhu thập thông tin người mua
InvoiceProviderController/invoice-providerscredential theo merchant
InvoiceProviderConfigController/invoice-provider-configsserial + chính sách phát hành
InvoiceConfigMappingController/invoice-config-mappingsđịnh tuyến principal → config
MerchantInvoiceProfileController/merchant-invoice-profilesđăng ký merchant
TaxInfoController/tax-infosđịnh danh thuế người bán/người mua
InvoiceOnboardingController/invoice-onboardingluồng đăng ký có hướng dẫn
BuyerClaimController/invoice-claimngười mua tự khai báo
VnAddressController/vn-addresstra cứu đơn vị hành chính VN
WebhookController/webhookscallback VNPAY/iiapi + commerce

Async topic (tham chiếu đầy đủ tại API Events):

HướngSố lượng
Inbound (Kafka)2 — PAYMENT_SUCCESS, Merchant CDC
Outbound (Kafka)0
WebSocket out1 — cập nhật trạng thái hoá đơn
BullMQ jobs in2 — phát hành (+kiểu phụ adjustment), hết hạn claim
BullMQ jobs outtự enqueue (phát hành, retry, hết hạn claim)

8. Components

ComponentFileMục đích
KafkaConsumerComponentsrc/components/kafka-consumer/component.tsSubscribe PAYMENT_SUCCESS + Merchant CDC → InvoiceWorkerService
InvoiceQueueComponentsrc/components/invoice-queue/component.tsBullMQ: 3 phân vùng ×
InvoiceWebSocketComponentsrc/components/websocket/component.tsWS trạng thái phát hành trực tiếp (Redis emitter)
InvoiceProviderConnectionComponentsrc/components/invoice-provider-connection/component.tsĐăng ký VNIS + provider theo merchant với @nx/iiapi
TVanConnectionComponentsrc/components/tvan-connection/component.tsĐăng ký client T-VAN với @nx/t-van
RedisComponentsrc/components/redis/component.tsRedis dùng chung (queue + emitter)
CronComponentsrc/components/cron/component.tsPhát hành theo lô định kỳ

9. Services

22 service. Điểm nổi bật bên dưới; phân lớp đầy đủ tại Architecture.

ServiceFileTóm tắt
InvoiceIssuanceServicesrc/services/invoice-actions/invoice-issuance.service.tsDựng payload từ đơn hàng + TaxInfo, gọi adapter, ghi audit
InvoiceAdjustmentServicesrc/services/invoice-actions/invoice-adjustment.service.tsHoá đơn điều chỉnh sau phát hành
InvoiceCancellationServicesrc/services/invoice-actions/invoice-cancellation.service.tsHuỷ hoá đơn đã phát hành
InvoiceResolveServicesrc/services/invoice-actions/invoice-resolve.service.tsĐối chiếu trạng thái nhà cung cấp/cơ quan thuế
InvoiceIssuanceQueueServicesrc/services/invoice-issuance-queue.service.tsEnqueue + xử lý phát hành/hết hạn claim; chính sách retry
InvoiceWorkerServicesrc/services/invoice-worker.service.tsKafka handler: handlePaymentSuccess, handleMerchantCDC
InvoiceScheduledIssuanceServicesrc/services/invoice-scheduled-issuance.service.tsPhát hành theo lô bằng cron
InvoiceProviderServicesrc/services/invoice-provider.service.tsCredential nhà cung cấp (đã mã hoá)
InvoiceProviderConfigServicesrc/services/invoice-provider-config.service.tsSerial + chính sách phát hành
InvoiceConfigMappingServicesrc/services/invoice-config-mapping.service.tsĐịnh tuyến principal → config
InvoiceRequestServicesrc/services/invoice-request.service.tsVòng đời invoice-request
MerchantInvoiceProfileServicesrc/services/merchant-invoice-profile.service.tsĐăng ký merchant
InvoiceOnboardingServicesrc/services/invoice-onboarding.service.tsLuồng onboarding có hướng dẫn
TaxInfoServicesrc/services/tax-info.service.tsĐồng bộ TaxInfo có thẩm quyền (nguồn CDC)
BuyerClaimService / BuyerClaimDeliveryServicesrc/services/buyer-claim*.service.tsToken claim, QR, gửi
CommerceWebhookServicesrc/services/commerce-webhook.service.tsHandler webhook commerce inbound
VNPAYInvoiceService / VNPAYInvoiceWebhookServicesrc/services/third-parties/*.tsPhát hành VNPAY + webhook inbound
InvoiceSocketEventServicesrc/services/invoice-socket-event.service.tsPhát sự kiện WS trạng thái hoá đơn
VnAddressServicesrc/services/vn-address.service.tsTra cứu đơn vị hành chính VN
AuthorizationServicesrc/services/authorization.service.tsKiểm tra truy cập ở mức merchant

10. Repositories

Toàn bộ schema + repository thuộc @nx/core; service này chỉ re-export và đăng ký (cộng thêm TransactionRepository cục bộ).

RepositoryBảngNguồn
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

Bảng trường đầy đủ: xem Domain Model.

11. Entry Points

FileMục đích
src/index.tsEntry service → bootstrapApplication()
src/migrate.tsEntry migration → bootstrapMigration()
src/application.tsApplication kế thừa VerifierApplication

Chế độ chạy: api, worker (BullMQ + hết hạn claim), migrate.

12. Configuration

Env var + feature flag + dữ liệu seed: xem Configuration.

13. Operations

Deployment + observability + bảo mật + runbook: xem Operations.

14. Trang liên quan

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