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

This commit is contained in:
ItsMeShashank
2023-06-07 12:54:18 +05:30
committed by GitHub
parent e484193101
commit 5c2232b737
2 changed files with 78 additions and 20 deletions

View File

@ -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
@ -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)?;
@ -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
}

View File

@ -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,
}
}
}
@ -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,
@ -2397,6 +2441,8 @@ pub enum WebhookEventStatus {
RequiresCapture,
Canceled,
Chargeable,
#[serde(other)]
Unknown,
}
#[derive(Debug, Deserialize, PartialEq)]