Skip to content

Commit

Permalink
make an api to toggle the extended card bin
Browse files Browse the repository at this point in the history
  • Loading branch information
Aprabhat19 committed Apr 30, 2024
1 parent 6fa1bf4 commit e09fb2d
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 5 deletions.
6 changes: 6 additions & 0 deletions crates/api_models/src/admin.rs
Expand Up @@ -1095,3 +1095,9 @@ pub struct ExtendedCardInfoChoice {
}

impl common_utils::events::ApiEventMetric for ExtendedCardInfoChoice {}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct ExtendedCardBinConfig {
pub enabled: bool,
}
impl common_utils::events::ApiEventMetric for ExtendedCardBinConfig {}
15 changes: 15 additions & 0 deletions crates/diesel_models/src/business_profile.rs
Expand Up @@ -36,6 +36,7 @@ pub struct BusinessProfile {
pub session_expiry: Option<i64>,
pub authentication_connector_details: Option<serde_json::Value>,
pub is_extended_card_info_enabled: Option<bool>,
pub enable_extended_card_bin: Option<bool>,
}

#[derive(Clone, Debug, Insertable, router_derive::DebugAsDisplay)]
Expand Down Expand Up @@ -63,6 +64,7 @@ pub struct BusinessProfileNew {
pub session_expiry: Option<i64>,
pub authentication_connector_details: Option<serde_json::Value>,
pub is_extended_card_info_enabled: Option<bool>,
pub enable_extended_card_bin: Option<bool>,
}

