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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
{
  "meta": {
    "instanceId": "a2434c94d549548a685cca39cc4614698e94f527bcea84eefa363f1037ae14cd"
  },
  "nodes": [
    {
      "id": "9be821db-fbc7-4168-962f-26c8382cefbf",
      "name": "If charge has customer",
      "type": "n8n-nodes-base.if",
      "position": [
        1560,
        880
      ],
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{ $json[\"customer\"] }}",
              "operation": "isNotEmpty"
            }
          ]
        }
      },
      "typeVersion": 1
    },
    {
      "id": "d06bae31-6856-4941-b86c-c611fc9d3da6",
      "name": "Get customer",
      "type": "n8n-nodes-base.stripe",
      "position": [
        2160,
        920
      ],
      "parameters": {
        "resource": "customer",
        "customerId": "={{ $json[\"customer\"] }}"
      },
      "credentials": {
        "stripeApi": {
          "id": "22",
          "name": "[UPDATE ME]"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "4e0d87bf-084f-4958-b2d3-cf7985f8c901",
      "name": "On schedule",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        -400,
        1400
      ],
      "parameters": {
        "rule": {
          "interval": [
            {}
          ]
        }
      },
      "typeVersion": 1
    },
    {
      "id": "fb620c92-5e22-4a9c-9320-847442b5e955",
      "name": "Remove duplicate customers",
      "type": "n8n-nodes-base.itemLists",
      "position": [
        1880,
        920
      ],
      "parameters": {
        "compare": "selectedFields",
        "options": {
          "removeOtherFields": true
        },
        "operation": "removeDuplicates",
        "fieldsToCompare": {
          "fields": [
            {
              "fieldName": "customer"
            }
          ]
        }
      },
      "typeVersion": 1
    },
    {
      "id": "3ad7554d-24b3-4ee2-8136-6a151bf06c71",
      "name": "Aggregate `amount_captured`",
      "type": "n8n-nodes-base.itemLists",
      "position": [
        1880,
        540
      ],
      "parameters": {
        "options": {},
        "operation": "aggregateItems",
        "fieldsToAggregate": {
          "fieldToAggregate": [
            {
              "fieldToAggregate": "amount_captured"
            }
          ]
        }
      },
      "typeVersion": 1
    },
    {
      "id": "c8448580-40f2-4cf6-87ba-80903555d5a5",
      "name": "Aggregate totals",
      "type": "n8n-nodes-base.code",
      "position": [
        2820,
        1360
      ],
      "parameters": {
        "jsCode": "// aggregate `amounts_captured` with the customer, taking note \n// that `aggregateAmountsPerContact` is the value in cents\nconst aggregateAmountsPerContact = new Object();\nfor (const item of $input.all()) {\n  if (aggregateAmountsPerContact[item.json.email] == null) {\n    aggregateAmountsPerContact[item.json.email] = 0;\n  }\n  aggregateAmountsPerContact[item.json.email] += item.json.amount_captured;\n}\n\n// parse the data in a way that is usable in future nodes, and\n// converts amounts from cents to dollars\nconst parsed = [];\nfor (const contact of Object.keys(aggregateAmountsPerContact)) {\n    parsed.push({\n        email: contact,\n        amount_captured: aggregateAmountsPerContact[contact] / 100\n    });\n}\n\nreturn parsed;"
      },
      "typeVersion": 1
    },
    {
      "id": "dedaf89e-84d1-4964-9c87-94beea4adf26",
      "name": "Create or update customer",
      "type": "n8n-nodes-base.hubspot",
      "position": [
        3140,
        1360
      ],
      "parameters": {
        "email": "={{$node[\"Aggregate totals\"].json[\"email\"]}}",
        "resource": "contact",
        "authentication": "oAuth2",
        "additionalFields": {
          "customPropertiesUi": {
            "customPropertiesValues": [
              {
                "value": "={{$node[\"Aggregate totals\"].json[\"amount_captured\"]}}",
                "property": "={{$(\"Configure\").first().json[\"contactPropertyId\"]}}"
              }
            ]
          }
        }
      },
      "credentials": {
        "hubspotOAuth2Api": {
          "id": "11",
          "name": "[UPDATE ME]"
        }
      },
      "notesInFlow": false,
      "typeVersion": 1
    },
    {
      "id": "4c419e90-facc-4a64-83f2-d349264338c6",
      "name": "Merge data",
      "type": "n8n-nodes-base.merge",
      "position": [
        2520,
        1360
      ],
      "parameters": {
        "mode": "combine",
        "options": {},
        "mergeByFields": {
          "values": [
            {
              "field1": "id",
              "field2": "customer"
            }
          ]
        }
      },
      "typeVersion": 2
    },
    {
      "id": "6a21495f-e567-4b0f-b584-34306bf7fa18",
      "name": "Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        2460,
        1160
      ],
      "parameters": {
        "width": 219.61431588546765,
        "height": 378.32426823578305,
        "content": "### `Merge data`\nMore specifically, we merge the Stripe data from `Get charges` and `Get customer` nodes. Only the charges with customers on them will continue."
      },
      "typeVersion": 1
    },
    {
      "id": "7319c8fe-9e55-43d9-a634-3a7884268016",
      "name": "Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        2760,
        1160
      ],
      "parameters": {
        "width": 218.46574043407196,
        "height": 379.1631729345614,
        "content": "### `Aggregate totals`\nGiven the merged data, we now aggregate the amounts from charges to the customers/contacts."
      },
      "typeVersion": 1
    },
    {
      "id": "c24d972b-270d-4467-9352-4ced18e377c0",
      "name": "Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1780,
        400
      ],
      "parameters": {
        "width": 297.57428772569784,
        "height": 325.06310253513686,
        "content": "### ``Aggregate `amount_captured` ``\nThis does nothing. It is an alternative way to find the totals of every charge in existence in Stripe. Potentially useful for debugging purposes."
      },
      "typeVersion": 1
    },
    {
      "id": "43da8885-fac3-4cb7-9f01-c4770cd0b030",
      "name": "Get all charges",
      "type": "n8n-nodes-base.stripe",
      "position": [
        1300,
        1380
      ],
      "parameters": {
        "resource": "charge",
        "operation": "getAll",
        "returnAll": true
      },
      "credentials": {
        "stripeApi": {
          "id": "22",
          "name": "[UPDATE ME]"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "abfe75f5-c36f-4904-a703-cb8d1d83b686",
      "name": "Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -960,
        1220
      ],
      "parameters": {
        "width": 504,
        "height": 510.0404950205649,
        "content": "## Sync Stripe charges to HubSpot contacts\nThis workflow pushes Stripe charges to HubSpot contacts. It uses the Stripe API to get all charges and the HubSpot API to update the contacts. The workflow will create a new HubSpot property to store the total amount charged. If the property already exists, it will update the property.\n\n### How it works\n1. On a schedule, the first Stripe node gets all charges. The default schedule is once a day at midnight.\n2. Once the charges are returned, the second Stripe node gets extra customer information.\n3. Once the customer information is returned, `Merge data` node will merge the customer information with the charges so that the next node `Aggregate totals` can calculate the total amount charged per contact.\n4. Once we have the total amount charged per contact, the `Create or update customer` node will create a new HubSpot property to store the total amount charged. If the property already exists, it will update the property.\n\n\n\nWorkflow written by [David Sha](https://davidsha.me)."
      },
      "typeVersion": 1
    },
    {
      "id": "67e44a47-18db-48a3-a08e-c4f2afb13a30",
      "name": "Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1780,
        760
      ],
      "parameters": {
        "width": 298.2919335506821,
        "height": 339.6783118583311,
        "content": "### `Remove duplicate customers`\nEnsures that we do not poll Stripe too many times unnecessarily. If multiple charges have the same customer, we ensure that we do not ask for the same information again."
      },
      "typeVersion": 1
    },
    {
      "id": "02d46492-f3ba-47fe-ba88-f2baad30fc73",
      "name": "Get HubSpot field",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        580,
        1540
      ],
      "parameters": {
        "url": "=https://api.hubapi.com/crm/v3/properties/contact/{{$(\"Configure\").first().json[\"contactPropertyId\"]}}",
        "options": {},
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "hubspotOAuth2Api"
      },
      "credentials": {
        "hubspotOAuth2Api": {
          "id": "11",
          "name": "[UPDATE ME]"
        }
      },
      "typeVersion": 3,
      "continueOnFail": true
    },
    {
      "id": "827882c4-5d3f-4cc6-b876-ae575a9a1b36",
      "name": "Create field in HubSpot",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        980,
        1660
      ],
      "parameters": {
        "url": "https://api.hubapi.com/crm/v3/properties/contact",
        "method": "POST",
        "options": {
          "response": {
            "response": {
              "neverError": true
            }
          }
        },
        "sendBody": true,
        "authentication": "predefinedCredentialType",
        "bodyParameters": {
          "parameters": [
            {
              "name": "name",
              "value": "={{$(\"Configure\").first().json[\"contactPropertyId\"]}}"
            },
            {
              "name": "label",
              "value": "={{$(\"Configure\").first().json[\"contactPropertyLabelName\"]}}"
            },
            {
              "name": "type",
              "value": "number"
            },
            {
              "name": "fieldType",
              "value": "number"
            },
            {
              "name": "groupName",
              "value": "contactinformation"
            },
            {
              "name": "formField",
              "value": "false"
            },
            {
              "name": "description",
              "value": "=The total spend determined by the charges in Stripe. This is a field required for \"{{$workflow.name}}\" n8n workflow."
            }
          ]
        },
        "nodeCredentialType": "hubspotOAuth2Api"
      },
      "credentials": {
        "hubspotOAuth2Api": {
          "id": "11",
          "name": "[UPDATE ME]"
        }
      },
      "typeVersion": 3
    },
    {
      "id": "b4092718-bf35-49b5-aefa-b9900596fcb5",
      "name": "Note5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        500,
        1480
      ],
      "parameters": {
        "width": 656.5118956254801,
        "height": 367.20468504951214,
        "content": "### Create HubSpot field if required\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n_These nodes create a HubSpot field if required.\nIt makes the contact field that this workflow uses \nto store the Stripe information. To disable this \nsection, in `Configure` node change `checkFields`\nto false._"
      },
      "typeVersion": 1
    },
    {
      "id": "6d74e2e3-bd95-4ccb-89c0-3d6f8f1e01f9",
      "name": "Configure",
      "type": "n8n-nodes-base.set",
      "position": [
        -80,
        1400
      ],
      "parameters": {
        "values": {
          "string": [
            {
              "name": "contactPropertyId",
              "value": "stripe___total_spend"
            },
            {
              "name": "contactPropertyLabelName",
              "value": "Stripe - Total Spend"
            }
          ],
          "boolean": [
            {
              "name": "checkFields",
              "value": true
            }
          ]
        },
        "options": {}
      },
      "typeVersion": 1
    },
    {
      "id": "8a8262bc-0742-4529-9f10-328c338854fe",
      "name": "Note6",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -200,
        1340
      ],
      "parameters": {
        "width": 338.8262165118159,
        "height": 505.43603897947025,
        "content": "### Configuration\n\n\n\n\n\n\n\n\n\n\n\n\nBy default, this does not need to be updated. \n\n__`contactPropertyId` (required)__: Only change if the specific HubSpot field ID has been taken.\n\n__`contactPropertyLabelName` (required)__: Change if you would like a different display name.\n\n__`checkFields` (required)__: Turn to false if you would like to optimise this workflow, provided this workflow has run once before with this configurable enabled. This will disable the section of this workflow which deals with creating a HubSpot field."
      },
      "typeVersion": 1
    },
    {
      "id": "fc640a31-2050-4276-a1f7-8154f61d2729",
      "name": "Note7",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        3080,
        1160
      ],
      "parameters": {
        "width": 219.86482940052417,
        "height": 377.58888520886353,
        "content": "### `Create or update customer`\nBy default, the only field updated is \"Stripe - Total Spend\". The contact is identified by its email."
      },
      "typeVersion": 1
    },
    {
      "id": "c91295e6-0306-4f3d-adcf-923fbef1c173",
      "name": "Skip field checking",
      "type": "n8n-nodes-base.if",
      "position": [
        240,
        1400
      ],
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": "={{$node[\"Configure\"].json[\"checkFields\"]}}",
              "value2": "={{false}}"
            }
          ]
        }
      },
      "typeVersion": 1
    },
    {
      "id": "8f8b5a15-4895-4c5a-b8ba-8592dd754aca",
      "name": "Do nothing",
      "type": "n8n-nodes-base.noOp",
      "position": [
        1880,
        1240
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "b953e439-955c-4046-9000-32cbb3577c27",
      "name": "Note8",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1780,
        1140
      ],
      "parameters": {
        "width": 298.2919335506821,
        "height": 247.94509463108915,
        "content": "### `Do nothing`\nThis is useful to know what Stripe charges had no customer assigned."
      },
      "typeVersion": 1
    },
    {
      "id": "ec2116e5-2a4a-4edf-a816-b15c349f23e0",
      "name": "If field exists",
      "type": "n8n-nodes-base.if",
      "position": [
        780,
        1540
      ],
      "parameters": {
        "conditions": {
          "number": [
            {
              "value1": "={{ $json[\"error\"][\"httpCode\"] }}",
              "value2": "404",
              "operation": "notEqual"
            }
          ]
        }
      },
      "typeVersion": 1
    }
  ],
  "connections": {
    "Configure": {
      "main": [
        [
          {
            "node": "Skip field checking",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge data": {
      "main": [
        [
          {
            "node": "Aggregate totals",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "On schedule": {
      "main": [
        [
          {
            "node": "Configure",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get customer": {
      "main": [
        [
          {
            "node": "Merge data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get all charges": {
      "main": [
        [
          {
            "node": "If charge has customer",
            "type": "main",
            "index": 0
          },
          {
            "node": "Merge data",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "If field exists": {
      "main": [
        [
          {
            "node": "Get all charges",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Create field in HubSpot",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Aggregate totals": {
      "main": [
        [
          {
            "node": "Create or update customer",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get HubSpot field": {
      "main": [
        [
          {
            "node": "If field exists",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Skip field checking": {
      "main": [
        [
          {
            "node": "Get all charges",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Get HubSpot field",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If charge has customer": {
      "main": [
        [
          {
            "node": "Remove duplicate customers",
            "type": "main",
            "index": 0
          },
          {
            "node": "Aggregate `amount_captured`",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Do nothing",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create field in HubSpot": {
      "main": [
        [
          {
            "node": "Get all charges",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Remove duplicate customers": {
      "main": [
        [
          {
            "node": "Get customer",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}