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

fix(router): hotfixes for stripe webhook event mapping and reference id retrieval #1368

Merged
merged 4 commits into from
Jun 7, 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
40 changes: 26 additions & 14 deletions crates/router/src/connector/stripe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1666,9 +1666,15 @@ impl api::IncomingWebhook for Stripe {
.change_context(errors::ConnectorError::WebhookReferenceIdNotFound)?;

Ok(match details.event_data.event_object.object {
stripe::WebhookEventObjectType::PaymentIntent
| stripe::WebhookEventObjectType::Charge
| stripe::WebhookEventObjectType::Dispute => {
stripe::WebhookEventObjectType::PaymentIntent => {
api_models::webhooks::ObjectReferenceId::PaymentId(
api_models::payments::PaymentIdType::ConnectorTransactionId(
details.event_data.event_object.id,
),
)
}

stripe::WebhookEventObjectType::Charge | stripe::WebhookEventObjectType::Dispute => {
api_models::webhooks::ObjectReferenceId::PaymentId(
api_models::payments::PaymentIdType::ConnectorTransactionId(
details
Expand All @@ -1693,7 +1699,7 @@ impl api::IncomingWebhook for Stripe {
&self,
request: &api::IncomingWebhookRequestDetails<'_>,
) -> CustomResult<api::IncomingWebhookEvent, errors::ConnectorError> {
let details: stripe::WebhookEvent = request
let details: stripe::WebhookEventTypeBody = request
.body
.parse_struct("WebhookEvent")
.change_context(errors::ConnectorError::WebhookReferenceIdNotFound)?;
vspecky marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -1705,21 +1711,27 @@ impl api::IncomingWebhook for Stripe {
stripe::WebhookEventType::PaymentIntentSucceed => {
api::IncomingWebhookEvent::PaymentIntentSuccess
}
stripe::WebhookEventType::ChargeSucceeded => {
if let Some(stripe::WebhookPaymentMethodDetails {
payment_method: stripe::WebhookPaymentMethodType::AchCreditTransfer,
}) = details.event_data.event_object.payment_method_details
{
api::IncomingWebhookEvent::PaymentIntentSuccess
} else {
api::IncomingWebhookEvent::EventNotSupported
}
}
stripe::WebhookEventType::SourceChargeable => {
api::IncomingWebhookEvent::SourceChargeable
}
stripe::WebhookEventType::ChargeSucceeded => {
api::IncomingWebhookEvent::PaymentIntentSuccess
}
stripe::WebhookEventType::DisputeCreated => api::IncomingWebhookEvent::DisputeOpened,
stripe::WebhookEventType::DisputeClosed => api::IncomingWebhookEvent::DisputeCancelled,
stripe::WebhookEventType::DisputeUpdated => api::IncomingWebhookEvent::try_from(
details
.event_data
.event_object
.status
.ok_or(errors::ConnectorError::WebhookEventTypeNotFound)?,
)?,
stripe::WebhookEventType::DisputeUpdated => details
.event_data
.event_object
.status
.map(Into::into)
.unwrap_or(api::IncomingWebhookEvent::EventNotSupported),
stripe::WebhookEventType::PaymentIntentPartiallyFunded => {
api::IncomingWebhookEvent::PaymentIntentPartiallyFunded
}
Expand Down
58 changes: 52 additions & 6 deletions crates/router/src/connector/stripe/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,17 +566,27 @@ pub enum StripeBankNames {
Boz,
}

impl TryFrom<WebhookEventStatus> for api_models::webhooks::IncomingWebhookEvent {
type Error = errors::ConnectorError;
fn try_from(value: WebhookEventStatus) -> Result<Self, Self::Error> {
Ok(match value {
impl From<WebhookEventStatus> for api_models::webhooks::IncomingWebhookEvent {
fn from(value: WebhookEventStatus) -> Self {
match value {
WebhookEventStatus::WarningNeedsResponse => Self::DisputeOpened,
WebhookEventStatus::WarningClosed => Self::DisputeCancelled,
WebhookEventStatus::WarningUnderReview => Self::DisputeChallenged,
WebhookEventStatus::Won => Self::DisputeWon,
WebhookEventStatus::Lost => Self::DisputeLost,
_ => Err(errors::ConnectorError::WebhookEventTypeNotFound)?,
})
WebhookEventStatus::NeedsResponse
| WebhookEventStatus::UnderReview
| WebhookEventStatus::ChargeRefunded
| WebhookEventStatus::Succeeded
| WebhookEventStatus::RequiresPaymentMethod
| WebhookEventStatus::RequiresConfirmation
| WebhookEventStatus::RequiresAction
| WebhookEventStatus::Processing
| WebhookEventStatus::RequiresCapture
| WebhookEventStatus::Canceled
| WebhookEventStatus::Chargeable
| WebhookEventStatus::Unknown => Self::EventNotSupported,
}
}
}

Expand Down Expand Up @@ -2297,12 +2307,46 @@ pub struct WebhookEvent {
pub event_data: WebhookEventData,
}

#[derive(Debug, Deserialize)]
pub struct WebhookEventTypeBody {
#[serde(rename = "type")]
pub event_type: WebhookEventType,
#[serde(rename = "data")]
pub event_data: WebhookStatusData,
}

#[derive(Debug, Deserialize)]
pub struct WebhookEventData {
#[serde(rename = "object")]
pub event_object: WebhookEventObjectData,
}

#[derive(Debug, Deserialize)]
pub struct WebhookStatusData {
#[serde(rename = "object")]
pub event_object: WebhookStatusObjectData,
}

#[derive(Debug, Deserialize)]
pub struct WebhookStatusObjectData {
pub status: Option<WebhookEventStatus>,
pub payment_method_details: Option<WebhookPaymentMethodDetails>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum WebhookPaymentMethodType {
AchCreditTransfer,
#[serde(other)]
Unknown,
}

#[derive(Debug, Deserialize)]
pub struct WebhookPaymentMethodDetails {
#[serde(rename = "type")]
pub payment_method: WebhookPaymentMethodType,
}

#[derive(Debug, Deserialize)]
pub struct WebhookEventObjectData {
pub id: String,
Expand Down Expand Up @@ -2397,6 +2441,8 @@ pub enum WebhookEventStatus {
RequiresCapture,
Canceled,
Chargeable,
#[serde(other)]
Unknown,
}

#[derive(Debug, Deserialize, PartialEq)]
Expand Down