forked from coracle/caravel
fix: stripe portal dead-end with callback return flow
This commit is contained in:
+8
-2
@@ -3,7 +3,7 @@ use std::sync::Arc;
|
|||||||
use anyhow::{Result, anyhow};
|
use anyhow::{Result, anyhow};
|
||||||
use axum::{
|
use axum::{
|
||||||
Json, Router,
|
Json, Router,
|
||||||
extract::{Path, State},
|
extract::{Path, Query as QueryParams, State},
|
||||||
http::{HeaderMap, StatusCode},
|
http::{HeaderMap, StatusCode},
|
||||||
response::{IntoResponse, Response},
|
response::{IntoResponse, Response},
|
||||||
routing::{get, post},
|
routing::{get, post},
|
||||||
@@ -1101,10 +1101,16 @@ async fn get_invoice_bolt11(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(serde::Deserialize)]
|
||||||
|
struct StripeSessionParams {
|
||||||
|
return_url: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
async fn create_stripe_session(
|
async fn create_stripe_session(
|
||||||
State(state): State<AppState>,
|
State(state): State<AppState>,
|
||||||
headers: HeaderMap,
|
headers: HeaderMap,
|
||||||
Path(pubkey): Path<String>,
|
Path(pubkey): Path<String>,
|
||||||
|
QueryParams(params): QueryParams<StripeSessionParams>,
|
||||||
) -> std::result::Result<Response, ApiError> {
|
) -> std::result::Result<Response, ApiError> {
|
||||||
let auth = state.api.extract_auth_pubkey(&headers)?;
|
let auth = state.api.extract_auth_pubkey(&headers)?;
|
||||||
state.api.require_admin_or_tenant(&auth, &pubkey)?;
|
state.api.require_admin_or_tenant(&auth, &pubkey)?;
|
||||||
@@ -1113,7 +1119,7 @@ async fn create_stripe_session(
|
|||||||
match state
|
match state
|
||||||
.api
|
.api
|
||||||
.billing
|
.billing
|
||||||
.stripe_create_portal_session(&tenant.stripe_customer_id)
|
.stripe_create_portal_session(&tenant.stripe_customer_id, params.return_url.as_deref())
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
Ok(url) => Ok(ok(StatusCode::OK, serde_json::json!({ "url": url }))),
|
Ok(url) => Ok(ok(StatusCode::OK, serde_json::json!({ "url": url }))),
|
||||||
|
|||||||
+10
-2
@@ -966,12 +966,20 @@ impl Billing {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn stripe_create_portal_session(&self, customer_id: &str) -> Result<String> {
|
pub async fn stripe_create_portal_session(
|
||||||
|
&self,
|
||||||
|
customer_id: &str,
|
||||||
|
return_url: Option<&str>,
|
||||||
|
) -> Result<String> {
|
||||||
|
let mut params = vec![("customer", customer_id.to_string())];
|
||||||
|
if let Some(url) = return_url {
|
||||||
|
params.push(("return_url", url.to_string()));
|
||||||
|
}
|
||||||
let resp = self
|
let resp = self
|
||||||
.http
|
.http
|
||||||
.post(format!("{STRIPE_API}/billing_portal/sessions"))
|
.post(format!("{STRIPE_API}/billing_portal/sessions"))
|
||||||
.bearer_auth(&self.stripe_secret_key)
|
.bearer_auth(&self.stripe_secret_key)
|
||||||
.form(&[("customer", customer_id)])
|
.form(¶ms)
|
||||||
.send()
|
.send()
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ export default function PaymentSetup(props: PaymentSetupProps) {
|
|||||||
setRedirecting(true)
|
setRedirecting(true)
|
||||||
setError("")
|
setError("")
|
||||||
try {
|
try {
|
||||||
const { url } = await createPortalSession(account()!.pubkey)
|
const { url } = await createPortalSession(account()!.pubkey, window.location.href)
|
||||||
window.location.href = url
|
window.location.href = url
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
setError(e instanceof Error ? e.message : "Failed to open billing portal")
|
setError(e instanceof Error ? e.message : "Failed to open billing portal")
|
||||||
|
|||||||
@@ -253,8 +253,9 @@ export function reactivateRelay(id: string) {
|
|||||||
return callApi<undefined, void>("POST", `/relays/${id}/reactivate`)
|
return callApi<undefined, void>("POST", `/relays/${id}/reactivate`)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createPortalSession(pubkey: string) {
|
export function createPortalSession(pubkey: string, returnUrl?: string) {
|
||||||
return callApi<undefined, { url: string }>("GET", `/tenants/${pubkey}/stripe/session`)
|
const query = returnUrl ? `?return_url=${encodeURIComponent(returnUrl)}` : ""
|
||||||
|
return callApi<undefined, { url: string }>("GET", `/tenants/${pubkey}/stripe/session${query}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getInvoiceBolt11(invoiceId: string) {
|
export function getInvoiceBolt11(invoiceId: string) {
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ export default function Account() {
|
|||||||
async function openPortal() {
|
async function openPortal() {
|
||||||
setPortalLoading(true)
|
setPortalLoading(true)
|
||||||
try {
|
try {
|
||||||
const { url } = await createPortalSession(account()!.pubkey)
|
const { url } = await createPortalSession(account()!.pubkey, window.location.href)
|
||||||
window.location.href = url
|
window.location.href = url
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
setError(e instanceof Error ? e.message : "Failed to open billing portal")
|
setError(e instanceof Error ? e.message : "Failed to open billing portal")
|
||||||
|
|||||||
Reference in New Issue
Block a user