#[derive(Clone, Debug, Default, AsChangeset, router_derive::DebugAsDisplay)]
Expand All @@ -87,6 +89,7 @@ pub struct BusinessProfileUpdateInternal {
pub session_expiry: Option<i64>,
pub authentication_connector_details: Option<serde_json::Value>,
pub is_extended_card_info_enabled: Option<bool>,
pub enable_extended_card_bin: Option<bool>,
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
Expand All @@ -113,6 +116,9 @@ pub enum BusinessProfileUpdate {
ExtendedCardInfoUpdate {
is_extended_card_info_enabled: Option<bool>,
},
ExtendedCardBinUpdate {
enable_extended_card_bin: Option<bool>,
},
}

impl From<BusinessProfileUpdate> for BusinessProfileUpdateInternal {
Expand Down Expand Up @@ -162,6 +168,12 @@ impl From<BusinessProfileUpdate> for BusinessProfileUpdateInternal {
is_extended_card_info_enabled,
..Default::default()
},
BusinessProfileUpdate::ExtendedCardBinUpdate {
enable_extended_card_bin,
} => Self {
enable_extended_card_bin,
..Default::default()
},
}
}
}
Expand Down Expand Up @@ -190,6 +202,7 @@ impl From<BusinessProfileNew> for BusinessProfile {
session_expiry: new.session_expiry,
authentication_connector_details: new.authentication_connector_details,
is_extended_card_info_enabled: new.is_extended_card_info_enabled,
enable_extended_card_bin: new.enable_extended_card_bin,
}
}
}
Expand All @@ -215,6 +228,7 @@ impl BusinessProfileUpdate {
session_expiry,
authentication_connector_details,
is_extended_card_info_enabled,
enable_extended_card_bin,
} = self.into();
BusinessProfile {
profile_name: profile_name.unwrap_or(source.profile_name),
Expand All @@ -237,6 +251,7 @@ impl BusinessProfileUpdate {
session_expiry,
authentication_connector_details,
is_extended_card_info_enabled,
enable_extended_card_bin,
..source
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/diesel_models/src/schema.rs
Expand Up @@ -194,6 +194,7 @@ diesel::table! {
session_expiry -> Nullable<Int8>,
authentication_connector_details -> Nullable<Jsonb>,
is_extended_card_info_enabled -> Nullable<Bool>,
enable_extended_card_bin -> Nullable<Bool>,
}
}

Expand Down
56 changes: 56 additions & 0 deletions crates/router/src/core/admin.rs
Expand Up @@ -2035,3 +2035,59 @@ pub fn validate_status_and_disabled(

Ok((connector_status, disabled))
}

pub async fn extended_card_bin_toggle(
state: AppState,
profile_id: &str,
merchant_id: &str,
request: admin_types::ExtendedCardBinConfig,
) -> RouterResponse<admin_types::ExtendedCardBinConfig> {
let db = state.store.as_ref();
let business_profile = db
.find_business_profile_by_profile_id(profile_id)
.await
.to_not_found_response(errors::ApiErrorResponse::BusinessProfileNotFound {
id: profile_id.to_string(),
})?;

utils::when(business_profile.merchant_id != merchant_id, || {
Err(errors::ApiErrorResponse::AccessForbidden {
resource: format!(
"{} as it is not valid for the merchant_id : {}",
profile_id, merchant_id
),
})
})?;

utils::when(
business_profile
.enable_extended_card_bin
.is_some_and(|existing_config| existing_config == request.enabled),
|| {
Err(errors::ApiErrorResponse::PreconditionFailed {
message: format!(
"The Extended Card Bin Config has already been set to {}",
request.enabled
),
})
},
)?;

if business_profile
.enable_extended_card_bin
.is_some_and(|existing_config| existing_config != request.enabled)
{
let business_profile_update =
storage::business_profile::BusinessProfileUpdate::ExtendedCardBinUpdate {
enable_extended_card_bin: Some(request.enabled),
};
db.update_business_profile_by_profile_id(business_profile, business_profile_update)
.await
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable(
"Failed to update the extended card bin config in business profile",
)?;
}

Ok(service_api::ApplicationResponse::Json(request))
}
15 changes: 11 additions & 4 deletions crates/router/src/core/payments/helpers.rs
Expand Up @@ -3631,12 +3631,19 @@ pub async fn get_additional_payment_data(
match pm_data {
api_models::payments::PaymentMethodData::Card(card_data) => {
let card_isin = Some(card_data.card_number.clone().get_card_isin());
let enable_extended_bin =db
.find_config_by_key_unwrap_or(profile_id, Some("false".to_string()))
.await.map_err(|err| services::logger::error!(message="Failed to fetch the config", extended_card_bin_error=?err)).ok();
let business_profile = db
.find_business_profile_by_profile_id(profile_id)
.await
.map_err(|err| {
services::logger::error!(?err, "Failed to fetch the business profile")
})
.ok();

let enable_extended_bin =
business_profile.and_then(|profile| profile.enable_extended_card_bin);

let card_extended_bin = match enable_extended_bin {
Some(config) if config.config == "true" => {
Some(config) if config => {
Some(card_data.card_number.clone().get_card_extended_bin())
}
_ => None,
Expand Down
21 changes: 21 additions & 0 deletions crates/router/src/routes/admin.rs
Expand Up @@ -634,3 +634,24 @@ pub async fn toggle_extended_card_info(
))
.await
}

#[instrument(skip_all, fields(flow= ?Flow::ToggleExtendedCardBin))]
pub async fn toggle_extended_card_bin(
state: web::Data<AppState>,
req: HttpRequest,
path: web::Path<(String, String)>,
json_payload: web::Json<api_models::admin::ExtendedCardBinConfig>,
) -> HttpResponse {
let flow = Flow::ToggleExtendedCardBin;
let (merchant_id, profile_id) = path.into_inner();
Box::pin(api::server_wrap(
flow,
state,
&req,
json_payload.into_inner(),
|state, _, payload, _| extended_card_bin_toggle(state, &profile_id, &merchant_id, payload),
&auth::AdminApiAuth,
api_locking::LockAction::NotApplicable,
))
.await
}
4 changes: 4 additions & 0 deletions crates/router/src/routes/app.rs
Expand Up @@ -1114,6 +1114,10 @@ impl BusinessProfile {
.service(
web::resource("/toggle_extended_card_info")
.route(web::post().to(toggle_extended_card_info)),
)
.service(
web::resource("/toggle_extended_card_bin")
.route(web::post().to(toggle_extended_card_bin)),
),
)
}
Expand Down
3 changes: 2 additions & 1 deletion crates/router/src/routes/lock_utils.rs
Expand Up @@ -167,7 +167,8 @@ impl From<Flow> for ApiIdentifier {
| Flow::BusinessProfileRetrieve
| Flow::BusinessProfileDelete
| Flow::BusinessProfileList
| Flow::ToggleExtendedCardInfo => Self::Business,
| Flow::ToggleExtendedCardInfo
| Flow::ToggleExtendedCardBin => Self::Business,

Flow::PaymentLinkRetrieve
| Flow::PaymentLinkInitiate
Expand Down
1 change: 1 addition & 0 deletions crates/router/src/types/api/admin.rs
Expand Up @@ -176,6 +176,7 @@ impl ForeignTryFrom<(domain::MerchantAccount, BusinessProfileCreate)>
field_name: "authentication_connector_details",
})?,
is_extended_card_info_enabled: None,
enable_extended_card_bin: None,
})
}
}
2 changes: 2 additions & 0 deletions crates/router_env/src/logger/types.rs
Expand Up @@ -408,6 +408,8 @@ pub enum Flow {
RetrievePollStatus,
/// Toggles the extended card info feature in profile level
ToggleExtendedCardInfo,
/// Toggles the extended card bin field at profile level
ToggleExtendedCardBin,
}

///
Expand Down

0 comments on commit e09fb2d

Please sign in to comment.