From 6f98bb4df793d783e5b45310bcfad0c1428f99af Mon Sep 17 00:00:00 2001 From: Travis Jenkins Date: Wed, 11 Oct 2023 14:06:31 -0400 Subject: [PATCH] Adding the CORS headers to the billing functions --- .../functions/billing/delete_tenant_payment_method.ts | 4 ++-- supabase/functions/billing/get_tenant_invoice_data.ts | 6 +++--- .../functions/billing/get_tenant_payment_methods.ts | 6 +++--- supabase/functions/billing/index.ts | 11 ++++++----- .../billing/set_tenant_primary_payment_method.ts | 6 +++--- supabase/functions/billing/setup_intent.ts | 6 +++--- supabase/functions/billing/shared.ts | 3 +++ 7 files changed, 23 insertions(+), 19 deletions(-) diff --git a/supabase/functions/billing/delete_tenant_payment_method.ts b/supabase/functions/billing/delete_tenant_payment_method.ts index ebe419f0d5..2327b98a6a 100644 --- a/supabase/functions/billing/delete_tenant_payment_method.ts +++ b/supabase/functions/billing/delete_tenant_payment_method.ts @@ -1,4 +1,4 @@ -import { customerQuery, StripeClient } from "./shared.ts"; +import { billingResponseHeaders, customerQuery, StripeClient } from "./shared.ts"; export interface DeleteTenantPaymentMethodsParams { tenant: string; @@ -20,7 +20,7 @@ export async function deleteTenantPaymentMethod( } } return [JSON.stringify({ status: "ok" }), { - headers: { "Content-Type": "application/json" }, + headers: billingResponseHeaders, status: 200, }]; } diff --git a/supabase/functions/billing/get_tenant_invoice_data.ts b/supabase/functions/billing/get_tenant_invoice_data.ts index f636150d2f..991f267759 100644 --- a/supabase/functions/billing/get_tenant_invoice_data.ts +++ b/supabase/functions/billing/get_tenant_invoice_data.ts @@ -1,4 +1,4 @@ -import { customerQuery, StripeClient } from "./shared.ts"; +import { billingResponseHeaders, customerQuery, StripeClient } from "./shared.ts"; export interface getTenantPaymentMethodsParams { tenant: string; @@ -46,14 +46,14 @@ export async function getTenantInvoice( }; return [JSON.stringify({ invoice: limited_invoice }), { - headers: { "Content-Type": "application/json" }, + headers: billingResponseHeaders, status: 200, }]; } } return [JSON.stringify({ invoice: null }), { - headers: { "Content-Type": "application/json" }, + headers: billingResponseHeaders, status: 200, }]; } diff --git a/supabase/functions/billing/get_tenant_payment_methods.ts b/supabase/functions/billing/get_tenant_payment_methods.ts index 065c611716..ff60300c04 100644 --- a/supabase/functions/billing/get_tenant_payment_methods.ts +++ b/supabase/functions/billing/get_tenant_payment_methods.ts @@ -1,4 +1,4 @@ -import { customerQuery, StripeClient } from "./shared.ts"; +import { billingResponseHeaders, customerQuery, StripeClient } from "./shared.ts"; export interface getTenantPaymentMethodsParams { tenant: string; @@ -17,7 +17,7 @@ export async function getTenantPaymentMethods( primary: customer.invoice_settings.default_payment_method, tenant: req_body.tenant }), { - headers: { "Content-Type": "application/json" }, + headers: billingResponseHeaders, status: 200, }]; } else { @@ -26,7 +26,7 @@ export async function getTenantPaymentMethods( primary: null, tenant: req_body.tenant }), { - headers: { "Content-Type": "application/json" }, + headers: billingResponseHeaders, status: 200, }]; } diff --git a/supabase/functions/billing/index.ts b/supabase/functions/billing/index.ts index 90b07907f0..82bf8f1e09 100644 --- a/supabase/functions/billing/index.ts +++ b/supabase/functions/billing/index.ts @@ -1,5 +1,5 @@ import { serve } from "https://deno.land/std@0.184.0/http/server.ts"; -import { corsHeaders } from "../_shared/cors.ts"; +import { billingResponseHeaders } from "./shared.ts"; import { setupIntent } from "./setup_intent.ts"; import { getTenantPaymentMethods } from "./get_tenant_payment_methods.ts"; import { deleteTenantPaymentMethod } from "./delete_tenant_payment_method.ts"; @@ -21,6 +21,7 @@ serve(async (req) => { const requested_tenant = request.tenant; // Create a Supabase client with the Auth context of the logged in user. // This is required in order to get the user's name and email address + const supabaseClient = createClient( Deno.env.get("SUPABASE_URL") ?? "", Deno.env.get("SUPABASE_ANON_KEY") ?? "", @@ -43,7 +44,7 @@ serve(async (req) => { if (!(grants.data ?? []).find((grant) => grant.object_role === requested_tenant)) { res = [JSON.stringify({ error: `Not authorized to requested grant` }), { - headers: { "Content-Type": "application/json" }, + headers: billingResponseHeaders, status: 401, }]; } else { @@ -59,7 +60,7 @@ serve(async (req) => { res = await getTenantInvoice(request, req); } else { res = [JSON.stringify({ error: "unknown_operation" }), { - headers: { "Content-Type": "application/json" }, + headers: billingResponseHeaders, status: 400, }]; } @@ -67,12 +68,12 @@ serve(async (req) => { } } catch (e) { res = [JSON.stringify({ error: e.message }), { - headers: { "Content-Type": "application/json" }, + headers: billingResponseHeaders, status: 400, }]; } - res[1] = { ...res[1], headers: { ...res[1]?.headers || {}, ...corsHeaders } }; + res[1] = { ...res[1], headers: { ...res[1]?.headers || {} } }; return new Response(...res); }); diff --git a/supabase/functions/billing/set_tenant_primary_payment_method.ts b/supabase/functions/billing/set_tenant_primary_payment_method.ts index 5546dc19af..dcead65443 100644 --- a/supabase/functions/billing/set_tenant_primary_payment_method.ts +++ b/supabase/functions/billing/set_tenant_primary_payment_method.ts @@ -1,4 +1,4 @@ -import { customerQuery, StripeClient } from "./shared.ts"; +import { billingResponseHeaders, customerQuery, StripeClient } from "./shared.ts"; export interface SetTenantPrimaryPaymentMethodParams { tenant: string; @@ -14,12 +14,12 @@ export async function setTenantPrimaryPaymentMethod( await StripeClient.customers.update(customer.id, { invoice_settings: { default_payment_method: req_body.id } }); return [JSON.stringify({ status: "ok" }), { - headers: { "Content-Type": "application/json" }, + headers: billingResponseHeaders, status: 200, }]; } else { return [JSON.stringify({ payment_methods: [], primary: null }), { - headers: { "Content-Type": "application/json" }, + headers: billingResponseHeaders, status: 200, }]; } diff --git a/supabase/functions/billing/setup_intent.ts b/supabase/functions/billing/setup_intent.ts index 04f0349090..5557bb4f25 100644 --- a/supabase/functions/billing/setup_intent.ts +++ b/supabase/functions/billing/setup_intent.ts @@ -1,5 +1,5 @@ import { SupabaseClient, User } from "https://esm.sh/@supabase/supabase-js@2.0.5"; -import { customerQuery, StripeClient, TENANT_METADATA_KEY } from "./shared.ts"; +import { billingResponseHeaders, customerQuery, StripeClient, TENANT_METADATA_KEY } from "./shared.ts"; async function findOrCreateCustomer(tenant: string, user: User) { if (!user.email) { @@ -50,7 +50,7 @@ export async function setupIntent( if (!user) { return [JSON.stringify({ error: "User not found" }), { - headers: { "Content-Type": "application/json" }, + headers: billingResponseHeaders, status: 400, }]; } @@ -65,7 +65,7 @@ export async function setupIntent( }); return [JSON.stringify({ intent_secret: intent.client_secret }), { - headers: { "Content-Type": "application/json" }, + headers: billingResponseHeaders, status: 200, }]; } diff --git a/supabase/functions/billing/shared.ts b/supabase/functions/billing/shared.ts index 1cd3ae3222..36e1bddab3 100644 --- a/supabase/functions/billing/shared.ts +++ b/supabase/functions/billing/shared.ts @@ -1,4 +1,5 @@ import Stripe from "stripe"; +import { corsHeaders } from '../_shared/cors.ts'; const STRIPE_API = Deno.env.get("STRIPE_API_KEY"); if (!STRIPE_API) { @@ -9,3 +10,5 @@ export const StripeClient = new Stripe(STRIPE_API, { apiVersion: "2022-11-15" }) export const TENANT_METADATA_KEY = "estuary.dev/tenant_name"; export const customerQuery = (tenant: string) => `metadata["${TENANT_METADATA_KEY}"]:"${tenant}"`; + +export const billingResponseHeaders = { "Content-Type": "application/json", ...corsHeaders }; \ No newline at end of file