refactor(merchant_account): add back api_key field for backward compatibility (#761)

This commit is contained in:
Sanchith Hegde
2023-03-17 15:30:20 +05:30
committed by GitHub
parent 20b93276fc
commit 661dd48ae5
11 changed files with 55 additions and 7 deletions

View File

@ -1,5 +1,5 @@
use common_utils::pii; use common_utils::pii;
use masking::Secret; use masking::{Secret, StrongSecret};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use url; use url;
use utoipa::ToSchema; use utoipa::ToSchema;
@ -18,6 +18,10 @@ pub struct MerchantAccountCreate {
#[schema(example = "NewAge Retailer")] #[schema(example = "NewAge Retailer")]
pub merchant_name: Option<String>, pub merchant_name: Option<String>,
/// API key that will be used for server side API access
#[schema(value_type = Option<String>, example = "Ah2354543543523")]
pub api_key: Option<StrongSecret<String>>,
/// Merchant related details /// Merchant related details
pub merchant_details: Option<MerchantDetails>, pub merchant_details: Option<MerchantDetails>,
@ -131,6 +135,10 @@ pub struct MerchantAccountResponse {
#[schema(example = "NewAge Retailer")] #[schema(example = "NewAge Retailer")]
pub merchant_name: Option<String>, pub merchant_name: Option<String>,
/// API key that will be used for server side API access
#[schema(value_type = Option<String>, example = "Ah2354543543523")]
pub api_key: Option<StrongSecret<String>>,
/// The URL to redirect after the completion of the operation /// The URL to redirect after the completion of the operation
#[schema(max_length = 255, example = "https://www.example.com/success")] #[schema(max_length = 255, example = "https://www.example.com/success")]
pub return_url: Option<String>, pub return_url: Option<String>,

View File

@ -1,13 +1,17 @@
use common_utils::ext_traits::ValueExt; use common_utils::ext_traits::ValueExt;
use error_stack::{report, FutureExt, ResultExt}; use error_stack::{report, FutureExt, IntoReport, ResultExt};
use storage_models::{enums, merchant_account}; use storage_models::{enums, merchant_account};
use uuid::Uuid; use uuid::Uuid;
use crate::{ use crate::{
consts, consts,
core::errors::{self, RouterResponse, RouterResult, StorageErrorExt}, core::{
api_keys,
errors::{self, RouterResponse, RouterResult, StorageErrorExt},
},
db::StorageInterface, db::StorageInterface,
pii::Secret, pii::Secret,
routes::AppState,
services::api as service_api, services::api as service_api,
types::{ types::{
self, api, self, api,
@ -27,11 +31,38 @@ pub fn create_merchant_publishable_key() -> String {
} }
pub async fn create_merchant_account( pub async fn create_merchant_account(
db: &dyn StorageInterface, state: &AppState,
req: api::MerchantAccountCreate, req: api::MerchantAccountCreate,
) -> RouterResponse<api::MerchantAccountResponse> { ) -> RouterResponse<api::MerchantAccountResponse> {
let db = &*state.store;
let publishable_key = Some(create_merchant_publishable_key()); let publishable_key = Some(create_merchant_publishable_key());
let api_key_request = api::CreateApiKeyRequest {
name: "Default API key".into(),
description: Some(
"An API key created by default when a user signs up on the HyperSwitch dashboard"
.into(),
),
expiration: api::ApiKeyExpiration::Never,
};
let api_key = match api_keys::create_api_key(
db,
&state.conf.api_keys,
#[cfg(feature = "kms")]
&state.conf.kms,
api_key_request,
req.merchant_id.clone(),
)
.await?
{
service_api::ApplicationResponse::Json(api::CreateApiKeyResponse { api_key, .. }) => {
Ok(api_key)
}
_ => Err(errors::ApiErrorResponse::InternalServerError)
.into_report()
.attach_printable("Unexpected create API key response"),
}?;
let merchant_details = Some( let merchant_details = Some(
utils::Encode::<api::MerchantDetails>::encode_to_value(&req.merchant_details) utils::Encode::<api::MerchantDetails>::encode_to_value(&req.merchant_details)
.change_context(errors::ApiErrorResponse::InvalidDataValue { .change_context(errors::ApiErrorResponse::InvalidDataValue {
@ -59,6 +90,7 @@ pub async fn create_merchant_account(
let merchant_account = storage::MerchantAccountNew { let merchant_account = storage::MerchantAccountNew {
merchant_id: req.merchant_id, merchant_id: req.merchant_id,
merchant_name: req.merchant_name, merchant_name: req.merchant_name,
api_key: Some(api_key),
merchant_details, merchant_details,
return_url: req.return_url.map(|a| a.to_string()), return_url: req.return_url.map(|a| a.to_string()),
webhook_details, webhook_details,

View File

@ -167,7 +167,7 @@ where
payment_method_id, payment_method_id,
mandate_reference, mandate_reference,
) { ) {
logger::error!("{:?}", new_mandate_data); logger::debug!("{:?}", new_mandate_data);
resp.request resp.request
.set_mandate_id(api_models::payments::MandateIds { .set_mandate_id(api_models::payments::MandateIds {
mandate_id: new_mandate_data.mandate_id.clone(), mandate_id: new_mandate_data.mandate_id.clone(),

View File

@ -179,6 +179,7 @@ impl MerchantAccountInterface for MockDb {
#[allow(clippy::as_conversions)] #[allow(clippy::as_conversions)]
id: accounts.len() as i32, id: accounts.len() as i32,
merchant_id: merchant_account.merchant_id, merchant_id: merchant_account.merchant_id,
api_key: merchant_account.api_key,
return_url: merchant_account.return_url, return_url: merchant_account.return_url,
enable_payment_response_hash: merchant_account enable_payment_response_hash: merchant_account
.enable_payment_response_hash .enable_payment_response_hash

View File

@ -33,7 +33,7 @@ pub async fn merchant_account_create(
state.get_ref(), state.get_ref(),
&req, &req,
json_payload.into_inner(), json_payload.into_inner(),
|state, _, req| create_merchant_account(&*state.store, req), |state, _, req| create_merchant_account(state, req),
&auth::AdminApiAuth, &auth::AdminApiAuth,
) )
.await .await

View File

@ -113,7 +113,6 @@ impl Payments {
) )
.service( .service(
web::resource("/{payment_id}/{merchant_id}/complete/{connector}") web::resource("/{payment_id}/{merchant_id}/complete/{connector}")
// .route(web::get().to(payments_redirect_response))
.route(web::post().to(payments_complete_authorize)), .route(web::post().to(payments_complete_authorize)),
); );
} }

View File

@ -13,6 +13,7 @@ impl ForeignFrom<storage::MerchantAccount> for MerchantAccountResponse {
Self { Self {
merchant_id: item.merchant_id, merchant_id: item.merchant_id,
merchant_name: item.merchant_name, merchant_name: item.merchant_name,
api_key: item.api_key,
return_url: item.return_url, return_url: item.return_url,
enable_payment_response_hash: item.enable_payment_response_hash, enable_payment_response_hash: item.enable_payment_response_hash,
payment_response_hash_key: item.payment_response_hash_key, payment_response_hash_key: item.payment_response_hash_key,

View File

@ -1,5 +1,6 @@
use common_utils::pii; use common_utils::pii;
use diesel::{AsChangeset, Identifiable, Insertable, Queryable}; use diesel::{AsChangeset, Identifiable, Insertable, Queryable};
use masking::StrongSecret;
use crate::{enums as storage_enums, schema::merchant_account}; use crate::{enums as storage_enums, schema::merchant_account};
@ -32,6 +33,7 @@ pub struct MerchantAccount {
pub locker_id: Option<String>, pub locker_id: Option<String>,
pub metadata: Option<pii::SecretSerdeValue>, pub metadata: Option<pii::SecretSerdeValue>,
pub routing_algorithm: Option<serde_json::Value>, pub routing_algorithm: Option<serde_json::Value>,
pub api_key: Option<StrongSecret<String>>,
} }
#[derive(Clone, Debug, Default, Insertable, router_derive::DebugAsDisplay)] #[derive(Clone, Debug, Default, Insertable, router_derive::DebugAsDisplay)]
@ -51,6 +53,7 @@ pub struct MerchantAccountNew {
pub locker_id: Option<String>, pub locker_id: Option<String>,
pub metadata: Option<pii::SecretSerdeValue>, pub metadata: Option<pii::SecretSerdeValue>,
pub routing_algorithm: Option<serde_json::Value>, pub routing_algorithm: Option<serde_json::Value>,
pub api_key: Option<StrongSecret<String>>,
} }
#[derive(Debug)] #[derive(Debug)]

View File

@ -176,6 +176,7 @@ diesel::table! {
locker_id -> Nullable<Varchar>, locker_id -> Nullable<Varchar>,
metadata -> Nullable<Jsonb>, metadata -> Nullable<Jsonb>,
routing_algorithm -> Nullable<Json>, routing_algorithm -> Nullable<Json>,
api_key -> Nullable<Varchar>,
} }
} }

View File

@ -0,0 +1 @@
ALTER TABLE merchant_account DROP COLUMN api_key;

View File

@ -0,0 +1,2 @@
ALTER TABLE merchant_account
ADD COLUMN api_key VARCHAR(128);