Provide default transaction actions

This commit is contained in:
Jonatan Witoszek 2023-10-09 12:05:58 +02:00
parent 06e7363783
commit ecc52d3f4d
No known key found for this signature in database
GPG key ID: D35056EA9CCE8472

28
main.ts
View file

@ -24,10 +24,18 @@ import * as log from "log/mod.ts";
const apl = new DenoAPL(); const apl = new DenoAPL();
type TransactionActions = "CHARGE" | "REFUND" | "CANCEL";
type TransactionProcessAction = "AUTHORIZATION" | "CHARGE";
interface TransactionRequestResponse { interface TransactionRequestResponse {
pspReference: string; pspReference: string;
result: string; result: string;
amount: string; amount: string;
data?: Record<string, string>;
time?: string;
externalUrl?: string;
nessage?: string;
actions?: Array<TransactionActions>;
} }
interface ActionRequestResponse { interface ActionRequestResponse {
@ -41,13 +49,26 @@ interface ActionRequestResponse {
}; };
} }
function getTransactionActions(
action: TransactionProcessAction,
): Array<TransactionActions> {
if (action === "CHARGE") {
return ["REFUND"];
}
if (action === "AUTHORIZATION") {
return ["CHARGE", "CANCEL"];
}
return [];
}
async function getTransactionResponse( async function getTransactionResponse(
req: Request, req: Request,
logger: log.Logger logger: log.Logger,
): Promise<Response> { ): Promise<Response> {
const json = await req.json(); const json = await req.json();
const amount = json.action.amount; const amount = json.action.amount;
const action = json.action.actionType ?? "CHARGE"; const action =
json.action.actionType ?? ("CHARGE" as TransactionProcessAction);
const saleorApiUrl = req.headers.get(SALEOR_API_URL_HEADER); const saleorApiUrl = req.headers.get(SALEOR_API_URL_HEADER);
const data = json?.data; const data = json?.data;
@ -74,13 +95,14 @@ async function getTransactionResponse(
pspReference: "initialize-test", pspReference: "initialize-test",
result: `${action}_SUCCESS`, result: `${action}_SUCCESS`,
amount, amount,
actions: getTransactionActions(action),
...data, ...data,
} satisfies TransactionRequestResponse); } satisfies TransactionRequestResponse);
} }
async function getActionResponse( async function getActionResponse(
req: Request, req: Request,
logger: log.Logger logger: log.Logger,
): Promise<Response> { ): Promise<Response> {
const json = await req.json(); const json = await req.json();
const amount = json.action.amount; const amount = json.action.amount;