@@ -17,7 +17,6 @@ import type {
1717import {
1818 getPaymentProviderConfig ,
1919 createProviderCheckout ,
20- getDodoClient ,
2120 type CheckoutResult ,
2221} from "./paymentProvider.ts" ;
2322import { StorageAdapterFactory } from "../../../factory" ;
@@ -26,7 +25,7 @@ import { apiKeyContextKey, type AuthContext } from "../../../context/auth";
2625import { wideEventContextKey } from "../../../context/requestContext" ;
2726import type { UserId } from "../../../config/identifiers" ;
2827import { DateTime } from "luxon" ;
29- import { handleAddSession } from "../../../storage/adapter /postgres/handlers " ;
28+ import { handleAddSession } from "../../../storage/db /postgres/helpers/sessions " ;
3029import { type ContextUnaryCall } from "../../../interface/types/context.ts" ;
3130
3231export async function createCheckoutLink (
@@ -45,16 +44,20 @@ export async function createCheckoutLink(
4544
4645 if ( auth . role === "dashboard" ) {
4746 return callback ?.(
48- AuthError . permissionDenied ( "Dashboard keys cannot create checkout links" )
47+ AuthError . permissionDenied (
48+ "Dashboard keys cannot create checkout links"
49+ )
4950 ) ;
5051 }
5152
52- if ( auth . role === "test" ) {
53+ if ( ! auth . mode ) {
5354 return callback ?.(
54- AuthError . permissionDenied ( "Test keys cannot create checkout links " )
55+ AuthError . permissionDenied ( "Auth mode not set on API key " )
5556 ) ;
5657 }
5758
59+ const mode = auth . mode ;
60+
5861 const config = getPaymentProviderConfig ( ) ;
5962 const validatedData = validateRequest ( req ) ;
6063 wideEventBuilder ?. setUser ( validatedData . userId ) ;
@@ -63,7 +66,7 @@ export async function createCheckoutLink(
6366 const custom_price = await calculatePrice (
6467 validatedData . userId ,
6568 beforeTimestamp ,
66- auth . mode !
69+ mode
6770 ) ;
6871 wideEventBuilder ?. setPaymentContext ( { priceAmount : custom_price } ) ;
6972
@@ -72,21 +75,24 @@ export async function createCheckoutLink(
7275 custom_price ,
7376 validatedData . userId ,
7477 auth . apiKeyId ,
75- beforeTimestamp
78+ beforeTimestamp ,
79+ mode
7680 ) ;
7781
78- const sessionResult = await ( handleAddSession as unknown as (
79- userId : UserId , sessionId : string , billedUpto : DateTime , mode ?: string
80- ) => Promise < { id : string } > ) (
82+ const sessionResult = await handleAddSession (
8183 validatedData . userId ,
8284 checkoutResult . sessionId ,
8385 beforeTimestamp ,
84- auth . mode !
86+ auth . apiKeyId ,
87+ mode ,
88+ checkoutResult . checkoutUrl
8589 ) ;
8690 wideEventBuilder ?. setPaymentContext ( { sessionId : sessionResult . id } ) ;
8791
92+ const proxyUrl = `${ process . env . APP_URL } /checkout/${ sessionResult . id } ` ;
93+
8894 const response = new CreateCheckoutLinkResponse ( ) ;
89- response . setCheckoutlink ( checkoutResult . checkoutUrl ) ;
95+ response . setCheckoutlink ( proxyUrl ) ;
9096 callback ?.( null , response ) ;
9197 } catch ( error ) {
9298 callback ?.( error as Error ) ;
@@ -109,7 +115,7 @@ function validateRequest(
109115async function calculatePrice (
110116 userId : UserId ,
111117 beforeTimestamp : DateTime ,
112- mode : string
118+ mode : "production" | "test"
113119) : Promise < number > {
114120 const storageAdapter =
115121 await StorageAdapterFactory . getEventStorageAdapter ( "PAYMENT" ) ;
@@ -118,9 +124,12 @@ async function calculatePrice(
118124 throw PaymentError . storageAdapterFailed ( "Storage adapter not available" ) ;
119125 }
120126
121- const price = await ( storageAdapter as unknown as {
122- price : ( userId : UserId , eventType : string , beforeTs : DateTime , mode ?: string ) => Promise < number > ;
123- } ) . price ( userId , "PAYMENT" , beforeTimestamp , mode ) ;
127+ const price = await storageAdapter . price (
128+ userId ,
129+ "PAYMENT" ,
130+ beforeTimestamp ,
131+ mode
132+ ) ;
124133
125134 if ( typeof price !== "number" || isNaN ( price ) || price < 0 ) {
126135 throw PaymentError . priceCalculationFailed (
@@ -137,15 +146,16 @@ async function createCheckoutSession(
137146 customPrice : number ,
138147 userId : string ,
139148 apiKeyId : string ,
140- beforeTimestamp : DateTime
149+ beforeTimestamp : DateTime ,
150+ mode : "test" | "production"
141151) : Promise < CheckoutResult > {
142152 const params : CheckoutParams = {
143153 customPrice,
144154 userId,
145155 apiKeyId,
146156 } ;
147157
148- const checkoutResult = await createProviderCheckout ( config , params ) ;
158+ const checkoutResult = await createProviderCheckout ( config , params , mode ) ;
149159
150160 if (
151161 ! checkoutResult . checkoutUrl ||
0 commit comments