Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding the CORS headers to the billing functions #1233

Merged
merged 4 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
13 changes: 7 additions & 6 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 @@ -14,13 +14,14 @@ serve(async (req) => {
let res: ConstructorParameters<typeof Response> = [null, {}];
try {
if (req.method === "OPTIONS") {
res = ["ok", { status: 200 }];
res = ["ok", { headers: billingResponseHeaders, status: 200 }];
} else {
const request = await req.json();

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 || billingResponseHeaders } };

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 };