Skip to content

Commit

Permalink
feat: added client_source, client_version in payment_attempt from pay…
Browse files Browse the repository at this point in the history
…ments confirm request headers (#4657)
  • Loading branch information
vsrivatsa-edinburgh authored May 17, 2024
1 parent 853f3b4 commit 7e44bbc
Show file tree
Hide file tree
Showing 17 changed files with 97 additions and 10 deletions.
4 changes: 3 additions & 1 deletion crates/api_models/src/payments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -540,9 +540,11 @@ impl RequestSurchargeDetails {
}
}

#[derive(Default, Debug, Clone, Copy)]
#[derive(Default, Debug, Clone)]
pub struct HeaderPayload {
pub payment_confirm_source: Option<api_enums::PaymentSource>,
pub client_source: Option<String>,
pub client_version: Option<String>,
pub x_hs_latency: Option<bool>,
}

Expand Down
16 changes: 16 additions & 0 deletions crates/diesel_models/src/payment_attempt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ pub struct PaymentAttempt {
pub mandate_data: Option<storage_enums::MandateDetails>,
pub fingerprint_id: Option<String>,
pub payment_method_billing_address_id: Option<String>,
pub client_source: Option<String>,
pub client_version: Option<String>,
}

impl PaymentAttempt {
Expand Down Expand Up @@ -150,6 +152,8 @@ pub struct PaymentAttemptNew {
pub mandate_data: Option<storage_enums::MandateDetails>,
pub fingerprint_id: Option<String>,
pub payment_method_billing_address_id: Option<String>,
pub client_source: Option<String>,
pub client_version: Option<String>,
}

impl PaymentAttemptNew {
Expand Down Expand Up @@ -233,6 +237,8 @@ pub enum PaymentAttemptUpdate {
authentication_connector: Option<String>,
authentication_id: Option<String>,
payment_method_billing_address_id: Option<String>,
client_source: Option<String>,
client_version: Option<String>,
},
VoidUpdate {
status: storage_enums::AttemptStatus,
Expand Down Expand Up @@ -388,6 +394,8 @@ pub struct PaymentAttemptUpdateInternal {
authentication_id: Option<String>,
fingerprint_id: Option<String>,
payment_method_billing_address_id: Option<String>,
client_source: Option<String>,
client_version: Option<String>,
}

impl PaymentAttemptUpdateInternal {
Expand Down Expand Up @@ -453,6 +461,8 @@ impl PaymentAttemptUpdate {
authentication_id,
payment_method_billing_address_id,
fingerprint_id,
client_source,
client_version,
} = PaymentAttemptUpdateInternal::from(self).populate_derived_fields(&source);
PaymentAttempt {
amount: amount.unwrap_or(source.amount),
Expand Down Expand Up @@ -501,6 +511,8 @@ impl PaymentAttemptUpdate {
payment_method_billing_address_id: payment_method_billing_address_id
.or(source.payment_method_billing_address_id),
fingerprint_id: fingerprint_id.or(source.fingerprint_id),
client_source: client_source.or(source.client_source),
client_version: client_version.or(source.client_version),
..source
}
}
Expand Down Expand Up @@ -587,6 +599,8 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
payment_method_billing_address_id,
fingerprint_id,
payment_method_id,
client_source,
client_version,
} => Self {
amount: Some(amount),
currency: Some(currency),
Expand Down Expand Up @@ -616,6 +630,8 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
fingerprint_id,
payment_method_id,
capture_method,
client_source,
client_version,
..Default::default()
},
PaymentAttemptUpdate::VoidUpdate {
Expand Down
4 changes: 4 additions & 0 deletions crates/diesel_models/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,10 @@ diesel::table! {
fingerprint_id -> Nullable<Varchar>,
#[max_length = 64]
payment_method_billing_address_id -> Nullable<Varchar>,
#[max_length = 64]
client_source -> Nullable<Varchar>,
#[max_length = 64]
client_version -> Nullable<Varchar>,
}
}

Expand Down
4 changes: 4 additions & 0 deletions crates/diesel_models/src/user/sample_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ pub struct PaymentAttemptBatchNew {
pub mandate_data: Option<MandateDetails>,
pub payment_method_billing_address_id: Option<String>,
pub fingerprint_id: Option<String>,
pub client_source: Option<String>,
pub client_version: Option<String>,
}

#[allow(dead_code)]
Expand Down Expand Up @@ -133,6 +135,8 @@ impl PaymentAttemptBatchNew {
mandate_data: self.mandate_data,
payment_method_billing_address_id: self.payment_method_billing_address_id,
fingerprint_id: self.fingerprint_id,
client_source: self.client_source,
client_version: self.client_version,
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ pub struct PaymentAttempt {
pub mandate_data: Option<MandateDetails>,
pub payment_method_billing_address_id: Option<String>,
pub fingerprint_id: Option<String>,
pub client_source: Option<String>,
pub client_version: Option<String>,
}

impl PaymentAttempt {
Expand Down Expand Up @@ -249,6 +251,8 @@ pub struct PaymentAttemptNew {
pub mandate_data: Option<MandateDetails>,
pub payment_method_billing_address_id: Option<String>,
pub fingerprint_id: Option<String>,
pub client_source: Option<String>,
pub client_version: Option<String>,
}

impl PaymentAttemptNew {
Expand Down Expand Up @@ -327,6 +331,8 @@ pub enum PaymentAttemptUpdate {
payment_method_billing_address_id: Option<String>,
fingerprint_id: Option<String>,
payment_method_id: Option<String>,
client_source: Option<String>,
client_version: Option<String>,
},
RejectUpdate {
status: storage_enums::AttemptStatus,
Expand Down
12 changes: 6 additions & 6 deletions crates/router/src/core/payments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ where
call_connector_action.clone(),
&validate_result,
schedule_time,
header_payload,
header_payload.clone(),
#[cfg(feature = "frm")]
frm_info.as_ref().and_then(|fi| fi.suggested_action),
#[cfg(not(feature = "frm"))]
Expand Down Expand Up @@ -364,7 +364,7 @@ where
call_connector_action.clone(),
&validate_result,
schedule_time,
header_payload,
header_payload.clone(),
#[cfg(feature = "frm")]
frm_info.as_ref().and_then(|fi| fi.suggested_action),
#[cfg(not(feature = "frm"))]
Expand Down Expand Up @@ -497,7 +497,7 @@ where
frm_info.and_then(|info| info.suggested_action),
#[cfg(not(feature = "frm"))]
None,
header_payload,
header_payload.clone(),
)
.await?;
}
Expand Down Expand Up @@ -528,7 +528,7 @@ where
None,
&key_store,
None,
header_payload,
header_payload.clone(),
)
.await?;
}
Expand Down Expand Up @@ -785,7 +785,7 @@ where
call_connector_action,
auth_flow,
eligible_routable_connectors,
header_payload,
header_payload.clone(),
)
.await?;

Expand Down Expand Up @@ -1579,7 +1579,7 @@ where
updated_customer,
key_store,
frm_suggestion,
header_payload,
header_payload.clone(),
)
.await?;

Expand Down
2 changes: 2 additions & 0 deletions crates/router/src/core/payments/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3554,6 +3554,8 @@ impl AttemptType {
// New payment method billing address can be passed for a retry
payment_method_billing_address_id: None,
fingerprint_id: None,
client_source: None,
client_version: None,
}
}

