-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
148 lines (126 loc) · 3.72 KB
/
Copy pathindex.ts
File metadata and controls
148 lines (126 loc) · 3.72 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
import SumUp from "@sumup/sdk";
const apiKey = process.env.SUMUP_API_KEY;
const merchantCode = process.env.SUMUP_MERCHANT_CODE;
if (!apiKey) {
console.error("Missing SUMUP_API_KEY env var.");
process.exit(1);
}
if (!merchantCode) {
console.error("Missing SUMUP_MERCHANT_CODE env var.");
process.exit(1);
}
const client = new SumUp({ apiKey });
const port = Number(process.env.PORT) || 8080;
function parseAmount(value: unknown) {
const amount = Number(value);
if (!Number.isFinite(amount) || amount <= 0) {
return null;
}
return {
currency: "EUR",
minor_unit: 2,
value: Math.round(amount * 100),
};
}
Bun.serve({
port,
async fetch(req) {
const url = new URL(req.url);
const segments = url.pathname.split("/").filter(Boolean);
if (segments.length === 1 && segments[0] === "readers") {
if (req.method === "GET") {
try {
const readers = await client.readers.list(merchantCode);
return Response.json(readers);
} catch (error) {
console.error("Failed to list readers:", error);
return Response.json(
{ error: "failed to list readers" },
{ status: 500 },
);
}
}
if (req.method !== "POST") {
return new Response("Not found", { status: 404 });
}
let payload: { pairing_code?: string; name?: string };
try {
payload = (await req.json()) as {
pairing_code?: string;
name?: string;
};
} catch {
return Response.json({ error: "invalid json" }, { status: 400 });
}
const pairingCode = String(payload.pairing_code ?? "").trim();
const name = String(payload.name ?? "").trim();
if (!pairingCode || !name) {
return Response.json(
{ error: "pairing_code and name are required" },
{ status: 400 },
);
}
try {
const reader = await client.readers.create(merchantCode, {
pairing_code: pairingCode,
name,
});
return Response.json(reader, { status: 201 });
} catch (error) {
console.error("Failed to create reader:", error);
return Response.json(
{ error: "failed to create reader" },
{ status: 500 },
);
}
}
if (
segments.length === 3 &&
segments[0] === "readers" &&
segments[2] === "checkout"
) {
if (req.method !== "POST") {
return new Response("Not found", { status: 404 });
}
let payload: { amount?: number };
try {
payload = (await req.json()) as { amount?: number };
} catch {
return Response.json({ error: "invalid json" }, { status: 400 });
}
const totalAmount = parseAmount(payload.amount);
if (!totalAmount) {
return Response.json(
{ error: "amount must be a positive number" },
{ status: 400 },
);
}
const readerId = segments[1];
if (!readerId) {
return Response.json(
{ error: "readerId is required" },
{ status: 400 },
);
}
try {
const checkout = await client.readers.createCheckout(
merchantCode,
readerId,
{
total_amount: totalAmount,
description: "Card reader checkout",
},
);
return Response.json(checkout, { status: 201 });
} catch (error) {
console.error("Failed to create reader checkout:", error);
return Response.json(
{ error: "failed to create reader checkout" },
{ status: 500 },
);
}
}
return new Response("Not found", { status: 404 });
},
});
console.log(`Bun server listening on http://localhost:${port}`);