-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind-non-paying-customers.js
60 lines (50 loc) · 1.5 KB
/
find-non-paying-customers.js
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
require("dotenv").config();
if (!process.env.STRIPE_API_KEY) {
throw new Error("Please set STRIPE_API_KEY as environment variable");
}
const fs = require("fs");
const stripe = require("stripe")(process.env.STRIPE_API_KEY);
function customerHasPayment(customer) {
return (
!!customer.default_source ||
!!customer.invoice_settings?.default_payment_method
);
}
async function start() {
const nonPaidCustomers = [];
let hasMore = true;
let page;
while (hasMore) {
const customersSearchResult = await stripe.customers.search({
// query is a required field, so we search only for customers with a defined name
query: "-name:null",
limit: 30,
page,
});
hasMore = customersSearchResult.has_more;
page = customersSearchResult.next_page;
nonPaidCustomers.push(
...customersSearchResult.data.filter(
(customer) => !customerHasPayment(customer)
)
);
}
const customersData = nonPaidCustomers.map((customer) => ({
billing_id: customer.id,
customer_name: customer.name,
}));
console.log(
`Found ${nonPaidCustomers.length} customers without payment method`
);
if (customersData.length) {
const csvHeader = Object.keys(customersData[0]).join(",") + "\n";
const csvRows = customersData
.map((obj) => Object.values(obj).join(",") + "\n")
.join("");
fs.writeFile("output.csv", csvHeader + csvRows, (err) => {
if (err) throw err;
console.log("CSV file has been saved!");
});
}
}
start();