# Scheduled Payment Webhooks

This section will discuss how each webhook is triggered after each step is completed, affecting the `PaymentRequest`, `PaymentIntent` & `SchedulePayment` entities.

## Schedule Webhooks Flow

The overall flow works as follows: when the payer authorizes the consent, a `payment_intent/created` webhook is triggered, followed by `payment_intent/completed` once the intent is confirmed. Then, one `scheduled_payment/created` webhook is triggered for each scheduled payment, and a `scheduled_payment/all_created` webhook once every schedule has been registered. As each scheduled payment is executed on its date, a `scheduled_payment/completed` webhook is triggered (or `scheduled_payment/error` if it fails), and finally `scheduled_payment/all_completed` when all schedules have finished.

## Example webhook payloads

Here are examples of webhook payloads in a case with two scheduled payments, and both becoming COMPLETED:

**payment_intent/created**

```json title="payment_intent/created"
{
  "paymentRequestId": "a0ed730e-5a44-491f-9a32-aa295c399fdf",
  "paymentIntentId": "056b8046-2f7c-4d97-b5d5-f5f54005db51",
  "event": "payment_intent/created",
  "eventId": "aa8f7239-101c-4553-afdd-9689a4ac46cd"
}
```

**payment_intent/completed**

```json title="payment_intent/completed"
{
  "paymentRequestId": "a0ed730e-5a44-491f-9a32-aa295c399fdf",
  "paymentIntentId": "056b8046-2f7c-4d97-b5d5-f5f54005db51",
  "event": "payment_intent/completed",
  "eventId": "1ecfb159-e506-48a3-895e-aed5241db4d9"
}
```

**scheduled_payment/created (1)**

```json title="scheduled_payment/created (1)"
{
  "paymentRequestId": "a0ed730e-5a44-491f-9a32-aa295c399fdf",
  "event": "scheduled_payment/created",
  "eventId": "5f519a62-87e1-45ce-be36-d2b185994c21",
  "scheduledPaymentId": "65c6f9cd-e69f-46cd-8103-80b42dd61bfd"
}
```

**scheduled_payment/created (2)**

```json title="scheduled_payment/created (2)"
{
  "paymentRequestId": "a0ed730e-5a44-491f-9a32-aa295c399fdf",
  "event": "scheduled_payment/created",
  "eventId": "c8e0dd66-5939-425d-bfd2-900ffa6921ae",
  "scheduledPaymentId": "502b68b5-f82a-4d1c-bcb3-e1e61fadc48a"
}
```

**scheduled_payment/all_created**

```json title="scheduled_payment/all_created"
{
  "paymentRequestId": "a0ed730e-5a44-491f-9a32-aa295c399fdf",
  "event": "scheduled_payment/all_created",
  "eventId": "4f45ca0a-3286-47b4-b8b3-195cd35cac01"
}
```

**scheduled_payment/completed (1)**

```json title="scheduled_payment/completed (1)"
{
  "paymentRequestId": "a0ed730e-5a44-491f-9a32-aa295c399fdf",
  "event": "scheduled_payment/completed",
  "eventId": "1d5d1aab-fc1a-40d4-aeae-15e7b9c11a10",
  "endToEndId": "E44471172202505211500U0d9ffa12345",
  "scheduledPaymentId": "65c6f9cd-e69f-46cd-8103-80b42dd61bfd"
}
```

**scheduled_payment/completed (2)**

```json title="scheduled_payment/completed (2)"
{
  "paymentRequestId": "a0ed730e-5a44-491f-9a32-aa295c399fdf",
  "event": "scheduled_payment/completed",
  "eventId": "9ebc222b-a94d-41e1-ad52-c0cf26f0357e",
  "endToEndId": "E44471172202505211500U0d9ffa12345",
  "scheduledPaymentId": "502b68b5-f82a-4d1c-bcb3-e1e61fadc48a"
}
```

**scheduled_payment/all_completed**

```json title="scheduled_payment/all_completed"
{
  "paymentRequestId": "a0ed730e-5a44-491f-9a32-aa295c399fdf",
  "event": "scheduled_payment/all_completed",
  "eventId": "7a9426aa-2ac3-4d8e-a5a4-fe780f116c6b"
}
```

> **Example**
>
> If you have configured all webhooks, following the example above of flow, this will trigger all those webhooks in that order.
>
> You will receive:
>
> - Two `payment_intent` webhooks (created & completed).
> - Five `scheduled_payment` webhooks.
>   - Since in this example we scheduled **two payments**, we will receive **two for each payment** that was scheduled (created & completed).
>   - One webhook when **all schedules** have finished.

### Webhook Errors

When you receive a `scheduled_payment/error` webhook, you can have one of the following error codes.

| Error Code | Meaning | Description |
|-----------|---------|-------------|
| `INSUFFICIENT_BALANCE` | Insufficient Balance | The account doesn't have enough balance to perform the payment. |
| `EXCEEDED_LIMIT` | Exceeded Limit | The payment amount exceeds the allowed limit. |
| `INVALID_AMOUNT` | Invalid Amount | The payment amount provided is invalid. |
| `INVALID_INVOICE` | Invalid Invoice | The provided invoice is invalid. |
| `INVALID_CONSENT` | Invalid Consent | The consent provided is invalid. |
| `PARAMETER_NOT_PROVIDED` | Parameter Not Provided | A required parameter was not provided. |
| `INVALID_PARAMETER` | Invalid Parameter | A parameter provided is invalid. |
| `NOT_PROVIDED` | Parameter Not Provided | A required parameter was not provided. |
| `PAYMENT_DIFFERENT_FROM_CONSENT` | Payment Different from Consent | The payment differs from the authorized consent. |
| `INVALID_PAYMENT_DETAIL` | Invalid Payment Detail | The payment details provided are invalid. |
| `PAYMENT_REJECTED_BY_HOLDER` | Payment Rejected by Holder | The account holder rejected the payment. |
| `IDEMPOTENCY_ERROR` | Idempotency Error | An idempotency error occurred, possibly due to duplicate requests. |
| `CONSENT_PENDING_AUTHORIZATION` | Consent Pending Authorization | The consent is pending authorization. |
| `INFRASTRUCTURE_FAILURE` | Infrastructure Failure | There was a failure in the infrastructure. |
| `SAME_ACCOUNT_ORIGIN_DESTINATION` | Same Account Origin and Destination | The origin and destination accounts are the same, which is not allowed. |
| `PAYMENT_SCHEDULING_FAILURE` | Payment Scheduling Failure | There was a failure in scheduling the payment. |
| `UNKNOWN_ERROR` | Unknown Error | An unknown error occurred either at the Open Finance institution or account holder side. |