-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add button that opens checkout or billing portal
- Loading branch information
1 parent
dcfb9a4
commit a2fc6c3
Showing
11 changed files
with
176 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
{ | ||
"SUPABASE_URL": "http://localhost:54321", | ||
"SUPABASE_ANON_KEY": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0" | ||
"SUPABASE_ANON_KEY": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0", | ||
"STRIPE_PREMIUM_PRICE": "price_1OovJ2FttF99a1NC1hbOKGdg", | ||
"STRIPE_BASIC_PRICE": "price_1OovHjFttF99a1NCrlGTo2ue" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
.branches | ||
.temp | ||
.env | ||
.env.local |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { User } from "supabase"; | ||
import { supabase } from "./supabase.ts"; | ||
|
||
async function getUserFromRequest(req: Request) { | ||
const token = req.headers.get("Authorization")!.replace("Bearer ", ""); | ||
const { data, error } = await supabase.auth.getUser(token); | ||
if (error) throw error; | ||
const user = data?.user; | ||
return user; | ||
} | ||
|
||
// handles cors and returns the user if function called from client | ||
export const corsHeaders = { | ||
"Access-Control-Allow-Origin": "*", | ||
"Access-Control-Allow-Headers": | ||
"authorization, x-client-info, apikey, content-type", | ||
}; | ||
|
||
export function resFromError(e: { message?: string }): Response { | ||
return new Response( | ||
JSON.stringify({ message: e?.message }), | ||
{ | ||
status: 400, | ||
headers: { "Content-Type": "application/json", ...corsHeaders }, | ||
}, | ||
); | ||
} | ||
|
||
export function clientRequestHandler( | ||
handler: (req: Request, user: User) => Promise<Response> | Response, | ||
) { | ||
const enhancedHandler = async (req: Request) => { | ||
// Handle CORS preflight requests | ||
if (req.method === "OPTIONS") { | ||
return new Response("ok", { headers: corsHeaders }); | ||
} | ||
|
||
try { | ||
const user = await getUserFromRequest(req); | ||
if (!user) throw Error("Authentication Error"); | ||
return await handler(req, user); | ||
} catch (e) { | ||
console.error(e); | ||
return resFromError(e); | ||
} | ||
}; | ||
Deno.serve(enhancedHandler); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import Stripe from "stripe"; | ||
|
||
export const stripe = Stripe(Deno.env.get("STRIPE_SK")!, { | ||
httpClient: Stripe.createFetchHttpClient(), | ||
apiVersion: "2022-08-01", | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { createClient } from "supabase"; | ||
|
||
export const supabase = createClient( | ||
Deno.env.get("SUPABASE_URL")!, | ||
Deno.env.get("SUPABASE_SERVICE_ROLE_KEY")!, | ||
{ | ||
auth: { | ||
persistSession: false, | ||
}, | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { clientRequestHandler, corsHeaders } from "../_shared/request.ts"; | ||
import { supabase } from "../_shared/supabase.ts"; | ||
import { stripe } from "../_shared/stripe.ts"; | ||
|
||
clientRequestHandler(async (req, user) => { | ||
const { price, return_url } = await req.json(); | ||
// get stripe information from user_metadata table! | ||
const { data: metadata } = await supabase.from("user_metadata").select( | ||
"stripe_customer_id,tier", | ||
).eq( | ||
"user_id", | ||
user.id, | ||
).maybeSingle(); | ||
let stripeCustomerId = metadata?.stripe_customer_id; | ||
const tier = metadata?.tier; | ||
if (!stripeCustomerId) { | ||
// create stripe customer if doesn't exist | ||
console.log("create stripe customer"); | ||
const customer = await stripe.customers.create({ | ||
phone: user.phone, | ||
email: user.email, | ||
metadata: { | ||
uid: user.id, | ||
}, | ||
}); | ||
stripeCustomerId = customer.id; | ||
await supabase.from("user_metadata").upsert({ | ||
user_id: user.id, | ||
stripe_customer_id: customer.id, | ||
}); | ||
} | ||
let redirect_url: string | undefined; | ||
if (tier) { | ||
// open billing portal | ||
console.log("open billing portal session"); | ||
const session = await stripe.billingPortal.sessions.create({ | ||
customer: stripeCustomerId, | ||
return_url, | ||
}); | ||
redirect_url = session?.url; | ||
} else { | ||
// if tier is null go to checkout page | ||
console.log("open checkout session"); | ||
const session = await stripe.checkout.sessions.create({ | ||
customer: stripeCustomerId, | ||
mode: "payment", | ||
line_items: [{ | ||
price, | ||
quantity: 1, | ||
}], | ||
allow_promotion_codes: true, | ||
success_url: return_url, | ||
cancel_url: return_url, | ||
}); | ||
redirect_url = session?.url; | ||
} | ||
return new Response( | ||
JSON.stringify({ redirect_url }), | ||
{ | ||
headers: { | ||
...corsHeaders, | ||
"Content-Type": "application/json", | ||
}, | ||
}, | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"imports": { | ||
"supabase": "https://esm.sh/@supabase/supabase-js@2.39.7", | ||
"stripe": "https://esm.sh/stripe@10.12.0?target=deno" | ||
} | ||
} |