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
fix(router): fix create modular payment method during v1 payments (#11218)
Prasunna Soppa5386ae2f6cd4
diff --git a/crates/router/src/core/payments/operations/payment_confirm.rs b/crates/router/src/core/payments/operations/payment_confirm.rsindex d763cf8087..cfeb0fea34 100644--- a/crates/router/src/core/payments/operations/payment_confirm.rs+++ b/crates/router/src/core/payments/operations/payment_confirm.rs@@ -1051,77 +1051,86 @@ impl<F: Clone + Send + Sync> Domain<F, api::PaymentsRequest, PaymentData<F>> for) -> RouterResult<()> {match feature_config.is_payment_method_modular_allowed {true => {- logger::info!("Organization is eligible for PM Modular Service, proceeding to create payment method using PM Modular Service.");- //check for req.payment_method_data, if card, create payment method with pm service- let payment_method_info = match req- .payment_method_data- .as_ref()- .and_then(|pmd| pmd.payment_method_data.as_ref())- {- Some(api_models::payments::PaymentMethodData::Card(_)) => {- let payment_method = req.payment_method.ok_or(- errors::ApiErrorResponse::MissingRequiredField {- field_name: "payment_method",- },- )?;-- let payment_method_type = req.payment_method_type.ok_or(- errors::ApiErrorResponse::MissingRequiredField {- field_name: "payment_method_type",- },- )?;-- let payment_method_data = req- .payment_method_data- .as_ref()- .and_then(|pmd| pmd.payment_method_data.clone())- .map(Into::into)- .ok_or(errors::ApiErrorResponse::MissingRequiredField {- field_name: "payment_method_data",- })?;-- match pm_transformers::create_payment_method_in_modular_service(- state,- platform.get_provider().get_account().get_id(),- business_profile.get_id(),- payment_method,- payment_method_type,- payment_method_data,- payment_data- .address- .get_request_payment_method_billing()- .cloned(),- payment_data- .payment_intent- .customer_id- .clone()- .get_required_value("customer_id")?,- )- .await- {- Ok(pm_info) => {- logger::info!(- "Payment method created in PM Modular service successfully"- );- Some(pm_info)- }- Err(err) => {- logger::error!(- "Error creating payment method in PM Modular service: {:?}",- err- );- None+ if req.customer_acceptance.is_some() {+ logger::info!("Organization is eligible for PM Modular Service, proceeding to create payment method using PM Modular Service.");+ //check for req.payment_method_data, if card, create payment method with pm service+ let payment_method_info = match req+ .payment_method_data+ .as_ref()+ .and_then(|pmd| pmd.payment_method_data.as_ref())+ {+ Some(api_models::payments::PaymentMethodData::Card(_)) => {+ let payment_method = req.payment_method.ok_or(+ errors::ApiErrorResponse::MissingRequiredField {+ field_name: "payment_method",+ },+ )?;++ let payment_method_type = req.payment_method_type.ok_or(+ errors::ApiErrorResponse::MissingRequiredField {+ field_name: "payment_method_type",+ },+ )?;++ let payment_method_data = req+ .payment_method_data+ .as_ref()+ .and_then(|pmd| pmd.payment_method_data.clone())+ .map(Into::into)+ .ok_or(errors::ApiErrorResponse::MissingRequiredField {+ field_name: "payment_method_data",+ })?;++ match pm_transformers::create_payment_method_in_modular_service(+ state,+ platform.get_provider().get_account().get_id(),+ business_profile.get_id(),+ payment_method,+ payment_method_type,+ payment_method_data,+ payment_data+ .address+ .get_request_payment_method_billing()+ .cloned(),+ payment_data+ .payment_intent+ .customer_id+ .clone()+ .get_required_value("customer_id")?,+ )+ .await+ {+ Ok(pm_info) => {+ logger::info!(+ "Payment method created in PM Modular service successfully"+ );+ Some(pm_info)+ }+ Err(err) => {+ logger::error!(+ "Error creating payment method in PM Modular service: {:?}",+ err+ );+ None+ }}}- }- _ => {- logger::info!("No supported payment method data found for creating payment method in PM Modular service.");- None- }- };- //set payment_data.payment_method_info- payment_data.set_payment_method_info(payment_method_info);- Ok(())+ _ => {+ logger::info!("No supported payment method data found for creating payment method in PM Modular service.");+ None+ }+ };+ //set payment_data.payment_method_info+ payment_data.payment_attempt.payment_method_id = payment_method_info+ .as_ref()+ .map(|pm_info| pm_info.get_id().clone());+ payment_data.set_payment_method_info(payment_method_info);++ Ok(())+ } else {+ logger::info!("Skipping create payment method in PM Modular Service as customer acceptance is not present in the request.");+ Ok(())+ }}false => {logger::info!("Organization is not eligible for PM Modular Service, skipping create payment method.");diff --git a/crates/router/src/core/payments/operations/payment_response.rs b/crates/router/src/core/payments/operations/payment_response.rsindex eaea5fba86..2754a646d5 100644--- a/crates/router/src/core/payments/operations/payment_response.rs+++ b/crates/router/src/core/payments/operations/payment_response.rs@@ -85,110 +85,138 @@ whereSome(enums::PaymentMethod::Card)) && resp.status.should_update_payment_method(){- // #1 - Retrieve payment_method_id from payment_method_info.- let payment_method_id = payment_data+ //#1 - Check if Payment method id is present in the payment data+ match payment_data.payment_method_info.as_ref()- .map(|info| info.get_id().clone())- .ok_or_else(|| {- logger::error!("Missing required Param payment_method_id");- ::payment_methods::errors::ModularPaymentMethodError::RetrieveFailed- })?;+ .map(|pm_info| pm_info.get_id().clone())+ {+ Some(payment_method_id) => {+ logger::info!("Payment method is card and eligible for modular update");- // #2 - Derive network transaction ID from the connector response.- let network_transaction_id = resp- .response- .as_ref()- .map_err(|err| {- logger::debug!(error=?err, "Failed to obtain the network_transaction_id from payment response in modular payment method update call");- })- .ok()- .and_then(types::PaymentsResponseData::get_network_transaction_id);-- // #3 - Fill payment method data for cards (update card holder name, nick_name & cvc).- // Use request payment method data for card_holder_name and nick_name- let payment_method_data =- request_payment_method_data.and_then(|method_data| match method_data {- domain::PaymentMethodData::Card(card) => {- Some(PaymentMethodUpdateData::Card(CardDetailUpdate {- card_holder_name: card.card_holder_name.clone(),- nick_name: card.nick_name.clone(),- card_cvc: None,- }))- }- _ => None,- });+ // #2 - Derive network transaction ID from the connector response.+ let (network_transaction_id, connector_token_details) = if matches!(+ payment_data.payment_attempt.setup_future_usage_applied,+ Some(common_enums::FutureUsage::OffSession)+ ) {+ let network_transaction_id = resp+ .response+ .as_ref()+ .map_err(|err| {+ logger::debug!(error=?err, "Failed to obtain the network_transaction_id from payment response in modular payment method update call");+ })+ .ok()+ .and_then(types::PaymentsResponseData::get_network_transaction_id);++ let connector_token_details = match resp+ .response+ .as_ref()+ .ok()+ .and_then(types::PaymentsResponseData::get_mandate_reference)+ {+ Some(mandate_reference) => {+ let connector_id = payment_data+ .payment_attempt+ .merchant_connector_id+ .clone()+ .ok_or_else(|| {+ logger::error!("Missing required Param merchant_connector_id");+ ::payment_methods::errors::ModularPaymentMethodError::RetrieveFailed+ })?;+ update_connector_mandate_details_for_the_flow(+ mandate_reference.connector_mandate_id.clone(),+ mandate_reference.mandate_metadata.clone(),+ mandate_reference+ .connector_mandate_request_reference_id+ .clone(),+ payment_data,+ )+ .change_context(+ ::payment_methods::errors::ModularPaymentMethodError::UpdateFailed,+ )?;+ mandate_reference+ .connector_mandate_id+ .map(|connector_mandate_id| {+ ::payment_methods::types::ConnectorTokenDetails {+ connector_id,+ token_type: TokenizationType::MultiUse,+ status: ConnectorTokenStatus::Active,+ connector_token_request_reference_id: mandate_reference+ .connector_mandate_request_reference_id,+ original_payment_authorized_amount: Some(+ payment_data+ .payment_attempt+ .net_amount+ .get_total_amount(),+ ),+ original_payment_authorized_currency: payment_data+ .payment_attempt+ .currency,+ metadata: mandate_reference.mandate_metadata,+ token: masking::Secret::new(connector_mandate_id),+ }+ })+ }+ None => None,+ };- // #4 - Build connector token details only when a mandate reference is available.- let connector_token_details = match resp- .response- .as_ref()- .ok()- .and_then(types::PaymentsResponseData::get_mandate_reference)- {- Some(mandate_reference) => {- let connector_id = payment_data- .payment_attempt- .merchant_connector_id- .clone()- .ok_or_else(|| {- logger::error!("Missing required Param merchant_connector_id");- ::payment_methods::errors::ModularPaymentMethodError::RetrieveFailed- })?;- update_connector_mandate_details_for_the_flow(- mandate_reference.connector_mandate_id.clone(),- mandate_reference.mandate_metadata.clone(),- mandate_reference- .connector_mandate_request_reference_id- .clone(),- payment_data,- )- .change_context(- ::payment_methods::errors::ModularPaymentMethodError::UpdateFailed,- )?;- mandate_reference- .connector_mandate_id- .map(- |connector_mandate_id| ::payment_methods::types::ConnectorTokenDetails {- connector_id,- token_type: TokenizationType::MultiUse,- status: ConnectorTokenStatus::Active,- connector_token_request_reference_id: mandate_reference- .connector_mandate_request_reference_id,- original_payment_authorized_amount: Some(- payment_data.payment_attempt.net_amount.get_total_amount(),- ),- original_payment_authorized_currency: payment_data- .payment_attempt- .currency,- metadata: mandate_reference.mandate_metadata,- token: masking::Secret::new(connector_mandate_id),- },- )- }- None => None,- };+ (network_transaction_id, connector_token_details)+ } else {+ (None, None)+ };- let payload = UpdatePaymentMethodV1Payload {- payment_method_data,- connector_token_details,- network_transaction_id: network_transaction_id.map(masking::Secret::new),- };+ // #3 - Fill payment method data for cards (update card holder name, nick_name & cvc).+ // Use request payment method data for card_holder_name and nick_name+ let payment_method_data =+ request_payment_method_data.and_then(|method_data| match method_data {+ domain::PaymentMethodData::CardToken(card) => {+ Some(PaymentMethodUpdateData::Card(CardDetailUpdate {+ card_holder_name: card.card_holder_name.clone(),+ nick_name: card.card_holder_name.clone(),+ card_cvc: None,+ }))+ }+ _ => None,+ });- // #5 - Execute the modular payment-method update call if there is something to be updated- if payload.payment_method_data.is_some()- || payload.connector_token_details.is_some()- || payload.network_transaction_id.is_some()- {- call_modular_payment_method_update(- state,- &payment_data.payment_attempt.merchant_id,- &payment_data.payment_attempt.profile_id,- &payment_method_id,- payload,- )- .await?;- payment_data.payment_attempt.payment_method_id = Some(payment_method_id);+ // #4 - Build connector token details only when a mandate reference is available.++ let payload = UpdatePaymentMethodV1Payload {+ payment_method_data,+ connector_token_details,+ network_transaction_id: network_transaction_id.map(masking::Secret::new),+ };++ // #5 - Execute the modular payment-method update call if there is something to be updated+ if payload.payment_method_data.is_some()+ || payload.connector_token_details.is_some()+ || payload.network_transaction_id.is_some()+ {+ match call_modular_payment_method_update(+ state,+ &payment_data.payment_attempt.merchant_id,+ &payment_data.payment_attempt.profile_id,+ &payment_method_id,+ payload,+ )+ .await+ {+ Ok(_) => {+ logger::info!("Successfully called modular payment method update");+ }+ Err(err) => {+ logger::error!("Failed to call modular payment method update: {}", err);+ }+ };+ payment_data.payment_attempt.payment_method_id =+ Some(payment_method_id.clone());+ } else {+ logger::info!("No updates found for modular payment method update call");+ }+ }+ _ => {+ logger::info!("Payment method is not eligible for modular update");+ }}}