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

feat(mca): Add new auth_type and a status field for mca #2883

Merged
merged 13 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from 10 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
9 changes: 9 additions & 0 deletions crates/api_models/src/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,9 @@ pub struct MerchantConnectorCreate {
pub profile_id: Option<String>,

pub pm_auth_config: Option<serde_json::Value>,

#[schema(value_type = ConnectorStatus, example = "inactive")]
pub status: Option<api_enums::ConnectorStatus>,
}

#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
Expand Down Expand Up @@ -709,6 +712,9 @@ pub struct MerchantConnectorResponse {
pub applepay_verified_domains: Option<Vec<String>>,

pub pm_auth_config: Option<serde_json::Value>,

#[schema(value_type = ConnectorStatus, example = "inactive")]
pub status: api_enums::ConnectorStatus,
}

/// Create a new Merchant Connector for the merchant account. The connector could be a payment processor / facilitator / acquirer or specialized services like Fraud / Accounting etc."
Expand Down Expand Up @@ -783,6 +789,9 @@ pub struct MerchantConnectorUpdate {
pub connector_webhook_details: Option<MerchantConnectorWebhookDetails>,

pub pm_auth_config: Option<serde_json::Value>,

#[schema(value_type = ConnectorStatus, example = "inactive")]
pub status: Option<api_enums::ConnectorStatus>,
}

///Details of FrmConfigs are mentioned here... it should be passed in payment connector create api call, and stored in merchant_connector_table
Expand Down
22 changes: 22 additions & 0 deletions crates/common_enums/src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1854,3 +1854,25 @@ pub enum ApplePayFlow {
Simplified,
Manual,
}

