مرجع

Pain001: المرجع التقني (CLI وواجهة Python وREST)

كل خيار ونقطة نهاية وسلوك في pain001 v0.0.90، مأخوذ من الشيفرة المُصدرة لا من الطموح.

يوثّق هذا المرجع واجهة سطر الأوامر وواجهة Python البرمجية وخدمة REST المصغّرة ومسار التحقق في pain001 v0.0.90. كل خيار ونقطة نهاية وسلوك مذكور هنا مأخوذ من الشيفرة المُصدرة، لا من الطموح.

يدعم Pain001 13 تعريفًا لرسائل ISO 20022: من pain.001.001.03 إلى pain.001.001.13 (بدء تحويل ائتماني للعميل، أحد عشر إصدارًا) وpain.008.001.02 / pain.008.001.08 (بدء خصم مباشر للعميل).


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 أو ملفات مشفّرة بـ PGP بامتداد .gpg / .asc.
-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 البرمجية#

from pain001.core.core import process_files

# Generate a validated pain.001.001.09 file from CSV
process_files(
    xml_message_type="pain.001.001.09",
    xml_template_file_path="template.xml",
    xsd_schema_file_path="schema.xsd",
    data_file_path="payments.csv",
    output_path="out/payments.xml",
)

يمر كل مستند مولَّد بثلاث طبقات قبل كتابته:

  1. التحقق من الإدخال: يُفحص كل سجل وفق JSON Schema الخاص بنوع الرسالة، مع تطبيع الأسماء البديلة للحقول وفحوص صياغة IBAN/BIC.
  2. كتاب قواعد النظام (اختياري): قواعد نظام الدفع الذي تحدده، من SEPA (SCT وInstant وSDD Core وB2B) إلى الأنظمة الوطنية مثل UK Faster Payments وCHAPS وBacs، وACH والتحويلات الأمريكية، وSwift CBPR+؛ ويسمّيها pain001 plugins list جميعًا.
  3. التحقق وفق 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.90 refuses it.

Runnable, self-checking scripts for the public API and the CLI live in the library's examples/ directory.


3. خدمة REST المصغّرة#

pip install "pain001[api]"
pain001 serve --host 0.0.0.0 --port 8000

جميع نقاط النهاية مركّبة تحت /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% لسلاسل التوثيق، وفحص أمني (Bandit وpip-audit). وتُولَّد قائمة SBOM بصيغة CycloneDX لكل بناء إصدار.

تابع مع دليل التثبيت أو خادم MCP لوكلاء الذكاء الاصطناعي أو مسرد المدفوعات.