Skip to content

Commit d25c74e

Browse files
committed
✨ add endpoint for retrieving payment options
1 parent ce1aff8 commit d25c74e

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

src/api/handlers.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,3 +234,46 @@ pub async fn coinbase_webhook(
234234
}
235235
}
236236
}
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+
}

src/api/routes.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ pub fn create_router(
4343
// Protected routes that require authentication
4444
let protected_routes = Router::new()
4545
.route("/info", get(handlers::get_user_info))
46-
.route("/block", get(handlers::get_latest_block));
46+
.route("/block", get(handlers::get_latest_block))
47+
.route("/credits-payment-options", get(handlers::get_payment_options));
4748

4849
let state = AppState {
4950
config,

0 commit comments

Comments
 (0)