Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(connector): [BOA/CYBERSOURCE] Update error handling #3157

Merged
merged 3 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 44 additions & 31 deletions crates/router/src/connector/bankofamerica.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,37 +203,50 @@ impl ConnectorCommon for Bankofamerica {
} else {
consts::NO_ERROR_MESSAGE
};

let (code, message) = match response.error_information {
Some(ref error_info) => (error_info.reason.clone(), error_info.message.clone()),
None => (
response
.reason
.map_or(consts::NO_ERROR_CODE.to_string(), |reason| {
reason.to_string()
}),
response
.message
.map_or(error_message.to_string(), |message| message),
),
};
let connector_reason = match response.details {
Some(details) => details
.iter()
.map(|det| format!("{} : {}", det.field, det.reason))
.collect::<Vec<_>>()
.join(", "),
None => message.clone(),
};

Ok(ErrorResponse {
status_code: res.status_code,
code,
message,
reason: Some(connector_reason),
attempt_status: None,
connector_transaction_id: None,
})
match response {
transformers::BankOfAmericaErrorResponse::StandardError(response) => {
let (code, message) = match response.error_information {
Some(ref error_info) => (error_info.reason.clone(), error_info.message.clone()),
None => (
response
.reason
.map_or(consts::NO_ERROR_CODE.to_string(), |reason| {
reason.to_string()
}),
response
.message
.map_or(error_message.to_string(), |message| message),
),
};
let connector_reason = match response.details {
Some(details) => details
.iter()
.map(|det| format!("{} : {}", det.field, det.reason))
.collect::<Vec<_>>()
.join(", "),
None => message.clone(),
};

Ok(ErrorResponse {
status_code: res.status_code,
code,
message,
reason: Some(connector_reason),
attempt_status: None,
connector_transaction_id: None,
})
}
transformers::BankOfAmericaErrorResponse::AuthenticationError(response) => {
Ok(ErrorResponse {
status_code: res.status_code,
code: consts::NO_ERROR_CODE.to_string(),
message: response.response.rmsg,
reason: None,
attempt_status: None,
connector_transaction_id: None,
})
}
}
}
}

Expand Down
37 changes: 17 additions & 20 deletions crates/router/src/connector/bankofamerica/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1016,32 +1016,24 @@ impl TryFrom<types::RefundsResponseRouterData<api::RSync, BankOfAmericaRsyncResp

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BankOfAmericaErrorResponse {
pub struct BankOfAmericaStandardErrorResponse {
pub error_information: Option<ErrorInformation>,
pub status: Option<String>,
pub message: Option<String>,
pub reason: Option<Reason>,
pub reason: Option<String>,
pub details: Option<Vec<Details>>,
}

#[derive(Debug, Deserialize, strum::Display)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum Reason {
MissingField,
InvalidData,
DuplicateRequest,
InvalidCard,
AuthAlreadyReversed,
CardTypeNotAccepted,
InvalidMerchantConfiguration,
ProcessorUnavailable,
InvalidAmount,
InvalidCardType,
InvalidPaymentId,
NotSupported,
SystemError,
ServerTimeout,
ServiceTimeout,
#[derive(Debug, Deserialize)]
pub struct BankOfAmericaAuthenticationErrorResponse {
pub response: AuthenticationErrorInformation,
}

#[derive(Debug, Deserialize)]
#[serde(untagged)]
pub enum BankOfAmericaErrorResponse {
StandardError(BankOfAmericaStandardErrorResponse),
AuthenticationError(BankOfAmericaAuthenticationErrorResponse),
}

#[derive(Debug, Deserialize, Clone)]
Expand All @@ -1056,3 +1048,8 @@ pub struct ErrorInformation {
pub message: String,
pub reason: String,
}

#[derive(Debug, Default, Deserialize)]
pub struct AuthenticationErrorInformation {
pub rmsg: String,
}
74 changes: 45 additions & 29 deletions crates/router/src/connector/cybersource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,44 +109,60 @@ impl ConnectorCommon for Cybersource {
&self,
res: types::Response,
) -> CustomResult<types::ErrorResponse, errors::ConnectorError> {
let response: cybersource::ErrorResponse = res
let response: cybersource::CybersourceErrorResponse = res
.response
.parse_struct("Cybersource ErrorResponse")
.change_context(errors::ConnectorError::ResponseDeserializationFailed)?;
let details = response.details.unwrap_or_default();
let connector_reason = details
.iter()
.map(|det| format!("{} : {}", det.field, det.reason))
.collect::<Vec<_>>()
.join(", ");

let error_message = if res.status_code == 401 {
consts::CONNECTOR_UNAUTHORIZED_ERROR
} else {
consts::NO_ERROR_MESSAGE
};

let (code, message) = match response.error_information {
Some(ref error_info) => (error_info.reason.clone(), error_info.message.clone()),
None => (
response
.reason
.map_or(consts::NO_ERROR_CODE.to_string(), |reason| {
reason.to_string()
}),
response
.message
.map_or(error_message.to_string(), |message| message),
),
};
Ok(types::ErrorResponse {
status_code: res.status_code,
code,
message,
reason: Some(connector_reason),
attempt_status: None,
connector_transaction_id: None,
})
match response {
transformers::CybersourceErrorResponse::StandardError(response) => {
let (code, message) = match response.error_information {
Some(ref error_info) => (error_info.reason.clone(), error_info.message.clone()),
None => (
response
.reason
.map_or(consts::NO_ERROR_CODE.to_string(), |reason| {
reason.to_string()
}),
response
.message
.map_or(error_message.to_string(), |message| message),
),
};
let connector_reason = match response.details {
Some(details) => details
.iter()
.map(|det| format!("{} : {}", det.field, det.reason))
.collect::<Vec<_>>()
.join(", "),
None => message.clone(),
};

Ok(types::ErrorResponse {
status_code: res.status_code,
code,
message,
reason: Some(connector_reason),
attempt_status: None,
connector_transaction_id: None,
})
}
transformers::CybersourceErrorResponse::AuthenticationError(response) => {
Ok(types::ErrorResponse {
status_code: res.status_code,
code: consts::NO_ERROR_CODE.to_string(),
message: response.response.rmsg,
reason: None,
attempt_status: None,
connector_transaction_id: None,
})
}
}
}
}

