-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathget-redeem-code.ts
90 lines (77 loc) · 3.34 KB
/
get-redeem-code.ts
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
import { verifyMessage } from "@ethersproject/wallet";
import { getGiftCardOrderId, getRevealMessageToSign } from "../shared/helpers";
import { getRedeemCodeParamsSchema } from "../shared/api-types";
import { getTransactionFromOrderId } from "./get-order";
import { commonHeaders, getAccessToken, getReloadlyApiBaseUrl } from "./utils/shared";
import { AccessToken, Context, ReloadlyFailureResponse, ReloadlyRedeemCodeResponse } from "./utils/types";
import { validateEnvVars, validateRequestMethod } from "./utils/validators";
import { RedeemCode } from "../shared/types";
export async function onRequest(ctx: Context): Promise<Response> {
try {
validateRequestMethod(ctx.request.method, "GET");
validateEnvVars(ctx);
const accessToken = await getAccessToken(ctx.env);
const { searchParams } = new URL(ctx.request.url);
const result = getRedeemCodeParamsSchema.safeParse({
transactionId: searchParams.get("transactionId"),
signedMessage: searchParams.get("signedMessage"),
wallet: searchParams.get("wallet"),
permitSig: searchParams.get("permitSig"),
});
if (!result.success) {
throw new Error(`Invalid parameters: ${JSON.stringify(result.error.errors)}`);
}
const { transactionId, signedMessage, wallet, permitSig } = result.data;
const errorResponse = Response.json({ message: "Given details are not valid to redeem code." }, { status: 403 });
if (verifyMessage(getRevealMessageToSign(transactionId), signedMessage) != wallet) {
console.error(
`Signed message verification failed: ${JSON.stringify({
signedMessage,
transactionId,
})}`
);
return errorResponse;
}
const orderId = getGiftCardOrderId(wallet, permitSig);
const order = await getTransactionFromOrderId(orderId, accessToken);
if (order?.transactionId != transactionId) {
console.error(
`Given transaction does not match with retrieved transactionId using generated orderId: ${JSON.stringify({
transactionId,
orderId,
transactionIdFromOrder: order?.transactionId,
})}`
);
return errorResponse;
}
const redeemCode = await getRedeemCode(transactionId, accessToken);
return Response.json(redeemCode, { status: 200 });
} catch (error) {
console.error("There was an error while processing your request.", error);
return Response.json({ message: "There was an error while processing your request." }, { status: 500 });
}
}
export async function getRedeemCode(transactionId: number, accessToken: AccessToken): Promise<RedeemCode[]> {
const url = `${getReloadlyApiBaseUrl(accessToken.isSandbox)}/orders/transactions/${transactionId}/cards`;
console.log(`Retrieving redeem codes from ${url}`);
const options = {
method: "GET",
headers: {
...commonHeaders,
Authorization: `Bearer ${accessToken.token}`,
},
};
const response = await fetch(url, options);
const responseJson = await response.json();
if (response.status != 200) {
throw new Error(
`Error from Reloadly API: ${JSON.stringify({
status: response.status,
message: (responseJson as ReloadlyFailureResponse).message,
})}`
);
}
console.log("Response status", response.status);
console.log(`Response from ${url}`, responseJson);
return responseJson as ReloadlyRedeemCodeResponse;
}