markdown SEPA信用转账(SCT)文件构建

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown SEPA信用转账(SCT)文件构建相关的知识,希望对你有一定的参考价值。

/// <summary>
/// Generate an XML document containing a SEPA Credit Transfer (SCT) 
/// </summary>
/// <param name="refunds">Collection of refunds from the debtor (a company) to the creditor (a customer)</param>
/// <returns>XML document</returns>
private XDocument GenerateXMLFile(IEnumerable<RefundObject> refunds)
{
   XDocument document = null;

   // Get the correct namespace to build the Pain 001.001.03 document
   XNamespace xsi = XNamespace.Get("http://www.w3.org/2001/XMLSchema-instance");
   XNamespace xmlns = "urn:iso:std:iso:20022:tech:xsd:pain.001.001.03";

   // The namespace needs to prefix all the element names down the tree. There is no "inheritance" of namespace in XML, therefore
   // use this anonymous function to concatenate namespace and element name.
   // On the long run, it makes the code (a little) more readable.
   // For example:
   //       new XElement(ns("CreDtTm"), DateTime.Now.ToString("yyyy-MM-ddThh:mm:ss")),
   // instead of 
   //       new XElement(xmlns + "CreDtTm", DateTime.Now.ToString("yyyy-MM-ddThh:mm:ss")),
   Func<string, XName> ns = elementName => xmlns + elementName;

   // get the payment details elements to add in the tree
   var paymentDetails = this.GetPaymentDetails(refunds, ns);

   document = new XDocument(new XDeclaration("1.0", Encoding.UTF8.BodyName, "yes"),
      new XElement(ns("Document"),
         new XAttribute(XNamespace.Xmlns + "xsi", xsi),
         new XElement(ns("CstmrCdtTrfInitn"),

            // Group Header
            new XElement(ns("GrpHdr"),
               new XElement(ns("MsgId"), ["Generate_Unique_ID"]),
               new XElement(ns("CreDtTm"), DateTime.Now.ToString("yyyy-MM-ddThh:mm:ss")),
               new XElement(ns("NbOfTxs"), refunds.Count()),
               new XElement(ns("CtrlSum"), refunds.Sum(x => x.Amount).ToString("#0.00", CultureInfo.InvariantCulture)),
               new XElement(ns("InitgPty"),
                  new XElement(ns("Nm"), "[Name_Of_Initiating_Party]"))

            ), // group header

            // add all the payment elements
            paymentDetails.ToArray()

         ) // CstmrCdtTrfInitn
      ) // Document xmlns
   );

   return document;
}

/// <summary>
/// Get the batch of payments to the creditors
/// </summary>
/// <param name="refunds">Collection of refunds</param>
/// <param name="ns">Delegate method to concatenate the namespace with the element name</param>
/// <returns>XML formatted payment batches</returns>
private IEnumerable<XElement> GetPaymentDetails(IEnumerable<RefundObject> refunds, Func<string, XName> ns)
{
   List<XElement> xelements = new List<XElement>();

   foreach (var refund in refunds)
   {
      // format the data before
      var endtoendid = refund.EndToEndId.HasValue ? refund.EndToEndId : "NOTPROVIDED";

      // add the refund info in the file
      xelements.Add(new XElement(ns("PmtInf"),
          new XElement(ns("PmtInfId"), refund.Id),
          new XElement(ns("PmtMtd"), "TRF"), // Payment method => always TRF = transfer
          new XElement(ns("BtchBookg"), "true"), // True => bulk posting, False => single posting
          new XElement(ns("NbOfTxs"), 1),
          new XElement(ns("CtrlSum"), refund.Amount),
          new XElement(ns("PmtTpInf"),
            new XElement(ns("SvcLvl"),
               new XElement(ns("Cd"), "SEPA"))), // Payment Type Info => SEPA
          new XElement(ns("ReqdExctnDt"), DateTime.Now.ToString("yyyy-MM-dd")), // Requested execution date
          new XElement(ns("Dbtr"),
            new XElement(ns("Nm"), "[Debtor_Name]")),
          new XElement(ns("DbtrAcct"),
            new XElement(ns("Id"),
               new XElement(ns("IBAN"), "[Debtor_IBAN]"))),
          new XElement(ns("DbtrAgt"),
            new XElement(ns("FinInstnId"),
               new XElement(ns("BIC"), "[Debtor_BIC]"))),
          new XElement(ns("ChrgBr"), "SLEV"), // Charge bearer => always "SLEV" in SEPA = share

          new XElement(ns("CdtTrfTxInf"),
             new XElement(ns("PmtId"),
               new XElement(ns("EndToEndId"), endtoendid)),
             new XElement(ns("Amt"),
               new XElement(ns("InstdAmt"),
                  new XAttribute("Ccy", "EUR"), refund.Amount)), // EUR 
             new XElement(ns("CdtrAgt"),
               new XElement(ns("FinInstnId"),
                  new XElement(ns("BIC"), "[Creditor_BIC]"))),
             new XElement(ns("Cdtr"),
               new XElement(ns("Nm"), "[Creditor_Name]")),
             new XElement(ns("CdtrAcct"),
               new XElement(ns("Id"),
                  new XElement(ns("IBAN"), "[Creditor_IBAN]"))),
             new XElement(ns("RmtInf"),
               new XElement(ns("Ustrd"), "[Remittance information]")) // 140 char max
          ) // CdtTrfTxInf
       )); // PmtInf
   }

   return xelements;
}
#### Introduction

SEPA data formats are based on the [ISO 20022 Standard](https://www.iso20022.org/).

The usual banking file format for a SEPA & Non-SEPA Credit Transfer is the Pain (payment initiation) format **001.001.03**.

However there is a specific German credit transfer XML format that is supported only by a limited amount of banks, the Pain **001.003.03**, but since November 2016, the recommended Standard is **001.001.03**.

For more on this, see the [german specifications](http://www.ebics.de/index.php?id=77) in section *DFÜ Agreement Annex 3 – Version 3.0, Chapter 4*.

#### Documentation
* [HypoVereinsbank](https://www.hypovereinsbank.de/content/dam/hypovereinsbank/unternehmen/pdf/Downloadcenter/SEPA-Formats-en.pdf)
* [Rabobank](https://www.rabobank.com/nl/images/format-description-sepa-credit-transfer.pdf)
* [Abbl](https://www.abbl.lu/content/uploads/2017/07/XML-Message-for-SEPA-Credit-Transfer-Initiation_November.2017.pdf)

#### Online validation tools
##### Generic XML

* https://www.w3schools.com/xml/xml_validator.asp
* https://codebeautify.org/xmlvalidator

##### SEPA

* https://xml-validator.nordea.lt/?locale=en
* https://www.mobilefish.com/services/sepa_xml_validation/sepa_xml_validation.php

以上是关于markdown SEPA信用转账(SCT)文件构建的主要内容,如果未能解决你的问题,请参考以下文章

基于python网络编程实现支持购物转账存取钱定时计算利息的信用卡系统

从贝宝转账到信用卡

是否可以从 PayPal 账户向银行账户或信用卡转账?

Stripe 可以同时向客户对象收费和转账吗?

贝宝 ACH 转账?

模拟实现ATM+购物商城的程序