Skip to content

Commit

Permalink
refactor(connector): [Volt] Refactor Payments and Refunds Webhooks (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
swangi-kumari authored Jan 18, 2024
1 parent ee0461e commit acb3296
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 38 deletions.
71 changes: 34 additions & 37 deletions crates/router/src/connector/volt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -635,43 +635,40 @@ impl api::IncomingWebhook for Volt {
&self,
request: &api::IncomingWebhookRequestDetails<'_>,
) -> CustomResult<api::webhooks::ObjectReferenceId, errors::ConnectorError> {
let x_volt_type =
utils::get_header_key_value(webhook_headers::X_VOLT_TYPE, request.headers)?;
if x_volt_type == "refund_confirmed" || x_volt_type == "refund_failed" {
let refund_webhook_body: volt::VoltRefundWebhookBodyReference = request
.body
.parse_struct("VoltRefundWebhookBodyReference")
.change_context(errors::ConnectorError::WebhookReferenceIdNotFound)?;

let refund_reference = match refund_webhook_body.external_reference {
Some(external_reference) => {
api_models::webhooks::RefundIdType::RefundId(external_reference)
}
None => api_models::webhooks::RefundIdType::ConnectorRefundId(
refund_webhook_body.refund,
),
};
Ok(api_models::webhooks::ObjectReferenceId::RefundId(
refund_reference,
))
} else {
let webhook_body: volt::VoltPaymentWebhookBodyReference = request
.body
.parse_struct("VoltPaymentWebhookBodyReference")
.change_context(errors::ConnectorError::WebhookReferenceIdNotFound)?;
let reference = match webhook_body.merchant_internal_reference {
Some(merchant_internal_reference) => {
api_models::payments::PaymentIdType::PaymentAttemptId(
merchant_internal_reference,
)
}
None => api_models::payments::PaymentIdType::ConnectorTransactionId(
webhook_body.payment,
),
};
Ok(api_models::webhooks::ObjectReferenceId::PaymentId(
reference,
))
let parsed_webhook_response = request
.body
.parse_struct::<volt::WebhookResponse>("VoltRefundWebhookBodyReference")
.change_context(errors::ConnectorError::WebhookReferenceIdNotFound)?;

match parsed_webhook_response {
volt::WebhookResponse::Payment(payment_response) => {
let reference = match payment_response.merchant_internal_reference {
Some(merchant_internal_reference) => {
api_models::payments::PaymentIdType::PaymentAttemptId(
merchant_internal_reference,
)
}
None => api_models::payments::PaymentIdType::ConnectorTransactionId(
payment_response.payment,
),
};
Ok(api_models::webhooks::ObjectReferenceId::PaymentId(
reference,
))
}
volt::WebhookResponse::Refund(refund_response) => {
let refund_reference = match refund_response.external_reference {
Some(external_reference) => {
api_models::webhooks::RefundIdType::RefundId(external_reference)
}
None => api_models::webhooks::RefundIdType::ConnectorRefundId(
refund_response.refund,
),
};
Ok(api_models::webhooks::ObjectReferenceId::RefundId(
refund_reference,
))
}
}
}

Expand Down
10 changes: 9 additions & 1 deletion crates/router/src/connector/volt/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ pub mod webhook_headers {
pub const X_VOLT_SIGNED: &str = "X-Volt-Signed";
pub const X_VOLT_TIMED: &str = "X-Volt-Timed";
pub const USER_AGENT: &str = "User-Agent";
pub const X_VOLT_TYPE: &str = "X-Volt-Type";
}

#[derive(Debug, Serialize)]
Expand Down Expand Up @@ -488,6 +487,15 @@ pub struct VoltRefundWebhookBodyReference {
pub external_reference: Option<String>,
}

#[derive(Debug, Deserialize, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
#[serde(untagged)]
pub enum WebhookResponse {
// the enum order shouldn't be changed as this is being used during serialization and deserialization
Refund(VoltRefundWebhookBodyReference),
Payment(VoltPaymentWebhookBodyReference),
}

#[derive(Debug, Deserialize, Serialize)]
#[serde(untagged)]
pub enum VoltWebhookBodyEventType {
Expand Down

0 comments on commit acb3296

Please sign in to comment.