Skip to content

Commit

Permalink
fix: Mute Stripe test notifications (#2894)
Browse files Browse the repository at this point in the history
  • Loading branch information
DafyddLlyr authored Mar 15, 2024
1 parent 865691e commit 04d3d22
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 13 deletions.
42 changes: 42 additions & 0 deletions api.planx.uk/modules/pay/service/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { GovUKPayment } from "@opensystemslab/planx-core/types";
import { isTestPayment } from "./utils";

describe("isTestPayment() helper function", () => {
const OLD_ENV = process.env;

beforeEach(() => {
process.env = { ...OLD_ENV };
});

afterAll(() => {
process.env = OLD_ENV;
});

test("sandbox payments", () => {
const result = isTestPayment({
payment_provider: "sandbox",
} as GovUKPayment);
expect(result).toBe(true);
});

test("other payment providers", () => {
const result = isTestPayment({ payment_provider: "Visa" } as GovUKPayment);
expect(result).toBe(false);
});

test("stripe payments (staging)", () => {
process.env.APP_ENVIRONMENT = "staging";
const result = isTestPayment({
payment_provider: "stripe",
} as GovUKPayment);
expect(result).toBe(true);
});

test("stripe payments (production)", () => {
process.env.APP_ENVIRONMENT = "production";
const result = isTestPayment({
payment_provider: "stripe",
} as GovUKPayment);
expect(result).toBe(false);
});
});
46 changes: 33 additions & 13 deletions api.planx.uk/modules/pay/service/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,42 @@ export const addGovPayPaymentIdToPaymentRequest = async (
}
};

/**
* Identify if a payment is using dummy card details for testing
* Docs: https://docs.payments.service.gov.uk/testing_govuk_pay/#mock-card-numbers-and-email-addresses
*/
export const isTestPayment = ({
payment_provider: paymentProvider,
}: GovUKPayment) => {
// Payment using "sandbox" account
const isSandbox = paymentProvider === "sandbox";
const isProduction = process.env.APP_ENVIRONMENT === "production";

// Payment using Stripe in non-production environment
// Stripe test accounts do not have a specific test code
const isStripeTest = paymentProvider === "stripe" && !isProduction;

return isSandbox || isStripeTest;
};

/**
* Notify #planx-notifications so we can monitor for subsequent submissions
*/
export async function postPaymentNotificationToSlack(
req: Request,
govUkResponse: GovUKPayment,
label = "",
) {
// if it's a prod payment, notify #planx-notifications so we can monitor for subsequent submissions
if (govUkResponse?.payment_provider !== "sandbox") {
const slack = SlackNotify(process.env.SLACK_WEBHOOK_URL!);
const getStatus = (state: GovUKPayment["state"]) =>
state.status + (state.message ? ` (${state.message})` : "");
const payMessage = `:coin: New GOV Pay payment ${label} *${
govUkResponse.payment_id
}* with status *${getStatus(govUkResponse.state)}* [${
req.params.localAuthority
}]`;
await slack.send(payMessage);
console.log("Payment notification posted to Slack");
}
if (isTestPayment(govUkResponse)) return;

const slack = SlackNotify(process.env.SLACK_WEBHOOK_URL!);
const getStatus = (state: GovUKPayment["state"]) =>
state.status + (state.message ? ` (${state.message})` : "");
const payMessage = `:coin: New GOV Pay payment ${label} *${
govUkResponse.payment_id
}* with status *${getStatus(govUkResponse.state)}* [${
req.params.localAuthority
}]`;
await slack.send(payMessage);
console.log("Payment notification posted to Slack");
}

0 comments on commit 04d3d22

Please sign in to comment.