Tài liệu tham khảo này mô tả giao diện dòng lệnh, Python API, vi dịch vụ REST và quy trình xác thực của pain001 v0.0.91. Mọi cờ lệnh, điểm cuối và hành vi được liệt kê tại đây đều lấy từ mã nguồn đã phát hành, không phải từ kỳ vọng.
Pain001 hỗ trợ 13 định nghĩa thông điệp ISO 20022: từ pain.001.001.03 đến pain.001.001.13 (Customer Credit Transfer Initiation, tức khởi tạo chuyển khoản tín dụng của khách hàng; mười một phiên bản) và pain.008.001.02 / pain.008.001.08 (Customer Direct Debit Initiation, tức khởi tạo ghi nợ trực tiếp của khách hàng).
1. Giao diện dòng lệnh#
Tệp thực thi pain001 nhóm các chức năng thành những lệnh con. Khi chạy chỉ với các cờ tạo tệp, lệnh generate được gọi ngầm định, nên các quy trình tự động hoá hiện có vẫn tiếp tục hoạt động.
| Lệnh con | Mục đích |
|---|---|
generate | Chuyển một tệp dữ liệu thành XML ISO 20022 đã được xác thực theo lược đồ (lệnh mặc định). |
validate | Xác thực dữ liệu đầu vào mà không ghi XML. |
check <file>... [--scheme] [--format] | Check pain.001 or pain.008 files you already have: the XSD of the message type each file declares, the message definition's cross-element rules, and any scheme profiles, policy or Schematron rulebook you name. |
verify-signature <file>... --cert <PEM> | Verify the XML-DSig signature on each file against the signer's certificate or public key; --trust-embedded-cert checks integrity only (requires the sign extra). |
versions [--format json] | Liệt kê toàn bộ 13 định nghĩa thông điệp được hỗ trợ. |
inspect <type> [--format json] | Hiển thị mẫu đi kèm, lược đồ, dữ liệu ví dụ và các định dạng đầu vào được chấp nhận cho một loại thông điệp. |
init <type> [-o FILE] | Tạo sẵn một mẫu CSV khởi đầu cho một loại thông điệp. |
serve [--host] [--port] [--reload] | Khởi chạy vi dịch vụ REST FastAPI (yêu cầu phần mở rộng api). |
mcp | Khởi chạy máy chủ Model Context Protocol tích hợp sẵn của thư viện lõi qua stdio (cần gói bổ sung mcp); máy chủ đầy đủ được phát hành dưới tên pain001-mcp. |
plugins list / show / disable | Liệt kê các plugin nạp, kiểm tra, quy tắc và ghi đã phát hiện, hiển thị một plugin, hoặc cho biết cách tắt việc phát hiện plugin của bên thứ ba. |
face <file> [--face] [--bindings] [-o] [--loss-report] | Project a pain.001 XML file into another standard's JSON shape, with a report of every element not carried. |
upload [<file>] --sftp <url> | Deliver a file atomically to an SFTP server you trust explicitly (requires the upload extra). |
Tuỳ chọn của generate
| Cờ | Mô tả |
|---|---|
-t, --xml-message-type <TYPE> | Định nghĩa thông điệp, ví dụ pain.001.001.09. |
-d, --data <FILE> | Dữ liệu đầu vào: .csv, .json, .jsonl, .db / .sqlite, .parquet, hoặc tệp mã hoá PGP .gpg / .asc. |
-o, --output-dir <DIR> | Thư mục nhận tệp XML được tạo. |
-m, --template <FILE> / -s, --schema <FILE> | Ghi đè mẫu Jinja2 hoặc lược đồ XSD đi kèm. |
-c, --config <FILE> | Nạp giá trị mặc định từ một hồ sơ cấu hình (--profile, --show-config). |
--dry-run (bí danh --validate-only) | Xác thực dữ liệu đầu vào theo JSON Schema, XSD và bộ quy tắc scheme mà không ghi kết quả. |
--scheme <NAME[,NAME]> | Áp dụng một hoặc nhiều bộ quy tắc của hệ thống thanh toán, phân tách bằng dấu phẩy. pain001 plugins list liệt kê mọi bộ quy tắc đã cài đặt. |
--explain / --format json | Khi dùng cùng --scheme, in gợi ý khắc phục cho từng vi phạm, dưới dạng văn bản hoặc JSON. |
--rules <FILE> | Enforce a private YAML CEL policy before generation (requires the rules extra). |
--schematron <PRESET or FILE> | Apply Schematron business rules: the sepa, fednow or cbpr preset, or your own .sch file. |
--decrypt-key <FILE> / --decrypt-passphrase-env <VAR> | Import a GPG private key, and name the variable that holds its passphrase, for .gpg / .asc input (requires the gpg extra). |
--streaming / --chunk-size <N> | Xử lý theo khối với bộ nhớ giới hạn cho các lô lớn (mặc định 1,000 giao dịch mỗi khối; mỗi khối trở thành một tệp XML riêng với NbOfTxs và CtrlSum được tính lại). |
--emit-metrics | Phát ra số liệu chạy ở định dạng máy đọc được cho các hệ thống giám sát. |
--envelop-bah | Wrap generated payment XML into an ISO 20022 Business Application Header (head.001.001.03) and BizData (head.003.001.01) envelope. |
--bah-sender <BIC/ID> | Sender financial institution BIC or organisation identifier for BAH <Fr>. |
--bah-receiver <BIC/ID> | Receiver financial institution BIC or organisation identifier for BAH <To>. |
--bah-msg-id <ID> | Business Message Identifier for BAH <BizMsgIdr> (defaults to generated UUID). |
--xml-sign-key <FILE> | PEM private key (RSA, or EC P-256/P-384) for a W3C XML Digital Signature (XML-DSig); the algorithm follows the key. |
--xml-sign-cert <FILE> | Optional PEM X.509 certificate to embed in XML-DSig <ds:KeyInfo>. |
--xml-sign-alg <ALG> | rsa-sha256, rsa-pss-sha256, ecdsa-sha256 or ecdsa-sha384; by default an RSA key signs rsa-sha256. |
--xml-sign-passphrase-env <VAR> | Environment variable holding the passphrase for --xml-sign-key. |
--list-templates / --show-template <TYPE> | List the bundled templates, or show one template's metadata, and exit. |
Mã thoát thân thiện với CI: 0 thành công, 1 lỗi xác thực, 2 lỗi cách dùng.
XML signing and verify-signature need the sign extra (pip install "pain001[sign]", which installs cryptography and lxml).
Signatures: pain001 verify-signature
pain001 verify-signature checks the signature on files you received or wrote. Pass the signer's certificate or public key with --cert; that is the trust anchor. --trust-embedded-cert verifies with each file's own certificate instead, which proves the file is unchanged since it was signed but not who signed it. It exits 0 when every file verifies, 1 when any does not, and 2 when it cannot run; --format json prints one report per file. Verification uses exclusive C14N 1.0 and accepts only RSA-SHA256, RSA-PSS and ECDSA signatures with SHA-256 to SHA-512 digests over the whole document; the certificate's chain and validity dates are not checked.
Faces: pain001 face
A face projects a validated pain.001 document into another standard's JSON request body. --face uk_open_banking targets the Open Banking Read/Write API v4.0.1 domestic payment, --face berlin_group targets NextGenPSD2 v1.3.16 payment initiation, and --face iso_2018 renames every element to its ISO 20022 name in snake_case. A face refuses a document that lacks an element its target requires instead of inventing a value; --loss-report lists every source element it did not carry. Faces are outbound only.
Delivery: pain001 upload
pain001 upload sends a finished file to an SFTP server you name, and only after you have trusted its host key: from a known_hosts file (--known-hosts) or an OpenSSH SHA256 fingerprint you verified independently (--accept-host-key). --print-host-key shows the server's fingerprint without logging in. It logs in with a private key (--identity-file, with --passphrase-env), a password read from an environment variable (--password-env), or your SSH agent. The file is written under a temporary name and renamed on the server, so the receiving side never sees a partial file.
2. Python API#
# Generate a validated pain.001.001.09 file from CSV
Mỗi tài liệu được tạo phải vượt qua ba lớp kiểm tra trước khi được ghi:
- Xác thực đầu vào: mỗi bản ghi được kiểm tra theo JSON Schema của loại thông điệp, kèm chuẩn hoá bí danh trường và kiểm tra cú pháp IBAN/BIC.
- Bộ quy tắc scheme (tùy chọn): các quy tắc của hệ thống thanh toán bạn chỉ định, từ SEPA (SCT, Instant, SDD Core, B2B) đến các hệ thống quốc gia như UK Faster Payments, CHAPS và Bacs, ACH và chuyển khoản điện tử của Mỹ, và Swift CBPR+;
pain001 plugins listliệt kê tất cả. - Xác thực XSD: XML sau khi kết xuất được xác thực theo lược đồ ISO 20022 chính thức bằng
xmlschematrước khi một byte nào được ghi xuống đĩa. Khi cài gói bổ sung tùy chọnfast(lxml), libxml2 chấp nhận tài liệu hợp lệ nhanh hơn; tài liệu bị nó từ chối sẽ đượcxmlschemakiểm tra lại, và kết luận đó là cuối cùng.
Số tiền được xử lý dưới dạng decimal.Decimal trong suốt quá trình tạo XML và xác thực scheme (không bao giờ dùng số thực IEEE 754), và các tổng kiểm soát NbOfTxs / CtrlSum được tính lại từ các bản ghi đã xác thực thay vì tin vào dữ liệu đầu vào.
Ngoài việc tạo pain.001, thư viện lõi còn cung cấp bộ phân tích và bộ tạo báo cáo trạng thái pain.002 (để quý vị đọc phản hồi chấp nhận/từ chối của ngân hàng) và bộ phân tích và bộ tạo sao kê camt.053 phục vụ đối soát cuối ngày, cùng một VersionMapper di trú bản ghi giữa các phiên bản thông điệp.
pain001.faces.project_face is the Python entry point for faces. sign_xml_document signs a document with XML-DSig, and verify_signature(signed_xml, trust_anchor=cert_pem) verifies one and returns a SignatureReport (valid, trust, algorithm, signer, reason); both need the sign extra. verify_xml_signature without a key or certificate still trusts the certificate inside the document, with a DeprecationWarning; pain001 0.0.91 refuses it.
Runnable, self-checking scripts for the public API and the CLI live in the library's examples/ directory.
3. Vi dịch vụ REST#
Tất cả các điểm cuối được gắn dưới /api/v1 (kèm bí danh không phiên bản /api):
| Phương thức & đường dẫn | Mục đích |
|---|---|
GET /api/v1/health | Đầu dò kiểm tra hoạt động. |
POST /api/v1/validate | Xác thực các bản ghi; trả về lỗi ở cấp trường. |
POST /api/v1/generate | Tạo XML đồng bộ. |
POST /api/v1/generate/async | Đưa một lô lớn vào hàng đợi để tạo trong nền. |
GET /api/v1/status/{job_id} | Thăm dò trạng thái một tác vụ bất đồng bộ. |
GET /api/v1/download/{job_id} | Tải về tệp XML đã hoàn tất. |
DELETE /api/v1/jobs/{job_id} | Dọn dẹp một tác vụ đã hoàn thành. |
GET /api/v1/faces | List the faces and the binding keys each accepts. |
POST /api/v1/faces/{face} | Project a pain.001 document sent as XML text; returns the payload and the loss report, or 422 with the reason the face refused it. |
GET /metrics | Số liệu Prometheus. |
Tài liệu tương tác được phục vụ tại /api/docs (Swagger UI), /api/redoc và /api/reference (Scalar), với tài liệu OpenAPI tại /openapi.json.
When the PAIN001_API_KEY environment variable is set, every endpoint except /health and /metrics requires it as a bearer token; when it is unset, the API is open. pain001 serve binds to 127.0.0.1 unless you pass --host.
4. Chuẩn hoá dữ liệu đầu vào#
Pain001 chuyển các tệp xuất từ thực tế thành bản ghi hợp lệ trước khi xác thực:
- Bí danh trường: các tên cột ERP phổ biến được ánh xạ về trường chuẩn (ví dụ
amount→payment_amount). - Chuẩn hoá IBAN / BIC: loại bỏ khoảng trắng, chuẩn hoá chữ hoa/chữ thường, sau đó kiểm tra (mod-97 theo ISO 13616 cho IBAN, cấu trúc ISO 9362 cho BIC).
- Ngày tháng: phân tích theo ISO 8601
YYYY-MM-DDcho ngày thực hiện. - Số tiền: được xử lý qua
decimal.Decimal; số tiền sai định dạng sẽ không vượt qua xác thực thay vì bị làm tròn âm thầm. - Bộ ký tự: các bộ chuyển tự đưa nội dung về bộ ký tự Latinh ISO 20022 được SWIFT và SEPA chấp nhận.
- Formula cells: CSV cells that start with
=,+,-or@are kept as supplied by default, so phone numbers and references survive;load_csv_data(path, sanitize_formula=True)escapes them with a leading quote instead.
5. Kiến trúc plugin#
Bộ công cụ có thể mở rộng qua bốn nhóm điểm vào: pain001.loaders, pain001.validators, pain001.schemes và pain001.writers. pain001-loader-xlsx đăng ký qua cơ chế này và được tự động phát hiện khi cài đặt; pain001-loader-mt101 là thư viện phân tích độc lập được dùng trực tiếp (và bởi công cụ convert_mt101 của máy chủ MCP). Công tắc ngắt PAIN001_DISABLE_PLUGINS=1 vô hiệu hoá hoàn toàn việc phát hiện plugin bên thứ ba trong các môi trường bị khoá chặt.
6. Cổng chất lượng#
Thư viện lõi được phát triển theo các cổng kiểm soát nghiêm ngặt, có thể kiểm chứng: bao phủ 100% dòng và nhánh được thực thi trong CI (--cov-fail-under=100), kiểu mypy nghiêm ngặt, bao phủ docstring 100%, và kiểm tra bảo mật (Bandit, pip-audit). Một SBOM CycloneDX được tạo cho mỗi bản dựng phát hành.
Tiếp tục với Hướng dẫn cài đặt, máy chủ MCP cho tác tử AI, hoặc bảng thuật ngữ thanh toán.
