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

@ -4134,3 +4134,115 @@ pub async fn get_extended_card_info(
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)
}