Skip to content

Commit

Permalink
feat(connector): [Bluesnap] Metadata to connector metadata mapping (#…
Browse files Browse the repository at this point in the history
…3331)

Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com>
  • Loading branch information
AkshayaFoiger and hyperswitch-bot[bot] authored Jan 30, 2024
1 parent 3d55e3b commit b2afdc3
Showing 1 changed file with 63 additions and 2 deletions.
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>,
}

#[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
}
}

0 comments on commit b2afdc3

Please sign in to comment.