{
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "nodes": [
    {
      "id": "a756e430-bc93-49d2-9da3-7892bd0374cc",
      "name": "Stripe Trigger",
      "type": "n8n-nodes-base.stripeTrigger",
      "position": [
        304,
        -224
      ],
      "parameters": {
        "events": [
          "checkout.session.completed"
        ]
      },
      "credentials": {
        "stripeApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "ef9d7765-6bf2-43d2-88e7-72d4688cfef1",
      "name": "Split Out",
      "type": "n8n-nodes-base.splitOut",
      "position": [
        528,
        -224
      ],
      "parameters": {
        "options": {},
        "fieldToSplitOut": "data"
      },
      "typeVersion": 1
    },
    {
      "id": "5d32ac6f-0a8e-4eb8-b4c1-04e96a90cd7e",
      "name": "onlyPaid",
      "type": "n8n-nodes-base.filter",
      "position": [
        752,
        -224
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "3fe041ec-755a-4862-97f6-9b3f0d8abb7c",
              "operator": {
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $json.payment_status }}",
              "rightValue": "paid"
            },
            {
              "id": "6d714e77-3c58-4908-913c-ea87923bcf5c",
              "operator": {
                "type": "number",
                "operation": "gt"
              },
              "leftValue": "={{ $json.amount_total }}",
              "rightValue": 0
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "a3a58c8a-3541-4d13-8946-478ac36c6941",
      "name": "getItems",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        976,
        -160
      ],
      "parameters": {
        "url": "=https://api.stripe.com/v1/payment_intents/{{ $json.payment_intent }}/amount_details_line_items",
        "options": {},
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "stripeApi"
      },
      "credentials": {
        "stripeApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.3
    },
    {
      "id": "10ec33fc-4bf0-491c-8432-b60387633edd",
      "name": "splitProducts",
      "type": "n8n-nodes-base.splitOut",
      "position": [
        1200,
        -160
      ],
      "parameters": {
        "options": {},
        "fieldToSplitOut": "data"
      },
      "typeVersion": 1
    },
    {
      "id": "627fe2de-c0bc-4764-b87c-62a6f65ab5a7",
      "name": "Merge",
      "type": "n8n-nodes-base.merge",
      "position": [
        1872,
        -224
      ],
      "parameters": {
        "mode": "combine",
        "options": {
          "clashHandling": {
            "values": {
              "resolveClash": "preferLast"
            }
          },
          "includeUnpaired": true
        },
        "combineBy": "combineByPosition"
      },
      "typeVersion": 3.2
    },
    {
      "id": "27db50d5-2f24-40b7-8631-6e782c7608a8",
      "name": "Aggregate",
      "type": "n8n-nodes-base.aggregate",
      "position": [
        1648,
        -160
      ],
      "parameters": {
        "options": {},
        "aggregate": "aggregateAllItemData",
        "destinationFieldName": "line_items"
      },
      "typeVersion": 1
    },
    {
      "id": "e58f36cf-a1fa-4710-b3e6-4d7c0b33b110",
      "name": "preparePayload",
      "type": "n8n-nodes-base.set",
      "position": [
        2096,
        -224
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "d3b73bff-7707-409e-b6e5-5012a6189df9",
              "name": "currency",
              "type": "string",
              "value": "={{ $json.currency?.toUpperCase() }}"
            },
            {
              "id": "db475850-cd27-4fb8-9b2f-871c6af5ad26",
              "name": "identifier",
              "type": "string",
              "value": "={{ $json.payment_intent }}"
            },
            {
              "id": "4b8028c2-0a65-46f0-a750-4f3328452fef",
              "name": "price",
              "type": "number",
              "value": "={{ $json.amount_total/100 }}"
            },
            {
              "id": "ea14de41-aff1-478e-a58b-31cbca192ba7",
              "name": "shipping_price",
              "type": "number",
              "value": "={{ $json.shipping_cost/100 }}"
            },
            {
              "id": "60e4d2c6-5e30-453e-81d3-65d383d2731b",
              "name": "total_items",
              "type": "number",
              "value": "={{ $json.line_items.length || 0 }}"
            },
            {
              "id": "965e4fdb-9c6e-457c-9322-7a5a77292506",
              "name": "line_items",
              "type": "array",
              "value": "={{ $json.line_items }}"
            },
            {
              "id": "f3a22503-ad07-41ff-845a-765a9570aec5",
              "name": "email",
              "type": "string",
              "value": "={{ $json.customer_details?.email }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "02a9b2e9-7277-41d9-b5cb-c9407cf9f1f6",
      "name": "prepareItemsData",
      "type": "n8n-nodes-base.set",
      "position": [
        1424,
        -160
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "29e1047f-4504-45a4-bdd3-d82b9dc37074",
              "name": "product_id",
              "type": "string",
              "value": "={{ $json.product_code }}"
            },
            {
              "id": "36be8500-37f3-4dc0-bdca-504f65b0ef14",
              "name": "product_title",
              "type": "string",
              "value": "={{ $json.product_name }}"
            },
            {
              "id": "75cb7818-71c4-46a2-b497-26368610aab1",
              "name": "price",
              "type": "number",
              "value": "={{ $json.unit_cost/100 }}"
            },
            {
              "id": "ab13eebf-18be-4616-a693-53f7be561886",
              "name": "quantity",
              "type": "number",
              "value": "={{ $json.quantity }}"
            },
            {
              "id": "d1a02675-7b0a-4e43-8a2f-45112c97ef00",
              "name": "variant_id",
              "type": "string",
              "value": "={{ $json.product_code }}"
            },
            {
              "id": "e827c6d2-b82a-4da1-b36c-ae8c76a3a32a",
              "name": "sku",
              "type": "string",
              "value": "={{ $json.product_code }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "85d325d5-fc65-4915-942b-b3c40c777d56",
      "name": "send2RDMKT",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        2320,
        -224
      ],
      "parameters": {
        "url": "https://api.rd.services/platform/events",
        "method": "POST",
        "options": {},
        "sendBody": true,
        "sendQuery": true,
        "sendHeaders": true,
        "authentication": "predefinedCredentialType",
        "bodyParameters": {
          "parameters": [
            {
              "name": "event_type",
              "value": "ECOMMERCE_ORDER_PAID"
            },
            {
              "name": "event_family",
              "value": "CDP"
            },
            {
              "name": "payload",
              "value": "={{ $json }}"
            }
          ]
        },
        "queryParameters": {
          "parameters": [
            {
              "name": "event_type",
              "value": "ECOMMERCE_ORDER_PAID"
            }
          ]
        },
        "headerParameters": {
          "parameters": [
            {
              "name": "accept",
              "value": "application/json"
            },
            {
              "name": "content-type",
              "value": "application/json"
            }
          ]
        },
        "nodeCredentialType": "rdStationMarketingOAuth2Api"
      },
      "credentials": {
        "rdStationMarketingOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.3
    },
    {
      "id": "22423fda-feca-4b6d-8832-763628663567",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -432,
        -560
      ],
      "parameters": {
        "width": 640,
        "height": 672,
        "content": "# Register Stripe payments on RD Station Marketing\n\n## Workflow to instantly register your Stripe checkout sales on RD Station Marketing\n\n### How it works\n- When a customer complete a checkout session, Stripe will automatically trigger this workflow\n- If payment is confirmed, the workflow will get the list of products sold\n- A new [Order & Payment Event](https://developers.rdstation.com/reference/evento-de-pedido-pago) will be registered on RD Station Marketing\n\n### Setup\n- [ ] Edit nodes Stripe Trigger and getItems to setup your Stripe Account credentials\n- [ ] Edit the final node send2RDMKT to setup your RD Station Marketing credentials\n\n### Requirements\n1. Stripe Account and [API keys](https://docs.stripe.com/keys)\n2. [RD Station Marketing](https://indiquei.app/L5nlhp) account\n3. Install community node [RD Station Marketing](https://www.npmjs.com/package/n8n-nodes-rdstation-marketing)\n\n\u2757\ufe0f Self-hosted only"
      },
      "typeVersion": 1
    },
    {
      "id": "b641761c-47c9-4e39-b8a9-bf5a77eed53d",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        256,
        -448
      ],
      "parameters": {
        "color": 7,
        "width": 624,
        "height": 464,
        "content": "## 1. Receive Webhook from Stripe and validate payment status\n\nTo use in production enviroment you'll need to get Webhook production URL and add to your Stripe account, on Webhook settings"
      },
      "typeVersion": 1
    },
    {
      "id": "f363dc5e-c5b1-4032-b310-f35ae73c96ba",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        928,
        -448
      ],
      "parameters": {
        "color": 7,
        "width": 848,
        "height": 464,
        "content": "## 2. Get list of items sold\n\nRemember to update your Stripe credentials on getItems node"
      },
      "typeVersion": 1
    },
    {
      "id": "7ae413b0-be93-4ab4-8216-701599732baa",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1792,
        -448
      ],
      "parameters": {
        "color": 7,
        "width": 704,
        "height": 464,
        "content": "## 3. Prepare data and register event\n\nFeel free to customize informantion sent to RD Station Marketing, like adding user information collected on checkout session"
      },
      "typeVersion": 1
    }
  ],
  "connections": {
    "Merge": {
      "main": [
        [
          {
            "node": "preparePayload",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "getItems": {
      "main": [
        [
          {
            "node": "splitProducts",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "onlyPaid": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 0
          },
          {
            "node": "getItems",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Aggregate": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Split Out": {
      "main": [
        [
          {
            "node": "onlyPaid",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "splitProducts": {
      "main": [
        [
          {
            "node": "prepareItemsData",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Stripe Trigger": {
      "main": [
        [
          {
            "node": "Split Out",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "preparePayload": {
      "main": [
        [
          {
            "node": "send2RDMKT",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "prepareItemsData": {
      "main": [
        [
          {
            "node": "Aggregate",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}