#[derive(
Clone,
Copy,
Debug,
Eq,
PartialEq,
strum::Display,
strum::EnumString,
serde::Deserialize,
serde::Serialize,
ToSchema,
Default,
)]
#[router_derive::diesel_enum(storage_type = "pg_enum")]
#[strum(serialize_all = "snake_case")]
#[serde(rename_all = "snake_case")]
pub enum ConnectorStatus {
#[default]
Inactive,
Active,
}
7 changes: 4 additions & 3 deletions crates/diesel_models/src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ pub mod diesel_exports {
pub use super::{
DbAttemptStatus as AttemptStatus, DbAuthenticationType as AuthenticationType,
DbCaptureMethod as CaptureMethod, DbCaptureStatus as CaptureStatus,
DbConnectorType as ConnectorType, DbCountryAlpha2 as CountryAlpha2, DbCurrency as Currency,
DbDisputeStage as DisputeStage, DbDisputeStatus as DisputeStatus,
DbEventClass as EventClass, DbEventObjectType as EventObjectType, DbEventType as EventType,
DbConnectorStatus as ConnectorStatus, DbConnectorType as ConnectorType,
DbCountryAlpha2 as CountryAlpha2, DbCurrency as Currency, DbDisputeStage as DisputeStage,
DbDisputeStatus as DisputeStatus, DbEventClass as EventClass,
DbEventObjectType as EventObjectType, DbEventType as EventType,
DbFraudCheckStatus as FraudCheckStatus, DbFraudCheckType as FraudCheckType,
DbFutureUsage as FutureUsage, DbIntentStatus as IntentStatus,
DbMandateStatus as MandateStatus, DbMandateType as MandateType,
Expand Down
4 changes: 4 additions & 0 deletions crates/diesel_models/src/merchant_connector_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub struct MerchantConnectorAccount {
#[diesel(deserialize_as = super::OptionalDieselArray<String>)]
pub applepay_verified_domains: Option<Vec<String>>,
pub pm_auth_config: Option<serde_json::Value>,
pub status: storage_enums::ConnectorStatus,
}

#[derive(Clone, Debug, Insertable, router_derive::DebugAsDisplay)]
Expand Down Expand Up @@ -70,6 +71,7 @@ pub struct MerchantConnectorAccountNew {
#[diesel(deserialize_as = super::OptionalDieselArray<String>)]
pub applepay_verified_domains: Option<Vec<String>>,
pub pm_auth_config: Option<serde_json::Value>,
pub status: storage_enums::ConnectorStatus,
}

#[derive(Clone, Debug, AsChangeset, router_derive::DebugAsDisplay)]
Expand All @@ -93,6 +95,7 @@ pub struct MerchantConnectorAccountUpdateInternal {
#[diesel(deserialize_as = super::OptionalDieselArray<String>)]
pub applepay_verified_domains: Option<Vec<String>>,
pub pm_auth_config: Option<serde_json::Value>,
pub status: Option<storage_enums::ConnectorStatus>,
}

impl MerchantConnectorAccountUpdateInternal {
Expand All @@ -115,6 +118,7 @@ impl MerchantConnectorAccountUpdateInternal {
frm_config: self.frm_config,
modified_at: self.modified_at.unwrap_or(source.modified_at),
pm_auth_config: self.pm_auth_config,
status: self.status.unwrap_or(source.status),

..source
}
Expand Down
1 change: 1 addition & 0 deletions crates/diesel_models/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,7 @@ diesel::table! {
profile_id -> Nullable<Varchar>,
applepay_verified_domains -> Nullable<Array<Nullable<Text>>>,
pm_auth_config -> Nullable<Jsonb>,
status -> ConnectorStatus,
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/kgraph_utils/benches/evaluation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ fn build_test_data<'a>(total_enabled: usize, total_pm_types: usize) -> graph::Kn
profile_id: None,
applepay_verified_domains: None,
pm_auth_config: None,
status: api_enums::ConnectorStatus::Inactive,
};

kgraph_utils::mca::make_mca_graph(vec![stripe_account]).expect("Failed graph construction")
Expand Down
1 change: 1 addition & 0 deletions crates/kgraph_utils/src/mca.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ mod tests {
profile_id: None,
applepay_verified_domains: None,
pm_auth_config: None,
status: api_enums::ConnectorStatus::Inactive,
};

make_mca_graph(vec![stripe_account]).expect("Failed graph construction")
Expand Down
1 change: 1 addition & 0 deletions crates/router/src/connector/square/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ impl TryFrom<&types::ConnectorAuthType> for SquareAuthType {
| types::ConnectorAuthType::SignatureKey { .. }
| types::ConnectorAuthType::MultiAuthKey { .. }
| types::ConnectorAuthType::CurrencyAuthKey { .. }
| types::ConnectorAuthType::TemporaryAuth { .. }
| types::ConnectorAuthType::NoKey { .. } => {
Err(errors::ConnectorError::FailedToObtainAuthType.into())
}
Expand Down
68 changes: 66 additions & 2 deletions crates/router/src/core/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,31 @@ pub async fn create_payment_connector(
payment_link_config: None,
};

let connector_status = match (req.status, auth) {
(Some(common_enums::ConnectorStatus::Active), types::ConnectorAuthType::TemporaryAuth) => {
return Err(errors::ApiErrorResponse::InvalidRequestData {
message: "Connector status cannot be active when using TemporaryAuth".to_string(),
}
.into());
}
(Some(status), _) => status,
(None, types::ConnectorAuthType::TemporaryAuth) => common_enums::ConnectorStatus::Inactive,
(None, _) => common_enums::ConnectorStatus::Active,
};

let disabled = match (req.disabled, connector_status) {
(Some(true), common_enums::ConnectorStatus::Inactive) => {
return Err(errors::ApiErrorResponse::InvalidRequestData {
message: "Connector cannot be enabled when connector_status is inactive or when using TemporaryAuth"
.to_string(),
}
.into());
}
(Some(disabled), _) => Some(disabled),
(None, common_enums::ConnectorStatus::Inactive) => Some(true),
(None, _) => None,
};
ThisIsMani marked this conversation as resolved.
Show resolved Hide resolved

state
.store
.update_specific_fields_in_merchant(merchant_id, merchant_account_update, &key_store)
Expand All @@ -886,7 +911,7 @@ pub async fn create_payment_connector(
.attach_printable("Unable to encrypt connector account details")?,
payment_methods_enabled,
test_mode: req.test_mode,
disabled: req.disabled,
disabled,
metadata: req.metadata,
frm_configs,
connector_label: Some(connector_label),
Expand All @@ -911,6 +936,7 @@ pub async fn create_payment_connector(
profile_id: Some(profile_id.clone()),
applepay_verified_domains: None,
pm_auth_config: req.pm_auth_config.clone(),
status: connector_status,
};

let mut default_routing_config =
Expand Down Expand Up @@ -1083,6 +1109,43 @@ pub async fn update_payment_connector(

let frm_configs = get_frm_config_as_secret(req.frm_configs);

let auth: types::ConnectorAuthType = req
.connector_account_details
.clone()
.unwrap_or(mca.connector_account_details.clone().into_inner())
.parse_value("ConnectorAuthType")
.change_context(errors::ApiErrorResponse::InvalidDataFormat {
field_name: "connector_account_details".to_string(),
expected_format: "auth_type and api_key".to_string(),
})?;

let connector_status = match (req.status, auth) {
(Some(common_enums::ConnectorStatus::Active), types::ConnectorAuthType::TemporaryAuth) => {
return Err(errors::ApiErrorResponse::InvalidRequestData {
message: "Connector status cannot be active when using TemporaryAuth".to_string(),
}
.into());
}
(Some(status), _) => Some(status),
(None, types::ConnectorAuthType::TemporaryAuth) => {
Some(common_enums::ConnectorStatus::Inactive)
}
(None, _) => None,
};

let disabled = match (req.disabled, connector_status.unwrap_or(mca.status)) {
(Some(true), common_enums::ConnectorStatus::Inactive) => {
return Err(errors::ApiErrorResponse::InvalidRequestData {
message: "Connector cannot be enabled when connector_status is inactive or when using TemporaryAuth"
.to_string(),
}
.into());
}
(Some(disabled), _) => Some(disabled),
(None, common_enums::ConnectorStatus::Inactive) => Some(true),
(None, _) => None,
};

let payment_connector = storage::MerchantConnectorAccountUpdate::Update {
merchant_id: None,
connector_type: Some(req.connector_type),
Expand All @@ -1098,7 +1161,7 @@ pub async fn update_payment_connector(
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("Failed while encrypting data")?,
test_mode: req.test_mode,
disabled: req.disabled,
disabled,
payment_methods_enabled,
metadata: req.metadata,
frm_configs,
Expand All @@ -1115,6 +1178,7 @@ pub async fn update_payment_connector(
},
applepay_verified_domains: None,
pm_auth_config: req.pm_auth_config,
status: connector_status,
};

let updated_mca = db
Expand Down
1 change: 1 addition & 0 deletions crates/router/src/core/verification/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ pub async fn check_existence_and_add_domain_to_db(
applepay_verified_domains: Some(already_verified_domains.clone()),
pm_auth_config: None,
connector_label: None,
status: None,
};
state
.store
Expand Down
2 changes: 2 additions & 0 deletions crates/router/src/db/merchant_connector_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,7 @@ impl MerchantConnectorAccountInterface for MockDb {
profile_id: t.profile_id,
applepay_verified_domains: t.applepay_verified_domains,
pm_auth_config: t.pm_auth_config,
status: t.status,
};
accounts.push(account.clone());
account
Expand Down Expand Up @@ -839,6 +840,7 @@ mod merchant_connector_account_cache_tests {
profile_id: Some(profile_id.to_string()),
applepay_verified_domains: None,
pm_auth_config: None,
status: common_enums::ConnectorStatus::Inactive,
};

db.insert_merchant_connector_account(mca.clone(), &merchant_key)
Expand Down
1 change: 1 addition & 0 deletions crates/router/src/openapi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ Never share your secret api keys. Keep them guarded and secure.
api_models::enums::AttemptStatus,
api_models::enums::CaptureStatus,
api_models::enums::ReconStatus,
api_models::enums::ConnectorStatus,
api_models::admin::MerchantConnectorCreate,
api_models::admin::MerchantConnectorUpdate,
api_models::admin::PrimaryBusinessDetails,
Expand Down
1 change: 1 addition & 0 deletions crates/router/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,7 @@ pub struct ResponseRouterData<Flow, R, Request, Response> {
#[derive(Default, Debug, Clone, serde::Deserialize)]
#[serde(tag = "auth_type")]
pub enum ConnectorAuthType {
TemporaryAuth,
HeaderKey {
api_key: Secret<String>,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub struct MerchantConnectorAccount {
pub profile_id: Option<String>,
pub applepay_verified_domains: Option<Vec<String>>,
pub pm_auth_config: Option<serde_json::Value>,
pub status: enums::ConnectorStatus,
}

#[derive(Debug)]
Expand All @@ -54,6 +55,7 @@ pub enum MerchantConnectorAccountUpdate {
applepay_verified_domains: Option<Vec<String>>,
pm_auth_config: Option<serde_json::Value>,
connector_label: Option<String>,
status: Option<enums::ConnectorStatus>,
},
}

Expand Down Expand Up @@ -89,6 +91,7 @@ impl behaviour::Conversion for MerchantConnectorAccount {
profile_id: self.profile_id,
applepay_verified_domains: self.applepay_verified_domains,
pm_auth_config: self.pm_auth_config,
status: self.status,
},
)
}
Expand Down Expand Up @@ -128,6 +131,7 @@ impl behaviour::Conversion for MerchantConnectorAccount {
profile_id: other.profile_id,
applepay_verified_domains: other.applepay_verified_domains,
pm_auth_config: other.pm_auth_config,
status: other.status,
})
}

Expand Down Expand Up @@ -155,6 +159,7 @@ impl behaviour::Conversion for MerchantConnectorAccount {
profile_id: self.profile_id,
applepay_verified_domains: self.applepay_verified_domains,
pm_auth_config: self.pm_auth_config,
status: self.status,
})
}
}
Expand All @@ -177,6 +182,7 @@ impl From<MerchantConnectorAccountUpdate> for MerchantConnectorAccountUpdateInte
applepay_verified_domains,
pm_auth_config,
connector_label,
status,
} => Self {
merchant_id,
connector_type,
Expand All @@ -194,6 +200,7 @@ impl From<MerchantConnectorAccountUpdate> for MerchantConnectorAccountUpdateInte
applepay_verified_domains,
pm_auth_config,
connector_label,
status,
},
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/router/src/types/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,7 @@ impl TryFrom<domain::MerchantConnectorAccount> for api_models::admin::MerchantCo
profile_id: item.profile_id,
applepay_verified_domains: item.applepay_verified_domains,
pm_auth_config: item.pm_auth_config,
status: item.status,
})
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- This file should undo anything in `up.sql`
ALTER TABLE merchant_connector_account DROP COLUMN IF EXISTS status;
DROP TYPE IF EXISTS "ConnectorStatus";
11 changes: 11 additions & 0 deletions migrations/2023-11-12-131143_connector-status-column/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- Your SQL goes here
CREATE TYPE "ConnectorStatus" AS ENUM ('active', 'inactive');

ALTER TABLE merchant_connector_account
ThisIsMani marked this conversation as resolved.
Show resolved Hide resolved
ADD COLUMN status "ConnectorStatus";

UPDATE merchant_connector_account SET status='active';

ALTER TABLE merchant_connector_account
ALTER COLUMN status SET NOT NULL,
ALTER COLUMN status SET DEFAULT 'inactive';
Loading
Loading