mirror of
				https://github.com/juspay/hyperswitch.git
				synced 2025-10-31 18:17:13 +08:00 
			
		
		
		
	feat(core): Add outgoing webhook for manual partial_capture events (#3388)
				
					
				
			This commit is contained in:
		| @ -51,7 +51,7 @@ use crate::{ | ||||
|             types::{encrypt_optional, AsyncLift}, | ||||
|         }, | ||||
|         storage, | ||||
|         transformers::{ForeignTryFrom, ForeignTryInto}, | ||||
|         transformers::ForeignFrom, | ||||
|     }, | ||||
| }; | ||||
|  | ||||
| @ -681,23 +681,6 @@ pub fn add_apple_pay_payment_status_metrics( | ||||
|     } | ||||
| } | ||||
|  | ||||
| impl ForeignTryFrom<enums::IntentStatus> for enums::EventType { | ||||
|     type Error = errors::ValidationError; | ||||
|  | ||||
|     fn foreign_try_from(value: enums::IntentStatus) -> Result<Self, Self::Error> { | ||||
|         match value { | ||||
|             enums::IntentStatus::Succeeded => Ok(Self::PaymentSucceeded), | ||||
|             enums::IntentStatus::Failed => Ok(Self::PaymentFailed), | ||||
|             enums::IntentStatus::Processing => Ok(Self::PaymentProcessing), | ||||
|             enums::IntentStatus::RequiresMerchantAction | ||||
|             | enums::IntentStatus::RequiresCustomerAction => Ok(Self::ActionRequired), | ||||
|             _ => Err(errors::ValidationError::IncorrectValueProvided { | ||||
|                 field_name: "intent_status", | ||||
|             }), | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
| pub async fn trigger_payments_webhook<F, Req, Op>( | ||||
|     merchant_account: domain::MerchantAccount, | ||||
|     business_profile: diesel_models::business_profile::BusinessProfile, | ||||
| @ -726,7 +709,9 @@ where | ||||
|  | ||||
|     if matches!( | ||||
|         status, | ||||
|         enums::IntentStatus::Succeeded | enums::IntentStatus::Failed | ||||
|         enums::IntentStatus::Succeeded | ||||
|             | enums::IntentStatus::Failed | ||||
|             | enums::IntentStatus::PartiallyCaptured | ||||
|     ) { | ||||
|         let payments_response = crate::core::payments::transformers::payments_to_payments_response( | ||||
|             req, | ||||
| @ -742,11 +727,7 @@ where | ||||
|             None, | ||||
|         )?; | ||||
|  | ||||
|         let event_type: enums::EventType = status | ||||
|             .foreign_try_into() | ||||
|             .into_report() | ||||
|             .change_context(errors::ApiErrorResponse::WebhookProcessingFailure) | ||||
|             .attach_printable("payment event type mapping failed")?; | ||||
|         let event_type = ForeignFrom::foreign_from(status); | ||||
|  | ||||
|         if let services::ApplicationResponse::JsonWithHeaders((payments_response_json, _)) = | ||||
|             payments_response | ||||
| @ -755,27 +736,34 @@ where | ||||
|             // This spawns this futures in a background thread, the exception inside this future won't affect | ||||
|             // the current thread and the lifecycle of spawn thread is not handled by runtime. | ||||
|             // So when server shutdown won't wait for this thread's completion. | ||||
|             tokio::spawn( | ||||
|                 async move { | ||||
|                     Box::pin( | ||||
|                         webhooks_core::create_event_and_trigger_appropriate_outgoing_webhook( | ||||
|                             m_state, | ||||
|                             merchant_account, | ||||
|                             business_profile, | ||||
|                             event_type, | ||||
|                             diesel_models::enums::EventClass::Payments, | ||||
|                             None, | ||||
|                             payment_id, | ||||
|                             diesel_models::enums::EventObjectType::PaymentDetails, | ||||
|                             webhooks::OutgoingWebhookContent::PaymentDetails( | ||||
|                                 payments_response_json, | ||||
|  | ||||
|             if let Some(event_type) = event_type { | ||||
|                 tokio::spawn( | ||||
|                     async move { | ||||
|                         Box::pin( | ||||
|                             webhooks_core::create_event_and_trigger_appropriate_outgoing_webhook( | ||||
|                                 m_state, | ||||
|                                 merchant_account, | ||||
|                                 business_profile, | ||||
|                                 event_type, | ||||
|                                 diesel_models::enums::EventClass::Payments, | ||||
|                                 None, | ||||
|                                 payment_id, | ||||
|                                 diesel_models::enums::EventObjectType::PaymentDetails, | ||||
|                                 webhooks::OutgoingWebhookContent::PaymentDetails( | ||||
|                                     payments_response_json, | ||||
|                                 ), | ||||
|                             ), | ||||
|                         ), | ||||
|                     ) | ||||
|                     .await | ||||
|                 } | ||||
|                 .in_current_span(), | ||||
|             ); | ||||
|                         ) | ||||
|                         .await | ||||
|                     } | ||||
|                     .in_current_span(), | ||||
|                 ); | ||||
|             } else { | ||||
|                 logger::warn!( | ||||
|                     "Outgoing webhook not sent because of missing event type status mapping" | ||||
|                 ); | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|  | ||||
|  | ||||
		Reference in New Issue
	
	Block a user
	 Sakil Mostak
					Sakil Mostak