diff --git a/package.json b/package.json index 601fad2..0e084a2 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@bowong/better-auth-stripe", "author": "Bowong", - "version": "1.3.27-g", + "version": "1.3.27-i", "main": "dist/index.cjs", "license": "MIT", "keywords": [ diff --git a/src/index.ts b/src/index.ts index 8c38464..c6adf8d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1408,7 +1408,6 @@ export const stripe = (options: O) => { { method: "POST", body: z.object({ - subscriptionId: z.string().meta({}), filter: z.object({ type: z.enum(['credit_grant', 'applicability_scope']), applicability_scope: z.object({ @@ -1431,7 +1430,6 @@ export const stripe = (options: O) => { async (ctx) => { const {user} = ctx.context.session; let customerId = user.stripeCustomerId; - let subscriptionId = ctx.body.subscriptionId; const referenceId = user.id; if (!customerId) { @@ -1460,17 +1458,25 @@ export const stripe = (options: O) => { }); } + const subscriptions = await client.subscriptions.list({customer : customerId}) + + if(subscriptions.data.length == 0){ + throw new APIError("BAD_REQUEST", { + message: "No Stripe subscriptions found for this user", + }); + } + try { // todo : stripe credit balance summary api not working yet, apply workaround - const { subscriptionId, ...other} = ctx.body; const creditBalance = await client.billing.creditBalanceSummary.retrieve( { - ...other, + ...ctx.body, customer: customerId } ); + const upcomingInvoice = await client.invoices.createPreview({ - subscription: subscriptionId + subscription: subscriptions.data[0]?.id }) // const usageProductLineItem = upcomingInvoice.lines.data // const usageProductLineItem = upcomingInvoice.lines.data.find( @@ -1494,7 +1500,8 @@ export const stripe = (options: O) => { tokenUsage: usage, tokenBalance, remainingTokenBalance, - currentCreditBalance + currentCreditBalance, + perTokenPrice }) } catch (error: any) { ctx.context.logger.error(