v1.3.27-d: Add credit-balance-summary method

This commit is contained in:
Yudi Xiao 2025-10-14 14:58:06 +08:00
parent 0705a5f74d
commit dc3f382cf6
3 changed files with 77 additions and 1 deletions

View File

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

View File

@ -1391,6 +1391,81 @@ export const stripe = <O extends StripeOptions>(options: O) => {
}
);
return ctx.json(result);
} catch (error: any) {
ctx.context.logger.error(
"Error submitting meter event",
error,
);
throw new APIError("BAD_REQUEST", {
message: error.message,
});
}
}
),
creditBalanceSummary: createAuthEndpoint(
"/subscription/credit/summary", // https://docs.stripe.com/api/billing/credit-balance-summary
{
method: "GET",
query: z.object({
filter: z.object({
type: z.enum(['credit_grant', 'applicability_scope']),
applicability_scope: z.object({
price_type: z.enum(['metered']).meta({
description: "The price type that credit grants can apply to. We currently only support the metered price type. Cannot be used in combination with prices."
})
}).optional().meta({
description: "The billing credit applicability scope for which to fetch credit balance summary"
}),
credit_grant: z.string().optional().meta({
description: "The credit grant for which to fetch credit balance summary"
})
})
}),
use: [
sessionMiddleware,
referenceMiddleware("credit-balance-summary"),
]
},
async (ctx) => {
const {user} = ctx.context.session;
let customerId = user.stripeCustomerId;
const referenceId = ctx.body.referenceId || user.id;
if (!customerId) {
const subscription = await ctx.context.adapter
.findMany<Subscription>({
model: "subscription",
where: [
{
field: "referenceId",
value: referenceId,
},
],
})
.then((subs) =>
subs.find(
(sub) => sub.status === "active" || sub.status === "trialing",
),
);
customerId = subscription?.stripeCustomerId;
}
if (!customerId) {
throw new APIError("BAD_REQUEST", {
message: "No Stripe customer found for this user",
});
}
try {
const result = await client.billing.creditBalanceSummary.retrieve(
{
...ctx.query,
customer: customerId
}
);
return ctx.json(result);
} catch (error: any) {
ctx.context.logger.error(

View File

@ -98,6 +98,7 @@ export type actionType =
| "meter-event"
| "adjust-meter-event"
| "meter-event-summary"
| "credit-balance-summary"
| "billing-portal";
export interface Subscription {