# Payments Service Idempotency

**Idempotency** is a crucial concept in payment processing systems to ensure that operations like fund transfers are not accidentally performed more than once. This is particularly important in scenarios where network issues, retries, or other uncertainties might lead to the same operation being requested multiple times. In the context of the LHV Connect API, idempotency helps in avoiding duplicate payments when the same request is inadvertently submitted more than once.

**Idempotency and the PAIN.001 File**

For the Payments service in LHV Connect, **idempotency is managed using the `MsgId`** (Message Identification) field in the PAIN.001 XML file. `MsgId` values are unique per client and cannot be reused. They are stored permanently in the backend system and are not archived or removed.&#x20;

Here's how it works:

* **`MsgId` Field**: The `MsgId` field is a unique identifier for each payment instruction. When a payment request is submitted, the system records the `MsgId`. If a subsequent request is made with the same `MsgId`, the system recognizes it as a duplicate and ignores the new request, ensuring that the payment is not processed more than once.
* **Uniqueness of `MsgId`**: It is critical that the `MsgId` is unique for each new payment request. Reusing the same `MsgId` across different requests will trigger the idempotency check, causing the system to reject the duplicate request. Therefore, careful management of `MsgId` values is essential for avoiding unintentional rejections or processing errors.
* **Practical Example**: Suppose a payment initiation request is sent with a `MsgId` of `PAY-12345`. If the payment process needs to be retried due to a timeout or any other issue, resending the request with the same `MsgId` (`PAY-12345`) will not result in a duplicate payment. The system will recognize this `MsgId` and ensure that only one payment is executed.

However, it **does not prevent the processing of payments that have similar amounts, payment details, or other fields**, as long as each request has a unique `MsgId`. This means that two payment requests with identical amounts, recipients, and dates can still be processed separately if their `MsgId` values differ. Therefore, while idempotency protects against accidental re-submission of the same transaction, it does not prevent intentional or unintentional creation of similar but distinct payments.

### Sample

Sample of related PAIN.001 request and response where uniqueness check finds a duplicate message:

<pre class="language-xml"><code class="lang-xml">-- Check is done with MsgId = UNIQUE-1001 field
&#x3C;?xml version="1.0" encoding="UTF-8"?>
&#x3C;Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03">
    &#x3C;CstmrCdtTrfInitn>
        &#x3C;GrpHdr>
            &#x3C;MsgId>PAY-12345&#x3C;/MsgId>
            &#x3C;CreDtTm>2024-08-21T10:00:00&#x3C;/CreDtTm>
            &#x3C;NbOfTxs>1&#x3C;/NbOfTxs>
            &#x3C;CtrlSum>1000.00&#x3C;/CtrlSum>
            &#x3C;InitgPty>
                &#x3C;Nm>Sample Company&#x3C;/Nm>
            &#x3C;/InitgPty>
        &#x3C;/GrpHdr>
        &#x3C;PmtInf>
            &#x3C;PmtInfId>PAYMENT-001&#x3C;/PmtInfId>
            &#x3C;PmtMtd>TRF&#x3C;/PmtMtd>
            &#x3C;BtchBookg>false&#x3C;/BtchBookg>
            &#x3C;NbOfTxs>1&#x3C;/NbOfTxs>
            &#x3C;CtrlSum>1000.00&#x3C;/CtrlSum>
            ...
            
-- Related Response message
&#x3C;?xml version="1.0" encoding="UTF-8" standalone="yes"?>
&#x3C;Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.002.001.03">
<strong>    &#x3C;CstmrPmtStsRpt>
</strong>        &#x3C;GrpHdr>
            &#x3C;MsgId>3000023259&#x3C;/MsgId>
            &#x3C;CreDtTm>2024-08-21T10:00:00.409+01:00&#x3C;/CreDtTm>
            &#x3C;InitgPty>
                &#x3C;Id>
                    &#x3C;OrgId>
                        &#x3C;BICOrBEI>LHVBGB2LXXX&#x3C;/BICOrBEI>
                    &#x3C;/OrgId>
                &#x3C;/Id>
            &#x3C;/InitgPty>
        &#x3C;/GrpHdr>
        &#x3C;OrgnlGrpInfAndSts>
            &#x3C;OrgnlMsgId>PAY-12345&#x3C;/OrgnlMsgId>
            &#x3C;OrgnlMsgNmId>pain.001.001.03&#x3C;/OrgnlMsgNmId>
            &#x3C;GrpSts>RJCT&#x3C;/GrpSts>
            &#x3C;StsRsnInf>
                &#x3C;Rsn>
                    &#x3C;Cd>NARR&#x3C;/Cd>
                &#x3C;/Rsn>
                &#x3C;AddtlInf>Duplicate message.&#x3C;/AddtlInf>
            &#x3C;/StsRsnInf>
        &#x3C;/OrgnlGrpInfAndSts>
    &#x3C;/CstmrPmtStsRpt>
&#x3C;/Document>
</code></pre>

### Summary

Idempotency in the Payments service of LHV Connect is effectively managed through the `MsgId` field in the PAIN.001 file. This ensures that payment requests are processed exactly once, even if they are submitted multiple times, thereby safeguarding against duplicate transactions and ensuring the reliability of the payment process.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.lhv.com/home/connect/services/payments/payments-service-idempotency.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
