diff --git a/src/requests/payment-request.ts b/src/requests/payment-request.ts index 124fcd3a..b47b7eb7 100644 --- a/src/requests/payment-request.ts +++ b/src/requests/payment-request.ts @@ -87,7 +87,7 @@ export async function createPaymentRequest( const payNode = await getPayNode(client, session); const govPayMetadata = payNode.data?.govPayMetadata || [ { key: "source", value: "PlanX" }, - { key: "isInviteToPay", value: true }, + { key: "paidViaInviteToPay", value: true }, { key: "flow", value: session.flow.slug }, ]; diff --git a/src/utils/govPayMetadata.test.ts b/src/utils/govPayMetadata.test.ts index 007ad931..30e2c711 100644 --- a/src/utils/govPayMetadata.test.ts +++ b/src/utils/govPayMetadata.test.ts @@ -18,6 +18,10 @@ const mockMetadata: GovPayMetadata[] = [ key: "key_someValueNotSetInPassport", value: "@someValueNotSetInPassportolean", }, + { + key: "paidViaInviteToPay", + value: "@paidViaInviteToPay", + }, ]; const mockPassport: Passport = { @@ -38,9 +42,15 @@ const mockPassport: Passport = { }, }; +const mockParams: Parameters[0] = { + metadata: mockMetadata, + userPassport: mockPassport, + paidViaInviteToPay: false, +}; + describe("transforming the payload to the correct format for GovPay", () => { it("handles variables set in the Editor", () => { - const result = formatGovPayMetadata(mockMetadata, mockPassport); + const result = formatGovPayMetadata(mockParams); expect(result).toMatchObject({ firstKey: "firstValue", secondKey: "secondValue", @@ -48,7 +58,7 @@ describe("transforming the payload to the correct format for GovPay", () => { }); it("handles variables set in the Passport", () => { - const result = formatGovPayMetadata(mockMetadata, mockPassport); + const result = formatGovPayMetadata(mockParams); expect(result).toMatchObject({ key_string: "agent", key_boolean: true, @@ -57,7 +67,7 @@ describe("transforming the payload to the correct format for GovPay", () => { }); describe("validating data", () => { - const formatted = formatGovPayMetadata(mockMetadata, mockPassport); + const formatted = formatGovPayMetadata(mockParams); const get = (testKey: string) => [ mockPassport.data[testKey], // Expected @@ -120,4 +130,28 @@ describe("validating data", () => { // String ends in ellipsis (...) expect(result).toMatch(/.*\.\.\.$/); }); + + describe("handling of `paidViaInviteToPay` key", () => { + it("sets value to true based on input", () => { + const formatted = formatGovPayMetadata({ + ...mockParams, + paidViaInviteToPay: true, + }); + + expect(formatted).toMatchObject({ + paidViaInviteToPay: true, + }); + }); + + it("sets value to false based on input", () => { + const formatted = formatGovPayMetadata({ + ...mockParams, + paidViaInviteToPay: false, + }); + + expect(formatted).toMatchObject({ + paidViaInviteToPay: false, + }); + }); + }); }); diff --git a/src/utils/govPayMetadata.ts b/src/utils/govPayMetadata.ts index 9a80e051..bc7713c9 100644 --- a/src/utils/govPayMetadata.ts +++ b/src/utils/govPayMetadata.ts @@ -9,6 +9,8 @@ import { type FormattedMetadata = NonNullable; +const ITP_KEY = "paidViaInviteToPay" as const; + const isPassportValue = (value: GovPayMetadataValue) => typeof value === "string" && value.startsWith("@"); @@ -16,13 +18,21 @@ const isPassportValue = (value: GovPayMetadataValue) => * Convert GovPayMetadata set in Editor to format accepted by GovPay API * Read dynamic data variables from passport and inject into output */ -const parseMetadata = ( - metadata: GovPayMetadata[], - passport: Passport, -): FormattedMetadata => { +const parseMetadata = ({ + metadata, + passport, + paidViaInviteToPay, +}: { + metadata: GovPayMetadata[]; + passport: Passport; + paidViaInviteToPay: boolean; +}): FormattedMetadata => { let entries: [string, GovPayMetadataValue][] = []; entries = metadata.map(({ key, value }) => { + // ITP data is set at runtime by user journey, and not read from passport directly + if (key === ITP_KEY) return [ITP_KEY, paidViaInviteToPay]; + if (!isPassportValue(value)) return [key, value]; // Remove "@" prefix @@ -35,6 +45,7 @@ const parseMetadata = ( }); const parsedMetadata = Object.fromEntries(entries); + return parsedMetadata; }; @@ -81,12 +92,21 @@ const validateMetadata = ( * - Static values (e.g. { vat_code: "abc123" }) * - Dynamic values (e.g. { property_type: "@project.propertyType" }) */ -export const formatGovPayMetadata = ( - metadata: GovPayMetadata[], - userPassport: IPassport, -): FormattedMetadata => { +export const formatGovPayMetadata = ({ + metadata, + userPassport, + paidViaInviteToPay, +}: { + metadata: GovPayMetadata[]; + userPassport: IPassport; + paidViaInviteToPay: boolean; +}): FormattedMetadata => { const passport = new Passport(userPassport); - const parsedAndValidated = parseMetadata(metadata, passport); + const parsedAndValidated = parseMetadata({ + metadata, + passport, + paidViaInviteToPay, + }); return parsedAndValidated; };