v1.3.27-i: credit-balance-summary no sub id needed
This commit is contained in:
parent
ea2e394348
commit
cb4f5501e0
|
|
@ -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": [
|
||||
|
|
|
|||
19
src/index.ts
19
src/index.ts
|
|
@ -1408,7 +1408,6 @@ export const stripe = <O extends StripeOptions>(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 = <O extends StripeOptions>(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 = <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 {
|
||||
// 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 = <O extends StripeOptions>(options: O) => {
|
|||
tokenUsage: usage,
|
||||
tokenBalance,
|
||||
remainingTokenBalance,
|
||||
currentCreditBalance
|
||||
currentCreditBalance,
|
||||
perTokenPrice
|
||||
})
|
||||
} catch (error: any) {
|
||||
ctx.context.logger.error(
|
||||
|
|
|
|||
Loading…
Reference in New Issue