-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreatePlans.js
38 lines (33 loc) · 1.06 KB
/
createPlans.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
/*
** Create product plans (prices) on Stripe from the supplied CSV
** Expect CSV file headers [plan_id, plan_name, price, interval]
*/
require('dotenv').config();
const fs = require('fs');
const csv = require('csv-parser');
const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY);
const { RateLimit } = require('async-sema');
const { MAX_REQUESTS_PER_SECOND } = require('./utils/constants');
const limit = RateLimit(MAX_REQUESTS_PER_SECOND);
const sourceCSV = process.argv.slice(2);
fs.createReadStream(`./mock-data/${sourceCSV}`)
.pipe(csv())
.on('data', async (row) => {
try {
await limit();
const { plan_id, plan_name, price, interval } = row;
const plan = await stripe.prices.create({
unit_amount: parseFloat(price, 10) * 100,
currency: 'usd',
recurring: { interval },
product_data: {
name: plan_name,
active: true,
},
lookup_key: plan_id
});
console.log(`Created Stripe plan ${plan.id} success!`)
} catch (error) {
console.log(error);
}
});