Browse Source

wip: black

Frank 1 month ago
parent
commit
82319bbd83
1 changed files with 30 additions and 14 deletions
  1. 30 14
      packages/console/app/src/routes/black/subscribe/[plan].tsx

+ 30 - 14
packages/console/app/src/routes/black/subscribe/[plan].tsx

@@ -1,4 +1,4 @@
-import { A, action, createAsync, json, query, redirect, useParams } from "@solidjs/router"
+import { A, createAsync, query, redirect, useParams } from "@solidjs/router"
 import { Title } from "@solidjs/meta"
 import { Title } from "@solidjs/meta"
 import { createEffect, createSignal, For, Match, Show, Switch } from "solid-js"
 import { createEffect, createSignal, For, Match, Show, Switch } from "solid-js"
 import { type Stripe, type PaymentMethod, loadStripe } from "@stripe/stripe-js"
 import { type Stripe, type PaymentMethod, loadStripe } from "@stripe/stripe-js"
@@ -35,6 +35,7 @@ const getWorkspaces = query(async () => {
             paymentMethodType: BillingTable.paymentMethodType,
             paymentMethodType: BillingTable.paymentMethodType,
             paymentMethodLast4: BillingTable.paymentMethodLast4,
             paymentMethodLast4: BillingTable.paymentMethodLast4,
             subscriptionID: BillingTable.subscriptionID,
             subscriptionID: BillingTable.subscriptionID,
+            timeSubscriptionBooked: BillingTable.timeSubscriptionBooked,
           },
           },
         })
         })
         .from(UserTable)
         .from(UserTable)
@@ -86,6 +87,14 @@ const createSetupIntent = async (input: { plan: string; workspaceID: string }) =
         },
         },
       })
       })
       customerID = customer.id
       customerID = customer.id
+      await Database.use((tx) =>
+        tx
+          .update(BillingTable)
+          .set({
+            customerID,
+          })
+          .where(eq(BillingTable.workspaceID, workspaceID)),
+      )
     }
     }
 
 
     const intent = await Billing.stripe().setupIntents.create({
     const intent = await Billing.stripe().setupIntents.create({
@@ -135,8 +144,7 @@ interface SuccessData {
 function Failure(props: { message: string }) {
 function Failure(props: { message: string }) {
   return (
   return (
     <div data-slot="failure">
     <div data-slot="failure">
-      <p data-slot="title">Uh oh, something went wrong</p>
-      <p data-slot="message">{props.message}</p>
+      <p data-slot="message">Uh oh! {props.message}</p>
     </div>
     </div>
   )
   )
 }
 }
@@ -197,7 +205,7 @@ function IntentForm(props: { plan: PlanID; workspaceID: string; onSuccess: (data
     const { error: confirmError, setupIntent } = await stripe()!.confirmSetup({
     const { error: confirmError, setupIntent } = await stripe()!.confirmSetup({
       elements: elements()!,
       elements: elements()!,
       confirmParams: {
       confirmParams: {
-        expand: ["setup_intent.payment_method"],
+        expand: ["payment_method"],
         payment_method_data: {
         payment_method_data: {
           allow_redisplay: "always",
           allow_redisplay: "always",
         },
         },
@@ -211,6 +219,8 @@ function IntentForm(props: { plan: PlanID; workspaceID: string; onSuccess: (data
       return
       return
     }
     }
 
 
+    // TODO
+    console.log(setupIntent)
     if (setupIntent?.status === "succeeded") {
     if (setupIntent?.status === "succeeded") {
       const pm = setupIntent.payment_method as PaymentMethod
       const pm = setupIntent.payment_method as PaymentMethod
 
 
@@ -274,7 +284,7 @@ export default function BlackSubscribe() {
   })
   })
 
 
   // Fetch setup intent when workspace is selected (unless workspace already has payment method)
   // Fetch setup intent when workspace is selected (unless workspace already has payment method)
-  createEffect(() => {
+  createEffect(async () => {
     const id = selectedWorkspace()
     const id = selectedWorkspace()
     if (!id) return
     if (!id) return
 
 
@@ -284,6 +294,15 @@ export default function BlackSubscribe() {
       return
       return
     }
     }
     if (ws?.billing?.paymentMethodID) {
     if (ws?.billing?.paymentMethodID) {
+      if (!ws?.billing?.timeSubscriptionBooked) {
+        await bookSubscription({
+          workspaceID: id,
+          plan: planData.id,
+          paymentMethodID: ws.billing.paymentMethodID!,
+          paymentMethodType: ws.billing.paymentMethodType!,
+          paymentMethodLast4: ws.billing.paymentMethodLast4 ?? undefined,
+        })
+      }
       setSuccess({
       setSuccess({
         plan: planData.id,
         plan: planData.id,
         paymentMethodType: ws.billing.paymentMethodType!,
         paymentMethodType: ws.billing.paymentMethodType!,
@@ -292,15 +311,12 @@ export default function BlackSubscribe() {
       return
       return
     }
     }
 
 
-    createSetupIntent({ plan, workspaceID: id })
-      .then((data) => {
-        if (data.error) {
-          setFailure(data.error)
-        } else if ("clientSecret" in data) {
-          setClientSecret(data.clientSecret)
-        }
-      })
-      .catch(() => setFailure("Failed to initialize payment"))
+    const result = await createSetupIntent({ plan, workspaceID: id })
+    if (result.error) {
+      setFailure(result.error)
+    } else if ("clientSecret" in result) {
+      setClientSecret(result.clientSecret)
+    }
   })
   })
 
 
   // Keyboard navigation for workspace picker
   // Keyboard navigation for workspace picker