-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathserver.js
171 lines (154 loc) · 4.28 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
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
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'});
const PORT = 5252;
async function createPaymentIntent(request) {
try {
const url =
process.env.HYPERSWITCH_SERVER_URL || 'https://sandbox.hyperswitch.io';
const apiResponse = await fetch(`${url}/payments`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
'api-key': process.env.HYPERSWITCH_SECRET_KEY,
},
body: JSON.stringify(request),
});
const paymentIntent = await apiResponse.json();
if (paymentIntent.error) {
console.error('Error - ', paymentIntent.error);
throw new Error(paymentIntent.error.message ?? 'Something went wrong.');
}
return paymentIntent;
} catch (error) {
console.error('Failed to create payment intent:', error);
throw new Error(
error.message ||
'Unexpected error occurred while creating payment intent.',
);
}
}
app.get('/create-payment-intent', async (req, res) => {
try {
const createPaymentBody = {
amount: 2999,
currency: 'USD',
authentication_type: 'no_three_ds',
customer_id: 'hyperswitch_demo_id',
capture_method: 'automatic',
email: '[email protected]',
billing: {
address: {
line1: '1467',
line2: 'Harrison Street',
line3: 'Harrison Street',
city: 'San Fransico',
state: 'California',
zip: '94122',
country: 'US',
first_name: 'joseph',
last_name: 'Doe',
},
},
shipping: {
address: {
line1: '1467',
line2: 'Harrison Street',
line3: 'Harrison Street',
city: 'San Fransico',
state: 'California',
zip: '94122',
country: 'US',
first_name: 'joseph',
last_name: 'Doe',
},
},
};
const profileId = process.env.PROFILE_ID;
if (profileId) {
createPaymentBody.profile_id = profileId;
}
var paymentIntent = await createPaymentIntent(createPaymentBody);
// Send publishable key and PaymentIntent details to client
res.send({
publishableKey: process.env.HYPERSWITCH_PUBLISHABLE_KEY,
clientSecret: paymentIntent.client_secret,
});
} catch (err) {
console.error(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.get('/netcetera-sdk-api-key', (req, res) => {
const apiKey = process.env.NETCETERA_SDK_API_KEY;
if (apiKey) {
res.status(200).send({netceteraApiKey: apiKey});
} else {
res.status(500).send({error: 'Netcetera SDK API key is missing'});
}
});
app.listen(PORT, () =>
console.info(`Node server listening at http://localhost:${PORT}`),
);