This repository was archived by the owner on Feb 20, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
200 lines (176 loc) · 6.67 KB
/
server.js
File metadata and controls
200 lines (176 loc) · 6.67 KB
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
import express from "express";
import fetch from "node-fetch";
import { config } from "dotenv";
import { storeWebhookInDB } from "./database.js";
import assert from "assert";
import { getBestQuote, verifySignature } from "./utils/index.js";
config();
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// TO DO:
// - Fix issues
// - Create collections from thunderclient
// - Remove defaults and add error messages for unsupported currencies, networks, etc.
// Receives the webhook from Onramper, verifies the signature and saves the response in the database
app.post("/webhooks", async (req, res) => {
try {
const signature = req.headers["X-Onramper-Webhook-Signature"]
console.log("webhook:", req.body)
/***** Verify the Webhook *****/
// Include verification in production by uncommenting the next 2 lines
// const verified = verifySignature(signature, process.env.ONRAMPER_SECRET, JSON.stringify(req.body))
// assert(verified, "Signature does not match")
await storeWebhookInDB(req.url, req.body, res)
return res.status(200).json({ message: "Success" })
} catch (error) {
return res.status(400).json({ error: error.message })
}
})
// Fix Here: Add req params
app.get("/v2/ramps/limits/:ramp/:fiat/:crypto/:paymentMethod", async (req, res) => {
try {
const { fiat, crypto, paymentMethod, ramp } = req.params;
const url = `https://api.onramper.com/supported/limits/${ramp}/${fiat}/${crypto}/${paymentMethod}`;
const response = await fetch(url);
const data = await response.json();
return res.status(200).json({data});
} catch (error) {
return res.status(400).json({ error: error.message })
}
})
app.get("/v2/ramps/getAllCurrencies", async (req, res) => {
try {
const url = "https://api.onramper.com/supported";
const response = await fetch(url, {
method: "GET",
headers: {
Authorization: process.env.ONRAMPER_API_KEY
}
});
const data = await response.json();
return res.status(200).json({data});
} catch (error) {
return res.status(400).json({ error: error.message })
}
})
app.get("/v2/ramps/paymentTypes", async (req, res) => {
try {
const url = "https://api.onramper.com/supported/payment-types";
const response = await fetch(url, {
method: "GET",
headers: {
Authorization: process.env.ONRAMPER_API_KEY
}
});
const data = await response.json();
return res.status(200).json({data});
} catch (error) {
return res.status(400).json({ error: error.message })
}
})
app.get("/v2/ramps/on/quotes", async (req, res) => {
try {
const { amount, paymentMethod, fiat, currency } = req.query;
assert(amount, "Amount is required")
assert(fiat, "fiat is required")
assert(currency, "currency is required")
const url = `https://api.onramper.com/quotes/${fiat}/${currency}?amount=${amount}&paymentMethod=${paymentMethod||"creditcard"}`;
const response = await fetch(url, {
method: "GET",
headers: {
Authorization: process.env.ONRAMPER_API_KEY
}
});
const data = await response.json();
return res.status(200).json({data});
} catch (error) {
return res.status(400).json({ error: error.message })
}
})
app.get("/v2/ramps/on/best-quote", async (req, res) => {
try {
const { amount, fiat, currency, paymentMethod, network } = req.query;
assert(amount, "Amount is required")
assert(network, "Network is required")
const supported = await fetch("https://api.onramper.com/supported", {
method: "GET",
headers: {
Authorization: process.env.ONRAMPER_API_KEY
}
});
const { crypto: cryptos } = (await supported.json()).message
const crypto = cryptos.find(c => c.code.toLowerCase() === currency && c.network.toLowerCase() === network);
const cryptoId = crypto?.id;
assert(cryptoId, "Currency is not supported")
const bestQuote = await getBestQuote({
source: fiat||"usd",
destination: cryptoId,
amount,
type: "buy",
paymentMethod: paymentMethod||"creditcard",
network: network
});
return res.status(200).json({data: bestQuote});
} catch (error) {
return res.status(400).json({ error: error.message })
}
})
app.get("/v2/ramps/on/checkout", async (req, res) => {
try {
/***************************************
* Authenticate Keyp ACCESS_TOKEN Here *
***************************************/
const { amount, fiat, currency, network, address } = req.query;
assert(amount, "Amount is required")
assert(address, "Wallet address is required")
assert(network, "Network is required")
const supported = await fetch("https://api.onramper.com/supported", {
method: "GET",
headers: {
Authorization: process.env.ONRAMPER_API_KEY
}
});
const { crypto: cryptos } = (await supported.json()).message
const crypto = cryptos.find(c => c.code.toLowerCase() === currency.toLowerCase() && c.network.toLowerCase() === network.toLowerCase());
const cryptoId = crypto?.id;
assert(cryptoId, "Currency is not supported")
const baseUrl = "https://buy.onramper.com/";
const url = `${baseUrl}?networkWallets=${network.toUpperCase()}:${address}&defaultAmount=${amount}&defaultFiat=${fiat}&defaultCrypto=${cryptoId.toUpperCase()}&apiKey=${process.env.ONRAMPER_API_KEY}`
return res.status(200).json({ url });
/*************************************************************************
* TO DO: The commented lines below are where the GET "/checkout/intent"
* endpoint is not working but preferred for getting the direct link
* to checkout (transak, moonpay, etc.)
*************************************************************************/
// const payload = {
// onramp: bestQuote.ramp,
// source: fiat,
// destination: cryptoId,
// amount: parseFloat(amount),
// type: "buy",
// paymentMethod,
// wallet: address,
// network: network,
// }
// console.log("payload:", payload)
// const response = await fetch(url, {
// method: "POST",
// headers: {
// "Content-Type": "application/json",
// "Authorization": process.env.ONRAMPER_API_KEY
// },
// body: JSON.stringify(payload)
// })
// console.log("status:", response.status, response.statusText)
// const result = await response.json();
// console.log("response:", result)
// assert(result.status === 200, result.message)
// return res.status(200).json({data: result});
} catch (error) {
return res.status(400).json({ error: error.message })
}
})
app.listen(3000, () => {
console.log("Server running on port 3000");
});