Skip to content

Commit

Permalink
feat(connector): [Gocardless] Add mandate webhoooks
Browse files Browse the repository at this point in the history
  • Loading branch information
deepanshu-iiitu authored and ArjunKarthik committed Oct 5, 2023
1 parent 4149965 commit 15ef799
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
30 changes: 30 additions & 0 deletions crates/router/src/connector/gocardless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,12 @@ impl api::IncomingWebhook for Gocardless {
api_models::webhooks::RefundIdType::ConnectorRefundId(link.refund.to_owned());
api::webhooks::ObjectReferenceId::RefundId(refund_id)
}
transformers::WebhooksLink::MandateWebhookLink(link) => {
let mandate_id = api_models::webhooks::MandateIdType::ConnectorMandateId(
link.mandate.to_owned(),
);
api::webhooks::ObjectReferenceId::MandateId(mandate_id)
}
};
Ok(reference_id)
}
Expand Down Expand Up @@ -792,6 +798,27 @@ impl api::IncomingWebhook for Gocardless {
api::IncomingWebhookEvent::EventNotSupported
}
},
transformers::WebhookAction::MandatesAction(action) => match action {
transformers::MandatesAction::Active | transformers::MandatesAction::Reinstated => {
api::IncomingWebhookEvent::MandateActive
}
transformers::MandatesAction::Expired
| transformers::MandatesAction::Cancelled
| transformers::MandatesAction::Failed
| transformers::MandatesAction::Consumed => {
api::IncomingWebhookEvent::MandateRevoked
}
transformers::MandatesAction::Created
| transformers::MandatesAction::CustomerApprovalGranted
| transformers::MandatesAction::CustomerApprovalSkipped
| transformers::MandatesAction::Transferred
| transformers::MandatesAction::Submitted
| transformers::MandatesAction::ResubmissionRequested
| transformers::MandatesAction::Replaced
| transformers::MandatesAction::Blocked => {
api::IncomingWebhookEvent::EventNotSupported
}
},
};
Ok(event_type)
}
Expand All @@ -817,6 +844,9 @@ impl api::IncomingWebhook for Gocardless {
transformers::WebhookResourceType::Refunds => serde_json::to_value(first_event)
.into_report()
.change_context(errors::ConnectorError::WebhookBodyDecodingFailed),
transformers::WebhookResourceType::Mandates => serde_json::to_value(first_event)
.into_report()
.change_context(errors::ConnectorError::WebhookBodyDecodingFailed),
}
}
}
31 changes: 29 additions & 2 deletions crates/router/src/connector/gocardless/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -873,13 +873,15 @@ pub struct WebhookEvent {
pub enum WebhookResourceType {
Payments,
Refunds,
Mandates,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum WebhookAction {
PaymentsAction(PaymentsAction),
RefundsAction(RefundsAction),
MandatesAction(MandatesAction),
}

#[derive(Debug, Serialize, Deserialize)]
Expand Down Expand Up @@ -909,11 +911,31 @@ pub enum RefundsAction {
FundsReturned,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum MandatesAction {
Created,
CustomerApprovalGranted,
CustomerApprovalSkipped,
Active,
Cancelled,
Failed,
Transferred,
Expired,
Submitted,
ResubmissionRequested,
Reinstated,
Replaced,
Consumed,
Blocked,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum WebhooksLink {
PaymentWebhooksLink(PaymentWebhooksLink),
RefundWebhookLink(RefundWebhookLink),
MandateWebhookLink(MandateWebhookLink),
}

#[derive(Debug, Serialize, Deserialize)]
Expand All @@ -926,12 +948,17 @@ pub struct PaymentWebhooksLink {
pub payment: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct MandateWebhookLink {
pub mandate: String,
}

impl TryFrom<&WebhookEvent> for GocardlessPaymentsResponse {
type Error = error_stack::Report<errors::ConnectorError>;
fn try_from(item: &WebhookEvent) -> Result<Self, Self::Error> {
let id = match &item.links {
WebhooksLink::PaymentWebhooksLink(link) => link.payment.to_owned(),
WebhooksLink::RefundWebhookLink(_) => {
WebhooksLink::RefundWebhookLink(_) | WebhooksLink::MandateWebhookLink(_) => {
Err(errors::ConnectorError::WebhookEventTypeNotFound)?
}
};
Expand Down Expand Up @@ -962,7 +989,7 @@ impl TryFrom<&WebhookAction> for GocardlessPaymentStatus {
| PaymentsAction::ResubmissionRequired
| PaymentsAction::Created => Err(errors::ConnectorError::WebhookEventTypeNotFound)?,
},
WebhookAction::RefundsAction(_) => {
WebhookAction::RefundsAction(_) | WebhookAction::MandatesAction(_) => {
Err(errors::ConnectorError::WebhookEventTypeNotFound)?
}
}
Expand Down

0 comments on commit 15ef799

Please sign in to comment.