-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
100 lines (80 loc) · 2.15 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
if (process.env.NODE_ENV !== "production") {
require("dotenv").config();
}
const express = require("express");
const stripe = require("stripe")(process.env.STRIPE_PRIVATE_KEY);
const { sfConn, sfUpdateSobject } = require("./utilities/sf");
const { validate } = require("./utilities/auth");
const app = require("./utilities/cors").whitelisting(express());
const port = process.env.PORT || 3000;
app.post("/payment", express.json(), validate, async (req, res) => {
try {
console.log("payment hit");
const session = await stripe.checkout.sessions.create({
payment_method_types: ["card"],
mode: "payment",
line_items: [
{
price_data: {
currency: "usd",
product_data: {
name: "NAME HOLDER",
},
unit_amount: 1,
},
quantity: 1,
},
],
metadata: {
recordId: req?.recordId,
},
success_url: `${process.env.ReturnUrl}/${req?.recordId}`,
cancel_url: `${process.env.ReturnUrl}/${req?.recordId}`,
});
// console.log(session.url)
res.json({ url: session.url });
} catch (e) {
console.log(e);
res.status(500).json({ error: e });
}
});
app.post(
"/webhook",
express.json({ type: "application/json" }),
async (req, res) => {
try {
const event = req.body;
console.log("event", event);
console.log("event.type ", event.type);
console.log("event.data.object.metadata ", event.data.object.metadata);
// Handle the event
if (event.type === "checkout.session.completed") {
console.log("checkoutSessionCompleted");
const { sfLoginUrl, sfUsername, sfPassword, sfToken } = process.env;
const connectionResult = await sfConn(
sfLoginUrl,
sfUsername,
sfPassword,
sfToken
);
// const record = {
// Id: event.data.object.metadata.recordId,
// Status__c: "In Progress",
// };
// const recordResult = await sfUpdateSobject(
// connectionResult.conn,
// "Application__c",
// record
// );
console.log(recordResult);
}
res.send();
} catch (error) {
console.log(error);
res.status(500).json({ error });
}
}
);
app.listen(port, () => {
console.log(`server is running on port 📡 ${port}`);
});