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(connector): [Bluesnap] Metadata to connector metadata mapping #3331

Merged
merged 14 commits into from
Jan 30, 2024
65 changes: 63 additions & 2 deletions crates/router/src/connector/bluesnap/transformers.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashMap;

use api_models::{enums as api_enums, payments};
use base64::Engine;
use common_utils::{
Expand All @@ -8,6 +10,7 @@ use common_utils::{
use error_stack::{IntoReport, ResultExt};
use masking::{ExposeInterface, PeekInterface};
use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::{
connector::utils::{
Expand All @@ -17,10 +20,16 @@ use crate::{
consts,
core::errors,
pii::Secret,
types::{self, api, storage::enums, transformers::ForeignTryFrom},
types::{
self, api,
storage::enums,
transformers::{ForeignFrom, ForeignTryFrom},
},
utils::{Encode, OptionExt},
};

const DISPLAY_METADATA: &str = "Y";

#[derive(Debug, Serialize)]
pub struct BluesnapRouterData<T> {
pub amount: String,
Expand Down Expand Up @@ -63,6 +72,21 @@ pub struct BluesnapPaymentsRequest {
transaction_fraud_info: Option<TransactionFraudInfo>,
card_holder_info: Option<BluesnapCardHolderInfo>,
merchant_transaction_id: Option<String>,
transaction_meta_data: Option<BluesnapMetadata>,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct BluesnapMetadata {
meta_data: Vec<RequestMetadata>,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RequestMetadata {
meta_key: Option<String>,
meta_value: Option<String>,
is_visible: Option<String>,
Comment on lines +87 to +89
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to keep these fields as optional? RequestMetadata should be instructed only incase of payment metadata is present

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These fields are optional at the connector end. Hence made them optional in RequestMetadata struct

}

#[derive(Debug, Serialize)]
Expand Down Expand Up @@ -241,6 +265,15 @@ impl TryFrom<&BluesnapRouterData<&types::PaymentsAuthorizeRouterData>> for Blues
Some(enums::CaptureMethod::Manual) => BluesnapTxnType::AuthOnly,
_ => BluesnapTxnType::AuthCapture,
};
let transaction_meta_data =
item.router_data
.request
.metadata
.as_ref()
.map(|metadata| BluesnapMetadata {
meta_data: Vec::<RequestMetadata>::foreign_from(metadata.peek().to_owned()),
});

let (payment_method, card_holder_info) = match item
.router_data
.request
Expand Down Expand Up @@ -405,6 +438,7 @@ impl TryFrom<&BluesnapRouterData<&types::PaymentsAuthorizeRouterData>> for Blues
}),
card_holder_info,
merchant_transaction_id: Some(item.router_data.connector_request_reference_id.clone()),
transaction_meta_data,
})
}
}
Expand Down Expand Up @@ -569,6 +603,7 @@ pub struct BluesnapCompletePaymentsRequest {
transaction_fraud_info: Option<TransactionFraudInfo>,
card_holder_info: Option<BluesnapCardHolderInfo>,
merchant_transaction_id: Option<String>,
transaction_meta_data: Option<BluesnapMetadata>,
}

impl TryFrom<&BluesnapRouterData<&types::PaymentsCompleteAuthorizeRouterData>>
Expand All @@ -590,6 +625,15 @@ impl TryFrom<&BluesnapRouterData<&types::PaymentsCompleteAuthorizeRouterData>>
.parse_value("BluesnapRedirectionResponse")
.change_context(errors::ConnectorError::ResponseDeserializationFailed)?;

let transaction_meta_data =
item.router_data
.request
.metadata
.as_ref()
.map(|metadata| BluesnapMetadata {
meta_data: Vec::<RequestMetadata>::foreign_from(metadata.peek().to_owned()),
});

let pf_token = item
.router_data
.request
Expand Down Expand Up @@ -637,6 +681,7 @@ impl TryFrom<&BluesnapRouterData<&types::PaymentsCompleteAuthorizeRouterData>>
)?,
merchant_transaction_id: Some(item.router_data.connector_request_reference_id.clone()),
pf_token,
transaction_meta_data,
})
}
}
Expand Down Expand Up @@ -1021,7 +1066,7 @@ pub struct BluesnapWebhookObjectResource {
reversal_ref_num: Option<String>,
}

impl TryFrom<BluesnapWebhookObjectResource> for serde_json::Value {
impl TryFrom<BluesnapWebhookObjectResource> for Value {
type Error = error_stack::Report<errors::ConnectorError>;
fn try_from(details: BluesnapWebhookObjectResource) -> Result<Self, Self::Error> {
let (card_transaction_type, processing_status, transaction_id) = match details
Expand Down Expand Up @@ -1118,3 +1163,19 @@ impl From<ErrorDetails> for utils::ErrorCodeAndMessage {
}
}
}

impl ForeignFrom<Value> for Vec<RequestMetadata> {
fn foreign_from(metadata: Value) -> Self {
let hashmap: HashMap<Option<String>, Option<Value>> =
serde_json::from_str(&metadata.to_string()).unwrap_or(HashMap::new());
let mut vector: Self = Self::new();
for (key, value) in hashmap {
vector.push(RequestMetadata {
meta_key: key,
meta_value: value.map(|field_value| field_value.to_string()),
is_visible: Some(DISPLAY_METADATA.to_string()),
});
}
vector
}
}
Loading