v1.3.27-d: Add credit-balance-summary method
This commit is contained in:
parent
0705a5f74d
commit
dc3f382cf6
|
|
@ -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": [
|
||||
|
|
|
|||
75
src/index.ts
75
src/index.ts
|
|
@ -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(
|
||||
|
|
|
|||
|
|
@ -98,6 +98,7 @@ export type actionType =
|
|||
| "meter-event"
|
||||
| "adjust-meter-event"
|
||||
| "meter-event-summary"
|
||||
| "credit-balance-summary"
|
||||
| "billing-portal";
|
||||
|
||||
export interface Subscription {
|
||||
|
|
|
|||
Loading…
Reference in New Issue