Skip to content

Commit

Permalink
Adding the CORS headers to the billing functions
Browse files Browse the repository at this point in the history
  • Loading branch information
travjenkins committed Oct 11, 2023
1 parent cf5f62a commit 6f98bb4
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 19 deletions.
4 changes: 2 additions & 2 deletions supabase/functions/billing/delete_tenant_payment_method.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { customerQuery, StripeClient } from "./shared.ts";
import { billingResponseHeaders, customerQuery, StripeClient } from "./shared.ts";

export interface DeleteTenantPaymentMethodsParams {
tenant: string;
Expand All @@ -20,7 +20,7 @@ export async function deleteTenantPaymentMethod(
}
}
return [JSON.stringify({ status: "ok" }), {
headers: { "Content-Type": "application/json" },
headers: billingResponseHeaders,
status: 200,
}];
}
6 changes: 3 additions & 3 deletions supabase/functions/billing/get_tenant_invoice_data.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { customerQuery, StripeClient } from "./shared.ts";
import { billingResponseHeaders, customerQuery, StripeClient } from "./shared.ts";

export interface getTenantPaymentMethodsParams {
tenant: string;
Expand Down Expand Up @@ -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,
}];
}
6 changes: 3 additions & 3 deletions supabase/functions/billing/get_tenant_payment_methods.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { customerQuery, StripeClient } from "./shared.ts";
import { billingResponseHeaders, customerQuery, StripeClient } from "./shared.ts";

export interface getTenantPaymentMethodsParams {
tenant: string;
Expand All @@ -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 {
Expand All @@ -26,7 +26,7 @@ export async function getTenantPaymentMethods(
primary: null,
tenant: req_body.tenant
}), {
headers: { "Content-Type": "application/json" },
headers: billingResponseHeaders,
status: 200,
}];
}
Expand Down
11 changes: 6 additions & 5 deletions supabase/functions/billing/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { serve } from "https://deno.land/[email protected]/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";
Expand All @@ -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") ?? "",
Expand All @@ -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 {
Expand All @@ -59,20 +60,20 @@ serve(async (req) => {
res = await getTenantInvoice(request, req);
} else {
res = [JSON.stringify({ error: "unknown_operation" }), {
headers: { "Content-Type": "application/json" },
headers: billingResponseHeaders,
status: 400,
}];
}
}
}
} 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);
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { customerQuery, StripeClient } from "./shared.ts";
import { billingResponseHeaders, customerQuery, StripeClient } from "./shared.ts";

export interface SetTenantPrimaryPaymentMethodParams {
tenant: string;
Expand All @@ -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,
}];
}
Expand Down
6 changes: 3 additions & 3 deletions supabase/functions/billing/setup_intent.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SupabaseClient, User } from "https://esm.sh/@supabase/[email protected]";
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) {
Expand Down Expand Up @@ -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,
}];
}
Expand All @@ -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,
}];
}
3 changes: 3 additions & 0 deletions supabase/functions/billing/shared.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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 };

0 comments on commit 6f98bb4

Please sign in to comment.