Skip to content

Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation

fix(subscriptions): require client secret for publishable-key estimate calls and scope profile lookup to merchant (#15120)

Jagane4705cb66380
diff --git a/api-reference/v1/openapi_spec_v1.json b/api-reference/v1/openapi_spec_v1.json
index e2c6c99c4b..756e25bf26 100644
--- a/api-reference/v1/openapi_spec_v1.json
+++ b/api-reference/v1/openapi_spec_v1.json
@@ -10078,6 +10078,16 @@
"nullable": true,
"minimum": 0
}
+ },
+ {
+ "name": "client_secret",
+ "in": "query",
+ "description": "Subscription client secret; required when authenticating with a publishable key",
+ "required": false,
+ "schema": {
+ "type": "string",
+ "nullable": true
+ }
}
],
"responses": {
@@ -10101,6 +10111,9 @@
"security": [
{
"api_key": []
+ },
+ {
+ "client_secret": []
}
]
}
@@ -24060,6 +24073,11 @@
"item_price_id"
],
"properties": {
+ "client_secret": {
+ "type": "string",
+ "description": "This is a token which expires after 15 minutes, used from the client to authenticate and create sessions from the SDK",
+ "nullable": true
+ },
"plan_id": {
"type": "string",
"description": "Identifier for the associated subscription plan.",
diff --git a/crates/api_models/src/subscription.rs b/crates/api_models/src/subscription.rs
index 081de00ab7..63a45efbe7 100644
--- a/crates/api_models/src/subscription.rs
+++ b/crates/api_models/src/subscription.rs
@@ -555,6 +555,10 @@ impl ApiEventMetric for UpdateSubscriptionRequest {}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, ToSchema)]
pub struct EstimateSubscriptionQuery {
+ #[schema(value_type = Option<String>)]
+ /// This is a token which expires after 15 minutes, used from the client to authenticate and create sessions from the SDK
+ pub client_secret: Option<ClientSecret>,
+
/// Identifier for the associated subscription plan.
pub plan_id: Option<String>,
diff --git a/crates/openapi/src/routes/subscriptions.rs b/crates/openapi/src/routes/subscriptions.rs
index 69ec4e822c..6e8111252f 100644
--- a/crates/openapi/src/routes/subscriptions.rs
+++ b/crates/openapi/src/routes/subscriptions.rs
@@ -302,7 +302,8 @@ pub async fn get_subscription_items() {}
("plan_id" = String, Query, description = "Plan ID for estimation"),
("customer_id" = Option<String>, Query, description = "Customer ID for personalized pricing"),
("coupon_id" = Option<String>, Query, description = "Coupon ID to apply discount"),
- ("trial_days" = Option<u32>, Query, description = "Number of trial days")
+ ("trial_days" = Option<u32>, Query, description = "Number of trial days"),
+ ("client_secret" = Option<String>, Query, description = "Subscription client secret; required when authenticating with a publishable key")
),
responses(
(status = 200, description = "Estimate retrieved successfully", body = EstimateSubscriptionResponse),
@@ -311,7 +312,7 @@ pub async fn get_subscription_items() {}
),
tag = "Subscriptions",
operation_id = "Get Subscription Estimate",
- security(("api_key" = []))
+ security(("api_key" = []), ("client_secret" = []))
)]
pub async fn get_estimate() {}
diff --git a/crates/router/src/routes/subscription.rs b/crates/router/src/routes/subscription.rs
index fb4fc32729..3410ce2c99 100644
--- a/crates/router/src/routes/subscription.rs
+++ b/crates/router/src/routes/subscription.rs
@@ -436,8 +436,11 @@ pub async fn get_estimate(
allow_connected_scope_operation: false,
allow_platform_self_operation: false,
};
+ let payload = query.into_inner();
+ // Publishable-key requests must carry a subscription client secret, which the core
+ // validates; a bare publishable key is not sufficient.
let (auth_type, _auth_flow) =
- match auth::check_authorization_header_or_get_auth(req.headers(), api_auth) {
+ match auth::check_sdk_auth_and_get_auth(req.headers(), &payload, api_auth) {
Ok(auth) => auth,
Err(err) => return oss_api::log_and_return_error_response(report!(err)),
};
@@ -445,8 +448,11 @@ pub async fn get_estimate(
flow,
state,
&req,
- query.into_inner(),
- |state, auth, query, _| {
+ payload,
+ |state, auth, mut query, _| {
+ if let Some(client_secret) = auth.client_secret {
+ query.client_secret = Some(subscription_types::ClientSecret::new(client_secret));
+ }
subscriptions::get_estimate(state.into(), auth.platform, profile_id.clone(), query)
},
&*auth_type,
diff --git a/crates/router/src/services/authentication.rs b/crates/router/src/services/authentication.rs
index a6d41c3799..17f7775510 100644
--- a/crates/router/src/services/authentication.rs
+++ b/crates/router/src/services/authentication.rs
@@ -5976,6 +5976,13 @@ impl ClientSecretFetch for api_models::subscription::GetSubscriptionItemsQuery {
}
}
+#[cfg(feature = "v1")]
+impl ClientSecretFetch for api_models::subscription::EstimateSubscriptionQuery {
+ fn get_client_secret(&self) -> Option<&String> {
+ self.client_secret.as_ref().map(|s| s.as_string())
+ }
+}
+
#[cfg(feature = "v1")]
impl ClientSecretFetch for api_models::authentication::AuthenticationEligibilityRequest {
fn get_client_secret(&self) -> Option<&String> {
diff --git a/crates/subscriptions/src/core.rs b/crates/subscriptions/src/core.rs
index bb0e5b7b72..8e2952e7fd 100644
--- a/crates/subscriptions/src/core.rs
+++ b/crates/subscriptions/src/core.rs
@@ -66,6 +66,7 @@ pub async fn create_subscription(
.attach_printable("subscriptions: failed to create subscription entry")?;
let estimate_request = subscription_types::EstimateSubscriptionQuery {
+ client_secret: None,
plan_id: request.plan_id.clone(),
item_price_id: request.item_price_id.clone(),
coupon_code: None,
@@ -474,6 +475,12 @@ pub async fn get_estimate(
let profile = SubscriptionHandler::find_business_profile(&state, &platform, &profile_id)
.await
.attach_printable("subscriptions: failed to find business profile in get_estimate")?;
+
+ if let Some(client_secret) = query.client_secret.clone() {
+ SubscriptionHandler::new(&state, &platform)
+ .find_and_validate_subscription(&client_secret.into())
+ .await?
+ };
let billing_handler = BillingHandler::create(
&state,
platform.get_processor().get_account(),
@@ -678,6 +685,7 @@ pub async fn update_subscription(
.await?;
let estimate_request = subscription_types::EstimateSubscriptionQuery {
+ client_secret: None,
plan_id: Some(request.plan_id.clone()),
item_price_id: request.item_price_id.clone(),
coupon_code: None,
diff --git a/crates/subscriptions/src/core/subscription_handler.rs b/crates/subscriptions/src/core/subscription_handler.rs
index dd40c187cc..1fa6395352 100644
--- a/crates/subscriptions/src/core/subscription_handler.rs
+++ b/crates/subscriptions/src/core/subscription_handler.rs
@@ -171,10 +171,15 @@ impl<'a> SubscriptionHandler<'a> {
profile_id: &common_utils::id_type::ProfileId,
) -> errors::SubscriptionResult<hyperswitch_domain_models::business_profile::Profile> {
let merchant_key_store = platform.get_processor().get_key_store();
+ let merchant_id = platform.get_processor().get_account().get_id();
state
.store
- .find_business_profile_by_profile_id(merchant_key_store, profile_id)
+ .find_business_profile_by_merchant_id_profile_id(
+ merchant_key_store,
+ merchant_id,
+ profile_id,
+ )
.await
.change_context(errors::ApiErrorResponse::ProfileNotFound {
id: profile_id.get_string_repr().to_string(),