Repository: juspay/hyperswitch
File: crates/router/src/core/webhooks/incoming.rs (Rust)

Code before:
    .await
    .attach_printable("Payout retrieval failed for given Payout request")?;

    let event_type: Option<enums::EventType> = payout_data.payout_attempt.status.into();

    // If event is NOT an UnsupportedEvent, trigger Outgoing Webhook
    if let Some(outgoing_event_type) = event_type {
        let payout_create_response =
            payouts::response_handler(&state, &platform, payout_data).await?;

        Box::pin(super::create_event_and_trigger_outgoing_webhook(
            state,
            platform,
            business_profile,
            outgoing_event_type,
            enums::EventClass::Payouts,
            payout_data
                .payout_attempt
                .payout_id
                .get_string_repr()
                .to_string(),
            enums::EventObjectType::PayoutDetails,
            api::OutgoingWebhookContent::PayoutDetails(Box::new(payout_create_response)),
            Some(payout_data.payout_attempt.created_at),
        ))
        .await?;
    }

    Ok(WebhookResponseTracker::Payout {
        payout_id: payout_data.payout_attempt.payout_id.clone(),
        status: payout_data.payout_attempt.status,
    })
}

async fn relay_refunds_incoming_webhook_flow(
    state: SessionState,
    platform: domain::Platform,
    business_profile: domain::Profile,
    webhook_details: api::IncomingWebhookDetails,
    event_type: webhooks::IncomingWebhookEvent,
    source_verified: bool,
) -> CustomResult<WebhookResponseTracker, errors::ApiErrorResponse> {
    let db = &*state.store;

    let relay_record = match webhook_details.object_reference_id {
        webhooks::ObjectReferenceId::RefundId(refund_id_type) => match refund_id_type {
            webhooks::RefundIdType::RefundId(refund_id) => {
                let relay_id = common_utils::id_type::RelayId::from_str(&refund_id)
                    .change_context(errors::ValidationError::IncorrectValueProvided {
                        field_name: "relay_id",
                    })

Review comment (refactor):
can we move this inside `create_payout_retrieve` since we would want sync api to trigger webhook as well

Code after:
    .await
    .attach_printable("Payout retrieval failed for given Payout request")?;

    let event_type: Option<enums::EventType> = payout_data.payout_attempt.status.into();

    // If event is NOT an UnsupportedEvent, trigger Outgoing Webhook
    if let Some(outgoing_event_type) = event_type {
        let payout_response = payouts::response_handler(&state, &platform, payout_data).await?;

        Box::pin(super::create_event_and_trigger_outgoing_webhook(
            state,
            platform.get_processor().clone(),
            business_profile,
            outgoing_event_type,
            enums::EventClass::Payouts,
            payout_data
                .payout_attempt
                .payout_id
                .get_string_repr()
                .to_string(),
            enums::EventObjectType::PayoutDetails,
            api::OutgoingWebhookContent::PayoutDetails(Box::new(payout_response)),
            Some(payout_data.payout_attempt.created_at),
        ))
        .await?;
    }

    Ok(WebhookResponseTracker::Payout {
        payout_id: payout_data.payout_attempt.payout_id.clone(),
        status: payout_data.payout_attempt.status,
    })
}

async fn relay_refunds_incoming_webhook_flow(
    state: SessionState,
    platform: domain::Platform,
    business_profile: domain::Profile,
    webhook_details: api::IncomingWebhookDetails,
    event_type: webhooks::IncomingWebhookEvent,
    source_verified: bool,
) -> CustomResult<WebhookResponseTracker, errors::ApiErrorResponse> {
    let db = &*state.store;

    let relay_record = match webhook_details.object_reference_id {
        webhooks::ObjectReferenceId::RefundId(refund_id_type) => match refund_id_type {
            webhooks::RefundIdType::RefundId(refund_id) => {
                let relay_id = common_utils::id_type::RelayId::from_str(&refund_id)
                    .change_context(errors::ValidationError::IncorrectValueProvided {
                        field_name: "relay_id",
                    })
                    .change_context(errors::ApiErrorResponse::InternalServerError)?;