203 lines
6.5 KiB
TypeScript
203 lines
6.5 KiB
TypeScript
import { describe, expect, it, vi } from "vitest";
|
|
|
|
import {
|
|
createMolliePaymentGateway,
|
|
MolliePaymentGatewayError,
|
|
normalizeMollieApiUrl,
|
|
parseMollieWebhookEvent,
|
|
redactMollieValue,
|
|
resolveMollieWebhookPayment,
|
|
} from "./mollie.js";
|
|
|
|
function jsonResponse(body: unknown, status = 200): Response {
|
|
return new Response(JSON.stringify(body), {
|
|
status,
|
|
headers: {
|
|
"content-type": "application/json",
|
|
},
|
|
});
|
|
}
|
|
|
|
function molliePayment(overrides: Record<string, unknown> = {}) {
|
|
return {
|
|
id: "tr_123",
|
|
status: "open",
|
|
amount: { currency: "EUR", value: "49.00" },
|
|
amountRefunded: { currency: "EUR", value: "0.00" },
|
|
amountRemaining: { currency: "EUR", value: "49.00" },
|
|
paidAt: null,
|
|
expiresAt: "2026-07-05T00:00:00+00:00",
|
|
metadata: {
|
|
tenantId: "tenant_1",
|
|
invoiceExternalId: "invoice_1",
|
|
paymentExternalId: "payment_1",
|
|
lagoCustomerId: null,
|
|
lagoSubscriptionId: null,
|
|
planCode: null,
|
|
seatQuantity: null,
|
|
},
|
|
_links: {
|
|
checkout: { href: "https://www.mollie.com/checkout/select-method/tr_123" },
|
|
},
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
describe("MolliePaymentGateway", () => {
|
|
it("creates checkout payments without HiveGrid domain data in the Mollie payload", async () => {
|
|
const fetchImpl = vi.fn<typeof fetch>(async () => jsonResponse(molliePayment()));
|
|
const gateway = createMolliePaymentGateway({
|
|
apiKey: "test_mollie_key",
|
|
profileId: "pfl_123",
|
|
fetchImpl,
|
|
});
|
|
|
|
await expect(
|
|
gateway.createPayment({
|
|
tenantId: "tenant_1",
|
|
invoiceExternalId: "invoice_1",
|
|
paymentExternalId: "payment_1",
|
|
amountCents: 4900,
|
|
currency: "eur",
|
|
redirectUrl: "https://app.hivegrid.de/admin/billing/return?invoice=invoice_1",
|
|
webhookUrl: "https://api.hivegrid.de/v1/billing/mollie/webhooks",
|
|
mollieCustomerId: "cst_123",
|
|
})
|
|
).resolves.toMatchObject({
|
|
provider: "mollie",
|
|
providerPaymentId: "tr_123",
|
|
externalId: "payment_1",
|
|
status: "open",
|
|
amountCents: 4900,
|
|
currency: "EUR",
|
|
checkoutUrl: "https://www.mollie.com/checkout/select-method/tr_123",
|
|
metadata: {
|
|
tenantId: "tenant_1",
|
|
invoiceExternalId: "invoice_1",
|
|
},
|
|
});
|
|
|
|
expect(fetchImpl).toHaveBeenCalledWith(
|
|
"https://api.mollie.com/v2/payments",
|
|
expect.objectContaining({
|
|
method: "POST",
|
|
headers: expect.objectContaining({
|
|
authorization: "Bearer test_mollie_key",
|
|
"content-type": "application/json",
|
|
}),
|
|
})
|
|
);
|
|
const request = JSON.parse(String(fetchImpl.mock.calls[0]?.[1]?.body));
|
|
expect(request).toMatchObject({
|
|
amount: { currency: "EUR", value: "49.00" },
|
|
description: "Invoice invoice_1",
|
|
redirectUrl: "https://app.hivegrid.de/admin/billing/return?invoice=invoice_1",
|
|
webhookUrl: "https://api.hivegrid.de/v1/billing/mollie/webhooks",
|
|
profileId: "pfl_123",
|
|
customerId: "cst_123",
|
|
metadata: {
|
|
tenantId: "tenant_1",
|
|
invoiceExternalId: "invoice_1",
|
|
paymentExternalId: "payment_1",
|
|
},
|
|
});
|
|
expect(JSON.stringify(request)).not.toContain("business");
|
|
expect(JSON.stringify(request)).not.toContain("seat");
|
|
expect(JSON.stringify(request)).not.toContain("billing@example.test");
|
|
});
|
|
|
|
it("verifies classic Mollie webhooks by parsing the id and refetching payment status", async () => {
|
|
const fetchImpl = vi.fn<typeof fetch>(async () =>
|
|
jsonResponse(
|
|
molliePayment({
|
|
status: "paid",
|
|
paidAt: "2026-07-04T12:00:00+00:00",
|
|
})
|
|
)
|
|
);
|
|
const gateway = createMolliePaymentGateway({
|
|
apiKey: "test_mollie_key",
|
|
fetchImpl,
|
|
});
|
|
|
|
const payment = await resolveMollieWebhookPayment(gateway, new URLSearchParams({ id: "tr_123" }));
|
|
|
|
expect(payment).toMatchObject({
|
|
providerPaymentId: "tr_123",
|
|
status: "paid",
|
|
paidAt: "2026-07-04T12:00:00+00:00",
|
|
});
|
|
expect(fetchImpl.mock.calls[0]?.[0]).toBe("https://api.mollie.com/v2/payments/tr_123");
|
|
});
|
|
|
|
it("maps refunded amounts to refunded payment status", async () => {
|
|
const fetchImpl = vi.fn<typeof fetch>(async () =>
|
|
jsonResponse(
|
|
molliePayment({
|
|
status: "paid",
|
|
amountRefunded: { currency: "EUR", value: "49.00" },
|
|
amountRemaining: { currency: "EUR", value: "0.00" },
|
|
})
|
|
)
|
|
);
|
|
const gateway = createMolliePaymentGateway({
|
|
apiKey: "test_mollie_key",
|
|
fetchImpl,
|
|
});
|
|
|
|
await expect(gateway.getPayment("tr_123")).resolves.toMatchObject({
|
|
status: "refunded",
|
|
amountRefundedCents: 4900,
|
|
amountRemainingCents: 0,
|
|
});
|
|
});
|
|
|
|
it("returns null on 404 reads and throws clear errors for failed calls", async () => {
|
|
const fetchImpl = vi.fn<typeof fetch>(async () => new Response("{}", { status: 404 }));
|
|
const gateway = createMolliePaymentGateway({
|
|
apiKey: "test_mollie_key",
|
|
fetchImpl,
|
|
});
|
|
|
|
await expect(gateway.getPayment("tr_missing")).resolves.toBeNull();
|
|
|
|
fetchImpl.mockResolvedValueOnce(new Response("{}", { status: 500 }));
|
|
await expect(
|
|
gateway.createPayment({
|
|
tenantId: "tenant_1",
|
|
invoiceExternalId: "invoice_1",
|
|
amountCents: 4900,
|
|
currency: "EUR",
|
|
redirectUrl: "https://app.hivegrid.de/return",
|
|
webhookUrl: "https://api.hivegrid.de/v1/billing/mollie/webhooks",
|
|
})
|
|
).rejects.toMatchObject({
|
|
name: "MolliePaymentGatewayError",
|
|
code: "REQUEST_FAILED",
|
|
status: 500,
|
|
message: "Mollie request failed with status 500.",
|
|
});
|
|
});
|
|
|
|
it("rejects invalid config, invalid webhook payloads and invalid response shapes", async () => {
|
|
expect(() => normalizeMollieApiUrl("file:///tmp/mollie")).toThrow(MolliePaymentGatewayError);
|
|
expect(() => createMolliePaymentGateway({ apiKey: " " })).toThrow(MolliePaymentGatewayError);
|
|
expect(() => parseMollieWebhookEvent({})).toThrow(MolliePaymentGatewayError);
|
|
|
|
const gateway = createMolliePaymentGateway({
|
|
apiKey: "test_mollie_key",
|
|
fetchImpl: vi.fn<typeof fetch>(async () => jsonResponse({ status: "paid" })),
|
|
});
|
|
await expect(gateway.getPayment("tr_123")).rejects.toMatchObject({
|
|
code: "RESPONSE_INVALID",
|
|
message: "Mollie response shape is invalid.",
|
|
});
|
|
});
|
|
|
|
it("redacts Mollie IDs and tokens for diagnostics", () => {
|
|
expect(redactMollieValue("test_mollie_key_123456")).toBe("test…3456");
|
|
expect(redactMollieValue("short")).toBe("[redacted]");
|
|
expect(redactMollieValue(undefined)).toBeNull();
|
|
});
|
|
});
|