@@ -234,3 +234,46 @@ pub async fn coinbase_webhook(
234
234
}
235
235
}
236
236
}
237
+
238
+ /// Handler for retrieving available payment options for credits
239
+ pub async fn get_payment_options (
240
+ State ( state) : State < crate :: api:: routes:: AppState > ,
241
+ UserId ( user_id) : UserId ,
242
+ ) -> impl IntoResponse {
243
+ let config = & state. config ;
244
+ let storage = & state. storage ;
245
+
246
+ // Verify the user exists
247
+ match storage. get_user ( & user_id) . await {
248
+ Ok ( _) => {
249
+ // Create the payment options response
250
+ let expiry = Utc :: now ( ) + Duration :: minutes ( 30 ) ;
251
+ let payment_options = PaymentRequiredResponse {
252
+ expiry,
253
+ offers : config. offers . clone ( ) ,
254
+ payment_context_token : user_id,
255
+ payment_request_url : format ! (
256
+ "http://{}:{}/l402/payment-request" ,
257
+ config. host, config. port
258
+ ) ,
259
+ } ;
260
+
261
+ ( StatusCode :: OK , Json ( payment_options) ) . into_response ( )
262
+ } ,
263
+ Err ( StorageError :: UserNotFound ) => {
264
+ (
265
+ StatusCode :: UNAUTHORIZED ,
266
+ Json ( json ! ( { "error" : "User not found" } ) ) ,
267
+ )
268
+ . into_response ( )
269
+ } ,
270
+ Err ( e) => {
271
+ error ! ( "Error retrieving user: {}" , e) ;
272
+ (
273
+ StatusCode :: INTERNAL_SERVER_ERROR ,
274
+ Json ( json ! ( { "error" : "Failed to retrieve user" } ) ) ,
275
+ )
276
+ . into_response ( )
277
+ }
278
+ }
279
+ }
0 commit comments