-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdonate.js
More file actions
74 lines (65 loc) · 2.1 KB
/
Copy pathdonate.js
File metadata and controls
74 lines (65 loc) · 2.1 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
/**
* POST /api/manndeshi/donate
*
* Creates a $2.75 USDC (Algorand) AlgoVoi payment link for the
* Manndeshi Foundation donation widget.
*
* Env vars (set via wrangler secret put):
* GATEWAY_URL — AlgoVoi API base (default: https://api.algovoi.co.uk)
* GATEWAY_API_KEY — AlgoVoi Bearer token
* GATEWAY_TENANT_ID — AlgoVoi tenant UUID
*/
const DONATION_AMOUNT = 2.75;
const DONATION_CURRENCY = 'USD';
const DONATION_NETWORK = 'algorand_mainnet';
const DONATION_LABEL = 'manndeshi | Manndeshi Foundation $2.75 USDC donation';
const EXPIRES_SECONDS = 3600; // 1 hour
export async function onRequestPost(context) {
const gatewayUrl = context.env.GATEWAY_URL ?? 'https://api.algovoi.co.uk';
const apiKey = context.env.GATEWAY_API_KEY;
const tenantId = context.env.GATEWAY_TENANT_ID;
if (!apiKey || !tenantId) {
return Response.json(
{ error: 'Donation service not configured.' },
{ status: 503, headers: cors() }
);
}
const res = await fetch(`${gatewayUrl}/v1/payment-links`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`,
'X-Tenant-Id': tenantId,
},
body: JSON.stringify({
amount: DONATION_AMOUNT,
currency: DONATION_CURRENCY,
label: DONATION_LABEL,
preferred_network: DONATION_NETWORK,
expires_in_seconds: EXPIRES_SECONDS,
}),
});
if (!res.ok) {
const err = await res.text();
console.error('AlgoVoi payment-links error:', res.status, err);
return Response.json(
{ error: 'Could not create payment link. Please try again.' },
{ status: 502, headers: cors() }
);
}
const { checkout_url } = await res.json();
return Response.json(
{ checkout_url },
{ headers: cors() }
);
}
export async function onRequestOptions() {
return new Response(null, { headers: cors() });
}
function cors() {
return {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'POST, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type',
};
}