-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ac93784
commit 88d7798
Showing
30 changed files
with
461 additions
and
243 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,14 +1,8 @@ | ||
VITE_PROD_ORIGIN=http://localhost:3000 | ||
DATABASE_URL='mysql://todo:[email protected]/todo?ssl={"rejectUnauthorized":true}' | ||
MANAGE_URL=https://mdm.example.com | ||
|
||
# Rust | ||
INTERNAL_SECRET=areallylongsecretthatyoushouldreplace | ||
MDM_URL=https://mdm.example.com | ||
ENTERPRISE_ENROLLMENT_URL=https://enterpriseenrollment.example.com | ||
|
||
# Billing | ||
STRIPE_PUBLISHABLE_KEY= | ||
STRIPE_SECRET_KEY= | ||
|
||
# Syncing users | ||
ENTRA_CLIENT_ID= | ||
|
@@ -19,5 +13,5 @@ FROM_ADDRESS=console | |
# AWS_ACCESS_KEY_ID= | ||
# AWS_SECRET_ACCESS_KEY= | ||
|
||
# Used for landing only | ||
# For `apps/landing` | ||
VITE_MATTRAX_CLOUD_ORIGIN=http://localhost:3000 # Should point to `apps/web` |
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
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,19 +1,19 @@ | ||
import { env } from "~/env"; | ||
// import { env } from "~/env"; | ||
|
||
export async function useStripe() { | ||
const secret = env.STRIPE_SECRET_KEY; | ||
if (!secret) throw new Error("Missing 'STRIPE_SECRET_KEY'"); | ||
// export async function useStripe() { | ||
// const secret = env.STRIPE_SECRET_KEY; | ||
// if (!secret) throw new Error("Missing 'STRIPE_SECRET_KEY'"); | ||
|
||
return import("stripe").then((mod) => { | ||
const Stripe = mod.default; | ||
// return import("stripe").then((mod) => { | ||
// const Stripe = mod.default; | ||
|
||
return Object.assign( | ||
new Stripe(secret, { | ||
apiVersion: "2024-06-20", | ||
timeout: 1500, | ||
httpClient: Stripe.createFetchHttpClient(), | ||
}), | ||
{ secret }, | ||
); | ||
}); | ||
} | ||
// return Object.assign( | ||
// new Stripe(secret, { | ||
// apiVersion: "2024-06-20", | ||
// timeout: 1500, | ||
// httpClient: Stripe.createFetchHttpClient(), | ||
// }), | ||
// { secret }, | ||
// ); | ||
// }); | ||
// } |
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 |
---|---|---|
@@ -1,77 +1,69 @@ | ||
import { TRPCError } from "@trpc/server"; | ||
import { eq } from "drizzle-orm"; | ||
import type Stripe from "stripe"; | ||
import { useStripe } from "~/api/stripe"; | ||
// import { useStripe } from "~/api/stripe"; | ||
import { organisations } from "~/db"; | ||
import { env } from "~/env"; | ||
import { createTRPCRouter, orgProcedure } from "../../helpers"; | ||
|
||
export const billingRouter = createTRPCRouter({ | ||
portalUrl: orgProcedure.mutation(async ({ ctx }) => { | ||
const [org] = await ctx.db | ||
.select({ | ||
name: organisations.name, | ||
billingEmail: organisations.billingEmail, | ||
stripeCustomerId: organisations.stripeCustomerId, | ||
}) | ||
.from(organisations) | ||
.where(eq(organisations.pk, ctx.org.pk)); | ||
if (!org) | ||
throw new TRPCError({ code: "NOT_FOUND", message: "organisation" }); // TODO: Proper error code which the frontend knows how to handle | ||
|
||
let customerId: string; | ||
const stripe = await useStripe(); | ||
|
||
if (!org.stripeCustomerId) { | ||
try { | ||
const customer = await stripe.customers.create({ | ||
name: org.name, | ||
email: org.billingEmail || undefined, | ||
}); | ||
|
||
await ctx.db | ||
.update(organisations) | ||
.set({ stripeCustomerId: customer.id }) | ||
.where(eq(organisations.pk, ctx.org.pk)); | ||
|
||
customerId = customer.id; | ||
} catch (err) { | ||
console.error("Error creating customer", err); | ||
throw new Error("Error creating customer"); | ||
} | ||
} else { | ||
customerId = org.stripeCustomerId; | ||
} | ||
|
||
// TODO: When using the official Stripe SDK, this endpoint causes the entire Edge Function to hang and i'm at a loss to why. | ||
// TODO: This will do for now but we should try and fix it. | ||
|
||
const body = new URLSearchParams({ | ||
customer: customerId, | ||
return_url: `${env.VITE_PROD_ORIGIN}/o/${ctx.org.slug}/settings`, | ||
}); | ||
|
||
const resp = await fetch( | ||
"https://api.stripe.com/v1/billing_portal/sessions", | ||
{ | ||
method: "POST", | ||
headers: { | ||
Authorization: `Bearer ${stripe.secret}`, | ||
"Content-Type": "application/x-www-form-urlencoded", | ||
}, | ||
body: body.toString(), | ||
}, | ||
); | ||
if (!resp.ok) { | ||
const body = await resp.text(); | ||
console.error("Error creating billing portal session", resp.status, body); | ||
throw new Error( | ||
`Error creating billing portal session: '${resp.status}' '${body}'`, | ||
); | ||
} | ||
const session: Stripe.Response<Stripe.BillingPortal.Session> = | ||
await resp.json(); | ||
|
||
return session.url; | ||
}), | ||
// portalUrl: orgProcedure.mutation(async ({ ctx }) => { | ||
// const [org] = await ctx.db | ||
// .select({ | ||
// name: organisations.name, | ||
// billingEmail: organisations.billingEmail, | ||
// stripeCustomerId: organisations.stripeCustomerId, | ||
// }) | ||
// .from(organisations) | ||
// .where(eq(organisations.pk, ctx.org.pk)); | ||
// if (!org) | ||
// throw new TRPCError({ code: "NOT_FOUND", message: "organisation" }); // TODO: Proper error code which the frontend knows how to handle | ||
// let customerId: string; | ||
// const stripe = await useStripe(); | ||
// if (!org.stripeCustomerId) { | ||
// try { | ||
// const customer = await stripe.customers.create({ | ||
// name: org.name, | ||
// email: org.billingEmail || undefined, | ||
// }); | ||
// await ctx.db | ||
// .update(organisations) | ||
// .set({ stripeCustomerId: customer.id }) | ||
// .where(eq(organisations.pk, ctx.org.pk)); | ||
// customerId = customer.id; | ||
// } catch (err) { | ||
// console.error("Error creating customer", err); | ||
// throw new Error("Error creating customer"); | ||
// } | ||
// } else { | ||
// customerId = org.stripeCustomerId; | ||
// } | ||
// // TODO: When using the official Stripe SDK, this endpoint causes the entire Edge Function to hang and i'm at a loss to why. | ||
// // TODO: This will do for now but we should try and fix it. | ||
// const body = new URLSearchParams({ | ||
// customer: customerId, | ||
// return_url: `${env.VITE_PROD_ORIGIN}/o/${ctx.org.slug}/settings`, | ||
// }); | ||
// const resp = await fetch( | ||
// "https://api.stripe.com/v1/billing_portal/sessions", | ||
// { | ||
// method: "POST", | ||
// headers: { | ||
// Authorization: `Bearer ${stripe.secret}`, | ||
// "Content-Type": "application/x-www-form-urlencoded", | ||
// }, | ||
// body: body.toString(), | ||
// }, | ||
// ); | ||
// if (!resp.ok) { | ||
// const body = await resp.text(); | ||
// console.error("Error creating billing portal session", resp.status, body); | ||
// throw new Error( | ||
// `Error creating billing portal session: '${resp.status}' '${body}'`, | ||
// ); | ||
// } | ||
// const session: Stripe.Response<Stripe.BillingPortal.Session> = | ||
// await resp.json(); | ||
// return session.url; | ||
// }), | ||
}); |
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
Oops, something went wrong.