Skip to content

Commit

Permalink
feat(connector): [Novalnet] Add support for disputes (#6560)
Browse files Browse the repository at this point in the history
  • Loading branch information
cookieg13 authored Nov 18, 2024
1 parent 053f810 commit 6881ce2
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
42 changes: 41 additions & 1 deletion crates/hyperswitch_connectors/src/connectors/novalnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use hyperswitch_interfaces::{
ConnectorValidation,
},
configs::Connectors,
errors,
disputes, errors,
events::connector_api_logs::ConnectorEvent,
types::{self, Response},
webhooks,
Expand Down Expand Up @@ -877,4 +877,44 @@ impl webhooks::IncomingWebhook for Novalnet {
.change_context(errors::ConnectorError::WebhookResourceObjectNotFound)?;
Ok(Box::new(notif))
}

fn get_dispute_details(
&self,
request: &webhooks::IncomingWebhookRequestDetails<'_>,
) -> CustomResult<disputes::DisputePayload, errors::ConnectorError> {
let notif: transformers::NovalnetWebhookNotificationResponse =
get_webhook_object_from_body(request.body)
.change_context(errors::ConnectorError::WebhookBodyDecodingFailed)?;
let (amount, currency, reason, reason_code) = match notif.transaction {
novalnet::NovalnetWebhookTransactionData::CaptureTransactionData(data) => {
(data.amount, data.currency, None, None)
}
novalnet::NovalnetWebhookTransactionData::CancelTransactionData(data) => {
(data.amount, data.currency, None, None)
}

novalnet::NovalnetWebhookTransactionData::RefundsTransactionData(data) => {
(data.amount, data.currency, None, None)
}

novalnet::NovalnetWebhookTransactionData::SyncTransactionData(data) => {
(data.amount, data.currency, data.reason, data.reason_code)
}
};

let dispute_status =
novalnet::get_novalnet_dispute_status(notif.event.event_type).to_string();
Ok(disputes::DisputePayload {
amount: novalnet::option_to_result(amount)?.to_string(),
currency: novalnet::option_to_result(currency)?.to_string(),
dispute_stage: api_models::enums::DisputeStage::Dispute,
connector_dispute_id: notif.event.tid.to_string(),
connector_reason: reason,
connector_reason_code: reason_code,
challenge_required_by: None,
connector_status: dispute_status,
created_at: None,
updated_at: None,
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1294,6 +1294,8 @@ pub enum WebhookEventType {
TransactionCapture,
TransactionCancel,
TransactionRefund,
Chargeback,
Credit,
}

#[derive(Serialize, Deserialize, Debug)]
Expand Down Expand Up @@ -1356,9 +1358,30 @@ pub fn get_incoming_webhook_event(
}
_ => IncomingWebhookEvent::RefundFailure,
},
WebhookEventType::Chargeback => IncomingWebhookEvent::DisputeOpened,
WebhookEventType::Credit => IncomingWebhookEvent::DisputeWon,
}
}

pub fn reverse_string(s: &str) -> String {
s.chars().rev().collect()
}

#[derive(Display, Debug, Serialize, Deserialize)]
pub enum WebhookDisputeStatus {
DisputeOpened,
DisputeWon,
Unknown,
}

pub fn get_novalnet_dispute_status(status: WebhookEventType) -> WebhookDisputeStatus {
match status {
WebhookEventType::Chargeback => WebhookDisputeStatus::DisputeOpened,
WebhookEventType::Credit => WebhookDisputeStatus::DisputeWon,
_ => WebhookDisputeStatus::Unknown,
}
}

pub fn option_to_result<T>(opt: Option<T>) -> Result<T, errors::ConnectorError> {
opt.ok_or(errors::ConnectorError::WebhookBodyDecodingFailed)
}

0 comments on commit 6881ce2

Please sign in to comment.