-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
222 lines (205 loc) · 7.32 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
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
const functions = require("firebase-functions");
const admin = require("firebase-admin");
const stripe = require("stripe")(process.env.STRIPE_SECRET_KEY, {
apiVersion: "2020-08-27",
});
admin.initializeApp();
exports.createStripeCustomer = functions.firestore
.document("customers/{customerId}")
.onCreate(async (snap, context) => {
try {
const newValue = snap.data();
const customer = await stripe.customers.create({
email: newValue.email,
});
await admin
.firestore()
.collection("customers")
.doc(context.params.customerId)
.update({
stripeCustomerId: customer.id,
});
} catch (error) {
functions.logger.error("Stripe customer ccreation error", error);
}
});
exports.createPaymentSheet = functions.https
.onRequest(async (request, response) => {
try {
const {productId} = request.body;
const customer = await _getCustomerFromRequest(request);
const product = await _getProduct(productId);
const stripeObject = {
amount: product.amount * 100, // Use smallest currency unit (cent)
currency: "USD",
payment_method_types: ["card"],
customer: customer.stripeCustomerId,
metadata: {
productId,
customerId: customer.id,
},
};
const ephemeralKey = await stripe.
ephemeralKeys.create({customer: customer.stripeCustomerId},
{apiVersion: "2020-08-27"},
);
const paymentIntent = await stripe.paymentIntents.create(stripeObject);
return response.status(200).send({
clientSecret: paymentIntent["client_secret"],
ephemeralKey: ephemeralKey.secret,
});
} catch (error) {
functions.logger.error("init payment sheet", error);
return response.sendStatus(400);
}
});
exports.createPaymentIntent = functions.https
.onRequest(async (request, response) => {
try {
const {productId, paymentMethodId, allowFutureUsage} = request.body;
const customer = await _getCustomerFromRequest(request);
const product = await _getProduct(productId);
const stripeObject = {
amount: product.amount * 100, // Use smallest currency unit (cent)
currency: "USD",
payment_method_types: ["card"],
customer: customer.stripeCustomerId,
metadata: {
productId,
customerId: customer.id,
},
...(allowFutureUsage && {setup_future_usage: "off_session"}),
...(paymentMethodId && {payment_method: paymentMethodId}),
};
const paymentIntent = await stripe.paymentIntents.create(stripeObject);
return response.status(200).send({
clientSecret: paymentIntent["client_secret"],
});
} catch (error) {
functions.logger.error("create payment intent", error);
return response.sendStatus(400);
}
});
// used to charge a card without intercation from the customer.
// Suitable for cron jobs. For this example, it will still be triggered
// from the front end as a second alternative to making payment with saved card.
exports.chargeCardOffSession = functions.https
.onRequest(async (request, response) => {
try {
const {productId} = request.body;
const customer = await _getCustomerFromRequest(request);
const product = await _getProduct(productId);
const stripeObject = {
amount: product.amount * 100, // Use smallest currency unit (cent)
currency: "USD",
customer: customer.stripeCustomerId,
payment_method: request.body.paymentMethodId,
off_session: true,
confirm: true,
metadata: {
productId,
customerId: customer.id,
},
};
const paymentIntent = await stripe.paymentIntents.create(stripeObject);
return response.status(200).send(paymentIntent);
} catch (error) {
// best to send a message to the customer letting them know
// that their card was not charged
functions.logger.error("intent create", error);
return response.sendStatus(400);
}
});
exports.fetchCustomerCards = functions.https.onRequest(
async (request, response) => {
try {
const customer = await _getCustomerFromRequest(request);
const paymentMethods = await stripe.paymentMethods.list({
customer: customer.stripeCustomerId,
type: "card",
});
return response.status(200).send(paymentMethods.data);
} catch (error) {
console.log(error);
functions.logger.error("Customer card error", error);
return response.sendStatus(400);
}
},
);
exports.deletePaymentMethod = functions.https.onRequest(
async (request, response) => {
try {
await stripe.paymentMethods.detach(request.body.paymentMethodId);
return response.sendStatus(200);
} catch (error) {
console.log(error);
functions.logger.info("delete payment method", {error: error});
return response.sendStatus(400);
}
},
);
exports.stripeWebhook = functions.https
.onRequest(async (request, response) => {
try {
let event;
try {
event = stripe.webhooks.constructEvent(
request.rawBody,
request.headers["stripe-signature"] || [],
process.env.STRIPE_WEBHOOK_SECRET,
);
} catch (error) {
functions.logger.info("stripe webhook verification error",
error);
return response.sendStatus(400);
}
if (event.type === "payment_intent.succeeded") {
const {customerId, productId} = request.body.data.object.metadata;
await _savePurchase(customerId, productId);
}
return response.sendStatus(200);
} catch (error) {
functions.logger.error("stripe webhook error", error);
return response.sendStatus(400);
}
});
/**
* Get product from firestore with Id
* @param {string} productId Id of product
*/
async function _getProduct(productId) {
const documentSnapshot = await admin
.firestore()
.collection("products")
.doc(productId).get();
return {id: documentSnapshot.id, ...documentSnapshot.data()};
}
/**
* Get the customer throught the firebase id tokenn sent inn the request header
* @param {object} request firebase id token of user gotten from client side
*/
async function _getCustomerFromRequest(request) {
const idToken = request.get("Authorization").split("Bearer ")[1];
const verifiedToken = await admin.auth().verifyIdToken(idToken);
const documentSnapshot = await admin
.firestore()
.collection("customers")
.doc(verifiedToken.uid).get();
return {id: documentSnapshot.id, ...documentSnapshot.data()};
}
/**
*
* @param {string} customerId firebase id of the customer
* that made the purchase.
* @param {string} productId product that was purchased by the customer.
*/
async function _savePurchase(customerId, productId) {
const product = await _getProduct(productId);
await admin
.firestore()
.collection("purchases").add({
customerId,
product,
datePurchased: admin.firestore.FieldValue.serverTimestamp(),
});
}