feat(router): add payments manual-update api (#5045)

Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com>
This commit is contained in:
Sai Harsha Vardhan
2024-06-26 19:10:09 +05:30
committed by GitHub
parent 2e1167acbc
commit ed021c1d99
13 changed files with 292 additions and 8 deletions

View File

@ -14,8 +14,9 @@ use crate::{
PaymentListResponse, PaymentListResponseV2, PaymentsApproveRequest, PaymentsCancelRequest, PaymentListResponse, PaymentListResponseV2, PaymentsApproveRequest, PaymentsCancelRequest,
PaymentsCaptureRequest, PaymentsCompleteAuthorizeRequest, PaymentsCaptureRequest, PaymentsCompleteAuthorizeRequest,
PaymentsExternalAuthenticationRequest, PaymentsExternalAuthenticationResponse, PaymentsExternalAuthenticationRequest, PaymentsExternalAuthenticationResponse,
PaymentsIncrementalAuthorizationRequest, PaymentsRejectRequest, PaymentsRequest, PaymentsIncrementalAuthorizationRequest, PaymentsManualUpdateRequest,
PaymentsResponse, PaymentsRetrieveRequest, PaymentsStartRequest, RedirectionResponse, PaymentsRejectRequest, PaymentsRequest, PaymentsResponse, PaymentsRetrieveRequest,
PaymentsStartRequest, RedirectionResponse,
}, },
}; };
impl ApiEventMetric for PaymentsRetrieveRequest { impl ApiEventMetric for PaymentsRetrieveRequest {
@ -239,3 +240,11 @@ impl ApiEventMetric for PaymentsExternalAuthenticationRequest {
} }
impl ApiEventMetric for ExtendedCardInfoResponse {} impl ApiEventMetric for ExtendedCardInfoResponse {}
impl ApiEventMetric for PaymentsManualUpdateRequest {
fn get_api_event_type(&self) -> Option<ApiEventsType> {
Some(ApiEventsType::Payment {
payment_id: self.payment_id.clone(),
})
}
}

View File

@ -4653,6 +4653,25 @@ pub struct PaymentsExternalAuthenticationRequest {
pub threeds_method_comp_ind: ThreeDsCompletionIndicator, pub threeds_method_comp_ind: ThreeDsCompletionIndicator,
} }
#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, ToSchema)]
pub struct PaymentsManualUpdateRequest {
/// The identifier for the payment
#[serde(skip)]
pub payment_id: String,
/// The identifier for the payment attempt
pub attempt_id: String,
/// Merchant ID
pub merchant_id: String,
/// The status of the attempt
pub attempt_status: Option<enums::AttemptStatus>,
/// Error code of the connector
pub error_code: Option<String>,
/// Error message of the connector
pub error_message: Option<String>,
/// Error reason of the connector
pub error_reason: Option<String>,
}
#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, ToSchema)] #[derive(Debug, serde::Serialize, serde::Deserialize, Clone, ToSchema)]
pub enum ThreeDsCompletionIndicator { pub enum ThreeDsCompletionIndicator {
/// 3DS method successfully completed /// 3DS method successfully completed

View File

@ -351,6 +351,15 @@ pub enum PaymentAttemptUpdate {
authentication_id: Option<String>, authentication_id: Option<String>,
updated_by: String, updated_by: String,
}, },
ManualUpdate {
status: Option<storage_enums::AttemptStatus>,
error_code: Option<String>,
error_message: Option<String>,
error_reason: Option<String>,
updated_by: String,
unified_code: Option<String>,
unified_message: Option<String>,
},
} }
#[derive(Clone, Debug, Default, AsChangeset, router_derive::DebugAsDisplay)] #[derive(Clone, Debug, Default, AsChangeset, router_derive::DebugAsDisplay)]
@ -884,6 +893,24 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
updated_by, updated_by,
..Default::default() ..Default::default()
}, },
PaymentAttemptUpdate::ManualUpdate {
status,
error_code,
error_message,
error_reason,
updated_by,
unified_code,
unified_message,
} => Self {
status,
error_code: error_code.map(Some),
error_message: error_message.map(Some),
error_reason: error_reason.map(Some),
updated_by,
unified_code: unified_code.map(Some),
unified_message: unified_message.map(Some),
..Default::default()
},
} }
} }
} }

View File

