מדריך זה מתעד את ממשק שורת הפקודה, את ה-Python API, את מיקרו-שירות ה-REST ואת צינור האימות של pain001 v0.0.91. כל דגל, כל נקודת קצה וכל התנהגות המפורטים כאן נלקחו מהקוד שנשלח בפועל, ולא משאיפות.
Pain001 תומכת ב13 הגדרות הודעה של ISO 20022: מ-pain.001.001.03 ועד pain.001.001.13 (Customer Credit Transfer Initiation, אחת עשרה גרסאות) ו-pain.008.001.02 / pain.008.001.08 (Customer Direct Debit Initiation).
1. ממשק שורת הפקודה#
קובץ ההרצה pain001 מארגן את הפונקציונליות שלו בתת-פקודות. הרצה עם דגלי יצירה בלבד מפעילה את generate באופן משתמע, כך שאוטומציות קיימות ממשיכות לפעול.
| תת-פקודה | מטרה |
|---|---|
generate | המרת קובץ נתונים ל-XML של ISO 20022 מאומת מול סכימה (פקודת ברירת המחדל). |
validate | אימות נתוני הקלט ללא כתיבת 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] | הצגת כל 13 הגדרות ההודעה הנתמכות. |
inspect <type> [--format json] | מציג את התבנית המצורפת, הסכימה, נתוני הדוגמה ותבניות הקלט הנתמכות לסוג הודעה. |
init <type> [-o FILE] | יצירת תבנית CSV התחלתית עבור סוג הודעה. |
serve [--host] [--port] [--reload] | הפעלת מיקרו-שירות ה-REST מבוסס FastAPI (מחייב את התוספת api). |
mcp | מפעיל את שרת Model Context Protocol המובנה של ספריית הליבה דרך stdio (נדרשת התוספת mcp); השרת המלא מופץ בשם pain001-mcp. |
plugins list / show / disable | מציג את תוספי הטעינה, האימות, הסכימה והכתיבה שנמצאו, מציג אחד מהם, או מסביר כיצד לבטל את גילוי התוספים של צד שלישי. |
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). |
אפשרויות generate
| דגל | תיאור |
|---|---|
-t, --xml-message-type <TYPE> | הגדרת הודעה, לדוגמה pain.001.001.09. |
-d, --data <FILE> | נתוני קלט: .csv, .json, .jsonl, .db / .sqlite, .parquet, או קובצי .gpg / .asc מוצפני PGP. |
-o, --output-dir <DIR> | התיקייה שאליה נכתב ה-XML שנוצר. |
-m, --template <FILE> / -s, --schema <FILE> | דריסת תבנית ה-Jinja2 או סכימת ה-XSD המצורפות. |
-c, --config <FILE> | טעינת ברירות מחדל מפרופיל תצורה (--profile, --show-config). |
--dry-run (כינוי חלופי: --validate-only) | אימות הקלט מול ה-JSON Schema, מול ה-XSD ומול ספר כללי המסלול, ללא כתיבת פלט. |
--scheme <NAME[,NAME]> | אוכף ספר כללים אחד או יותר של סכימה, מופרדים בפסיקים. pain001 plugins list מציג כל ספר כללים מותקן. |
--explain / --format json | עם --scheme, מדפיס רמז לתיקון לכל הפרה, כטקסט או כ-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> | עיבוד במקטעים עם צריכת זיכרון חסומה עבור אצוות גדולות (ברירת מחדל 1,000 עסקאות למקטע; כל מקטע הופך לקובץ XML נפרד עם NbOfTxs ו-CtrlSum מחושבים מחדש). |
--emit-metrics | פליטת מדדי ריצה קריאים למכונה עבור צינורות תצפיתיות. |
--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. |
קודי היציאה ידידותיים ל-CI: 0 הצלחה, 1 כשל אימות, 2 שגיאת שימוש.
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
כל מסמך שנוצר עובר שלוש שכבות לפני שהוא נכתב:
- אימות קלט: כל רשומה נבדקת מול ה-JSON Schema של סוג ההודעה, עם נרמול כינויי שדות ובדיקות תחביר של IBAN/BIC.
- ספר כללי מסלול (אופציונלי): הכללים של מערכת התשלומים שתציינו, מ-SEPA (SCT, Instant, SDD Core, B2B) ועד מערכות לאומיות כמו UK Faster Payments, CHAPS ו-Bacs, ACH והעברות בארה"ב, ו-Swift CBPR+;
pain001 plugins listמציג את כולם. - אימות XSD: ה-XML שנוצר מאומת מול סכימת ISO 20022 הרשמית באמצעות
xmlschemaלפני שנכתב ולו בית אחד לדיסק. כשהתוספת האופציונליתfast(lxml) מותקנת, libxml2 מקבל מסמכים תקינים מהר יותר; מסמך שהוא דוחה נבדק שוב על ידיxmlschema, והכרעתו סופית.
סכומים כספיים מטופלים כ-decimal.Decimal במהלך יצירת ה-XML ואימות המסלול (לעולם לא כמספרי נקודה צפה לפי IEEE 754), וסכומי הבקרה NbOfTxs / CtrlSum מחושבים מחדש מתוך הרשומות המאומתות במקום להסתמך על הקלט.
מעבר ליצירת pain.001, ספריית הליבה כוללת גם מנתח ומחולל דוחות סטטוס pain.002 (כדי שתוכלו לקרוא את תשובת האישור או הדחייה של הבנק) וכן מנתח ומחולל דפי חשבון camt.053 לצורכי התאמות בסוף היום, ולצדם VersionMapper הממיר רשומות בין גרסאות הודעה.
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. מיקרו-שירות REST#
כל נקודות הקצה ממוקמות תחת /api/v1 (עם כינוי /api ללא גרסה):
| מתודה ונתיב | מטרה |
|---|---|
GET /api/v1/health | בדיקת חיות. |
POST /api/v1/validate | אימות רשומות; מחזיר שגיאות ברמת השדה. |
POST /api/v1/generate | יצירת XML סינכרונית. |
POST /api/v1/generate/async | הכנסת אצווה גדולה לתור ליצירה ברקע. |
GET /api/v1/status/{job_id} | תשאול משימה אסינכרונית. |
GET /api/v1/download/{job_id} | הורדת ה-XML המוגמר. |
DELETE /api/v1/jobs/{job_id} | ניקוי משימה שהושלמה. |
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 | מדדי Prometheus. |
תיעוד אינטראקטיבי מוגש בכתובות /api/docs (Swagger UI), /api/redoc ו-/api/reference (Scalar), ומסמך ה-OpenAPI זמין בכתובת /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. נרמול קלט#
Pain001 מתאימה ייצוא נתונים מהעולם האמיתי לרשומות תקפות עוד לפני האימות:
- כינויי שדות: שמות עמודות נפוצים ממערכות ERP ממופים לשדות קנוניים (לדוגמה
amount→payment_amount). - נרמול IBAN / BIC: הסרת רווחים, איחוד אותיות רישיות ולאחר מכן בדיקה (mod-97 לפי ISO 13616 עבור IBAN, מבנה לפי ISO 9362 עבור BIC).
- תאריכים: ניתוח לפי ISO 8601 בתבנית
YYYY-MM-DDעבור תאריכי הביצוע. - סכומים: מנותבים דרך
decimal.Decimal; סכומים פגומים נכשלים באימות במקום להתעגל בשקט. - ערכת תווים: פונקציות עזר לתעתיק מצמצמות את התוכן לערכת התווים הלטינית של ISO 20022 המתקבלת ב-SWIFT וב-SEPA.
- 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. ארכיטקטורת תוספים#
חבילת הכלים ניתנת להרחבה באמצעות ארבע קבוצות נקודות כניסה: pain001.loaders, pain001.validators, pain001.schemes ו-pain001.writers. pain001-loader-xlsx נרשם דרך המנגנון הזה ומאותר אוטומטית עם ההתקנה; pain001-loader-mt101 הוא ספריית ניתוח עצמאית הנצרכת ישירות (וגם על ידי הכלי convert_mt101 של שרת ה-MCP). מתג ניתוק, PAIN001_DISABLE_PLUGINS=1, משבית לחלוטין איתור תוספים של צד שלישי בסביבות נעולות.
6. שערי איכות#
ספריית הליבה מפותחת מול שערים מחמירים וניתנים לאימות: 100% כיסוי שורות וגם הסתעפויות הנאכף ב-CI (--cov-fail-under=100), טיפוסיות mypy מחמירה, 100% כיסוי docstring ובדיקות אבטחה סטטיות (Bandit, pip-audit). עבור כל בניית גרסה נוצר SBOM בפורמט CycloneDX.
המשיכו אל מדריך ההתקנה, אל שרת ה-MCP לסוכני AI או אל מילון מונחי התשלומים.
