{
  "name": "FlowPantry - stripe-failed-payment-recovery",
  "flow": [
    {
      "id": 1,
      "module": "gateway:CustomWebHook",
      "version": 1,
      "parameters": {
        "hook": null,
        "maxResults": 1
      },
      "mapper": {},
      "metadata": {
        "designer": {
          "x": 0,
          "y": 0
        }
      }
    },
    {
      "id": 2,
      "module": "builtin:BasicRouter",
      "version": 1,
      "parameters": {},
      "metadata": {
        "designer": {
          "x": 300,
          "y": 0
        }
      },
      "routes": [
        {
          "flow": [
            {
              "id": 3,
              "module": "http:ActionSendData",
              "version": 3,
              "parameters": {},
              "mapper": {
                "url": "https://api.resend.com/emails",
                "method": "post",
                "headers": [
                  {
                    "name": "Authorization",
                    "value": "Bearer YOUR_RESEND_API_KEY"
                  }
                ],
                "qs": [],
                "serializeUrl": false,
                "parseResponse": true,
                "shareCookies": false,
                "rejectUnauthorized": true,
                "followRedirect": true,
                "useQuerystring": false,
                "gzip": true,
                "useMtls": false,
                "timeout": "",
                "bodyType": "raw",
                "contentType": "application/json",
                "inputMethod": "jsonString",
                "data": "{\"from\": \"YOUR_NAME <you@yourdomain.com>\", \"to\": \"{{1.data.object.customer_email}}\", \"subject\": \"Your YOUR BUSINESS payment didn't go through\", \"text\": \"Hi {{1.data.object.customer_name}},\\n\\nYour {{formatNumber(1.data.object.amount_due / 100; 2)}} {{upper(1.data.object.currency)}} payment to YOUR BUSINESS didn't go through. This is usually an expired or replaced card.\\n\\nUpdate your card or pay now: {{1.data.object.hosted_invoice_url}}\\n\\nIf you do nothing, we'll try the card again on {{formatDate(parseDate(1.data.object.next_payment_attempt; X); ddd, MMM D)}}.\\n\\n\\u2014 YOUR BUSINESS\"}"
              },
              "metadata": {
                "designer": {
                  "x": 600,
                  "y": 0
                }
              },
              "filter": {
                "name": "First failure",
                "conditions": [
                  [
                    {
                      "a": "{{1.data.object.attempt_count}}",
                      "o": "number:equal",
                      "b": "1"
                    },
                    {
                      "a": "{{1.type}}",
                      "o": "text:equal",
                      "b": "invoice.payment_failed"
                    },
                    {
                      "a": "{{1.data.object.customer_email}}",
                      "o": "exist",
                      "b": ""
                    },
                    {
                      "a": "{{1.data.object.next_payment_attempt}}",
                      "o": "exist",
                      "b": ""
                    }
                  ]
                ]
              }
            }
          ]
        },
        {
          "flow": [
            {
              "id": 4,
              "module": "http:ActionSendData",
              "version": 3,
              "parameters": {},
              "mapper": {
                "url": "https://api.resend.com/emails",
                "method": "post",
                "headers": [
                  {
                    "name": "Authorization",
                    "value": "Bearer YOUR_RESEND_API_KEY"
                  }
                ],
                "qs": [],
                "serializeUrl": false,
                "parseResponse": true,
                "shareCookies": false,
                "rejectUnauthorized": true,
                "followRedirect": true,
                "useQuerystring": false,
                "gzip": true,
                "useMtls": false,
                "timeout": "",
                "bodyType": "raw",
                "contentType": "application/json",
                "inputMethod": "jsonString",
                "data": "{\"from\": \"YOUR_NAME <you@yourdomain.com>\", \"to\": \"{{1.data.object.customer_email}}\", \"subject\": \"Still having trouble with your YOUR BUSINESS payment\", \"text\": \"Hi {{1.data.object.customer_name}},\\n\\nWe tried your card again for the {{formatNumber(1.data.object.amount_due / 100; 2)}} {{upper(1.data.object.currency)}} payment and it was declined (attempt {{1.data.object.attempt_count}}).\\n\\nTwo-minute fix: {{1.data.object.hosted_invoice_url}}\\n\\nNext try: {{formatDate(parseDate(1.data.object.next_payment_attempt; X); ddd, MMM D)}}.\\n\\n\\u2014 YOUR BUSINESS\"}"
              },
              "metadata": {
                "designer": {
                  "x": 600,
                  "y": 0
                }
              },
              "filter": {
                "name": "Retry failed",
                "conditions": [
                  [
                    {
                      "a": "{{1.data.object.attempt_count}}",
                      "o": "number:greaterorequal",
                      "b": "2"
                    },
                    {
                      "a": "{{1.type}}",
                      "o": "text:equal",
                      "b": "invoice.payment_failed"
                    },
                    {
                      "a": "{{1.data.object.customer_email}}",
                      "o": "exist",
                      "b": ""
                    },
                    {
                      "a": "{{1.data.object.next_payment_attempt}}",
                      "o": "exist",
                      "b": ""
                    }
                  ]
                ]
              }
            }
          ]
        },
        {
          "flow": [
            {
              "id": 5,
              "module": "http:ActionSendData",
              "version": 3,
              "parameters": {},
              "mapper": {
                "url": "https://api.resend.com/emails",
                "method": "post",
                "headers": [
                  {
                    "name": "Authorization",
                    "value": "Bearer YOUR_RESEND_API_KEY"
                  }
                ],
                "qs": [],
                "serializeUrl": false,
                "parseResponse": true,
                "shareCookies": false,
                "rejectUnauthorized": true,
                "followRedirect": true,
                "useQuerystring": false,
                "gzip": true,
                "useMtls": false,
                "timeout": "",
                "bodyType": "raw",
                "contentType": "application/json",
                "inputMethod": "jsonString",
                "data": "{\"from\": \"YOUR_NAME <you@yourdomain.com>\", \"to\": \"{{1.data.object.customer_email}}\", \"subject\": \"Action needed: your YOUR BUSINESS payment\", \"text\": \"Hi {{1.data.object.customer_name}},\\n\\nWe weren't able to collect your {{formatNumber(1.data.object.amount_due / 100; 2)}} {{upper(1.data.object.currency)}} payment after {{1.data.object.attempt_count}} tries, and we won't retry the card again automatically.\\n\\nPlease pay or update your card here to keep your service active: {{1.data.object.hosted_invoice_url}}\\n\\nReply if something changed.\\n\\n\\u2014 YOUR BUSINESS\"}"
              },
              "metadata": {
                "designer": {
                  "x": 600,
                  "y": 0
                }
              },
              "filter": {
                "name": "Final failure",
                "conditions": [
                  [
                    {
                      "a": "{{1.data.object.next_payment_attempt}}",
                      "o": "notexist",
                      "b": ""
                    },
                    {
                      "a": "{{1.type}}",
                      "o": "text:equal",
                      "b": "invoice.payment_failed"
                    },
                    {
                      "a": "{{1.data.object.customer_email}}",
                      "o": "exist",
                      "b": ""
                    }
                  ]
                ]
              }
            },
            {
              "id": 6,
              "module": "http:ActionSendData",
              "version": 3,
              "parameters": {},
              "mapper": {
                "url": "https://api.twilio.com/2010-04-01/Accounts/YOUR_TWILIO_SID/Messages.json",
                "method": "post",
                "headers": [],
                "qs": [],
                "serializeUrl": false,
                "parseResponse": true,
                "shareCookies": false,
                "rejectUnauthorized": true,
                "followRedirect": true,
                "useQuerystring": false,
                "gzip": true,
                "useMtls": false,
                "timeout": "",
                "username": "YOUR_TWILIO_SID",
                "password": "YOUR_TWILIO_AUTH_TOKEN",
                "bodyType": "x_www_form_urlencoded",
                "data": "",
                "formFields": [
                  {
                    "key": "To",
                    "value": "OWNER_PHONE"
                  },
                  {
                    "key": "From",
                    "value": "YOUR_TWILIO_NUMBER"
                  },
                  {
                    "key": "Body",
                    "value": "Stripe gave up on {{1.data.object.customer_name}} ({{1.data.object.customer_email}}): {{formatNumber(1.data.object.amount_due / 100; 2)}} {{upper(1.data.object.currency)}} after {{1.data.object.attempt_count}} tries. Invoice {{1.data.object.id}}"
                  }
                ]
              },
              "metadata": {
                "designer": {
                  "x": 900,
                  "y": 0
                }
              }
            }
          ]
        }
      ]
    }
  ],
  "metadata": {
    "instant": true,
    "version": 1,
    "scenario": {
      "roundtrips": 1,
      "maxErrors": 3,
      "autoCommit": true,
      "autoCommitTriggerLast": true,
      "sequential": false,
      "slots": null,
      "confidential": false,
      "dataloss": false,
      "dlq": false,
      "freshVariables": false
    },
    "designer": {
      "orphans": []
    },
    "zone": "us1.make.com"
  }
}