@ -206,6 +206,10 @@ pub enum PaymentIntentUpdate {
CompleteAuthorizeUpdate { CompleteAuthorizeUpdate {
shipping_address_id: Option<String>, shipping_address_id: Option<String>,
}, },
ManualUpdate {
status: Option<storage_enums::IntentStatus>,
updated_by: String,
},
} }
#[derive(Clone, Debug, Default, AsChangeset, router_derive::DebugAsDisplay)] #[derive(Clone, Debug, Default, AsChangeset, router_derive::DebugAsDisplay)]
@ -508,6 +512,11 @@ impl From<PaymentIntentUpdate> for PaymentIntentUpdateInternal {
shipping_address_id, shipping_address_id,
..Default::default() ..Default::default()
}, },
PaymentIntentUpdate::ManualUpdate { status, updated_by } => Self {
status,
updated_by,
..Default::default()
},
} }
} }
} }

View File

@ -454,6 +454,15 @@ pub enum PaymentAttemptUpdate {
authentication_id: Option<String>, authentication_id: Option<String>,
updated_by: String, updated_by: String,
}, },
ManualUpdate {
status: Option<storage_enums::AttemptStatus>,
error_code: Option<String>,
error_message: Option<String>,
error_reason: Option<String>,
updated_by: String,
unified_code: Option<String>,
unified_message: Option<String>,
},
} }
impl ForeignIDRef for PaymentAttempt { impl ForeignIDRef for PaymentAttempt {

View File

@ -214,6 +214,10 @@ pub enum PaymentIntentUpdate {
CompleteAuthorizeUpdate { CompleteAuthorizeUpdate {
shipping_address_id: Option<String>, shipping_address_id: Option<String>,
}, },
ManualUpdate {
status: Option<storage_enums::IntentStatus>,
updated_by: String,
},
} }
#[derive(Clone, Debug, Default)] #[derive(Clone, Debug, Default)]
@ -442,6 +446,12 @@ impl From<PaymentIntentUpdate> for PaymentIntentUpdateInternal {
shipping_address_id, shipping_address_id,
..Default::default() ..Default::default()
}, },
PaymentIntentUpdate::ManualUpdate { status, updated_by } => Self {
status,
modified_at: Some(common_utils::date_time::now()),
updated_by,
..Default::default()
},
} }
} }
} }
@ -611,6 +621,9 @@ impl From<PaymentIntentUpdate> for DieselPaymentIntentUpdate {
} => Self::CompleteAuthorizeUpdate { } => Self::CompleteAuthorizeUpdate {
shipping_address_id, shipping_address_id,
}, },
PaymentIntentUpdate::ManualUpdate { status, updated_by } => {
Self::ManualUpdate { status, updated_by }
}
} }
} }
} }

View File

