# Recommended workflows

## Visual Workflow

### Flow 1: Reconcile Using Webhook Payload
```mermaid
sequenceDiagram
  autonumber
  participant Merchant as Merchant System
  participant SX as SX Digital Pay API
  participant Checkout as SX Checkout
  participant Customer as Customer
  participant Webhook as Merchant Webhook

  Merchant->>SX: POST /payment-links (title, amount, webhookUrl, optional webhookSecret, optional callbackUrl)
  SX-->>Merchant: paymentLink (id, url/shortUrl, webhookSecret*)
  Note over Merchant,SX: webhookSecret is returned only at creation time.

  Merchant->>Customer: Redirect to url/shortUrl
  Customer->>Checkout: Open link and pay
  Checkout->>SX: Process payment (PIX/Credit Card)
  SX-->>SX: Update paymentStatus and payments[]
  Checkout-->>Customer: Redirect to callbackUrl (if configured)

  SX-->>Webhook: POST event payment_link.payment_status_changed
  Webhook-->>Merchant: Verify HMAC signature
  Note over Merchant,Webhook: Signature verification is highly recommended.
  Note over SX,Webhook: If delivery fails, SX retries with exponential backoff (up to 6 times over ~17h).
  Merchant-->>Merchant: Reconcile payment using webhook payload
```

### Flow 2: Reconcile by Fetching Link Details
```mermaid
sequenceDiagram
  autonumber
  participant Merchant as Merchant System
  participant SX as SX Digital Pay API
  participant Checkout as SX Checkout
  participant Customer as Customer
  participant Webhook as Merchant Webhook

  Merchant->>SX: POST /payment-links (title, amount, webhookUrl, optional webhookSecret, optional callbackUrl)
  SX-->>Merchant: paymentLink (id, url/shortUrl, webhookSecret*)
  Note over Merchant,SX: webhookSecret is returned only at creation time.

  Merchant->>Customer: Redirect to url/shortUrl
  Customer->>Checkout: Open link and pay
  Checkout->>SX: Process payment (PIX/Credit Card)
  SX-->>SX: Update paymentStatus and payments[]
  Checkout-->>Customer: Redirect to callbackUrl (if configured)

  SX-->>Webhook: POST event payment_link.payment_status_changed
  Webhook-->>Merchant: Verify HMAC signature and extract link id
  Note over Merchant,Webhook: Signature verification is optional (API response is the source of truth).
  Note over SX,Webhook: If delivery fails, SX retries with exponential backoff (up to 6 times over ~17h).
  Merchant->>SX: GET /payment-links/{id}
  SX-->>Merchant: paymentLink (paymentStatus, payments[])
  Merchant-->>Merchant: Reconcile payment using API response
```

## Webhook Delivery Guarantees

SX Digital Pay provides automatic retry for failed webhook deliveries:

- **First attempt**: Immediate (synchronous with the event)
- **Retries**: 1min → 5min → 15min → 1h → 4h → 12h
- **Total window**: ~17 hours before permanent failure

For complete details, see [Webhook Delivery System](https://developers.sxdigitalpay.com/webhook-delivery-system-1955363m0.md).

