forked from nexxtway/rainbow-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
97 lines (86 loc) · 2.94 KB
/
index.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
/* eslint-disable camelcase */
const functions = require('firebase-functions');
const express = require('express');
const cors = require('cors');
const { secret_key, customer_id } = functions.config().stripe;
const stripe = require('stripe')(secret_key);
exports.createPaymentIntent = functions.https.onCall(async (data) => {
try {
const { amount } = data;
const paymentIntent = await stripe.paymentIntents.create({
amount,
currency: 'usd',
payment_method_types: ['card'],
customer: customer_id,
setup_future_usage: 'off_session',
});
return paymentIntent.client_secret;
} catch (error) {
functions.logger.error('Error creating a setup intent', error);
throw new functions.https.HttpsError('internal', error.message);
}
});
exports.createSetupIntent = functions.https.onCall(async () => {
try {
const setupIntent = await stripe.setupIntents.create({
payment_method_types: ['card'],
customer: customer_id,
});
return setupIntent.client_secret;
} catch (error) {
functions.logger.error('Error creating a setup intent', error);
throw new functions.https.HttpsError('internal', error.message);
}
});
exports.getCustomerCards = functions.https.onCall(async () => {
try {
const res = await stripe.paymentMethods.list({
customer: customer_id,
type: 'card',
});
return res.data.map((card) => {
const {
id,
card: { brand, exp_month, exp_year, last4 },
} = card;
return {
id,
brand,
last4,
expMonth: exp_month,
expYear: exp_year,
};
});
} catch (error) {
functions.logger.error('Error fetching customer cards', error);
throw new functions.https.HttpsError('internal', error.message);
}
});
exports.removeCard = functions.https.onCall(async (data) => {
const { paymentMethod } = data;
try {
const removedPaymentMethod = await stripe.paymentMethods.detach(paymentMethod);
return removedPaymentMethod;
} catch (error) {
functions.logger.error('Error removing a payment method', error);
throw new functions.https.HttpsError('internal', error.message);
}
});
const app = express();
app.use(
cors({
origin: '*',
}),
);
app.use((req, res, next) => {
if (Math.random() > 0.5) {
return res.status(403).send({
code: 'permission-denied',
message: `You do not have permissions to excecute this operation.`,
});
}
return next();
});
app.get('/helloWorld', (req, res) => res.send({ message: 'Hello World!' }));
app.post('/customHelloWorld', (req, res) => res.send({ message: `Hello World! ${req.body.name}` }));
exports.expressTestFn = functions.https.onRequest(app);