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(connector): [Stripe] capture error message and error code for failed payment, capture, void and refunds (#3237)
AkshayaFoiger2c52b377e05b
diff --git a/crates/router/src/connector/stripe/transformers.rs b/crates/router/src/connector/stripe/transformers.rsindex 8d397d0ebb..8061234eef 100644--- a/crates/router/src/connector/stripe/transformers.rs+++ b/crates/router/src/connector/stripe/transformers.rs@@ -20,6 +20,7 @@ use crate::{connector::utils::{self as connector_util, ApplePay, ApplePayDecrypt, PaymentsPreProcessingData, RouterData,},+ consts,core::errors,services,types::{@@ -226,6 +227,8 @@ pub struct ChargesResponse {pub currency: String,pub status: StripePaymentStatus,pub source: StripeSourceResponse,+ pub failure_code: Option<String>,+ pub failure_message: Option<String>,}#[derive(Debug, Eq, PartialEq, Serialize)]@@ -2266,6 +2269,7 @@ pub struct SetupIntentResponse {pub next_action: Option<StripeNextActionResponse>,pub payment_method_options: Option<StripePaymentMethodOptions>,pub latest_attempt: Option<LatestAttempt>,+ pub last_setup_error: Option<ErrorDetails>,}impl ForeignFrom<(Option<StripePaymentMethodOptions>, String)> for types::MandateReference {@@ -2328,13 +2332,16 @@ impl<F, T>let connector_metadata =get_connector_metadata(item.response.next_action.as_ref(), item.response.amount)?;- Ok(Self {- status: enums::AttemptStatus::from(item.response.status),- // client_secret: Some(item.response.client_secret.clone().as_str()),- // description: item.response.description.map(|x| x.as_str()),- // statement_descriptor_suffix: item.response.statement_descriptor_suffix.map(|x| x.as_str()),- // three_ds_form,- response: Ok(types::PaymentsResponseData::TransactionResponse {+ let status = enums::AttemptStatus::from(item.response.status);++ let response = if connector_util::is_payment_failure(status) {+ types::PaymentsResponseData::try_from((+ &item.response.last_payment_error,+ item.http_code,+ item.response.id.clone(),+ ))+ } else {+ Ok(types::PaymentsResponseData::TransactionResponse {resource_id: types::ResponseId::ConnectorTransactionId(item.response.id.clone()),redirection_data,mandate_reference,@@ -2342,7 +2349,16 @@ impl<F, T>network_txn_id,connector_response_reference_id: Some(item.response.id),incremental_authorization_allowed: None,- }),+ })+ };++ Ok(Self {+ status,+ // client_secret: Some(item.response.client_secret.clone().as_str()),+ // description: item.response.description.map(|x| x.as_str()),+ // statement_descriptor_suffix: item.response.statement_descriptor_suffix.map(|x| x.as_str()),+ // three_ds_form,+ response,amount_captured: item.response.amount_received,..item.data})@@ -2469,32 +2485,19 @@ impl<F, T>},))});- let error_res =- item.response- .last_payment_error- .as_ref()- .map(|error| types::ErrorResponse {- code: error.code.to_owned(),- message: error.code.to_owned(),- reason: error- .decline_code- .clone()- .map(|decline_code| {- format!(- "message - {}, decline_code - {}",- error.message, decline_code- )- })- .or(Some(error.message.clone())),- status_code: item.http_code,- attempt_status: None,- connector_transaction_id: None,- });let connector_metadata =get_connector_metadata(item.response.next_action.as_ref(), item.response.amount)?;- let response = error_res.map_or(+ let status = enums::AttemptStatus::from(item.response.status.to_owned());++ let response = if connector_util::is_payment_failure(status) {+ types::PaymentsResponseData::try_from((+ &item.response.payment_intent_fields.last_payment_error,+ item.http_code,+ item.response.id.clone(),+ ))+ } else {Ok(types::PaymentsResponseData::TransactionResponse {resource_id: types::ResponseId::ConnectorTransactionId(item.response.id.clone()),redirection_data,@@ -2503,9 +2506,8 @@ impl<F, T>network_txn_id: None,connector_response_reference_id: Some(item.response.id.clone()),incremental_authorization_allowed: None,- }),- Err,- );+ })+ };Ok(Self {status: enums::AttemptStatus::from(item.response.status.to_owned()),@@ -2534,10 +2536,15 @@ impl<F, T>let mandate_reference = item.response.payment_method.map(|pm| {types::MandateReference::foreign_from((item.response.payment_method_options, pm))});-- Ok(Self {- status: enums::AttemptStatus::from(item.response.status),- response: Ok(types::PaymentsResponseData::TransactionResponse {+ let status = enums::AttemptStatus::from(item.response.status);+ let response = if connector_util::is_payment_failure(status) {+ types::PaymentsResponseData::try_from((+ &item.response.last_setup_error,+ item.http_code,+ item.response.id.clone(),+ ))+ } else {+ Ok(types::PaymentsResponseData::TransactionResponse {resource_id: types::ResponseId::ConnectorTransactionId(item.response.id.clone()),redirection_data,mandate_reference,@@ -2545,7 +2552,12 @@ impl<F, T>network_txn_id: Option::foreign_from(item.response.latest_attempt),connector_response_reference_id: Some(item.response.id),incremental_authorization_allowed: None,- }),+ })+ };++ Ok(Self {+ status,+ response,..item.data})}@@ -2755,6 +2767,7 @@ pub struct RefundResponse {pub metadata: StripeMetadata,pub payment_intent: String,pub status: RefundStatus,+ pub failure_reason: Option<String>,}impl TryFrom<types::RefundsResponseRouterData<api::Execute, RefundResponse>>@@ -2764,11 +2777,29 @@ impl TryFrom<types::RefundsResponseRouterData<api::Execute, RefundResponse>>fn try_from(item: types::RefundsResponseRouterData<api::Execute, RefundResponse>,) -> Result<Self, Self::Error> {- Ok(Self {- response: Ok(types::RefundsResponseData {+ let refund_status = enums::RefundStatus::from(item.response.status);+ let response = if connector_util::is_refund_failure(refund_status) {+ Err(types::ErrorResponse {+ code: consts::NO_ERROR_CODE.to_string(),+ message: item+ .response+ .failure_reason+ .clone()+ .unwrap_or_else(|| consts::NO_ERROR_MESSAGE.to_string()),+ reason: item.response.failure_reason,+ status_code: item.http_code,+ attempt_status: None,+ connector_transaction_id: Some(item.response.id),+ })+ } else {+ Ok(types::RefundsResponseData {connector_refund_id: item.response.id,- refund_status: enums::RefundStatus::from(item.response.status),- }),+ refund_status,+ })+ };++ Ok(Self {+ response,..item.data})}@@ -2781,17 +2812,35 @@ impl TryFrom<types::RefundsResponseRouterData<api::RSync, RefundResponse>>fn try_from(item: types::RefundsResponseRouterData<api::RSync, RefundResponse>,) -> Result<Self, Self::Error> {- Ok(Self {- response: Ok(types::RefundsResponseData {+ let refund_status = enums::RefundStatus::from(item.response.status);+ let response = if connector_util::is_refund_failure(refund_status) {+ Err(types::ErrorResponse {+ code: consts::NO_ERROR_CODE.to_string(),+ message: item+ .response+ .failure_reason+ .clone()+ .unwrap_or_else(|| consts::NO_ERROR_MESSAGE.to_string()),+ reason: item.response.failure_reason,+ status_code: item.http_code,+ attempt_status: None,+ connector_transaction_id: Some(item.response.id),+ })+ } else {+ Ok(types::RefundsResponseData {connector_refund_id: item.response.id,- refund_status: enums::RefundStatus::from(item.response.status),- }),+ refund_status,+ })+ };++ Ok(Self {+ response,..item.data})}}-#[derive(Debug, Default, Eq, PartialEq, Deserialize, Serialize)]+#[derive(Clone, Debug, Default, Eq, PartialEq, Deserialize, Serialize)]pub struct ErrorDetails {pub code: Option<String>,#[serde(rename = "type")]@@ -2802,7 +2851,7 @@ pub struct ErrorDetails {pub payment_intent: Option<PaymentIntentErrorResponse>,}-#[derive(Debug, Default, Eq, PartialEq, Deserialize, Serialize)]+#[derive(Clone, Debug, Default, Eq, PartialEq, Deserialize, Serialize)]pub struct PaymentIntentErrorResponse {pub id: String,}@@ -3085,9 +3134,25 @@ impl<F, T> TryFrom<types::ResponseRouterData<F, ChargesResponse, T, types::Payme&connector_source_response.source,).change_context(errors::ConnectorError::ResponseHandlingFailed)?;- Ok(Self {- status: enums::AttemptStatus::from(item.response.status),- response: Ok(types::PaymentsResponseData::TransactionResponse {+ let status = enums::AttemptStatus::from(item.response.status);+ let response = if connector_util::is_payment_failure(status) {+ Err(types::ErrorResponse {+ code: item+ .response+ .failure_code+ .unwrap_or_else(|| crate::consts::NO_ERROR_CODE.to_string()),+ message: item+ .response+ .failure_message+ .clone()+ .unwrap_or_else(|| crate::consts::NO_ERROR_MESSAGE.to_string()),+ reason: item.response.failure_message,+ status_code: item.http_code,+ attempt_status: Some(status),+ connector_transaction_id: Some(item.response.id),+ })+ } else {+ Ok(types::PaymentsResponseData::TransactionResponse {resource_id: types::ResponseId::ConnectorTransactionId(item.response.id.clone()),redirection_data: None,mandate_reference: None,@@ -3095,7 +3160,12 @@ impl<F, T> TryFrom<types::ResponseRouterData<F, ChargesResponse, T, types::Paymenetwork_txn_id: None,connector_response_reference_id: Some(item.response.id),incremental_authorization_allowed: None,- }),+ })+ };++ Ok(Self {+ status,+ response,..item.data})}@@ -3674,6 +3744,49 @@ fn get_transaction_metadata(meta_data}+impl TryFrom<(&Option<ErrorDetails>, u16, String)> for types::PaymentsResponseData {+ type Error = types::ErrorResponse;+ fn try_from(+ (response, http_code, response_id): (&Option<ErrorDetails>, u16, String),+ ) -> Result<Self, Self::Error> {+ let (code, error_message) = match response {+ Some(error_details) => (+ error_details+ .code+ .to_owned()+ .unwrap_or_else(|| consts::NO_ERROR_CODE.to_string()),+ error_details+ .message+ .to_owned()+ .unwrap_or_else(|| consts::NO_ERROR_MESSAGE.to_string()),+ ),+ None => (+ consts::NO_ERROR_CODE.to_string(),+ consts::NO_ERROR_MESSAGE.to_string(),+ ),+ };++ Err(types::ErrorResponse {+ code,+ message: error_message.clone(),+ reason: response.clone().and_then(|res| {+ res.decline_code+ .clone()+ .map(|decline_code| {+ format!(+ "message - {}, decline_code - {}",+ error_message, decline_code+ )+ })+ .or(Some(error_message.clone()))+ }),+ status_code: http_code,+ attempt_status: None,+ connector_transaction_id: Some(response_id),+ })+ }+}+#[cfg(test)]mod test_validate_shipping_address_against_payment_method {#![allow(clippy::unwrap_used)]