|
| 1 | +import type { Stripe } from "stripe" |
1 | 2 | import { Billing } from "@opencode-ai/console-core/billing.js" |
2 | 3 | import type { APIEvent } from "@solidjs/start/server" |
3 | 4 | import { and, Database, eq, sql } from "@opencode-ai/console-core/drizzle/index.js" |
@@ -111,27 +112,17 @@ export async function POST(input: APIEvent) { |
111 | 112 | const customerID = body.data.object.customer as string |
112 | 113 | const invoiceID = body.data.object.latest_invoice as string |
113 | 114 | const subscriptionID = body.data.object.id as string |
| 115 | + const paymentMethodID = body.data.object.default_payment_method as string |
114 | 116 |
|
115 | 117 | if (!workspaceID) throw new Error("Workspace ID not found") |
116 | 118 | if (!userID) throw new Error("User ID not found") |
117 | 119 | if (!customerID) throw new Error("Customer ID not found") |
118 | 120 | if (!invoiceID) throw new Error("Invoice ID not found") |
119 | 121 | if (!subscriptionID) throw new Error("Subscription ID not found") |
120 | | - |
121 | | - // get payment id from invoice |
122 | | - const invoice = await Billing.stripe().invoices.retrieve(invoiceID, { |
123 | | - expand: ["payments"], |
124 | | - }) |
125 | | - const paymentID = invoice.payments?.data[0].payment.payment_intent as string |
126 | | - if (!paymentID) throw new Error("Payment ID not found") |
| 122 | + if (!paymentMethodID) throw new Error("Payment method ID not found") |
127 | 123 |
|
128 | 124 | // get payment method for the payment intent |
129 | | - const paymentIntent = await Billing.stripe().paymentIntents.retrieve(paymentID, { |
130 | | - expand: ["payment_method"], |
131 | | - }) |
132 | | - const paymentMethod = paymentIntent.payment_method |
133 | | - if (!paymentMethod || typeof paymentMethod === "string") throw new Error("Payment method not expanded") |
134 | | - |
| 125 | + const paymentMethod = await Billing.stripe().paymentMethods.retrieve(paymentMethodID) |
135 | 126 | await Actor.provide("system", { workspaceID }, async () => { |
136 | 127 | // look up current billing |
137 | 128 | const billing = await Billing.get() |
@@ -200,26 +191,18 @@ export async function POST(input: APIEvent) { |
200 | 191 | const amountInCents = body.data.object.amount_paid |
201 | 192 | const customerID = body.data.object.customer as string |
202 | 193 | const subscriptionID = body.data.object.parent?.subscription_details?.subscription as string |
| 194 | + const productID = body.data.object.lines?.data[0].pricing?.price_details?.product as string |
203 | 195 |
|
204 | 196 | if (!customerID) throw new Error("Customer ID not found") |
205 | 197 | if (!invoiceID) throw new Error("Invoice ID not found") |
206 | 198 | if (!subscriptionID) throw new Error("Subscription ID not found") |
207 | 199 |
|
208 | 200 | // get coupon id from subscription |
209 | | - const subscriptionData = await Billing.stripe().subscriptions.retrieve(subscriptionID, { |
210 | | - expand: ["discounts"], |
211 | | - }) |
212 | | - const couponID = |
213 | | - typeof subscriptionData.discounts[0] === "string" |
214 | | - ? subscriptionData.discounts[0] |
215 | | - : subscriptionData.discounts[0]?.coupon?.id |
216 | | - const productID = subscriptionData.items.data[0].price.product as string |
217 | | - |
218 | | - // get payment id from invoice |
219 | 201 | const invoice = await Billing.stripe().invoices.retrieve(invoiceID, { |
220 | | - expand: ["payments"], |
| 202 | + expand: ["discounts", "payments"], |
221 | 203 | }) |
222 | | - const paymentID = invoice.payments?.data[0].payment.payment_intent as string |
| 204 | + const paymentID = invoice.payments?.data[0]?.payment.payment_intent as string |
| 205 | + const couponID = (invoice.discounts[0] as Stripe.Discount).coupon?.id as string |
223 | 206 | if (!paymentID) { |
224 | 207 | // payment id can be undefined when using coupon |
225 | 208 | if (!couponID) throw new Error("Payment ID not found") |
|
0 commit comments