@ -4134,3 +4134,115 @@ pub async fn get_extended_card_info(
payments_api::ExtendedCardInfoResponse { payload }, payments_api::ExtendedCardInfoResponse { payload },
)) ))
} }
#[cfg(feature = "olap")]
pub async fn payments_manual_update(
state: SessionState,
req: api_models::payments::PaymentsManualUpdateRequest,
) -> RouterResponse<serde_json::Value> {
let api_models::payments::PaymentsManualUpdateRequest {
payment_id,
attempt_id,
merchant_id,
attempt_status,
error_code,
error_message,
error_reason,
} = req;
let key_store = state
.store
.get_merchant_key_store_by_merchant_id(
&merchant_id,
&state.store.get_master_key().to_vec().into(),
)
.await
.to_not_found_response(errors::ApiErrorResponse::MerchantAccountNotFound)
.attach_printable("Error while fetching the key store by merchant_id")?;
let merchant_account = state
.store
.find_merchant_account_by_merchant_id(&merchant_id, &key_store)
.await
.to_not_found_response(errors::ApiErrorResponse::MerchantAccountNotFound)
.attach_printable("Error while fetching the merchant_account by merchant_id")?;
let payment_attempt = state
.store
.find_payment_attempt_by_payment_id_merchant_id_attempt_id(
&payment_id,
&merchant_id,
&attempt_id.clone(),
merchant_account.storage_scheme,
)
.await
.to_not_found_response(errors::ApiErrorResponse::PaymentNotFound)
.attach_printable(
"Error while fetching the payment_attempt by payment_id, merchant_id and attempt_id",
)?;
let payment_intent = state
.store
.find_payment_intent_by_payment_id_merchant_id(
&payment_id,
&merchant_account.merchant_id,
&key_store,
merchant_account.storage_scheme,
)
.await
.to_not_found_response(errors::ApiErrorResponse::PaymentNotFound)
.attach_printable("Error while fetching the payment_intent by payment_id, merchant_id")?;
let option_gsm = if let Some(((code, message), connector_name)) = error_code
.as_ref()
.zip(error_message.as_ref())
.zip(payment_attempt.connector.as_ref())
{
helpers::get_gsm_record(
&state,
Some(code.to_string()),
Some(message.to_string()),
connector_name.to_string(),
// We need to get the unified_code and unified_message of the Authorize flow
"Authorize".to_string(),
)
.await
} else {
None
};
// Update the payment_attempt
let attempt_update = storage::PaymentAttemptUpdate::ManualUpdate {
status: attempt_status,
error_code,
error_message,
error_reason,
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),
};
let updated_payment_attempt = state
.store
.update_payment_attempt_with_attempt_id(
payment_attempt.clone(),
attempt_update,
merchant_account.storage_scheme,
)
.await
.to_not_found_response(errors::ApiErrorResponse::PaymentNotFound)
.attach_printable("Error while updating the payment_attempt")?;
// If the payment_attempt is active attempt for an intent, update the intent status
if payment_intent.active_attempt.get_id() == payment_attempt.attempt_id {
let intent_status = enums::IntentStatus::foreign_from(updated_payment_attempt.status);
let payment_intent_update = storage::PaymentIntentUpdate::ManualUpdate {
status: Some(intent_status),
updated_by: merchant_account.storage_scheme.to_string(),
};
state
.store
.update_payment_intent(
payment_intent,
payment_intent_update,
&key_store,
merchant_account.storage_scheme,
)
.await
.to_not_found_response(errors::ApiErrorResponse::PaymentNotFound)
.attach_printable("Error while updating payment_intent")?;
}
Ok(services::ApplicationResponse::StatusOk)
}

View File

@ -460,6 +460,10 @@ impl Payments {
) )
.service(web::resource("/filter").route(web::post().to(get_filters_for_payments))) .service(web::resource("/filter").route(web::post().to(get_filters_for_payments)))
.service(web::resource("/v2/filter").route(web::get().to(get_payment_filters))) .service(web::resource("/v2/filter").route(web::get().to(get_payment_filters)))
.service(
web::resource("/{payment_id}/manual-update")
.route(web::put().to(payments_manual_update)),
)
} }
#[cfg(feature = "oltp")] #[cfg(feature = "oltp")]
{ {

View File

@ -124,7 +124,8 @@ impl From<Flow> for ApiIdentifier {
| Flow::PaymentsExternalAuthentication | Flow::PaymentsExternalAuthentication
| Flow::PaymentsAuthorize | Flow::PaymentsAuthorize
| Flow::GetExtendedCardInfo | Flow::GetExtendedCardInfo
| Flow::PaymentsCompleteAuthorize => Self::Payments, | Flow::PaymentsCompleteAuthorize
| Flow::PaymentsManualUpdate => Self::Payments,
Flow::PayoutsCreate Flow::PayoutsCreate
| Flow::PayoutsRetrieve | Flow::PayoutsRetrieve

View File

@ -1394,6 +1394,35 @@ pub async fn post_3ds_payments_authorize(
.await .await
} }
#[cfg(feature = "olap")]
pub async fn payments_manual_update(
state: web::Data<app::AppState>,
req: actix_web::HttpRequest,
json_payload: web::Json<payment_types::PaymentsManualUpdateRequest>,
path: web::Path<String>,
) -> impl Responder {
let flow = Flow::PaymentsManualUpdate;
let mut payload = json_payload.into_inner();
let payment_id = path.into_inner();
let locking_action = payload.get_locking_input(flow.clone());
tracing::Span::current().record("payment_id", &payment_id);
payload.payment_id = payment_id;
Box::pin(api::server_wrap(
flow,
state,
&req,
payload,
|state, _auth, req, _req_state| payments::payments_manual_update(state, req),
&auth::AdminApiAuth,
locking_action,
))
.await
}
/// Retrieve endpoint for merchant to fetch the encrypted customer payment method data /// Retrieve endpoint for merchant to fetch the encrypted customer payment method data
#[instrument(skip_all, fields(flow = ?Flow::GetExtendedCardInfo, payment_id))] #[instrument(skip_all, fields(flow = ?Flow::GetExtendedCardInfo, payment_id))]
pub async fn retrieve_extended_card_info( pub async fn retrieve_extended_card_info(
@ -1654,3 +1683,19 @@ impl GetLockingInput for payment_types::PaymentsExternalAuthenticationRequest {
} }
} }
} }
impl GetLockingInput for payment_types::PaymentsManualUpdateRequest {
fn get_locking_input<F>(&self, flow: F) -> api_locking::LockAction
where
F: types::FlowMetric,
lock_utils::ApiIdentifier: From<F>,
{
api_locking::LockAction::Hold {
input: api_locking::LockingInput {
unique_locking_key: self.payment_id.to_owned(),
api_identifier: lock_utils::ApiIdentifier::from(flow),
override_lock_retries: None,
},
}
}
}

