feat(router): added incoming refund webhooks flow (#683)

Co-authored-by: sai harsha <sai.harsha@sai.harsha-MacBookPro>
This commit is contained in:
saiharsha-juspay
2023-02-28 19:15:25 +05:30
committed by GitHub
parent 2e99152d35
commit f12abbcef4
12 changed files with 302 additions and 11 deletions

View File

@ -9,7 +9,7 @@ use crate::{
consts,
core::{
errors::{self, CustomResult, RouterResponse},
payments,
payments, refunds,
},
db::StorageInterface,
logger,
@ -90,6 +90,87 @@ async fn payments_incoming_webhook_flow(
Ok(())
}
#[instrument(skip_all)]
async fn refunds_incoming_webhook_flow(
state: AppState,
merchant_account: storage::MerchantAccount,
webhook_details: api::IncomingWebhookDetails,
connector_name: &str,
source_verified: bool,
event_type: api_models::webhooks::IncomingWebhookEvent,
) -> CustomResult<(), errors::WebhooksFlowError> {
let db = &*state.store;
//find refund by connector refund id
let refund = db
.find_refund_by_merchant_id_connector_refund_id_connector(
&merchant_account.merchant_id,
&webhook_details.object_reference_id,
connector_name,
merchant_account.storage_scheme,
)
.await
.change_context(errors::WebhooksFlowError::ResourceNotFound)
.attach_printable_lazy(|| "Failed fetching the refund")?;
let refund_id = refund.refund_id.to_owned();
//if source verified then update refund status else trigger refund sync
let updated_refund = if source_verified {
let refund_update = storage::RefundUpdate::StatusUpdate {
connector_refund_id: None,
sent_to_gateway: true,
refund_status: event_type
.foreign_try_into()
.into_report()
.change_context(errors::WebhooksFlowError::RefundsCoreFailed)?,
};
state
.store
.update_refund(
refund.to_owned(),
refund_update,
merchant_account.storage_scheme,
)
.await
.change_context(errors::WebhooksFlowError::RefundsCoreFailed)
.attach_printable_lazy(|| {
format!(
"Failed while updating refund: refund_id: {}",
refund_id.to_owned()
)
})?
} else {
refunds::refund_retrieve_core(&state, merchant_account.clone(), refund_id.to_owned())
.await
.change_context(errors::WebhooksFlowError::RefundsCoreFailed)
.attach_printable_lazy(|| {
format!(
"Failed while updating refund: refund_id: {}",
refund_id.to_owned()
)
})?
};
let event_type: enums::EventType = updated_refund
.refund_status
.foreign_try_into()
.into_report()
.change_context(errors::WebhooksFlowError::RefundsCoreFailed)?;
let refund_response: api_models::refunds::RefundResponse = updated_refund
.foreign_try_into()
.into_report()
.change_context(errors::WebhooksFlowError::RefundsCoreFailed)?;
create_event_and_trigger_outgoing_webhook(
state,
merchant_account,
event_type,
enums::EventClass::Refunds,
None,
refund_id,
enums::EventObjectType::RefundDetails,
api::OutgoingWebhookContent::RefundDetails(refund_response),
)
.await?;
Ok(())
}
#[allow(clippy::too_many_arguments)]
#[instrument(skip_all)]
async fn create_event_and_trigger_outgoing_webhook(
@ -272,7 +353,7 @@ pub async fn webhooks_core(
)?,
};
let flow_type: api::WebhookFlow = event_type.into();
let flow_type: api::WebhookFlow = event_type.to_owned().into();
match flow_type {
api::WebhookFlow::Payment => payments_incoming_webhook_flow(
state.clone(),
@ -284,6 +365,18 @@ pub async fn webhooks_core(
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("Incoming webhook flow for payments failed")?,
api::WebhookFlow::Refund => refunds_incoming_webhook_flow(
state.clone(),
merchant_account,
webhook_details,
connector_name,
source_verified,
event_type,
)
.await
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("Incoming webhook flow for refunds failed")?,
api::WebhookFlow::ReturnResponse => {}
_ => Err(errors::ApiErrorResponse::InternalServerError)