{
  "schema_version": "1.0.0",
  "id": "stripe-failed-payment-text",
  "title": "Text a customer when a Stripe payment fails",
  "category": "finance",
  "difficulty": "intermediate",
  "featured": false,
  "description": "When a card payment fails, send the customer a short SMS with a link to update their card, and post the failure to the billing channel.",
  "tags": [
    "stripe",
    "twilio",
    "sms",
    "dunning",
    "slack"
  ],
  "apps": [
    "Stripe",
    "Code by Zapier",
    "Filter by Zapier",
    "Twilio",
    "Slack"
  ],
  "plan": {
    "minimum": "Professional",
    "reasons": [
      "multi-step Zap: 5 steps after the trigger",
      "Filter by Zapier"
    ]
  },
  "tasks": {
    "typical": 4,
    "max": 4
  },
  "trigger": {
    "app": "Stripe",
    "event": "New Failed Payment",
    "kind": "instant",
    "title": "Payment failed",
    "why": "Fires when a charge fails.",
    "config": {
      "Account": "YOUR_STRIPE_ACCOUNT"
    },
    "outputs": [
      "charge_id",
      "customer_id",
      "customer_name",
      "customer_email",
      "customer_phone",
      "amount",
      "currency",
      "failure_message",
      "created"
    ],
    "sample": "sample-trigger.json"
  },
  "steps": [
    {
      "key": "phone",
      "kind": "code",
      "app": "Code by Zapier",
      "event": "Run JavaScript",
      "title": "Format the phone number",
      "why": "Turns whatever the customer typed at checkout into E.164 so Twilio accepts it, and rejects numbers too short to be real.",
      "code": {
        "language": "javascript",
        "module": "normalize-phone",
        "file": "code/normalize-phone.js"
      },
      "input_data": {
        "phone": "{{trigger.customer_phone}}",
        "default_country": "1"
      },
      "outputs": [
        "e164",
        "valid",
        "extension"
      ]
    },
    {
      "key": "textable",
      "kind": "filter",
      "app": "Filter by Zapier",
      "event": "Only continue if...",
      "title": "Only valid mobile numbers",
      "why": "Customers without a usable number still appear in Slack via Stripe's own emails; the text step needs a number.",
      "conditions": [
        [
          {
            "field": "{{phone.valid}}",
            "operator": "(Boolean) Is true"
          }
        ]
      ],
      "usually": true
    },
    {
      "key": "sms",
      "kind": "code",
      "app": "Code by Zapier",
      "event": "Run JavaScript",
      "title": "Fit the message into one SMS",
      "why": "Fills the first name and link and trims to one GSM-7 segment, so it costs one message and arrives in one piece.",
      "code": {
        "language": "javascript",
        "module": "fit-sms",
        "file": "code/fit-sms.js"
      },
      "input_data": {
        "template": "Hi {first_name}, your payment to {business} did not go through, usually an expired card. You can update it here: {link}",
        "first_name": "{{trigger.customer_name}}",
        "business": "YOUR_BUSINESS_NAME",
        "link": "YOUR_BILLING_PORTAL_LINK"
      },
      "outputs": [
        "message",
        "encoding",
        "length",
        "segments",
        "trimmed"
      ]
    },
    {
      "key": "text",
      "kind": "action",
      "app": "Twilio",
      "event": "Send SMS",
      "title": "Send the text",
      "why": "One message, sent from your Twilio number.",
      "fields": {
        "From Number": "YOUR_TWILIO_NUMBER",
        "To Number": "{{phone.e164}}",
        "Message": "{{sms.message}}",
        "Send Large Messages?": "No"
      },
      "outputs": [
        "sid",
        "status"
      ]
    },
    {
      "key": "billing",
      "kind": "action",
      "app": "Slack",
      "event": "Send Channel Message",
      "title": "Post it to #billing",
      "why": "The team sees which customers were contacted and why the card failed.",
      "fields": {
        "Channel": "#billing",
        "Message Text": "Payment failed for {{trigger.customer_name}} ({{trigger.amount}} {{trigger.currency}}): {{trigger.failure_message}}. Texted {{phone.e164}}.",
        "Send as a bot?": "Yes",
        "Bot Name": "Zap alerts",
        "Include a link to this Zap?": "No"
      },
      "outputs": [
        "ts",
        "permalink"
      ]
    }
  ]
}
