v1.3.27-i: credit-balance-summary no sub id needed

This commit is contained in:
Yudi Xiao 2025-10-15 18:06:15 +08:00
parent ea2e394348
commit cb4f5501e0
2 changed files with 14 additions and 7 deletions

View File

@ -1,7 +1,7 @@
{ {
"name": "@bowong/better-auth-stripe", "name": "@bowong/better-auth-stripe",
"author": "Bowong", "author": "Bowong",
"version": "1.3.27-g", "version": "1.3.27-i",
"main": "dist/index.cjs", "main": "dist/index.cjs",
"license": "MIT", "license": "MIT",
"keywords": [ "keywords": [

View File

@ -1408,7 +1408,6 @@ export const stripe = <O extends StripeOptions>(options: O) => {
{ {
method: "POST", method: "POST",
body: z.object({ body: z.object({
subscriptionId: z.string().meta({}),
filter: z.object({ filter: z.object({
type: z.enum(['credit_grant', 'applicability_scope']), type: z.enum(['credit_grant', 'applicability_scope']),
applicability_scope: z.object({ applicability_scope: z.object({
@ -1431,7 +1430,6 @@ export const stripe = <O extends StripeOptions>(options: O) => {
async (ctx) => { async (ctx) => {
const {user} = ctx.context.session; const {user} = ctx.context.session;
let customerId = user.stripeCustomerId; let customerId = user.stripeCustomerId;
let subscriptionId = ctx.body.subscriptionId;
const referenceId = user.id; const referenceId = user.id;
if (!customerId) { if (!customerId) {
@ -1460,17 +1458,25 @@ export const stripe = <O extends StripeOptions>(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 { try {
// todo : stripe credit balance summary api not working yet, apply workaround // todo : stripe credit balance summary api not working yet, apply workaround
const { subscriptionId, ...other} = ctx.body;
const creditBalance = await client.billing.creditBalanceSummary.retrieve( const creditBalance = await client.billing.creditBalanceSummary.retrieve(
{ {
...other, ...ctx.body,
customer: customerId customer: customerId
} }
); );
const upcomingInvoice = await client.invoices.createPreview({ const upcomingInvoice = await client.invoices.createPreview({
subscription: subscriptionId subscription: subscriptions.data[0]?.id
}) })
// const usageProductLineItem = upcomingInvoice.lines.data // const usageProductLineItem = upcomingInvoice.lines.data
// const usageProductLineItem = upcomingInvoice.lines.data.find( // const usageProductLineItem = upcomingInvoice.lines.data.find(
@ -1494,7 +1500,8 @@ export const stripe = <O extends StripeOptions>(options: O) => {
tokenUsage: usage, tokenUsage: usage,
tokenBalance, tokenBalance,
remainingTokenBalance, remainingTokenBalance,
currentCreditBalance currentCreditBalance,
perTokenPrice
}) })
} catch (error: any) { } catch (error: any) {
ctx.context.logger.error( ctx.context.logger.error(