Skip to content

Commit

Permalink
refactor(blocklist): separate error generic-error for blocked payments
Browse files Browse the repository at this point in the history
  • Loading branch information
prajjwalkumar17 committed Jan 18, 2024
1 parent 243a76d commit f0438d6
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 6 deletions.
1 change: 1 addition & 0 deletions crates/api_models/src/errors/actix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ impl actix_web::ResponseError for ApiErrorResponse {
Self::MethodNotAllowed(_) => StatusCode::METHOD_NOT_ALLOWED,
Self::NotFound(_) => StatusCode::NOT_FOUND,
Self::BadRequest(_) => StatusCode::BAD_REQUEST,
Self::GenericError(_) => StatusCode::OK,
}
}

Expand Down
4 changes: 4 additions & 0 deletions crates/api_models/src/errors/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ pub enum ApiErrorResponse {
NotFound(ApiError),
MethodNotAllowed(ApiError),
BadRequest(ApiError),
GenericError(ApiError),
}

impl ::core::fmt::Display for ApiErrorResponse {
Expand Down Expand Up @@ -122,6 +123,7 @@ impl ApiErrorResponse {
| Self::NotFound(i)
| Self::MethodNotAllowed(i)
| Self::BadRequest(i)
| Self::GenericError(i)
| Self::ConnectorError(i, _) => i,
}
}
Expand All @@ -139,6 +141,7 @@ impl ApiErrorResponse {
| Self::NotFound(i)
| Self::MethodNotAllowed(i)
| Self::BadRequest(i)
| Self::GenericError(i)
| Self::ConnectorError(i, _) => i,
}
}
Expand All @@ -156,6 +159,7 @@ impl ApiErrorResponse {
| Self::NotFound(_)
| Self::BadRequest(_) => "invalid_request",
Self::InternalServerError(_) => "api",
Self::GenericError(_) => "blocked",
Self::ConnectorError(_, _) => "connector",
}
}
Expand Down
23 changes: 22 additions & 1 deletion crates/router/src/compatibility/stripe/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,14 @@ pub enum StripeErrorCode {
connector: String,
status_code: u16,
},

#[error(error_type = StripeErrorType::CardError, code = "", message = "{code}: {message}")]
PaymentBlockedError {
code: u16,
message: String,
status: String,
reason: String,
},

#[error(error_type = StripeErrorType::HyperswitchError, code = "", message = "The connector provided in the request is incorrect or not available")]
IncorrectConnectorNameGiven,
Expand Down Expand Up @@ -520,7 +528,17 @@ impl From<errors::ApiErrorResponse> for StripeErrorCode {
connector_name,
},
errors::ApiErrorResponse::DuplicatePaymentMethod => Self::DuplicatePaymentMethod,
errors::ApiErrorResponse::PaymentBlocked => Self::PaymentFailed,
errors::ApiErrorResponse::PaymentBlockedError {
code,
message,
status,
reason,
} => Self::PaymentBlockedError {
code,
message,
status,
reason,
},
errors::ApiErrorResponse::ClientSecretInvalid => Self::PaymentIntentInvalidParameter {
param: "client_secret".to_owned(),
},
Expand Down Expand Up @@ -679,6 +697,9 @@ impl actix_web::ResponseError for StripeErrorCode {
Self::ExternalConnectorError { status_code, .. } => {
StatusCode::from_u16(*status_code).unwrap_or(StatusCode::INTERNAL_SERVER_ERROR)
}
Self::PaymentBlockedError { code, .. } => {
StatusCode::from_u16(*code).unwrap_or(StatusCode::OK)
}
Self::LockTimeout => StatusCode::LOCKED,
}
}
Expand Down
9 changes: 7 additions & 2 deletions crates/router/src/core/errors/api_error_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,13 @@ pub enum ApiErrorResponse {
PaymentNotSucceeded,
#[error(error_type = ErrorType::ValidationError, code = "HE_03", message = "The specified merchant connector account is disabled")]
MerchantConnectorAccountDisabled,
#[error(error_type = ErrorType::ValidationError, code = "HE_03", message = "The specified payment is blocked")]
PaymentBlocked,
#[error(error_type = ErrorType::ValidationError, code = "HE_03", message = "{code}: {message}")]
PaymentBlockedError{
code: u16,
message: String,
status: String,
reason: String,
},
#[error(error_type= ErrorType::ObjectNotFound, code = "HE_04", message = "Successful payment not found for the given payment id")]
SuccessfulPaymentNotFound,
#[error(error_type = ErrorType::ObjectNotFound, code = "HE_04", message = "The connector provided in the request is incorrect or not available")]
Expand Down
6 changes: 5 additions & 1 deletion crates/router/src/core/errors/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,11 @@ impl ErrorSwitch<api_models::errors::types::ApiErrorResponse> for ApiErrorRespon
AER::BadRequest(ApiError::new("HE", 3, "Mandate Validation Failed", Some(Extra { reason: Some(reason.clone()), ..Default::default() })))
}
Self::PaymentNotSucceeded => AER::BadRequest(ApiError::new("HE", 3, "The payment has not succeeded yet. Please pass a successful payment to initiate refund", None)),
Self::PaymentBlocked => AER::BadRequest(ApiError::new("HE", 3, "The payment is blocked", None)),
Self::PaymentBlockedError {
message,
reason,
..
} => AER::GenericError(ApiError::new("HE", 3, format!("{message}"), Some(Extra { reason: Some(reason.clone()), ..Default::default() }))),
Self::SuccessfulPaymentNotFound => {
AER::NotFound(ApiError::new("HE", 4, "Successful payment not found for the given payment id", None))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,7 @@ impl<F: Clone, Ctx: PaymentMethodRetrieve>

// Block the payment if the entry was present in the Blocklist
if is_pm_blocklisted {
return Err(errors::ApiErrorResponse::PaymentBlocked.into());
return Err(errors::ApiErrorResponse::PaymentBlockedError { code: 200, message: "The specified Payment failed".to_string(), status: "Failed".to_string(), reason: "Blocked".to_string() }.into());
}

Ok((Box::new(self), payment_data))
Expand Down
2 changes: 1 addition & 1 deletion crates/router/src/db/blocklist_lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl BlocklistLookupInterface for Store {
merchant_id: &str,
fingerprint: &str,
) -> CustomResult<storage::BlocklistLookup, errors::StorageError> {
let conn = connection::pg_connection_write(self).await?;
let conn = connection::pg_connection_read(self).await?;
storage::BlocklistLookup::find_by_merchant_id_fingerprint(&conn, merchant_id, fingerprint)
.await
.map_err(Into::into)
Expand Down

0 comments on commit f0438d6

Please sign in to comment.