Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(connector): [NMI] Implement webhook for Payments and Refunds #3164

Merged
merged 5 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 117 additions & 7 deletions crates/router/src/connector/nmi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ pub mod transformers;

use std::fmt::Debug;

use common_utils::{ext_traits::ByteSliceExt, request::RequestContent};
use common_utils::{crypto, ext_traits::ByteSliceExt, request::RequestContent};
use diesel_models::enums;
use error_stack::{IntoReport, ResultExt};
use regex::Regex;
use transformers as nmi;

use super::utils as connector_utils;
Expand All @@ -15,6 +16,7 @@ use crate::{
types::{
self,
api::{self, ConnectorCommon, ConnectorCommonExt},
transformers::ForeignFrom,
ErrorResponse,
},
};
Expand Down Expand Up @@ -94,6 +96,14 @@ impl ConnectorValidation for Nmi {
),
}
}

fn validate_psync_reference_id(
&self,
_data: &types::PaymentsSyncRouterData,
) -> CustomResult<(), errors::ConnectorError> {
// in case we dont have transaction id, we can make psync using attempt id
Ok(())
}
}

impl
Expand Down Expand Up @@ -782,24 +792,124 @@ impl ConnectorIntegration<api::RSync, types::RefundsData, types::RefundsResponse

#[async_trait::async_trait]
impl api::IncomingWebhook for Nmi {
fn get_webhook_object_reference_id(
fn get_webhook_source_verification_algorithm(
&self,
_request: &api::IncomingWebhookRequestDetails<'_>,
) -> CustomResult<Box<dyn crypto::VerifySignature + Send>, errors::ConnectorError> {
Ok(Box::new(crypto::HmacSha256))
}

fn get_webhook_source_verification_signature(
&self,
request: &api::IncomingWebhookRequestDetails<'_>,
_connector_webhook_secrets: &api_models::webhooks::ConnectorWebhookSecrets,
) -> CustomResult<Vec<u8>, errors::ConnectorError> {
let sig_header =
connector_utils::get_header_key_value("webhook-signature", request.headers)?;

let regex_pattern = r"t=(.*),s=(.*)";

if let Some(captures) = Regex::new(regex_pattern)
.into_report()
.change_context(errors::ConnectorError::WebhookSignatureNotFound)?
.captures(sig_header)
{
let signature = captures
.get(1)
.ok_or(errors::ConnectorError::WebhookSignatureNotFound)
.into_report()?
.as_str();
return Ok(signature.as_bytes().to_vec());
}

Err(errors::ConnectorError::WebhookSignatureNotFound).into_report()
}

fn get_webhook_source_verification_message(
&self,
request: &api::IncomingWebhookRequestDetails<'_>,
_merchant_id: &str,
_connector_webhook_secrets: &api_models::webhooks::ConnectorWebhookSecrets,
) -> CustomResult<Vec<u8>, errors::ConnectorError> {
let sig_header =
connector_utils::get_header_key_value("webhook-signature", request.headers)?;

let regex_pattern = r"t=(.*),s=(.*)";

if let Some(captures) = Regex::new(regex_pattern)
.into_report()
.change_context(errors::ConnectorError::WebhookSignatureNotFound)?
.captures(sig_header)
{
let nonce = captures
.get(0)
.ok_or(errors::ConnectorError::WebhookSignatureNotFound)
.into_report()?
.as_str();

let message = format!("{}.{}", nonce, String::from_utf8_lossy(request.body));

return Ok(message.into_bytes());
}
Err(errors::ConnectorError::WebhookSignatureNotFound).into_report()
}

fn get_webhook_object_reference_id(
&self,
request: &api::IncomingWebhookRequestDetails<'_>,
) -> CustomResult<api_models::webhooks::ObjectReferenceId, errors::ConnectorError> {
Err(errors::ConnectorError::WebhooksNotImplemented).into_report()
let reference_body: nmi::NmiWebhookObjectReference = request
.body
.parse_struct("nmi NmiWebhookObjectReference")
.change_context(errors::ConnectorError::WebhookResourceObjectNotFound)?;

let object_reference_id = match reference_body.event_body.action.action_type {
nmi::NmiActionType::Sale => api_models::webhooks::ObjectReferenceId::PaymentId(
api_models::payments::PaymentIdType::PaymentAttemptId(
reference_body.event_body.order_id,
),
),
nmi::NmiActionType::Refund => api_models::webhooks::ObjectReferenceId::RefundId(
api_models::webhooks::RefundIdType::RefundId(reference_body.event_body.order_id),
),
_ => Err(errors::ConnectorError::WebhooksNotImplemented).into_report()?,
};

Ok(object_reference_id)
}

fn get_webhook_event_type(
&self,
_request: &api::IncomingWebhookRequestDetails<'_>,
request: &api::IncomingWebhookRequestDetails<'_>,
) -> CustomResult<api::IncomingWebhookEvent, errors::ConnectorError> {
Ok(api::IncomingWebhookEvent::EventNotSupported)
let event_type_body: nmi::NmiWebhookEventBody = request
.body
.parse_struct("nmi NmiWebhookEventType")
.change_context(errors::ConnectorError::WebhookResourceObjectNotFound)?;

Ok(api::IncomingWebhookEvent::foreign_from(
event_type_body.event_type,
))
}

fn get_webhook_resource_object(
&self,
_request: &api::IncomingWebhookRequestDetails<'_>,
request: &api::IncomingWebhookRequestDetails<'_>,
) -> CustomResult<Box<dyn masking::ErasedMaskSerialize>, errors::ConnectorError> {
Err(errors::ConnectorError::WebhooksNotImplemented).into_report()
let webhook_body: nmi::NmiWebhookBody = request
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since, this object will be consumed by PSync handle_response, let's have try_from webhook response object to PSync response object for payments and in case of refund return the object as it is. This will ensure that any changes in PSync will not break the incoming webhook flow

.body
.parse_struct("nmi NmiWebhookBody")
.change_context(errors::ConnectorError::WebhookResourceObjectNotFound)?;

match webhook_body.event_body.action.action_type {
nmi::NmiActionType::Sale
| nmi::NmiActionType::Auth
| nmi::NmiActionType::Capture
| nmi::NmiActionType::Void
| nmi::NmiActionType::Credit => {
Ok(Box::new(nmi::SyncResponse::try_from(&webhook_body)?))
}
nmi::NmiActionType::Refund => Ok(Box::new(webhook_body)),
}
}
}
Loading
Loading