-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstripe.js
232 lines (216 loc) · 5.93 KB
/
stripe.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
223
224
225
226
227
228
229
230
231
232
const express = require("express");
const router = express.Router();
const Stripe_Key = "sk_test_...jQb";
const stripe = require("stripe")(Stripe_Key);
const customerId = "cus_IDxx...XTO";
router.get("/", (req, res) => {
res.status(200).json({
message: "Stripe Hello World!",
});
});
// Create a new customer for stripe
router.post("/newCustomer", async (req, res) => {
console.log("\n\n Body Passed:", req.body);
try {
const customer = await stripe.customers.create(
{
email: req.body.email,
}
// {
// // If you are using your own api then you can add your organization account here. So it will link the customer with your organization
// stripeAccount: process.env.StripeAccountId,
//}
);
return res.status(200).send({
// customerDetails: customer,
customerId: customer.id,
customerEmail: customer.email,
});
} catch (error) {
return res.status(400).send({ Error: error.raw.message });
}
});
// Add a new card of the customer
router.post("/addNewCard", async (req, res) => {
console.log("\n\n Body Passed:", req.body);
const {
cardNumber,
cardExpMonth,
cardExpYear,
cardCVC,
cardName,
country,
postal_code,
} = req.body;
if (!cardNumber || !cardExpMonth || !cardExpYear || !cardCVC) {
return res.status(400).send({
Error: "Please Provide All Necessary Details to save the card",
});
}
try {
const cardToken = await stripe.tokens.create({
card: {
name: cardName,
number: cardNumber,
exp_month: cardExpMonth,
exp_year: cardExpYear,
cvc: cardCVC,
address_country: country,
address_zip: postal_code,
},
// customer: customer.stripe_id,
// stripe_account: StripeAccountId,
});
const card = await stripe.customers.createSource(customerId, {
source: `${cardToken.id}`,
});
return res.status(200).send({
card: card.id,
});
} catch (error) {
return res.status(400).send({
Error: error.raw.message,
});
}
});
// Get List of all saved card of the customers
router.get("/viewAllCards", async (req, res) => {
let cards = [];
try {
const savedCards = await stripe.customers.listSources(customerId, {
object: "card",
});
const cardDetails = Object.values(savedCards.data);
cardDetails.forEach((cardData) => {
let obj = {
cardId: cardData.id,
cardType: cardData.brand,
cardExpDetails: `${cardData.exp_month}/${cardData.exp_year}`,
cardLast4: cardData.last4,
};
cards.push(obj);
});
return res.status(200).send({
cardDetails: cards,
});
} catch (error) {
return res.status(400).send({
Error: error.raw.message,
});
}
});
// Update saved card details of the customer
router.post("/updateCardDetails", async (req, res) => {
const { cardName, cardExpMonth, cardExpYear, cardId } = req.body;
if (!cardId) {
return res.status(400).send({
Error: "CardID is Required to update",
});
}
try {
const card = await stripe.customers.updateSource(customerId, cardId, {
name: cardName,
exp_month: cardExpMonth,
exp_year: cardExpYear,
});
return res.status(200).send({
updatedCard: card,
});
} catch (error) {
return res.status(400).send({
Error: error.raw.message,
});
}
});
// Delete a saved card of the customer
router.post("/deleteCard", async (req, res) => {
console.log("\n\n Body Passed:", req.body);
const { cardId } = req.body;
if (!cardId) {
return res.status(400).send({
Error: "CardId is required to delete Card",
});
}
try {
const deleteCard = await stripe.customers.deleteSource(customerId, cardId);
return res.status(200).send(deleteCard);
} catch (error) {
return res.status(400).send({
Error: error.raw.message,
});
}
});
// Create a payment charge
router.post("/createCharge", async (req, res) => {
console.log("\n\n Body Passed:", req.body);
const { amount, cardId, oneTime, email } = req.body;
if (oneTime) {
const {
cardNumber,
cardExpMonth,
cardExpYear,
cardCVC,
country,
postalCode,
} = req.body;
if (!cardNumber || !cardExpMonth || !cardExpYear || !cardCVC) {
return res.status(400).send({
Error: "Necessary Card Details are required for One Time Payment",
});
}
try {
const cardToken = await stripe.tokens.create({
card: {
number: cardNumber,
exp_month: cardExpMonth,
exp_year: cardExpYear,
cvc: cardCVC,
address_state: country,
address_zip: postalCode,
},
});
const charge = await stripe.charges.create({
amount: amount,
currency: "usd",
source: cardToken.id,
receipt_email: email,
description: `Stripe Charge Of Amount ${amount} for One Time Payment`,
});
if (charge.status === "succeeded") {
return res.status(200).send({ Success: charge });
} else {
return res
.status(400)
.send({ Error: "Please try again later for One Time Payment" });
}
} catch (error) {
return res.status(400).send({
Error: error.raw.message,
});
}
} else {
try {
const createCharge = await stripe.charges.create({
amount: amount,
currency: "usd",
receipt_email: email,
customer: customerId,
card: cardId,
description: `Stripe Charge Of Amount ${amount} for Payment`,
});
console.log("\n\n Start 2");
if (createCharge.status === "succeeded") {
return res.status(200).send({ Success: createCharge });
} else {
return res
.status(400)
.send({ Error: "Please try again later for payment" });
}
} catch (error) {
return res.status(400).send({
Error: error,
});
}
}
});
module.exports = router;