mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-11-03 21:37:41 +08:00
feat(compatibility): add support for stripe compatible webhooks (#1728)
This commit is contained in:
committed by
GitHub
parent
14c2d72509
commit
87ae99f7f2
@ -1 +0,0 @@
|
||||
|
||||
45
crates/router/src/core/webhooks/types.rs
Normal file
45
crates/router/src/core/webhooks/types.rs
Normal file
@ -0,0 +1,45 @@
|
||||
use api_models::webhooks;
|
||||
use common_utils::{crypto::SignMessage, ext_traits};
|
||||
use error_stack::ResultExt;
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::{core::errors, headers, services::request::Maskable};
|
||||
|
||||
pub trait OutgoingWebhookType:
|
||||
Serialize + From<webhooks::OutgoingWebhook> + Sync + Send + std::fmt::Debug
|
||||
{
|
||||
fn get_outgoing_webhooks_signature(
|
||||
&self,
|
||||
payment_response_hash_key: Option<String>,
|
||||
) -> errors::CustomResult<Option<String>, errors::WebhooksFlowError>;
|
||||
|
||||
fn add_webhook_header(header: &mut Vec<(String, Maskable<String>)>, signature: String);
|
||||
}
|
||||
|
||||
impl OutgoingWebhookType for webhooks::OutgoingWebhook {
|
||||
fn get_outgoing_webhooks_signature(
|
||||
&self,
|
||||
payment_response_hash_key: Option<String>,
|
||||
) -> errors::CustomResult<Option<String>, errors::WebhooksFlowError> {
|
||||
let webhook_signature_payload =
|
||||
ext_traits::Encode::<serde_json::Value>::encode_to_string_of_json(self)
|
||||
.change_context(errors::WebhooksFlowError::OutgoingWebhookEncodingFailed)
|
||||
.attach_printable("failed encoding outgoing webhook payload")?;
|
||||
|
||||
Ok(payment_response_hash_key
|
||||
.map(|key| {
|
||||
common_utils::crypto::HmacSha512::sign_message(
|
||||
&common_utils::crypto::HmacSha512,
|
||||
key.as_bytes(),
|
||||
webhook_signature_payload.as_bytes(),
|
||||
)
|
||||
})
|
||||
.transpose()
|
||||
.change_context(errors::WebhooksFlowError::OutgoingWebhookSigningFailed)
|
||||
.attach_printable("Failed to sign the message")?
|
||||
.map(hex::encode))
|
||||
}
|
||||
fn add_webhook_header(header: &mut Vec<(String, Maskable<String>)>, signature: String) {
|
||||
header.push((headers::X_WEBHOOK_SIGNATURE.to_string(), signature.into()))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user