feat(connector): [Stax] Implement Bank Debits and Webhooks for Connector Stax (#1832)

Co-authored-by: Arjun Karthik <m.arjunkarthik@gmail.com>
This commit is contained in:
DEEPANSHU BANSAL
2023-08-08 20:07:00 +05:30
committed by GitHub
parent 58a0cb7814
commit 0f2bb6c09b
8 changed files with 207 additions and 12 deletions

View File

@ -2,15 +2,18 @@ pub mod transformers;
use std::fmt::Debug;
use common_utils::ext_traits::ByteSliceExt;
use error_stack::{IntoReport, ResultExt};
use masking::PeekInterface;
use transformers as stax;
use self::stax::StaxWebhookEventType;
use super::utils::{to_connector_meta, RefundsRequestData};
use crate::{
configs::settings,
consts,
core::errors::{self, CustomResult},
db::StorageInterface,
headers,
services::{
self,
@ -20,7 +23,7 @@ use crate::{
types::{
self,
api::{self, ConnectorCommon, ConnectorCommonExt},
ErrorResponse, Response,
domain, ErrorResponse, Response,
},
utils::{self, BytesExt},
};
@ -751,24 +754,86 @@ impl ConnectorIntegration<api::RSync, types::RefundsData, types::RefundsResponse
#[async_trait::async_trait]
impl api::IncomingWebhook for Stax {
async fn verify_webhook_source(
&self,
_db: &dyn StorageInterface,
_request: &api::IncomingWebhookRequestDetails<'_>,
_merchant_id: &str,
_connector_label: &str,
_key_store: &domain::MerchantKeyStore,
) -> CustomResult<bool, errors::ConnectorError> {
Ok(false)
}
fn get_webhook_object_reference_id(
&self,
_request: &api::IncomingWebhookRequestDetails<'_>,
request: &api::IncomingWebhookRequestDetails<'_>,
) -> CustomResult<api::webhooks::ObjectReferenceId, errors::ConnectorError> {
Err(errors::ConnectorError::WebhooksNotImplemented).into_report()
let webhook_body: stax::StaxWebhookBody = request
.body
.parse_struct("StaxWebhookBody")
.change_context(errors::ConnectorError::WebhookReferenceIdNotFound)?;
match webhook_body.transaction_type {
stax::StaxWebhookEventType::Refund => {
Ok(api_models::webhooks::ObjectReferenceId::RefundId(
api_models::webhooks::RefundIdType::ConnectorRefundId(webhook_body.id),
))
}
stax::StaxWebhookEventType::Unknown => {
Err(errors::ConnectorError::WebhookEventTypeNotFound.into())
}
stax::StaxWebhookEventType::PreAuth
| stax::StaxWebhookEventType::Capture
| stax::StaxWebhookEventType::Charge
| stax::StaxWebhookEventType::Void => {
Ok(api_models::webhooks::ObjectReferenceId::PaymentId(
api_models::payments::PaymentIdType::ConnectorTransactionId(match webhook_body
.transaction_type
{
stax::StaxWebhookEventType::Capture => webhook_body
.auth_id
.ok_or(errors::ConnectorError::WebhookReferenceIdNotFound)?,
_ => webhook_body.id,
}),
))
}
}
}
fn get_webhook_event_type(
&self,
_request: &api::IncomingWebhookRequestDetails<'_>,
request: &api::IncomingWebhookRequestDetails<'_>,
) -> CustomResult<api::IncomingWebhookEvent, errors::ConnectorError> {
Err(errors::ConnectorError::WebhooksNotImplemented).into_report()
let details: stax::StaxWebhookBody = request
.body
.parse_struct("StaxWebhookEventType")
.change_context(errors::ConnectorError::WebhookEventTypeNotFound)?;
Ok(match &details.transaction_type {
StaxWebhookEventType::Refund => match &details.success {
true => api::IncomingWebhookEvent::RefundSuccess,
false => api::IncomingWebhookEvent::RefundFailure,
},
StaxWebhookEventType::Capture | StaxWebhookEventType::Charge => {
match &details.success {
true => api::IncomingWebhookEvent::PaymentIntentSuccess,
false => api::IncomingWebhookEvent::PaymentIntentFailure,
}
}
StaxWebhookEventType::PreAuth
| StaxWebhookEventType::Void
| StaxWebhookEventType::Unknown => api::IncomingWebhookEvent::EventNotSupported,
})
}
fn get_webhook_resource_object(
&self,
_request: &api::IncomingWebhookRequestDetails<'_>,
request: &api::IncomingWebhookRequestDetails<'_>,
) -> CustomResult<serde_json::Value, errors::ConnectorError> {
Err(errors::ConnectorError::WebhooksNotImplemented).into_report()
let reference_object: serde_json::Value = serde_json::from_slice(request.body)
.into_report()
.change_context(errors::ConnectorError::WebhookResourceObjectNotFound)?;
Ok(reference_object)
}
}