Skip to content

Commit

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

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 8c0c49c commit 864a8d7
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 29 deletions.
83 changes: 54 additions & 29 deletions crates/router/src/connector/stripe/transformers.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::ops::Deref;
use std::{collections::HashMap, ops::Deref};

use api_models::{self, enums as api_enums, payments};
use common_utils::{
Expand All @@ -9,11 +9,11 @@ use common_utils::{
};
use data_models::mandates::AcceptanceType;
use error_stack::{IntoReport, ResultExt};
use masking::{ExposeInterface, ExposeOptionInterface, Secret};
use masking::{ExposeInterface, ExposeOptionInterface, PeekInterface, Secret};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use time::PrimitiveDateTime;
use url::Url;
use uuid::Uuid;

use crate::{
collect_missing_value_keys,
Expand Down Expand Up @@ -105,7 +105,7 @@ pub struct PaymentIntentRequest {
pub statement_descriptor_suffix: Option<String>,
pub statement_descriptor: Option<String>,
#[serde(flatten)]
pub meta_data: StripeMetadata,
pub meta_data: HashMap<String, String>,
pub return_url: String,
pub confirm: bool,
pub mandate: Option<Secret<String>>,
Expand Down Expand Up @@ -145,12 +145,6 @@ pub struct StripeMetadata {

#[derive(Debug, Eq, PartialEq, Serialize)]
pub struct SetupIntentRequest {
#[serde(rename = "metadata[order_id]")]
pub metadata_order_id: String,
#[serde(rename = "metadata[txn_id]")]
pub metadata_txn_id: String,
#[serde(rename = "metadata[txn_uuid]")]
pub metadata_txn_uuid: String,
pub confirm: bool,
pub usage: Option<enums::FutureUsage>,
pub customer: Option<Secret<String>>,
Expand All @@ -159,6 +153,8 @@ pub struct SetupIntentRequest {
#[serde(flatten)]
pub payment_data: StripePaymentMethodData,
pub payment_method_options: Option<StripePaymentMethodOptions>, // For mandate txns using network_txns_id, needs to be validated
#[serde(flatten)]
pub meta_data: Option<HashMap<String, String>>,
}

#[derive(Debug, Eq, PartialEq, Serialize)]
Expand Down Expand Up @@ -218,6 +214,8 @@ pub struct ChargesRequest {
pub currency: String,
pub customer: Secret<String>,
pub source: Secret<String>,
#[serde(flatten)]
pub meta_data: Option<HashMap<String, String>>,
}

#[derive(Clone, Debug, Default, Eq, PartialEq, Deserialize)]
Expand Down Expand Up @@ -1877,15 +1875,15 @@ impl TryFrom<&types::PaymentsAuthorizeRouterData> for PaymentIntentRequest {
None
}
});

let meta_data = get_transaction_metadata(item.request.metadata.clone(), order_id);

Ok(Self {
amount: item.request.amount, //hopefully we don't loose some cents here
currency: item.request.currency.to_string(), //we need to copy the value and not transfer ownership
statement_descriptor_suffix: item.request.statement_descriptor_suffix.clone(),
statement_descriptor: item.request.statement_descriptor.clone(),
meta_data: StripeMetadata {
order_id: Some(order_id),
is_refund_id_as_reference: None,
},
meta_data,
return_url: item
.request
.router_return_url
Expand Down Expand Up @@ -1945,10 +1943,6 @@ fn get_payment_method_type_for_saved_payment_method_payment(
impl TryFrom<&types::SetupMandateRouterData> for SetupIntentRequest {
type Error = error_stack::Report<errors::ConnectorError>;
fn try_from(item: &types::SetupMandateRouterData) -> Result<Self, Self::Error> {
let metadata_order_id = item.connector_request_reference_id.clone();
let metadata_txn_id = format!("{}_{}_{}", item.merchant_id, item.payment_id, "1");
let metadata_txn_uuid = Uuid::new_v4().to_string();

//Only cards supported for mandates
let pm_type = StripePaymentMethodType::Card;
let payment_data = StripePaymentMethodData::try_from((
Expand All @@ -1957,17 +1951,20 @@ impl TryFrom<&types::SetupMandateRouterData> for SetupIntentRequest {
pm_type,
))?;

let meta_data = Some(get_transaction_metadata(
item.request.metadata.clone(),
item.connector_request_reference_id.clone(),
));

Ok(Self {
confirm: true,
metadata_order_id,
metadata_txn_id,
metadata_txn_uuid,
payment_data,
return_url: item.request.router_return_url.clone(),
off_session: item.request.off_session,
usage: item.request.setup_future_usage,
payment_method_options: None,
customer: item.connector_customer.to_owned().map(Secret::new),
meta_data,
})
}
}
Expand Down Expand Up @@ -2355,7 +2352,7 @@ impl<F, T>
pub fn get_connector_metadata(
next_action: Option<&StripeNextActionResponse>,
amount: i64,
) -> CustomResult<Option<serde_json::Value>, errors::ConnectorError> {
) -> CustomResult<Option<Value>, errors::ConnectorError> {
let next_action_response = next_action
.and_then(|next_action_response| match next_action_response {
StripeNextActionResponse::DisplayBankTransferInstructions(response) => {
Expand Down Expand Up @@ -3058,12 +3055,20 @@ impl TryFrom<&types::PaymentsAuthorizeRouterData> for ChargesRequest {
type Error = error_stack::Report<errors::ConnectorError>;

fn try_from(value: &types::PaymentsAuthorizeRouterData) -> Result<Self, Self::Error> {
Ok(Self {
amount: value.request.amount.to_string(),
currency: value.request.currency.to_string(),
customer: Secret::new(value.get_connector_customer_id()?),
source: Secret::new(value.get_preprocessing_id()?),
})
{
let order_id = value.connector_request_reference_id.clone();
let meta_data = Some(get_transaction_metadata(
value.request.metadata.clone(),
order_id,
));
Ok(Self {
amount: value.request.amount.to_string(),
currency: value.request.currency.to_string(),
customer: Secret::new(value.get_connector_customer_id()?),
source: Secret::new(value.get_preprocessing_id()?),
meta_data,
})
}
}
}

Expand Down Expand Up @@ -3169,7 +3174,7 @@ impl<F, T>

#[derive(Debug, Deserialize)]
pub struct WebhookEventDataResource {
pub object: serde_json::Value,
pub object: Value,
}

#[derive(Debug, Deserialize)]
Expand Down Expand Up @@ -3649,6 +3654,26 @@ pub struct DisputeObj {
pub status: String,
}

fn get_transaction_metadata(
merchant_metadata: Option<Secret<Value>>,
order_id: String,
) -> HashMap<String, String> {
let mut meta_data = HashMap::from([("metadata[order_id]".to_string(), order_id)]);
let mut request_hash_map = HashMap::new();

if let Some(metadata) = merchant_metadata {
let hashmap: HashMap<String, Value> =
serde_json::from_str(&metadata.peek().to_string()).unwrap_or(HashMap::new());

for (key, value) in hashmap {
request_hash_map.insert(format!("metadata[{}]", key), value.to_string());
}

meta_data.extend(request_hash_map)
};
meta_data
}

#[cfg(test)]
mod test_validate_shipping_address_against_payment_method {
#![allow(clippy::unwrap_used)]
Expand Down
1 change: 1 addition & 0 deletions crates/router/src/core/payments/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1440,6 +1440,7 @@ impl<F: Clone> TryFrom<PaymentAdditionalData<'_, F>> for types::SetupMandateRequ
Some(RequestIncrementalAuthorization::True)
| Some(RequestIncrementalAuthorization::Default)
),
metadata: payment_data.payment_intent.metadata.clone(),
})
}
}
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 @@ -591,6 +591,7 @@ pub struct SetupMandateRequestData {
pub return_url: Option<String>,
pub payment_method_type: Option<storage_enums::PaymentMethodType>,
pub request_incremental_authorization: bool,
pub metadata: Option<pii::SecretSerdeValue>,
}

#[derive(Debug, Clone)]
Expand Down

0 comments on commit 864a8d7

Please sign in to comment.