From 5be0c2bfd28e5a898842e1e24b51b41439aa92b3 Mon Sep 17 00:00:00 2001 From: Sai Harsha Vardhan <56996463+sai-harsha-vardhan@users.noreply.github.com> Date: Thu, 22 Aug 2024 15:48:15 +0530 Subject: [PATCH] refactor(router): add connector_transaction_id, send response body and use admin_api_auth_with_merchant_id for payments manual update flow (#5658) --- crates/api_models/src/events/payment.rs | 14 ++++++++--- crates/api_models/src/payments.rs | 23 +++++++++++++++++++ crates/diesel_models/src/payment_attempt.rs | 4 +++- .../src/payments/payment_attempt.rs | 1 + crates/router/src/core/payments.rs | 17 ++++++++++++-- crates/router/src/routes/payments.rs | 2 +- .../src/payments/payment_attempt.rs | 4 ++++ 7 files changed, 58 insertions(+), 7 deletions(-) diff --git a/crates/api_models/src/events/payment.rs b/crates/api_models/src/events/payment.rs index 941cbb3330..c4459223a5 100644 --- a/crates/api_models/src/events/payment.rs +++ b/crates/api_models/src/events/payment.rs @@ -22,9 +22,9 @@ use crate::{ PaymentsApproveRequest, PaymentsCancelRequest, PaymentsCaptureRequest, PaymentsCompleteAuthorizeRequest, PaymentsExternalAuthenticationRequest, PaymentsExternalAuthenticationResponse, PaymentsIncrementalAuthorizationRequest, - PaymentsManualUpdateRequest, PaymentsRejectRequest, PaymentsRequest, PaymentsResponse, - PaymentsRetrieveRequest, PaymentsSessionResponse, PaymentsStartRequest, - RedirectionResponse, + PaymentsManualUpdateRequest, PaymentsManualUpdateResponse, PaymentsRejectRequest, + PaymentsRequest, PaymentsResponse, PaymentsRetrieveRequest, PaymentsSessionResponse, + PaymentsStartRequest, RedirectionResponse, }, }; impl ApiEventMetric for PaymentsRetrieveRequest { @@ -262,6 +262,14 @@ impl ApiEventMetric for PaymentsManualUpdateRequest { } } +impl ApiEventMetric for PaymentsManualUpdateResponse { + fn get_api_event_type(&self) -> Option { + Some(ApiEventsType::Payment { + payment_id: self.payment_id.clone(), + }) + } +} + impl ApiEventMetric for PaymentsSessionResponse { fn get_api_event_type(&self) -> Option { Some(ApiEventsType::Payment { diff --git a/crates/api_models/src/payments.rs b/crates/api_models/src/payments.rs index fcf884492e..e303fbc979 100644 --- a/crates/api_models/src/payments.rs +++ b/crates/api_models/src/payments.rs @@ -5054,6 +5054,29 @@ pub struct PaymentsManualUpdateRequest { pub error_message: Option, /// Error reason of the connector pub error_reason: Option, + /// A unique identifier for a payment provided by the connector + pub connector_transaction_id: Option, +} + +#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, ToSchema)] +pub struct PaymentsManualUpdateResponse { + /// The identifier for the payment + pub payment_id: String, + /// The identifier for the payment attempt + pub attempt_id: String, + /// Merchant ID + #[schema(value_type = String)] + pub merchant_id: id_type::MerchantId, + /// The status of the attempt + pub attempt_status: enums::AttemptStatus, + /// Error code of the connector + pub error_code: Option, + /// Error message of the connector + pub error_message: Option, + /// Error reason of the connector + pub error_reason: Option, + /// A unique identifier for a payment provided by the connector + pub connector_transaction_id: Option, } #[derive(Debug, serde::Serialize, serde::Deserialize, Clone, ToSchema)] diff --git a/crates/diesel_models/src/payment_attempt.rs b/crates/diesel_models/src/payment_attempt.rs index 525c681a1e..84811f2262 100644 --- a/crates/diesel_models/src/payment_attempt.rs +++ b/crates/diesel_models/src/payment_attempt.rs @@ -436,6 +436,7 @@ pub enum PaymentAttemptUpdate { updated_by: String, unified_code: Option, unified_message: Option, + connector_transaction_id: Option, }, } @@ -1645,6 +1646,7 @@ impl From for PaymentAttemptUpdateInternal { updated_by, unified_code, unified_message, + connector_transaction_id, } => Self { status, error_code: error_code.map(Some), @@ -1657,7 +1659,7 @@ impl From for PaymentAttemptUpdateInternal { amount: None, net_amount: None, currency: None, - connector_transaction_id: None, + connector_transaction_id, amount_to_capture: None, connector: None, authentication_type: None, diff --git a/crates/hyperswitch_domain_models/src/payments/payment_attempt.rs b/crates/hyperswitch_domain_models/src/payments/payment_attempt.rs index 54c86f3a4e..5f2bc035f5 100644 --- a/crates/hyperswitch_domain_models/src/payments/payment_attempt.rs +++ b/crates/hyperswitch_domain_models/src/payments/payment_attempt.rs @@ -473,6 +473,7 @@ pub enum PaymentAttemptUpdate { updated_by: String, unified_code: Option, unified_message: Option, + connector_transaction_id: Option, }, } diff --git a/crates/router/src/core/payments.rs b/crates/router/src/core/payments.rs index db24b02880..287e144f39 100644 --- a/crates/router/src/core/payments.rs +++ b/crates/router/src/core/payments.rs @@ -4416,7 +4416,7 @@ pub async fn get_extended_card_info( pub async fn payments_manual_update( state: SessionState, req: api_models::payments::PaymentsManualUpdateRequest, -) -> RouterResponse { +) -> RouterResponse { let api_models::payments::PaymentsManualUpdateRequest { payment_id, attempt_id, @@ -4425,6 +4425,7 @@ pub async fn payments_manual_update( error_code, error_message, error_reason, + connector_transaction_id, } = req; let key_manager_state = &(&state).into(); let key_store = state @@ -4494,6 +4495,7 @@ pub async fn payments_manual_update( updated_by: merchant_account.storage_scheme.to_string(), unified_code: option_gsm.as_ref().and_then(|gsm| gsm.unified_code.clone()), unified_message: option_gsm.and_then(|gsm| gsm.unified_message), + connector_transaction_id, }; let updated_payment_attempt = state .store @@ -4525,5 +4527,16 @@ pub async fn payments_manual_update( .to_not_found_response(errors::ApiErrorResponse::PaymentNotFound) .attach_printable("Error while updating payment_intent")?; } - Ok(services::ApplicationResponse::StatusOk) + Ok(services::ApplicationResponse::Json( + api_models::payments::PaymentsManualUpdateResponse { + payment_id: updated_payment_attempt.payment_id, + attempt_id: updated_payment_attempt.attempt_id, + merchant_id: updated_payment_attempt.merchant_id, + attempt_status: updated_payment_attempt.status, + error_code: updated_payment_attempt.error_code, + error_message: updated_payment_attempt.error_message, + error_reason: updated_payment_attempt.error_reason, + connector_transaction_id: updated_payment_attempt.connector_transaction_id, + }, + )) } diff --git a/crates/router/src/routes/payments.rs b/crates/router/src/routes/payments.rs index 38ab38925e..d0c4cf42b7 100644 --- a/crates/router/src/routes/payments.rs +++ b/crates/router/src/routes/payments.rs @@ -1486,7 +1486,7 @@ pub async fn payments_manual_update( &req, payload, |state, _auth, req, _req_state| payments::payments_manual_update(state, req), - &auth::AdminApiAuth, + &auth::AdminApiAuthWithMerchantId::default(), locking_action, )) .await diff --git a/crates/storage_impl/src/payments/payment_attempt.rs b/crates/storage_impl/src/payments/payment_attempt.rs index 03a9ac6e45..a599618615 100644 --- a/crates/storage_impl/src/payments/payment_attempt.rs +++ b/crates/storage_impl/src/payments/payment_attempt.rs @@ -1917,6 +1917,7 @@ impl DataModelExt for PaymentAttemptUpdate { updated_by, unified_code, unified_message, + connector_transaction_id, } => DieselPaymentAttemptUpdate::ManualUpdate { status, error_code, @@ -1925,6 +1926,7 @@ impl DataModelExt for PaymentAttemptUpdate { updated_by, unified_code, unified_message, + connector_transaction_id, }, } } @@ -2267,6 +2269,7 @@ impl DataModelExt for PaymentAttemptUpdate { updated_by, unified_code, unified_message, + connector_transaction_id, } => Self::ManualUpdate { status, error_code, @@ -2275,6 +2278,7 @@ impl DataModelExt for PaymentAttemptUpdate { updated_by, unified_code, unified_message, + connector_transaction_id, }, } }