# Stripe recipes

> Text customers a thank-you when they pay, and text yourself when a payment fails.

Three recipes for Stripe. Each names its trigger and the BlueBot step, lists the fields to map and says how to test it with your own number before anyone else gets a text.

## How Stripe connects

- **[Zapier](https://usebluebot.com/docs/zapier)**: BlueBot’s Zapier app has a Send Text action and instant triggers for incoming texts and every change of state.
- **[API](https://usebluebot.com/docs/api)**: Your own code calls BlueBot’s REST API and listens to its signed webhooks.

## Thank customers when they pay

| Part             | What it is                        |
| ---------------- | --------------------------------- |
| Trigger          | When a payment succeeds in Stripe |
| Action           | BlueBot’s **Send Text** action    |
| Connects through | [Zapier](https://usebluebot.com/docs/zapier)            |

### Steps

1. Collect a phone number at checkout.
2. Start a Zap with Stripe’s trigger for a new successful payment.
3. Add BlueBot’s Send Text action and map the customer’s phone to To.

### Map the fields

| BlueBot field                | Map it to                                                                                                                                   |
| ---------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------- |
| To                           | The person’s mobile number from Stripe. US numbers can leave off the +1.                                                                    |
| Text                         | Hi {{first_name}}, your payment came through. Thank you! Reply here with any questions.                                                   |
| `{{first_name}}` in the text | Stripe’s first name field.                                                                                                                  |
| Metadata                     | `source` set to `stripe`, and `record_id` set to the record’s id, so the status and reply webhooks for this text can find the record again. |
| Service                      | Auto, the default: iMessage when the person uses it, otherwise SMS through your iPhone.                                                     |

### Test it

Put your own number in **To** and press **Test step**. A text arrives on your phone from your own number, and Zapier shows the message BlueBot returned with `state: "queued"`. Then map Stripe’s field back into **To** and turn the Zap on.

## Text yourself when a payment fails

| Part             | What it is                     |
| ---------------- | ------------------------------ |
| Trigger          | When a payment fails in Stripe |
| Action           | BlueBot’s **Send Text** action |
| Connects through | [Zapier](https://usebluebot.com/docs/zapier)         |

### Steps

1. Start a Zap with Stripe’s trigger for a failed payment.
2. Add BlueBot’s Send Text action with your own number in To.
3. Put the customer’s email and the failure message in Text.

### Map the fields

| BlueBot field                     | Map it to                                                                                                                                   |
| --------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------- |
| To                                | The person’s mobile number from Stripe. US numbers can leave off the +1.                                                                    |
| Text                              | Payment failed for {{customer_email}}: {{failure_message}}                                                                              |
| `{{customer_email}}` in the text  | Stripe’s customer email field.                                                                                                              |
| `{{failure_message}}` in the text | Stripe’s failure message field.                                                                                                             |
| Metadata                          | `source` set to `stripe`, and `record_id` set to the record’s id, so the status and reply webhooks for this text can find the record again. |
| Service                           | Auto, the default: iMessage when the person uses it, otherwise SMS through your iPhone.                                                     |

### Test it

Put your own number in **To** and press **Test step**. A text arrives on your phone from your own number, and Zapier shows the message BlueBot returned with `state: "queued"`. Then map Stripe’s field back into **To** and turn the Zap on.

## Thank customers from your webhook handler

| Part             | What it is                                |
| ---------------- | ----------------------------------------- |
| Trigger          | Stripe’s checkout.session.completed event |
| Action           | `POST /v1/messages`                       |
| Connects through | [API](https://usebluebot.com/docs/api)                          |

### Steps

1. In the handler for checkout.session.completed, read customer_details.phone.
2. Send the text with POST /v1/messages and the session’s ID as the Idempotency-Key, so a retried Stripe event never texts twice.
3. Put the session ID in metadata to match BlueBot’s delivery webhooks to the order.

### Map the fields

| BlueBot field                | Map it to                                                                                                                                   |
| ---------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------- |
| `to`                         | The person’s mobile number from Stripe. US numbers can leave off the +1.                                                                    |
| `text`                       | Hi {{first_name}}, thanks for your order! We’ll text you here when it ships.                                                              |
| `{{first_name}}` in the text | Stripe’s first name field.                                                                                                                  |
| `metadata`                   | `source` set to `stripe`, and `record_id` set to the record’s id, so the status and reply webhooks for this text can find the record again. |
| `service`                    | Auto, the default: iMessage when the person uses it, otherwise SMS through your iPhone.                                                     |

```bash
curl -X POST "https://usebluebot.com/api/v1/messages" \
  -H "Authorization: Bearer $BLUEBOT_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: stripe-1042" \
  -d '{
    "to": "+15555550123",
    "text": "Hi Maria, thanks for your order! We’ll text you here when it ships.",
    "metadata": {
      "source": "stripe",
      "record_id": "1042"
    }
  }'
```

```ts
import BlueBot from '@usebluebot/sdk';

const client = new BlueBot({ apiKey: process.env.BLUEBOT_API_KEY });

const message = await client.messages.send({
  to: '+15555550123',
  text: 'Hi Maria, thanks for your order! We’ll text you here when it ships.',
  metadata: {
    source: 'stripe',
    record_id: '1042',
  },
}, { idempotencyKey: 'stripe-1042' });
console.log(message);
```

```python
import os
from bluebot import BlueBot

client = BlueBot(api_key=os.environ["BLUEBOT_API_KEY"])

message = client.messages.send(
    to="+15555550123",
    text="Hi Maria, thanks for your order! We’ll text you here when it ships.",
    metadata={
        "source": "stripe",
        "record_id": "1042",
    },
    idempotency_key="stripe-1042",
)
print(message)
```

### Test it

Run the request with your own number in `to`. A text arrives on your phone from your own number.

## Before you turn it on

- Text only people who asked to hear from you. Anyone who replies STOP goes on your stop list, and BlueBot cancels texts to them. [Stop words](https://usebluebot.com/docs/concepts/stop-words)
- A text that can’t go yet waits with a hold rather than failing: quiet hours, your Mac away, a daily limit. [Holds](https://usebluebot.com/docs/concepts/holds)
- [Stripe with BlueBot](https://usebluebot.com/integrations/stripe) on the integrations page says what else the two do together.
