This repository has been archived by the owner on Jan 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.js
98 lines (87 loc) · 2.59 KB
/
setup.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
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
/**
* setup.js
* Stripe Payments Demo. Created by Romain Huet (@romainhuet)
* and Thorsten Schaeff (@thorwebdev).
*
* This is a one-time setup script for your server. It creates a set of fixtures,
* namely products and SKUs, that are used to create a random basket session.
*/
'use strict';
const fs = require('fs');
const config = require('./src/functions/helpers/configHelper');
const stripe = require('stripe')(config.stripe.secretKey);
stripe.setApiVersion(config.stripe.apiVersion);
// Replace this list with information about your store's products.
const products = [
{
id: 'increment',
name: 'Increment Magazine',
price: 399,
attributes: {issue: 'Issue #3 “Development”'},
},
{
id: 'shirt',
name: 'Stripe Shirt',
price: 999,
attributes: {size: 'Small Standard', gender: 'Woman'},
},
{
id: 'pins',
name: 'Stripe Pins',
price: 799,
attributes: {set: 'Collector Set'},
},
];
// Creates a collection of Stripe Products and SKUs to use in your storefront
const createStoreProducts = async () => {
try {
const stripeProducts = await Promise.all(
products.map(async product => {
const stripeProduct = await stripe.products.create({
id: product.id,
name: product.name,
type: 'good',
attributes: Object.keys(product.attributes),
metadata: product.metadata,
});
const stripeSku = await stripe.skus.create({
product: stripeProduct.id,
price: product.price,
currency: config.currency,
attributes: product.attributes,
inventory: {type: 'infinite'},
});
return {stripeProduct, stripeSku};
})
);
console.log(
`🛍️ Successfully created ${
stripeProducts.length
} products on your Stripe account.`
);
} catch (error) {
console.log(`⚠️ Error: ${error.message}`);
return;
}
};
// Set up the Stripe CLI.
// https://github.com/stripe/stripe-cli
const writeCLIConfig = async () => {
const configFilePath = `${process.env.HOME}/.config/stripe`;
const stripeCLIConfig = `
[stripe-payments-demo]
device_name = "stripe-payments-demo"
secret_key = "${config.stripe.secretKey}"
`;
if (!fs.existsSync(configFilePath)) {
fs.mkdirSync(configFilePath);
}
fs.readFile(`${configFilePath}/config.toml`, function(err, data) {
if (err || !data.includes('stripe-payments-demo')) {
fs.appendFileSync(`${configFilePath}/config.toml`, stripeCLIConfig);
console.log(`Stripe CLI configuration set up.`);
}
});
};
writeCLIConfig();
createStoreProducts();