> ## Documentation Index
> Fetch the complete documentation index at: https://developers.scoutworks.app/llms.txt
> Use this file to discover all available pages before exploring further.

# Tell the treasurer when a Direct Debit fails

> An email to the treasurer within a minute of a Direct Debit failing, with the parent, the amount and what it was for. With Power Automate or Zapier.

> [!NOTE]
>
> * **For:** treasurers
> * **You get:** an email each time a Direct Debit fails or is charged back
> * **Time:** about twenty minutes
> * **Scopes:** `payments:read` and `payments.people:read`

When a Direct Debit fails, the sooner the family hears, the sooner it is sorted. This recipe uses a [webhook](https://developers.scoutworks.app/guides/webhooks.md): Scoutworks sends a `payment.failed` message the moment GoCardless tells us, and your flow emails the treasurer.

The message holds only ids, so the flow reads the payment from the API to get the parent's name and the amount. That read is also the check that the message is real: a forged message names a payment the API does not list as failed.

You need:

* a group admin, to add the webhook;
* a key with `payments:read` and `payments.people:read`, created in **Settings → Integrations → API keys**;
* Power Automate with a Premium licence, or a paid Zapier plan.

> [!NOTE]
>
> The sandbox group cannot send webhooks, so build this on your own group. Until a Direct Debit fails, test it with the **Send test event** button: the flow below ignores the test, which shows the address works.

## With Power Automate

**1. Create the flow**

In [Power Automate](https://make.powerautomate.com), choose **Create → Instant cloud flow**, then **Skip**. Search for the trigger **When an HTTP request is received** (it is premium). Set **Who can trigger the flow?** to **Anyone**. Choose **Save**: Power Automate then shows the **HTTP URL**. Copy it.

**2. Add the webhook in Scoutworks**

In **Settings → Integrations → Webhooks**, choose **Add webhook**. Paste the HTTP URL, describe it as **Treasurer: failed Direct Debits**, tick **payment.failed** and choose **Add webhook**. You do not need the signing secret for this flow.

**3. Only act on payment.failed**

Add a **Condition**: `triggerBody()?['type']` **is equal to** `payment.failed`. Put the next steps under **True**. A test event goes to **False** and does nothing.

**4. Read the payment back**

Under **True**, add **HTTP**:

* **Method:** GET
* **URI:** type `https://api.scoutworks.app/api/v1/payments?limit=200&updatedSince=` and then add this **Expression**: `encodeUriComponent(addHours(triggerBody()?['timestamp'], -1))`
* **Headers:** `Authorization` → `Bearer ` followed by your key

Open the action's **Settings** and turn on **Secure inputs** and **Secure outputs**, so the run history hides the key and the parent's details.

**5. Find this payment**

Add **Filter array**. **From:** the expression `body('HTTP')?['data']`. Filter query, in advanced mode:

```text theme={null}
@and(equals(item()?['id'], triggerBody()?['data']?['paymentId']), contains(createArray('failed', 'cancelled', 'customer_approval_denied', 'charged_back'), item()?['status']))
```

This keeps the payment only if the API also says it failed, was cancelled, was refused by the parent's bank (`customer_approval_denied`) or was charged back. Add a **Condition**: `length(body('Filter_array'))` **is greater than** `0`. If it is not, the message did not match a failed payment, so stop there.

**6. Email the treasurer**

Under that condition's **True**, add **Office 365 Outlook → Send an email (V2)**. To: the treasurer. Subject: `A Direct Debit has failed`. Body, as an expression:

```text theme={null}
concat('A Direct Debit of ', formatNumber(div(float(triggerBody()?['data']?['amountPence']), 100), 'C2', 'en-GB'), ' from ', coalesce(first(body('Filter_array'))?['parent']?['name'], 'a parent'), ' (', coalesce(first(body('Filter_array'))?['parent']?['email'], 'no email'), ') due on ', substring(triggerBody()?['data']?['chargeDate'], 0, 10), ' did not go through. GoCardless says: ', first(body('Filter_array'))?['status'], '. See Payments in Scoutworks.')
```

Choose **Save**. Power Automate answers each message with `202 Accepted` as soon as it arrives, which counts as delivered.

## With Zapier

**1. Catch the hook**

Create a Zap. For the trigger, choose **Webhooks by Zapier → Catch Hook**. Copy the webhook URL Zapier shows.

**2. Add the webhook in Scoutworks**

In **Settings → Integrations → Webhooks**, choose **Add webhook**. Paste the Zapier URL, tick **payment.failed** and choose **Add webhook**. Then choose **Deliveries and test… → Send test event**, and in Zapier choose **Test trigger** to load the test message.

**3. Only act on payment.failed**

Add **Filter by Zapier**: only continue if **Type** (text) **exactly matches** `payment.failed`.

**4. Work out the time to read from**

Add **Formatter by Zapier → Date / Time → Add/Subtract Time**. Input: **Timestamp**. Expression: `-1 hour`. To format: `YYYY-MM-DDTHH:mm:ss`. To timezone: **UTC**.

**5. Read the payment back**

Add **Webhooks by Zapier → GET**:

* **URL:** `https://api.scoutworks.app/api/v1/payments?limit=200&updatedSince=` followed by the output of the Formatter step, then the letter `Z`

The `Z` marks the time as UTC. Do not let Zapier add an offset such as `+00:00`: a `+` in a web address reads as a space, and the API refuses the time.

* **Headers:** `Authorization` → `Bearer ` followed by your key

**6. Check it matched**

Add **Filter by Zapier**: only continue if **Data Id** from the GET step (text) **contains** **Data Payment Id** from the trigger.

Zapier joins the list into one line of text, so this checks the payment exists and changed in the last hour, but not its status. That is a weaker check than the Power Automate flow's. Keep the Zapier URL secret.

**7. Email the treasurer**

Add **Email by Zapier → Send Outbound Email**, or Gmail or Outlook. To: the treasurer. Subject: `A Direct Debit has failed`. In the body, insert **Data Amount Pence**, **Data Charge Date** and **Data Status** from the trigger, and the parent's name and email from the GET step.

Zapier answers each message straight away, which counts as delivered.

## Check it works

1. In Scoutworks, choose **Deliveries and test… → Send test event**. The delivery shows as delivered, and nothing is emailed.
2. When a real Direct Debit fails, the treasurer gets the email within a minute or two.
3. If an email ever goes missing, open **Deliveries and test…**. A failed delivery shows the answer your flow gave. Fix the flow, then choose **Resend**.

## Variations

* **Post to Teams instead.** Replace the email with **Microsoft Teams → Post message in a chat or channel**.
* **Collected payments too.** Tick **payment.confirmed** as well. In that branch, keep payments whose status is `confirmed` or `paid_out`.
* **A list, not an email.** [Who hasn't paid](https://developers.scoutworks.app/recipes/who-hasnt-paid.md) lists every failed Direct Debit in a sheet, refreshed each morning.

## If something goes wrong

* **No email, and the delivery shows delivered**: the read-back found nothing. Check the key has `payments:read`, and that the URI still ends with the `updatedSince` expression. A group with more than 200 payments changed in one hour needs a second page: follow `nextCursor`.
* **The parent's name is missing**: the key does not have `payments.people:read`.
* **Deliveries fail with 401 or 403**: the flow's address changed, or the flow was switched off. Copy the new address into the webhook with **Edit…**.

See [Webhooks](https://developers.scoutworks.app/guides/webhooks.md) for how messages are sent and retried, and [Errors](https://developers.scoutworks.app/guides/errors.md) for every API code.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.