Skip to content

Commit 4ec9db8

Browse files
committed
task: Use Stripe PaymentIntents API for credit history
1 parent 8b7bebe commit 4ec9db8

File tree

16 files changed

+145073
-32991
lines changed

16 files changed

+145073
-32991
lines changed

bifrost/lib/clients/jawnTypes/private.ts

Lines changed: 12454 additions & 12427 deletions
Large diffs are not rendered by default.

bifrost/lib/clients/jawnTypes/public.ts

Lines changed: 12519 additions & 130 deletions
Large diffs are not rendered by default.

docs/swagger.json

Lines changed: 37710 additions & 0 deletions
Large diffs are not rendered by default.

valhalla/jawn/src/controllers/public/stripeController.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import type { JawnAuthenticatedRequest } from "../../types/request";
1616
import { isError } from "../../packages/common/result";
1717
import express from "express";
1818
import { checkFeatureFlag } from "../../lib/utils/featureFlags";
19+
import Stripe from "stripe";
1920

2021
export interface UpgradeToProRequest {
2122
addons?: {
@@ -36,6 +37,23 @@ export interface CreateCloudGatewayCheckoutSessionRequest {
3637
amount: number;
3738
}
3839

40+
export enum PaymentIntentSearchKind {
41+
CREDIT_PURCHASES = "credit_purchases",
42+
}
43+
44+
export interface SearchPaymentIntentsRequest {
45+
search_kind: PaymentIntentSearchKind;
46+
limit?: number;
47+
page?: string;
48+
}
49+
50+
export interface StripePaymentIntentsResponse {
51+
data: Stripe.PaymentIntent[];
52+
has_more: boolean;
53+
next_page: string | null;
54+
count: number;
55+
}
56+
3957

4058
export interface LLMUsage {
4159
model: string;
@@ -363,6 +381,35 @@ export class StripeController extends Controller {
363381
const result = await stripeManager.migrateToPro();
364382
}
365383

384+
@Get("/payment-intents/search")
385+
public async searchPaymentIntents(
386+
@Request() request: JawnAuthenticatedRequest,
387+
@Query() search_kind: string,
388+
@Query() limit?: number,
389+
@Query() page?: string
390+
): Promise<StripePaymentIntentsResponse> {
391+
// Check if search_kind is valid
392+
if (!Object.values(PaymentIntentSearchKind).includes(search_kind as PaymentIntentSearchKind)) {
393+
this.setStatus(400);
394+
throw new Error(`Invalid search_kind: ${search_kind}. Supported types: ${Object.values(PaymentIntentSearchKind).join(", ")}`);
395+
}
396+
397+
const searchKind = search_kind as PaymentIntentSearchKind;
398+
const stripeManager = new StripeManager(request.authParams);
399+
const result = await stripeManager.searchPaymentIntents(
400+
searchKind,
401+
limit ?? 10,
402+
page
403+
);
404+
405+
if (isError(result)) {
406+
this.setStatus(500);
407+
throw new Error(result.error);
408+
}
409+
410+
return result.data;
411+
}
412+
366413
@Get("/subscription")
367414
public async getSubscription(
368415
@Request() request: JawnAuthenticatedRequest

valhalla/jawn/src/managers/stripe/StripeManager.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import {
33
LLMUsage,
44
UpgradeToProRequest,
55
UpgradeToTeamBundleRequest,
6+
StripePaymentIntentsResponse,
7+
PaymentIntentSearchKind,
68
} from "../../controllers/public/stripeController";
79
import { clickhouseDb } from "../../lib/db/ClickhouseWrapper";
810
import { Database } from "../../lib/db/database.types";
@@ -1345,5 +1347,57 @@ WHERE (${builtFilter.filter})`,
13451347
return err(`Error retrieving purchased seats: ${error.message}`);
13461348
}
13471349
}
1350+
1351+
public async searchPaymentIntents(
1352+
searchKind: PaymentIntentSearchKind,
1353+
limit: number = 10,
1354+
page?: string
1355+
): Promise<Result<StripePaymentIntentsResponse, string>> {
1356+
try {
1357+
let query: string;
1358+
1359+
// Build query based on search kind
1360+
switch (searchKind) {
1361+
case PaymentIntentSearchKind.CREDIT_PURCHASES:
1362+
const productId = process.env.STRIPE_CLOUD_GATEWAY_TOKEN_USAGE_PRODUCT;
1363+
1364+
if (!productId) {
1365+
console.error(
1366+
"[Stripe API] STRIPE_CLOUD_GATEWAY_TOKEN_USAGE_PRODUCT not configured"
1367+
);
1368+
return err("Stripe product ID not configured");
1369+
}
1370+
1371+
query = `metadata['productId']:'${productId}' AND metadata['orgId']:'${this.authParams.organizationId}'`;
1372+
break;
1373+
1374+
default:
1375+
return err(`Unsupported search kind: ${searchKind}`);
1376+
}
1377+
1378+
// Search payment intents using Stripe API
1379+
const searchParams: any = {
1380+
query,
1381+
limit,
1382+
};
1383+
1384+
// Add page parameter if provided (Stripe uses page token for search pagination)
1385+
if (page) {
1386+
searchParams.page = page;
1387+
}
1388+
1389+
const paymentIntents = await this.stripe.paymentIntents.search(searchParams);
1390+
1391+
return ok({
1392+
data: paymentIntents.data,
1393+
has_more: paymentIntents.has_more,
1394+
next_page: paymentIntents.next_page || null,
1395+
count: paymentIntents.data.length,
1396+
});
1397+
} catch (error: any) {
1398+
console.error("Error searching payment intents:", error);
1399+
return err("Failed to search payment intents");
1400+
}
1401+
}
13481402
}
13491403

valhalla/jawn/src/tsoa-build/private/routes.ts

Lines changed: 7633 additions & 7588 deletions
Large diffs are not rendered by default.

valhalla/jawn/src/tsoa-build/public/routes.ts

Lines changed: 11669 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)