Expand Down
83 changes: 40 additions & 43 deletions crates/router/src/connector/cybersource/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1286,49 +1286,6 @@ impl<F>
}
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ErrorResponse {
pub error_information: Option<ErrorInformation>,
pub status: Option<String>,
pub message: Option<String>,
pub reason: Option<Reason>,
pub details: Option<Vec<Details>>,
}

#[derive(Debug, Deserialize, strum::Display)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum Reason {
MissingField,
InvalidData,
DuplicateRequest,
InvalidCard,
AuthAlreadyReversed,
CardTypeNotAccepted,
InvalidMerchantConfiguration,
ProcessorUnavailable,
InvalidAmount,
InvalidCardType,
InvalidPaymentId,
NotSupported,
SystemError,
ServerTimeout,
ServiceTimeout,
}

#[derive(Debug, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Details {
pub field: String,
pub reason: String,
}

#[derive(Debug, Deserialize)]
pub struct ErrorInformation {
pub message: String,
pub reason: String,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CybersourceRefundRequest {
Expand Down Expand Up @@ -1428,3 +1385,43 @@ impl TryFrom<types::RefundsResponseRouterData<api::RSync, CybersourceRsyncRespon
})
}
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CybersourceStandardErrorResponse {
pub error_information: Option<ErrorInformation>,
pub status: Option<String>,
pub message: Option<String>,
pub reason: Option<String>,
pub details: Option<Vec<Details>>,
}

#[derive(Debug, Deserialize)]
pub struct CybersourceAuthenticationErrorResponse {
pub response: AuthenticationErrorInformation,
}

#[derive(Debug, Deserialize)]
#[serde(untagged)]
pub enum CybersourceErrorResponse {
StandardError(CybersourceStandardErrorResponse),
AuthenticationError(CybersourceAuthenticationErrorResponse),
}

#[derive(Debug, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Details {
pub field: String,
pub reason: String,
}

#[derive(Debug, Default, Deserialize)]
pub struct ErrorInformation {
pub message: String,
pub reason: String,
}

#[derive(Debug, Default, Deserialize)]
pub struct AuthenticationErrorInformation {
pub rmsg: String,
}
Loading