# Set up composable tender refunds

**Process refunds to a custom tender through an external module that you build, with InStore recording only the result.**

## How composable tender refunds work

A composable tender refund is a refund that your own module processes inside the InStore POS. InStore loads the module, gives it the refund amount, and waits for the module to report success or cancellation. How the module issues the refund, and which services it calls, is up to you: InStore doesn't call a payment processor endpoint for these refunds.

Use composable tender refunds for tenders that the built-in refund types don't cover, such as store credit, rebates, or trade-in credit. Composable tender refunds use the `Custom` payment type.

Composable tender refunds use the same refund session and webhooks as [delegated refunds](/instore/integration/refunds/delegated-refunds.md). This page describes only what's different for a `Custom` tender. Set up delegated refunds before setting up composable tender refunds.

### Set up a composable tender refund

1. Build and deploy a custom module. If you already use a custom module for checkout payments, you can reuse it. See [Build the module](/instore/integration/refunds/composable-tender-extension-refunds.md#build-the-module).
2. [Create a payment processor](/instore/integration/payments/build-payment-components.md#create-a-payment-processor) with `integrationConfiguration.type` set to `Custom`. Set `integrationConfiguration.url` to the URL of your module's remote entry file, and `integrationConfiguration.module` to the exposed module key, such as `./Component`.
3. [Create a payment option](/instore/integration/payments/build-payment-components.md#create-a-payment-option) with `method` set to `Custom`, and with `paymentProcessorId` set to the processor that you created.
4. In the [refund object](/instore/integration/refunds/delegated-refunds.md#refund-object), reference the payment option in `refunds[].paymentOption`, or in `refunds[].fallbackRefundTypes[].paymentOption` to offer it as a fallback.

```json title="Sample Create Payment Processor request body: Custom"
{
  "key": "processor-store-credit-1",
  "name": {
    "en-US": "Store credit"
  },
  "integrationConfiguration": {
    "type": "Custom",
    "url": "https://modules.example.com/store-credit/remoteEntry.js",
    "module": "./Component",
    "process": "store-credit"
  }
}
```

`integrationConfiguration.process` is optional. InStore doesn't interpret it, and passes it to your module as the `process` property. Use it to run different flows from one module.

```json title="Sample instruction for a composable tender refund"
{
  "refundAmount": { "amount": 1500, "currency": "USD" },
  "paymentOption": { "typeId": "paymentOption", "key": "option-store-credit-us-1" },
  "order": { "typeId": "order", "key": "order-1" },
  "originalCTPayment": { "typeId": "payment", "id": "pay_3Nk82jLkQ4eR1mWx" },
  "metaData": {
    "prompt": "Refund will be made to store credit",
    "promptInfo": "Store credit: -$15.00"
  }
}
```

A composable tender refund can also be [unreferenced](/instore/integration/refunds/delegated-refunds.md#unreferenced-refunds). In that case, leave out `order` and `originalCTPayment`.

### How a composable tender refund runs

1. The store associate reviews the refund plan and presses **Next**.
2. InStore shows the refund prompt for the refund instruction, using the `metaData` that you sent. The associate presses **Next** to start the refund.
3. InStore loads your module and passes it the [module properties](/instore/integration/refunds/composable-tender-extension-refunds.md#module-properties). In its prompt area, InStore shows the `Refund.promptMessageCustom` string, which defaults to `Complete the refund using the form below.`
4. Your module processes the refund, then calls either `finishPayment` or `cancelPayment`.
   - After `finishPayment`, InStore records a `Custom` tender against the refund session and continues with the next refund instruction.
   - After `cancelPayment`, InStore treats the attempt as failed, the same as a refund that your payment processor declines.
5. When every refund instruction is complete, InStore finalizes the refund session and sends the [finalize refund webhook](/instore/integration/refunds/delegated-refunds.md#finalize-refund-webhook) and the [receipt data request](/instore/integration/refunds/delegated-refunds.md#refund-receipt-data-webhook).

InStore doesn't send a [refund webhook](/instore/integration/refunds/delegated-refunds.md#refund-webhook) to your processor for a `Custom` tender, and doesn't create or update a commercetools [Payment](/search.md?urn=ctp:api:type:Payment) or [Transaction](/search.md?urn=ctp:api:type:Transaction) for it. Your module or your own services are responsible for issuing the refund and for any updates to commercetools.

### Build the module

InStore loads the module that you configured on the [payment processor object](/instore/integration/payments/build-payment-components.md#create-a-payment-processor), with the same webpack Module Federation setup as a checkout custom payment module. The module must use the remote name `InStore_CustomPayment`. For the full configuration, see [Composable tender extension](/instore/integration/payments/overview.md#composable-tender-extension).

If one module handles both checkout payments and refunds, read `transactionType` to tell them apart: InStore sets it to `refund` for a refund, and doesn't send it for a checkout payment.

#### Module properties

InStore passes the following properties to your module's exposed component.

| Property | Data type | Description |
| --- | --- | --- |
| `transactionType` | String | Always `refund` for a composable tender refund. |
| `cartKey` | String | The `key` of the return Cart from the refund object, or its `id` when the refund object referenced the Cart by `id`. |
| `amountDue` | Object | The amount to refund for this refund instruction, as a [Dinero.js](https://v1.dinerojs.com/) object. Positive. |
| `amountRequested` | Object | The same amount as `amountDue`. |
| `tenantKey` | String | Identifies the tenant. |
| `locationKey` | String | The key of the location. |
| `workstationKey` | String | The key of the workstation. |
| `process` | String | The `integrationConfiguration.process` value of the payment processor. Absent when you didn't set one. |
| `finishPayment` | Function | Call it when the refund succeeds. See [Report a successful refund](/instore/integration/refunds/composable-tender-extension-refunds.md#report-a-successful-refund). |
| `cancelPayment` | Function | Call it when the refund fails or the store associate abandons it. InStore ignores any arguments. |
| `showBackButton` | Function | Call it with `true` to show a **Back** button in the InStore POS, and with `false` to hide it. When the store associate presses **Back**, InStore calls `cancelPayment`. |

#### Report a successful refund

Call `finishPayment` with an object in the following format, and wait for the returned promise to settle.

```js title="Sample finishPayment call"
await finishPayment({
  result: 'Success',
  data: {
    cartKey,
    processor: 'processor-store-credit-1',
    method: 'Store credit',
    amount: { amount: 1500, currency: 'USD', precision: 2 },
    creditAccountId: 'sc-55102',
    approvalCode: 'A-123',
  },
});
```

| Field | Data type | Description |
| --- | --- | --- |
| `result` | String | `Success`. Call `finishPayment` only when the refund succeeded. To report a failure, call `cancelPayment` instead. |
| `data.method` | String | Optional. InStore records it as the payment method of the tender. |
| `data.cartKey`, `data.processor`, `data.amount` | Various | Optional. Accepted for compatibility with checkout custom payment modules. InStore doesn't store these values. InStore records the amount from the refund instruction, not `data.amount`. |
| Any other key in `data` | Any | InStore stores every other key unchanged in the tender's `custom` object, and reports it back to you in the finalize refund webhook and the receipt data request. In the sample above, `custom` is `{ "creditAccountId": "sc-55102", "approvalCode": "A-123" }`. |

### Changes to the payloads that InStore sends you

#### Finalize refund webhook

A composable tender refund appears in the [finalize refund webhook](/instore/integration/refunds/delegated-refunds.md#finalize-refund-webhook) with a `Custom` `tenderInformation` block.

```json title="Sample refund transaction for a Custom tender"
{
  "paymentOption": { "typeId": "paymentOption", "id": "option-store-credit-us-1" },
  "amount": { "currencyCode": "USD", "centAmount": 1500, "fractionDigits": 2 },
  "originalRefundTransactionId": "pay_3Nk82jLkQ4eR1mWx",
  "tenderInformation": {
    "type": "Custom",
    "tenderItemId": "6a3c091003353f78bbe2c094",
    "payment": { "typeId": "payment", "id": "pay_3Nk82jLkQ4eR1mWx" },
    "custom": {
      "creditAccountId": "sc-55102",
      "approvalCode": "A-123"
    }
  }
}
```

| Field | Data type | Presence | Description |
| --- | --- | --- | --- |
| `tenderInformation.type` | String | Required | Always `Custom` for a composable tender refund. |
| `tenderInformation.tenderItemId` | String | Required | The internal identifier of the InStore tender. |
| `tenderInformation.payment` | Object | Optional | A reference to the original commercetools [Payment](/search.md?urn=ctp:api:type:Payment) from the refund instruction's `originalCTPayment`. Absent when the refund instruction has no `originalCTPayment`. |
| `tenderInformation.custom` | Object | Optional | The keys that your module passed in `finishPayment` `data`, apart from `cartKey`, `processor`, `method`, and `amount`. |

#### Receipt data request

In the [receipt data request](/instore/integration/refunds/delegated-refunds.md#refund-receipt-data-webhook), the tender appears in `data.tenderItems[]` with `tender_type` set to `Custom`. It also carries `payment_method`, with the `data.method` value from your module, and `custom`, with the same object as in the finalize refund webhook. Either field is absent when your module didn't supply it.

### Customize text strings

You can override the following strings for composable tender refunds. For more information, see [Override core strings](/instore/customization/override-core-module-styling/override-core-strings.md).

| String key | Default text (`en-US`) |
| --- | --- |
| `Refund.promptMessageCustom` | Complete the refund using the form below. |
| `Refund.Custom` | Custom |

`Refund.Custom` is the label for the `Custom` payment method on the refund screens.

## Related pages

- [Area overview page with navigation](/instore.md)
- [Previous page: Set up delegated refunds](/instore/integration/refunds/delegated-refunds.md)
- [Next page: Handle failed refund attempts](/instore/integration/refunds/failed-refunds.md)
- [Search documentation and API specs](/search.md)