# Receipts (MDN)

Every AS2 message is answered with a receipt, the **MDN** (message disposition notification). It is what makes AS2 more than an upload: the sender holds a signed statement from the receiver that the file arrived, and arrived unchanged.

## What a receipt says

| Part | Says |
|---|---|
| which message | the id of the message it answers |
| who | the AS2 names of both sides |
| the outcome | processed, or failed, with the reason: could not decrypt, signature not valid, and so on |
| the checksum (MIC) | computed by the receiver over what it received. The sender compares it with its own. Equal means: not one byte changed. |
| a signature | the receiver's, if a signed receipt was asked for. It makes the receipt proof: only the receiver could have made it. |

A receipt that says "processed", with a checksum that matches, signed by the partner, is proof of delivery. Anything less is not.

## At once, or later

| | At once (synchronous) | Later (asynchronous) |
|---|---|---|
| how | in the answer to the message itself | in a new connection, from the receiver to an address the sender named |
| needs | nothing more | an address of the sender that the receiver can reach, and a way in |
| use | whenever both can | when the receiver takes long over each message, or by the partner's rule |

The sender chooses, in the message. The receiver follows.

## When this server receives

### Version 1

- A receipt is sent for every message that could be processed, in the way the sender asked for.
- It is **always signed**, with the key that decrypts: the **AS2/3 Decryption Key** of the partner's user. The partner checks it against the certificate they encrypt with.
- A copy is stored beside the received file, under the message's id with `.out.mdn` at the end.
- For a receipt asked for later, the server posts it to the address in the message, right after storing the file.

### Version 2

- The receipt is signed with the station's receipt key, if it has one, or with its decryption key when the station is set up that way, which is what partners of version 1 expect.
- A receipt is asked for later only goes out if the partner entry allows it: never, to listed addresses, or to one fixed address. A sender cannot make the server post to an address of its choosing.
- A message that arrives a second time gets the first receipt again, and the file is not stored twice.
- Receipts are kept in the engine's own record of messages, 30 days unless changed. They are not written beside the files.

## When this server sends

The [AS2 task](Task-AS2.md) asks for a receipt with every message and checks it.

| In the task | Means |
|---|---|
| **Async MDN** off | the receipt is expected in the answer |
| **Async MDN** on, with **Response URL** | the receipt is expected later, at that address of this server |
| **MDN Wait Timeout** | how long the task waits for a later receipt |
| **MIC Algorithm** | the checksum the partner is asked to use. "It is very unusual to change this value at all." |

What the task does with it:

- compares the checksum with its own;
- gives the file `{mdn_result}`: empty when all is well, otherwise what was wrong;
- puts the receipt on the list as an item, so that the job can file it.

Version 1 compares the checksum only. Version 2 also checks the receipt's signature against the partner's certificate and reads the stated outcome, so a receipt that says "failed" with a correct checksum is a failure. See [Sending](AS2-Sending.md).

### Where a later receipt arrives

The partner posts it to the **Response URL**, signing in as a user of this server in the way of [version 1](AS2-Receiving.md#the-as2-name-carries-the-login). The server recognises it as a receipt, hands it to the task that waits, and stores it in that user's folder as a file ending in `.mdn`.

## Keeping receipts

Receipts are business records. Partners and auditors ask for them, sometimes years later.

| Receipt | Is found | Keep it by |
|---|---|---|
| for a file you received (version 1) | beside the file, as `.out.mdn` | moving it with the file, in the job that takes the file on |
| for a file you sent | on the task's list, and for later ones in the receipt user's folder | a [Move](Task-Move.md) to an archive after the AS2 task |
| version 2 | in the engine's record, by the admin commands that list messages | exporting what you must keep before the retention ends |

Name archive folders by date, and keep the sent file and its receipt together.

## Things to know

- **No receipt is not a failure of delivery, and not a success.** The file may have arrived. Ask the partner before sending again: a second send can be booked twice on their side.
- **A receipt that says "failed" came from a partner who did receive something.** Read the reason in it; it is usually exact.
- **Checksums that never match** point at the algorithm or at compression. Both sides must compute over the same bytes: agree SHA-256, and test once without compression.
- **Unsigned receipts** are accepted by many systems and prove little. Ask for signed ones.
- **A partner who cannot check your signed receipt** has the wrong certificate for it: in version 1 it is the certificate of your decryption key.
