-
Notifications
You must be signed in to change notification settings - Fork 8
/
server.js
133 lines (123 loc) · 3.26 KB
/
server.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
const express = require('express');
const cors = require('cors');
const app = express();
app.use(cors());
app.use(express.static('./dist'));
app.use(express.json());
require('dotenv').config({ path: './.env' });
try {
var hyper = require('@juspay-tech/hyperswitch-node')(
process.env.HYPERSWITCH_SECRET_KEY,
);
} catch (err) {
console.error('process.env.HYPERSWITCH_SECRET_KEY not found, ', err.message);
process.exit(0);
}
app.get('/create-payment-intent', async (req, res) => {
try {
var paymentIntent = await hyper.paymentIntents.create({
amount: 2999,
currency: 'USD',
authentication_type: 'no_three_ds',
customer_id: 'hyperswitch_demo_id',
capture_method: 'automatic',
email: '[email protected]',
business_country: 'US',
business_label: 'default',
billing: {
address: {
line1: '1467',
line2: 'Harrison Street',
line3: 'Harrison Street',
city: 'San Fransico',
state: 'California',
zip: '94122',
country: 'PL',
first_name: 'joseph',
last_name: 'Doe',
}
},
shipping: {
address: {
line1: '1467',
line2: 'Harrison Street',
line3: 'Harrison Street',
city: 'San Fransico',
state: 'California',
zip: '94122',
country: 'PL',
first_name: 'joseph',
last_name: 'Doe',
}
},
});
// Send publishable key and PaymentIntent details to client
res.send({
publishableKey: process.env.HYPERSWITCH_PUBLISHABLE_KEY,
clientSecret: paymentIntent.client_secret,
});
} catch (err) {
console.log(err)
return res.status(400).send({
error: {
message: err.message,
},
});
}
});
app.get('/create-ephemeral-key', async (req, res) => {
try {
const response = await fetch(
`https://sandbox.hyperswitch.io/ephemeral_keys`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
'api-key': process.env.HYPERSWITCH_SECRET_KEY,
},
body: JSON.stringify({ customer_id: "hyperswitch_sdk_demo_id" }),
},
);
const ephemeralKey = await response.json();
res.send({
ephemeralKey: ephemeralKey.secret,
});
} catch (err) {
return res.status(400).send({
error: {
message: err.message,
},
});
}
});
app.get('/payment_methods', async (req, res) => {
try {
const response = await fetch(
`https://sandbox.hyperswitch.io/payment_methods`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
'api-key': process.env.HYPERSWITCH_SECRET_KEY,
},
body: JSON.stringify({ customer_id: 'hyperswitch_sdk_demo_id' }),
},
);
const json = await response.json();
res.send({
customerId: json.customer_id,
paymentMethodId: json.payment_method_id,
clientSecret: json.client_secret,
publishableKey: process.env.HYPERSWITCH_PUBLISHABLE_KEY,
});
} catch (err) {
return res.status(400).send({
error: {
message: err.message,
},
});
}
});
app.listen(5252, () =>
console.log(`Node server listening at http://localhost:5252`),
);