Skip to content

Commit

Permalink
♻️ (donate) externalize constants
Browse files Browse the repository at this point in the history
  • Loading branch information
mlbrgl committed Dec 27, 2023
1 parent 7349d43 commit 9f03b89
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 21 deletions.
3 changes: 2 additions & 1 deletion functions/donation/_utils/checkout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
getErrorMessageDonation,
JsonError,
} from "@ourworldindata/utils"
import { STRIPE_API_VERSION } from "./constants.js"

function getPaymentMethodTypes(
donation: DonationRequest
Expand All @@ -27,7 +28,7 @@ export async function createCheckoutSession(
key: string
) {
const stripe = new Stripe(key, {
apiVersion: "2023-10-16",
apiVersion: STRIPE_API_VERSION,
maxNetworkRetries: 2,
})

Expand Down
18 changes: 18 additions & 0 deletions functions/donation/_utils/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export const CORS_HEADERS = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "POST, OPTIONS",
// The Content-Type header is required to allow requests to be sent with a
// Content-Type of "application/json". This is because "application/json" is
// not an allowed value for Content-Type to be considered a CORS-safelisted
// header.
// - https://developer.mozilla.org/en-US/docs/Glossary/CORS-safelisted_request_header
"Access-Control-Allow-Headers": "Content-Type",
}

export const DEFAULT_HEADERS = {
...CORS_HEADERS,
"Content-Type": "application/json",
} // CORS headers need to be sent in responses to both preflight ("OPTIONS") and
// actual requests.

export const STRIPE_API_VERSION = "2023-10-16"
19 changes: 1 addition & 18 deletions functions/donation/donate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
stringifyUnknownError,
} from "@ourworldindata/utils"
import { Value } from "@sinclair/typebox/value"
import { DEFAULT_HEADERS, CORS_HEADERS } from "./_utils/constants.js"

interface DonateEnvVars {
ASSETS: Fetcher
Expand All @@ -19,24 +20,6 @@ const hasDonateEnvVars = (env: any): env is DonateEnvVars => {
return !!env.ASSETS && !!env.STRIPE_SECRET_KEY && !!env.RECAPTCHA_SECRET_KEY
}

// CORS headers need to be sent in responses to both preflight ("OPTIONS") and
// actual requests.
const CORS_HEADERS = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "POST, OPTIONS",
// The Content-Type header is required to allow requests to be sent with a
// Content-Type of "application/json". This is because "application/json" is
// not an allowed value for Content-Type to be considered a CORS-safelisted
// header.
// - https://developer.mozilla.org/en-US/docs/Glossary/CORS-safelisted_request_header
"Access-Control-Allow-Headers": "Content-Type",
}

export const DEFAULT_HEADERS = {
...CORS_HEADERS,
"Content-Type": "application/json",
}

// This function is called when the request is a preflight request ("OPTIONS").
export const onRequestOptions: PagesFunction = async () => {
return new Response(null, {
Expand Down
4 changes: 2 additions & 2 deletions functions/donation/thank-you.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Stripe from "stripe"
import { DEFAULT_HEADERS } from "./donate.js"
import { DEFAULT_HEADERS, STRIPE_API_VERSION } from "./_utils/constants.js"
import { JsonError, stringifyUnknownError } from "@ourworldindata/utils"
import { MailgunEnvVars, sendMail } from "./_utils/email.js"

Expand Down Expand Up @@ -83,7 +83,7 @@ export const onRequestPost: PagesFunction = async ({
)

const stripe = new Stripe(env.STRIPE_SECRET_KEY, {
apiVersion: "2023-10-16",
apiVersion: STRIPE_API_VERSION,
maxNetworkRetries: 2,
})

Expand Down

0 comments on commit 9f03b89

Please sign in to comment.