-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
247 lines (229 loc) · 6.1 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
//const functions = require('firebase-functions');
const express = require("express");
const app = express();
const { resolve } = require("path");
// This is your real test secret API key.
const stripe = require("stripe")(
""
//"sk_test_51Hv7mcEVPhT8RTjUXpoDt0Ouq8dN1IsHZBeESVLPcp4iTbIxl4Bvc3YyUKiBpquGvmGXhcTrVuDztYHpyavwXoet00n4rNVKPU"
);
app.use(express.static("."));
app.use(express.json());
const calculateOrderAmount = (items) => {
console.log(items[0].amount);
return items[0].amount;
};
// create payment intent for secure payment
app.post("/create-payment-intent", async (req, res) => {
const { items } = req.body;
const { currency } = req.body;
console.log(req.body);
try {
// Create a PaymentIntent with the order amount and currency
const paymentIntent = await stripe.paymentIntents.create({
amount: calculateOrderAmount(items),
currency: currency,
});
// Create or retrieve the Stripe Customer object associated with your user.
let customer = await stripe.customers.create();
// Create an ephemeral key for the Customer; this allows the app to display saved payment methods and save new ones
const ephemeralKey = await stripe.ephemeralKeys.create(
{ customer: customer.id },
{ apiVersion: "2020-08-27" }
);
res.send({
// publishableKey: "pk_test_51Hv7mcEVPhT8RTjUDqMes8WIjZXxP6g71HiT7l2TmhVwf1qgMLKPd6i7iSh0NX7Anul4qFKVOveJasQobeTBG3ju00twHUiNCk",
publishableKey: "",
clientSecret: paymentIntent.client_secret,
ephemeralK: ephemeralKey.secret,
current_customer: customer.id,
});
} catch (e) {
console.log(e)
res.send({
success: false
})
}
});
app.get("", (req, res) => {
res.send("Server is up and running");
});
// app.post("/payout", (req, res) => {
// stripe.accounts.createExternalAccount(
// "acct_1KjohySAqc8rIN9R",
// {
// // card_1KjoBvSJgjKVvQEw4HAucGg8
// external_account: {
// object: "card_1KjoBvSJgjKVvQEw4HAucGg8",
// },
// },
// function (err, card) {
// // asynchronously called
// if (err) return res.send(err);
// res.send(card);
// }
// );
// });
// get btok (bank token) for adding it to a bank account
app.post("/createBankToken", async (req, res) => {
const { account_holder_name } = req.body;
const { routing_number } = req.body;
const { account_number } = req.body;
try {
const token = await stripe.tokens.create({
bank_account: {
country: "GB",
currency: "gbp",
account_holder_name: account_holder_name,
account_holder_type: "individual",
routing_number: routing_number,
account_number: account_number,
},
});
console.log(token);
res.send({
token: token.id,
message: "token created!",
success: true,
});
} catch (e) {
res.send({
success: false,
});
console.log(e);
}
});
// transfer money to a stripe connect account
app.post("/transfer", async (req, res) => {
const { amount } = req.body;
const { destination } = req.body;
try {
const transfer = await stripe.transfers.create({
amount: amount,
currency: "gbp",
destination: destination,
});
res.send({
success: true,
message: "Transfer Successful!",
});
console.log(transfer);
} catch (e) {
res.send({
success: false,
message: "Transfer failed!",
});
console.log(e);
}
});
// get all balance of the organization
app.post("/balance", async (req, res) => {
try {
const balance = await stripe.balance.retrieve();
res.send({
available: balance.available[0].amount,
pending: balance.pending[0].amount,
success: true,
});
} catch (e) {
res.send({
success: false,
});
console.log(e);
}
});
// get balance of particular account
app.post("/balance/account", async (req, res) => {
try {
const { stripeAccount } = req.body;
const balance = await stripe.balance.retrieve({
stripeAccount: stripeAccount,
});
res.send({
available: balance.available[0].amount,
pending: balance.pending[0].amount,
success: true,
});
} catch (e) {
res.send({
success: false,
});
console.log(e);
}
});
// add bank account to connected(which we created with create account endpoint) account
app.post("/addBankAccount", async (req, res) => {
const { account } = req.body;
const { token } = req.body;
try {
const cardToken = await stripe.accounts.createExternalAccount(account, {
external_account: token,
});
res.send({ token: cardToken.id, message: "Account Added", success: true });
} catch (e) {
res.send({
success: false,
});
console.log(e);
}
});
// create a user account to connect account
app.post("/createAccount", async (req, res) => {
const { email } = req.body;
const { first_name } = req.body;
const { last_name } = req.body;
try {
const account = await stripe.accounts.create({
type: "custom",
country: "GB",
email: email,
capabilities: {
card_payments: {
requested: true,
},
transfers: {
requested: true,
},
},
business_type: "individual",
tos_acceptance: {
date: Math.floor(Date.now() / 1000),
ip: "1.23.121.84",
},
individual: {
address: {
line1: "address_full_match",
postal_code: "SW1W 0NY",
state: "Texas",
city: "Texas",
},
dob: {
day: "01",
month: "01",
year: "1901",
},
email: email,
first_name: first_name,
last_name: last_name,
phone: "7976694567",
id_number: "lO1DEQWBbQ",
},
business_profile: {
url: "https://swapshop.me/",
mcc: 7512,
},
});
res.send({
accountId: account.id,
message: "Account Created",
success: true,
});
} catch (e) {
res.send({
success: false,
});
console.log(e);
}
});
const PORT = process.env.PORT || 5001;
app.listen(PORT, () => console.log(`Node server listening on port ${PORT}`));