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
Rust adyenbeginner-friendlyfeaturedfinancefintechhacktoberfesthigh-performanceopen-sourceorchestrationpaymentpayment-gatewaypayment-integrationpayment-processingpaymentspayments-platformrestful-apirustsdkstripeworks-with-react
feat(subscription): Add estimate to subscriptions flow and remove amount and currency (#9835)
Gaurav Rawatc2c489407cb4
diff --git a/crates/api_models/src/subscription.rs b/crates/api_models/src/subscription.rsindex 812fe2ef5a..d4ca7fd4e4 100644--- a/crates/api_models/src/subscription.rs+++ b/crates/api_models/src/subscription.rs@@ -15,17 +15,11 @@ use crate::{/// including plan, profile, merchant connector, and optional customer info.#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, ToSchema)]pub struct CreateSubscriptionRequest {- /// Amount to be charged for the invoice.- pub amount: MinorUnit,-- /// Currency for the amount.- pub currency: api_enums::Currency,-/// Merchant specific Unique identifier.pub merchant_reference_id: Option<String>,/// Identifier for the associated item_price_id for the subscription.- pub item_price_id: Option<String>,+ pub item_price_id: String,/// Identifier for the subscription plan.pub plan_id: Option<String>,@@ -354,17 +348,11 @@ impl ApiEventMetric for ConfirmSubscriptionRequest {}#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, ToSchema)]pub struct CreateAndConfirmSubscriptionRequest {- /// Amount to be charged for the invoice.- pub amount: Option<MinorUnit>,-- /// Currency for the amount.- pub currency: Option<api_enums::Currency>,-/// Identifier for the associated plan_id.pub plan_id: Option<String>,/// Identifier for the associated item_price_id for the subscription.- pub item_price_id: Option<String>,+ pub item_price_id: String,/// Idenctifier for the coupon code for the subscription.pub coupon_code: Option<String>,@@ -477,10 +465,6 @@ pub struct UpdateSubscriptionRequest {pub plan_id: String,/// Identifier for the associated item_price_id for the subscription.pub item_price_id: String,- /// Amount to be charged for the invoice.- pub amount: MinorUnit,- /// Currency for the amount.- pub currency: api_enums::Currency,}impl ApiEventMetric for UpdateSubscriptionRequest {}diff --git a/crates/router/src/core/subscription.rs b/crates/router/src/core/subscription.rsindex 07e2d671bf..1943006897 100644--- a/crates/router/src/core/subscription.rs+++ b/crates/router/src/core/subscription.rs@@ -59,22 +59,39 @@ pub async fn create_subscription(request.merchant_reference_id.clone(),&profile.clone(),request.plan_id.clone(),- request.item_price_id.clone(),+ Some(request.item_price_id.clone()),).await.attach_printable("subscriptions: failed to create subscription entry")?;++ let estimate_request = subscription_types::EstimateSubscriptionQuery {+ plan_id: request.plan_id.clone(),+ item_price_id: request.item_price_id.clone(),+ coupon_code: None,+ };++ let estimate = billing_handler+ .get_subscription_estimate(&state, estimate_request)+ .await?;+let invoice_handler = subscription.get_invoice_handler(profile.clone());let payment = invoice_handler- .create_payment_with_confirm_false(subscription.handler.state, &request)+ .create_payment_with_confirm_false(+ subscription.handler.state,+ &request,+ estimate.total,+ estimate.currency,+ ).await.attach_printable("subscriptions: failed to create payment")?;+let invoice = invoice_handler.create_invoice_entry(&state,billing_handler.merchant_connector_id,Some(payment.payment_id.clone()),- request.amount,- request.currency,+ estimate.total,+ estimate.currency,connector_enums::InvoiceStatus::InvoiceCreated,billing_handler.connector_data.connector_name,None,@@ -90,7 +107,7 @@ pub async fn create_subscription(payment.payment_method_id.clone(),None,request.plan_id,- request.item_price_id,+ Some(request.item_price_id),),).await@@ -186,7 +203,7 @@ pub async fn create_and_confirm_subscription(request.merchant_reference_id.clone(),&profile.clone(),request.plan_id.clone(),- request.item_price_id.clone(),+ Some(request.item_price_id.clone()),).await.attach_printable("subscriptions: failed to create subscription entry")?;@@ -219,16 +236,14 @@ pub async fn create_and_confirm_subscription(.create_subscription_on_connector(&state,subs_handler.subscription.clone(),- request.item_price_id.clone(),+ Some(request.item_price_id.clone()),request.get_billing_address(),).await?;let invoice_details = subscription_create_response.invoice_details;- let (amount, currency) = InvoiceHandler::get_amount_and_currency(- (request.amount, request.currency),- invoice_details.clone(),- );+ let (amount, currency) =+ InvoiceHandler::get_amount_and_currency((None, None), invoice_details.clone());let payment_response = invoice_handler.create_and_confirm_payment(&state, &request, amount, currency)@@ -272,7 +287,7 @@ pub async fn create_and_confirm_subscription(payment_response.payment_method_id.clone(),Some(SubscriptionStatus::from(subscription_create_response.status).to_string()),request.plan_id,- request.item_price_id,+ Some(request.item_price_id),),).await?;@@ -501,15 +516,33 @@ pub async fn update_subscription(None,None,None,- Some(request.plan_id),- Some(request.item_price_id),+ Some(request.plan_id.clone()),+ Some(request.item_price_id.clone()),),).await?;+ let billing_handler = BillingHandler::create(+ &state,+ merchant_context.get_merchant_account(),+ merchant_context.get_merchant_key_store(),+ profile.clone(),+ )+ .await?;++ let estimate_request = subscription_types::EstimateSubscriptionQuery {+ plan_id: Some(request.plan_id.clone()),+ item_price_id: request.item_price_id.clone(),+ coupon_code: None,+ };++ let estimate = billing_handler+ .get_subscription_estimate(&state, estimate_request)+ .await?;+let update_request = InvoiceUpdateRequest::update_amount_and_currency(- request.amount,- request.currency.to_string(),+ estimate.total,+ estimate.currency.to_string(),);let invoice_entry = invoice_handler@@ -519,8 +552,8 @@ pub async fn update_subscription(let _payment_response = invoice_handler.update_payment(&state,- request.amount,- request.currency,+ estimate.total,+ estimate.currency,invoice_entry.payment_intent_id.ok_or(errors::ApiErrorResponse::MissingRequiredField {field_name: "payment_intent_id",diff --git a/crates/router/src/core/subscription/invoice_handler.rs b/crates/router/src/core/subscription/invoice_handler.rsindex a6d04a71d9..7576e95ce8 100644--- a/crates/router/src/core/subscription/invoice_handler.rs+++ b/crates/router/src/core/subscription/invoice_handler.rs@@ -118,11 +118,13 @@ impl InvoiceHandler {&self,state: &SessionState,request: &subscription_types::CreateSubscriptionRequest,+ amount: MinorUnit,+ currency: api_enums::Currency,) -> errors::RouterResult<subscription_types::PaymentResponseData> {let payment_details = &request.payment_details;let payment_request = subscription_types::CreatePaymentsRequestData {- amount: request.amount,- currency: request.currency,+ amount,+ currency,customer_id: Some(self.subscription.customer_id.clone()),billing: request.billing.clone(),shipping: request.shipping.clone(),