Skip to content

Commit

Permalink
fix: Set up defaults for non-updated Pay components
Browse files Browse the repository at this point in the history
  • Loading branch information
DafyddLlyr committed Mar 26, 2024
1 parent 6e4db83 commit ab99e19
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
15 changes: 14 additions & 1 deletion api.planx.uk/modules/pay/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,25 @@ export async function buildPaymentPayload(
]),
);

const defaultGovPayMetadata = {
source: "PlanX",
// Payment requests have /pay path suffix, so get flow-slug from second-to-last position
flow:
(req.query.returnURL as string)
.split("?")?.[0]
?.split("/")
?.slice(-2, -1)?.[0] || "Could not parse service name",
inviteToPay: true,
};

const metadata = govPayMetadata.length ? govPayMetadata : defaultGovPayMetadata;

const createPaymentBody: GovPayCreatePayment = {
amount: parseInt(req.params.paymentAmount),
reference: req.query.sessionId as string,
description: "New application (nominated payee)",
return_url: req.query.returnURL as string,
metadata: govPayMetadata,
metadata,
};

req.body = createPaymentBody;
Expand Down
12 changes: 11 additions & 1 deletion editor.planx.uk/src/@planx/components/Pay/Public/Pay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ function Component(props: Props) {
passport,
environment,
teamSlug,
flowSlug,
] = useStore((state) => [
state.id,
state.sessionId,
Expand All @@ -65,8 +66,17 @@ function Component(props: Props) {
state.computePassport(),
state.previewEnvironment,
state.teamSlug,
state.flowSlug,
]);
const fee = props.fn ? Number(passport.data?.[props.fn]) : 0;
const defaultMetadata = [
{ key: "source", value: "PlanX" },
{ key: "flow", value: flowSlug },
{ key: "isInviteToPay", value: false },
];
const metadata = props.govPayMetadata.length
? props.govPayMetadata
: defaultMetadata;

// Handles UI states
const reducer = (_state: ComponentState, action: Action): ComponentState => {
Expand Down Expand Up @@ -236,7 +246,7 @@ function Component(props: Props) {
await axios
.post(
getGovUkPayUrlForTeam({ sessionId, flowId, teamSlug }),
createPayload(fee, sessionId, props.govPayMetadata),
createPayload(fee, sessionId, metadata),
)
.then(async (res) => {
const payment = await resolvePaymentResponse(res.data);
Expand Down

0 comments on commit ab99e19

Please sign in to comment.