Expand Down
11 changes: 11 additions & 0 deletions crates/router/src/core/payments/operations/payment_confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1069,6 +1069,15 @@ impl<F: Clone> UpdateTracker<F, PaymentData<F>, api::PaymentsRequest> for Paymen
.map(|surcharge_details| surcharge_details.final_amount)
.unwrap_or(payment_data.payment_attempt.amount);

let client_source = header_payload
.client_source
.clone()
.or(payment_data.payment_attempt.client_source.clone());
let client_version = header_payload
.client_version
.clone()
.or(payment_data.payment_attempt.client_version.clone());

let m_payment_data_payment_attempt = payment_data.payment_attempt.clone();
let m_payment_method_id = payment_data.payment_attempt.payment_method_id.clone();
let m_browser_info = browser_info.clone();
Expand Down Expand Up @@ -1136,6 +1145,8 @@ impl<F: Clone> UpdateTracker<F, PaymentData<F>, api::PaymentsRequest> for Paymen
payment_method_billing_address_id,
fingerprint_id: m_fingerprint_id,
payment_method_id: m_payment_method_id,
client_source,
client_version,
},
storage_scheme,
)
Expand Down
2 changes: 2 additions & 0 deletions crates/router/src/core/payments/operations/payment_create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,8 @@ impl PaymentCreate {
fingerprint_id: None,
authentication_connector: None,
authentication_id: None,
client_source: None,
client_version: None,
},
additional_pm_data,
))
Expand Down
3 changes: 2 additions & 1 deletion crates/router/src/core/payments/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::{
payments::{self, helpers},
utils as core_utils,
},
headers::X_PAYMENT_CONFIRM_SOURCE,
routes::{metrics, AppState},
services::{self, RedirectForm},
types::{
Expand Down Expand Up @@ -488,7 +489,7 @@ where
.unwrap_or_default();
if let Some(payment_confirm_source) = payment_intent.payment_confirm_source {
headers.push((
"payment_confirm_source".to_string(),
X_PAYMENT_CONFIRM_SOURCE.to_string(),
Maskable::new_normal(payment_confirm_source.to_string()),
))
}
Expand Down
3 changes: 3 additions & 0 deletions crates/router/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ pub mod headers {
pub const X_REQUEST_ID: &str = "X-Request-Id";
pub const STRIPE_COMPATIBLE_WEBHOOK_SIGNATURE: &str = "Stripe-Signature";
pub const STRIPE_COMPATIBLE_CONNECT_ACCOUNT: &str = "Stripe-Account";
pub const X_CLIENT_VERSION: &str = "X-Client-Version";
pub const X_CLIENT_SOURCE: &str = "X-Client-Source";
pub const X_PAYMENT_CONFIRM_SOURCE: &str = "X-Payment-Confirm-Source";
pub const CONTENT_LENGTH: &str = "Content-Length";
}

Expand Down
2 changes: 1 addition & 1 deletion crates/router/src/routes/payments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ pub async fn payments_confirm(
req_state,
auth.merchant_account,
auth.key_store,
header_payload,
header_payload.clone(),
req,
auth_flow,
)
Expand Down
12 changes: 11 additions & 1 deletion crates/router/src/types/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use masking::{ExposeInterface, PeekInterface};
use super::domain;
use crate::{
core::errors,
headers::{X_CLIENT_SOURCE, X_CLIENT_VERSION, X_PAYMENT_CONFIRM_SOURCE},
services::authentication::get_header_value_by_key,
types::{
api::{self as api_types, routing as routing_types},
Expand Down Expand Up @@ -1011,7 +1012,7 @@ impl ForeignTryFrom<&HeaderMap> for payments::HeaderPayload {
type Error = error_stack::Report<errors::ApiErrorResponse>;
fn foreign_try_from(headers: &HeaderMap) -> Result<Self, Self::Error> {
let payment_confirm_source: Option<api_enums::PaymentSource> =
get_header_value_by_key("payment_confirm_source".into(), headers)?
get_header_value_by_key(X_PAYMENT_CONFIRM_SOURCE.into(), headers)?
.map(|source| {
source
.to_owned()
Expand All @@ -1038,8 +1039,17 @@ impl ForeignTryFrom<&HeaderMap> for payments::HeaderPayload {
let x_hs_latency = get_header_value_by_key(X_HS_LATENCY.into(), headers)
.map(|value| value == Some("true"))
.unwrap_or(false);

let client_source =
get_header_value_by_key(X_CLIENT_SOURCE.into(), headers)?.map(|val| val.to_string());

let client_version =
get_header_value_by_key(X_CLIENT_VERSION.into(), headers)?.map(|val| val.to_string());

Ok(Self {
payment_confirm_source,
client_source,
client_version,
x_hs_latency: Some(x_hs_latency),
})
}
Expand Down
2 changes: 2 additions & 0 deletions crates/storage_impl/src/mock_db/payment_attempt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ impl PaymentAttemptInterface for MockDb {
mandate_data: payment_attempt.mandate_data,
payment_method_billing_address_id: payment_attempt.payment_method_billing_address_id,
fingerprint_id: payment_attempt.fingerprint_id,
client_source: payment_attempt.client_source,
client_version: payment_attempt.client_version,
};
payment_attempts.push(payment_attempt.clone());
Ok(payment_attempt)
Expand Down
18 changes: 18 additions & 0 deletions crates/storage_impl/src/payments/payment_attempt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,8 @@ impl<T: DatabaseStore> PaymentAttemptInterface for KVRouterStore<T> {
.payment_method_billing_address_id
.clone(),
fingerprint_id: payment_attempt.fingerprint_id.clone(),
client_source: payment_attempt.client_source.clone(),
client_version: payment_attempt.client_version.clone(),
};

let field = format!("pa_{}", created_attempt.attempt_id);
Expand Down Expand Up @@ -1164,6 +1166,8 @@ impl DataModelExt for PaymentAttempt {
mandate_data: self.mandate_data.map(|d| d.to_storage_model()),
payment_method_billing_address_id: self.payment_method_billing_address_id,
fingerprint_id: self.fingerprint_id,
client_source: self.client_source,
client_version: self.client_version,
}
}

Expand Down Expand Up @@ -1228,6 +1232,8 @@ impl DataModelExt for PaymentAttempt {
.map(MandateDetails::from_storage_model),
payment_method_billing_address_id: storage_model.payment_method_billing_address_id,
fingerprint_id: storage_model.fingerprint_id,
client_source: storage_model.client_source,
client_version: storage_model.client_version,
}
}
}
Expand Down Expand Up @@ -1290,6 +1296,8 @@ impl DataModelExt for PaymentAttemptNew {
mandate_data: self.mandate_data.map(|d| d.to_storage_model()),
payment_method_billing_address_id: self.payment_method_billing_address_id,
fingerprint_id: self.fingerprint_id,
client_source: self.client_source,
client_version: self.client_version,
}
}

Expand Down Expand Up @@ -1352,6 +1360,8 @@ impl DataModelExt for PaymentAttemptNew {
.map(MandateDetails::from_storage_model),
payment_method_billing_address_id: storage_model.payment_method_billing_address_id,
fingerprint_id: storage_model.fingerprint_id,
client_source: storage_model.client_source,
client_version: storage_model.client_version,
}
}
}
Expand Down Expand Up @@ -1470,6 +1480,8 @@ impl DataModelExt for PaymentAttemptUpdate {
authentication_connector,
authentication_id,
payment_method_billing_address_id,
client_source,
client_version,
} => DieselPaymentAttemptUpdate::ConfirmUpdate {
amount,
currency,
Expand Down Expand Up @@ -1498,6 +1510,8 @@ impl DataModelExt for PaymentAttemptUpdate {
authentication_connector,
authentication_id,
payment_method_billing_address_id,
client_source,
client_version,
},
Self::VoidUpdate {
status,
Expand Down Expand Up @@ -1773,6 +1787,8 @@ impl DataModelExt for PaymentAttemptUpdate {
authentication_connector,
authentication_id,
payment_method_billing_address_id,
client_source,
client_version,
} => Self::ConfirmUpdate {
amount,
currency,
Expand Down Expand Up @@ -1801,6 +1817,8 @@ impl DataModelExt for PaymentAttemptUpdate {
authentication_connector,
authentication_id,
payment_method_billing_address_id,
client_source,
client_version,
},
DieselPaymentAttemptUpdate::VoidUpdate {
status,
Expand Down
Loading

0 comments on commit 7e44bbc

Please sign in to comment.