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(core): added customer phone_number and email to session token response for click to pay #6863

Merged
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion api-reference/openapi_spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -8688,7 +8688,8 @@
"merchant_category_code",
"merchant_country_code",
"transaction_amount",
"transaction_currency_code"
"transaction_currency_code",
"email"
],
"properties": {
"dpa_id": {
Expand Down Expand Up @@ -8724,6 +8725,17 @@
},
"transaction_currency_code": {
"$ref": "#/components/schemas/Currency"
},
"phone_number": {
"type": "string",
"nullable": true
},
"email": {
"$ref": "#/components/schemas/Email"
},
"phone_country_code": {
"type": "string",
"nullable": true
}
}
},
Expand Down
4 changes: 4 additions & 0 deletions crates/api_models/src/payments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6997,6 +6997,10 @@ pub struct ClickToPaySessionResponse {
pub transaction_amount: StringMajorUnit,
#[schema(value_type = Currency)]
pub transaction_currency_code: common_enums::Currency,
pub phone_number: Option<String>,
#[schema(value_type = Email)]
pub email: Option<Email>,
pub phone_country_code: Option<String>,
}

#[cfg(feature = "v1")]
Expand Down
54 changes: 53 additions & 1 deletion crates/router/src/core/payments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ use error_stack::{report, ResultExt};
use events::EventInfo;
use futures::future::join_all;
use helpers::{decrypt_paze_token, ApplePayData};
use hyperswitch_domain_models::payments::payment_intent::CustomerData;
#[cfg(feature = "v2")]
use hyperswitch_domain_models::payments::{
PaymentConfirmData, PaymentIntentData, PaymentStatusData,
Expand Down Expand Up @@ -3399,7 +3400,8 @@ pub async fn get_session_token_for_click_to_pay(
payment_intent: &hyperswitch_domain_models::payments::PaymentIntent,
) -> RouterResult<api_models::payments::SessionToken> {
use common_utils::{id_type::MerchantConnectorAccountId, types::AmountConvertor};
use hyperswitch_domain_models::payments::ClickToPayMetaData;
use hyperswitch_domain_models::payments::{payment_intent::CustomerData, ClickToPayMetaData};
use masking::ExposeOptionInterface;

use crate::consts::CLICK_TO_PAY;

Expand Down Expand Up @@ -3439,6 +3441,21 @@ pub async fn get_session_token_for_click_to_pay(
.change_context(errors::ApiErrorResponse::PreconditionFailed {
message: "Failed to convert amount to string major unit for clickToPay".to_string(),
})?;

let optional_customer_details = payment_intent
.customer_details
.as_ref()
.map(|details| {
serde_json::from_value::<CustomerData>(details.clone().into_inner().expose())
})
.transpose()
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("failed to parse customer data from payment intent")?;
optional_customer_details
.as_ref()
.map(validate_customer_details_for_click_to_pay)
.transpose()?;

Ok(api_models::payments::SessionToken::ClickToPay(Box::new(
api_models::payments::ClickToPaySessionResponse {
dpa_id: click_to_pay_metadata.dpa_id,
Expand All @@ -3451,10 +3468,45 @@ pub async fn get_session_token_for_click_to_pay(
merchant_country_code: click_to_pay_metadata.merchant_country_code,
transaction_amount,
transaction_currency_code: transaction_currency,
phone_number: optional_customer_details
.as_ref()
.and_then(|details| details.phone.clone().expose_option()),
email: optional_customer_details
.as_ref()
.and_then(|details| details.email.clone()),
phone_country_code: optional_customer_details
.and_then(|details| details.phone_country_code.clone()),
},
)))
}

fn validate_customer_details_for_click_to_pay(customer_details: &CustomerData) -> RouterResult<()> {
if customer_details.email.is_some() {
return Ok(());
}

match (
customer_details.phone.as_ref(),
customer_details.phone_country_code.as_ref(),
) {
(Some(_), Some(_)) => Ok(()),
(None, None) => Err(errors::ApiErrorResponse::MissingRequiredFields {
field_names: vec!["phone", "phone_country_code"],
})
.attach_printable(
"phone number && phone_country_code is not present in payment_intent.customer_details",
),
(None, Some(_)) => Err(errors::ApiErrorResponse::MissingRequiredField {
field_name: "phone",
})
.attach_printable("phone number is not present in payment_intent.customer_details"),
(Some(_), None) => Err(errors::ApiErrorResponse::MissingRequiredField {
field_name: "phone_country_code",
})
.attach_printable("phone_country_code is not present in payment_intent.customer_details"),
}
}

#[cfg(feature = "v1")]
pub async fn call_create_connector_customer_if_required<F, Req, D>(
state: &SessionState,
Expand Down
Loading