View File

@ -8,11 +8,11 @@ pub use api_models::payments::{
PaymentRetrieveBody, PaymentRetrieveBodyWithCredentials, PaymentsApproveRequest, PaymentRetrieveBody, PaymentRetrieveBodyWithCredentials, PaymentsApproveRequest,
PaymentsCancelRequest, PaymentsCaptureRequest, PaymentsCompleteAuthorizeRequest, PaymentsCancelRequest, PaymentsCaptureRequest, PaymentsCompleteAuthorizeRequest,
PaymentsExternalAuthenticationRequest, PaymentsIncrementalAuthorizationRequest, PaymentsExternalAuthenticationRequest, PaymentsIncrementalAuthorizationRequest,
PaymentsRedirectRequest, PaymentsRedirectionResponse, PaymentsRejectRequest, PaymentsRequest, PaymentsManualUpdateRequest, PaymentsRedirectRequest, PaymentsRedirectionResponse,
PaymentsResponse, PaymentsResponseForm, PaymentsRetrieveRequest, PaymentsSessionRequest, PaymentsRejectRequest, PaymentsRequest, PaymentsResponse, PaymentsResponseForm,
PaymentsSessionResponse, PaymentsStartRequest, PgRedirectResponse, PhoneDetails, PaymentsRetrieveRequest, PaymentsSessionRequest, PaymentsSessionResponse, PaymentsStartRequest,
RedirectionResponse, SessionToken, TimeRange, UrlDetails, VerifyRequest, VerifyResponse, PgRedirectResponse, PhoneDetails, RedirectionResponse, SessionToken, TimeRange, UrlDetails,
WalletData, VerifyRequest, VerifyResponse, WalletData,
}; };
use error_stack::ResultExt; use error_stack::ResultExt;
pub use hyperswitch_domain_models::router_flow_types::payments::{ pub use hyperswitch_domain_models::router_flow_types::payments::{

View File

@ -446,6 +446,8 @@ pub enum Flow {
ToggleConnectorAgnosticMit, ToggleConnectorAgnosticMit,
/// Get the extended card info associated to a payment_id /// Get the extended card info associated to a payment_id
GetExtendedCardInfo, GetExtendedCardInfo,
/// Manually update the payment details like status, error code, error message etc.
PaymentsManualUpdate,
} }
/// ///

View File

@ -1748,6 +1748,23 @@ impl DataModelExt for PaymentAttemptUpdate {
authentication_id, authentication_id,
updated_by, updated_by,
}, },
Self::ManualUpdate {
status,
error_code,
error_message,
error_reason,
updated_by,
unified_code,
unified_message,
} => DieselPaymentAttemptUpdate::ManualUpdate {
status,
error_code,
error_message,
error_reason,
updated_by,
unified_code,
unified_message,
},
} }
} }
@ -2079,6 +2096,23 @@ impl DataModelExt for PaymentAttemptUpdate {
authentication_id, authentication_id,
updated_by, updated_by,
}, },
DieselPaymentAttemptUpdate::ManualUpdate {
status,
error_code,
error_message,
error_reason,
updated_by,
unified_code,
unified_message,
} => Self::ManualUpdate {
status,
error_code,
error_message,
error_reason,
updated_by,
unified_code,
unified_message,
},
} }
} }
} }