dummy-payment-server/main.ts

173 lines
4.9 KiB
TypeScript
Raw Normal View History

2023-07-26 12:03:09 +00:00
import { serve } from "wren/mod.ts";
import { GET, POST } from "wren/route.ts";
import * as Response from "wren/response.ts";
import { AppManifest } from "./types.ts";
2023-08-07 11:39:41 +00:00
import {
cancelSub,
chargeSub,
gatewayInitialize,
refundSub,
transactionInitialize,
2023-08-07 15:24:47 +00:00
transactionProcess,
2023-08-07 11:39:41 +00:00
} from "./subscriptions.ts";
2023-01-11 09:52:15 +00:00
interface ActionRequestResponse {
pspReference: string;
event?: {
type: string;
amount?: string;
time?: Date;
message?: string;
externalUrl?: string;
2023-07-26 12:03:09 +00:00
};
2023-01-11 09:52:15 +00:00
}
function getResponse(type: string, amount: string): ActionRequestResponse {
2023-02-02 16:32:07 +00:00
// Uncomment for reporting without status update
2023-01-11 09:52:15 +00:00
// return {
2023-02-02 16:32:07 +00:00
// pspReference: `${type}-1234`,
2023-01-11 09:52:15 +00:00
// }
2023-02-02 16:32:07 +00:00
return {
2023-07-26 12:03:09 +00:00
pspReference: `${type}-1234`,
event: {
type,
amount,
message: "Example created by dummy server",
},
};
2023-02-02 16:32:07 +00:00
}
function getUrl(req: Request) {
const domain = req.headers.get("host");
if (domain) {
return `https://${domain}`;
}
2023-07-26 12:03:09 +00:00
return "http://localhost:5544";
2023-01-11 09:52:15 +00:00
}
const routes = [
2023-07-26 12:03:09 +00:00
GET("/", () => Response.OK("Hello, Root")),
2023-02-02 16:32:07 +00:00
GET("/manifest", (req) => {
const URL = getUrl(req);
2023-01-11 09:52:15 +00:00
return Response.OK({
id: "witoszekdev.dummy-payment-app",
name: "Dummy Payment App",
appUrl: URL,
version: "1.0.0",
2023-08-07 12:51:48 +00:00
permissions: [
"HANDLE_PAYMENTS",
"HANDLE_CHECKOUTS",
"MANAGE_ORDERS",
"MANAGE_USERS",
],
2023-01-11 09:52:15 +00:00
tokenTargetUrl: `${URL}/install`,
webhooks: [
2023-08-07 11:39:41 +00:00
{
name: "Gateway Initialize",
targetUrl: `${URL}/gateway-initialize`,
query: gatewayInitialize,
syncEvents: ["PAYMENT_GATEWAY_INITIALIZE_SESSION"],
},
{
name: "Transaction Initialize",
targetUrl: `${URL}/transaction-initialize`,
query: transactionInitialize,
syncEvents: ["TRANSACTION_INITIALIZE_SESSION"],
},
2023-08-07 15:24:47 +00:00
{
name: "Transaction Process",
targetUrl: `${URL}/transaction-process`,
query: transactionProcess,
syncEvents: ["TRANSACTION_PROCESS_SESSION"],
},
2023-01-11 09:52:15 +00:00
{
name: "Charge Request",
targetUrl: `${URL}/transaction-charge-requested`,
query: chargeSub,
2023-07-26 12:03:09 +00:00
syncEvents: ["TRANSACTION_CHARGE_REQUESTED"],
2023-01-11 09:52:15 +00:00
},
{
name: "Refund Request",
targetUrl: `${URL}/transaction-refund-requested`,
query: refundSub,
2023-07-26 12:03:09 +00:00
syncEvents: ["TRANSACTION_REFUND_REQUESTED"],
2023-01-11 09:52:15 +00:00
},
{
name: "Cancel Request",
targetUrl: `${URL}/transaction-cancelation-requested`,
query: cancelSub,
2023-07-26 12:03:09 +00:00
syncEvents: ["TRANSACTION_CANCELATION_REQUESTED"],
2023-01-11 09:52:15 +00:00
},
],
2023-07-26 12:03:09 +00:00
} satisfies AppManifest);
2023-01-11 09:52:15 +00:00
}),
POST("/install", async (req) => {
console.log("install");
const json = await req.json();
console.log("install", json);
return Response.OK({
2023-07-26 12:03:09 +00:00
success: true,
});
2023-01-11 09:52:15 +00:00
}),
2023-08-07 11:39:41 +00:00
POST("/gateway-initialize", async (req: Request) => {
const json = await req.json();
console.log("gateway initialize", json);
console.log("headers", req.headers);
return Response.OK({
data: {
some: "data",
},
});
}),
POST("/transaction-initialize", async (req: Request) => {
const json = await req.json();
console.log("transaction initialize", json);
console.log("headers", req.headers);
const amount = json.action.amount;
2023-08-07 15:36:11 +00:00
const action = json.action.actionType ?? "CHARGE";
2023-08-07 11:39:41 +00:00
return Response.OK({
pspReference: "initialize-test",
2023-08-07 15:36:11 +00:00
result: json?.data?.final ? `${action}_SUCCESS` : `${action}_REQUEST`,
2023-08-07 15:24:47 +00:00
amount,
});
}),
POST("/transaction-process", async (req: Request) => {
const json = await req.json();
console.log("transaction process", json);
console.log("headers", req.headers);
const amount = json.action.amount;
2023-08-07 15:36:11 +00:00
const action = json.action.actionType ?? "CHARGE";
2023-08-07 15:24:47 +00:00
return Response.OK({
pspReference: "initialize-test",
2023-08-07 15:36:11 +00:00
result: json?.data?.final ? `${action}_SUCCESS` : `${action}_REQUEST`,
2023-08-07 11:39:41 +00:00
amount,
});
}),
2023-07-26 12:03:09 +00:00
POST("/transaction-charge-requested", async (req: Request) => {
const json = await req.json();
2023-01-11 09:52:15 +00:00
console.log("charge request", json);
2023-07-26 12:03:09 +00:00
console.log("headers", req.headers);
2023-01-11 09:52:15 +00:00
const amount = json.action.amount;
2023-07-26 12:03:09 +00:00
return Response.OK(getResponse("CHARGE_SUCCESS", amount));
2023-01-11 09:52:15 +00:00
}),
POST("/transaction-refund-requested", async (req) => {
2023-07-26 12:03:09 +00:00
const json = await req.json();
2023-01-11 09:52:15 +00:00
console.log("refund request", json);
const amount = json.action.amount;
2023-07-26 12:03:09 +00:00
return Response.OK(getResponse("REFUND_SUCCESS", amount));
2023-01-11 09:52:15 +00:00
}),
POST("/transaction-cancelation-requested", async (req) => {
2023-07-26 12:03:09 +00:00
const json = await req.json();
2023-01-11 09:52:15 +00:00
console.log("cancel request", json);
2023-07-26 12:03:09 +00:00
const amount = json.action.amount;
return Response.OK(getResponse("CANCEL_SUCCESS", amount));
2023-01-11 09:52:15 +00:00
}),
POST("/transaction-action-request", async (req) => {
const json = await req.json();
2023-07-26 12:03:09 +00:00
console.log("received old async event", json);
2023-01-11 09:52:15 +00:00
return Response.OK("Accepted");
2023-07-26 12:03:09 +00:00
}),
2023-01-11 09:52:15 +00:00
];
serve(routes);