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

refactor(core): add error handling wrapper to wehbook #6636

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Sakilmostak
Copy link
Contributor

@Sakilmostak Sakilmostak commented Nov 21, 2024

Type of Change

  • Bugfix
  • New feature
  • Enhancement
  • Refactoring
  • Dependency updates
  • Documentation
  • CI/CD

Description

In webhook, for specific connectors, we want to consume every webhook that arrives even if a error occurs while processing. This is done to keep the webhook channel open with the connector.
In this pr, a error wrapper is introduced to handle this logic where based on connector, we choose whether to send error or not.

Additional Changes

  • This PR modifies the API contract
  • This PR modifies the database schema
  • This PR modifies application configuration/environment variables

Motivation and Context

How did you test it?

Tested through Postman:

  • Create a MCA (Adyenplatform):
  • Setup webhook endpoint in dashboard (Adyenplatform):
  • Create a payout:
{
    "amount": 100,
    "currency": "EUR",
    "customer_id": "sakilcust",
    "email": "[email protected]",
    "phone": "999999999",
    "phone_country_code": "+65",
    "description": "Its my first payout request",
    "payout_type": "bank",
    "priority": "instant",
    "payout_method_data": {
        "bank": {
            "iban": "DE89370400440532013000"
        }
    },
    "billing": {
        "address": {
            "line1": "Raadhuisplein",
            "line2": "92",
            "city": "Hoogeveen",
            "state": "FL",
            "zip": "7901 BW",
            "country": "NL",
            "first_name": "John",
            "last_name": "Doe"
        },
        "phone": {
            "number": "0650242319",
            "country_code": "+31"
        }
    },
    "entity_type": "Individual",
    "recurring": true,
    "confirm": true,
    "auto_fulfill": true
}
  • You should receive webhook for the following payout to the endpoint mentioned during merchant creation

Case 2:

  • Setup ngrok to intercept webhook
  • Create a payout (same body as above)
  • delete the payout_attempt from the payout_attempt table against the generated payout_id
  • See the log in ngrok for response sent to the adyenplatform
  • The status should be 200 with x-http-code present in headers

Checklist

  • I formatted the code cargo +nightly fmt --all
  • I addressed lints thrown by cargo clippy
  • I reviewed the submitted code
  • I added unit tests for my changes where possible

@Sakilmostak Sakilmostak added A-core Area: Core flows C-refactor Category: Refactor A-webhooks Area: Webhook flows labels Nov 21, 2024
@Sakilmostak Sakilmostak added this to the November 2024 Release milestone Nov 21, 2024
@Sakilmostak Sakilmostak self-assigned this Nov 21, 2024
@Sakilmostak Sakilmostak requested review from a team as code owners November 21, 2024 20:33
@Sakilmostak Sakilmostak requested a review from a team as a code owner November 26, 2024 08:04
@@ -1344,3 +1344,7 @@ pub async fn trigger_refund_outgoing_webhook(
) -> RouterResult<()> {
todo!()
}

pub fn is_webhook_not_found_error(err: &errors::ApiErrorResponse) -> bool {
matches!(err, errors::ApiErrorResponse::WebhookResourceNotFound)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add all the other NotFound errors which can occur like PaymentNotFound, RefundNotFound etc.

@@ -117,14 +117,104 @@ pub async fn incoming_webhooks_wrapper<W: types::OutgoingWebhookType>(
Ok(application_response)
}

async fn incoming_webhooks_error_wrapper<W: types::OutgoingWebhookType>(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we try to eliminate this wrapper?

@@ -282,6 +282,9 @@ impl Connector {
pub fn is_pre_processing_required_before_authorize(&self) -> bool {
matches!(self, Self::Airwallex)
}
pub fn should_connector_bypass_error_if_webhook_not_found(&self) -> bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub fn should_connector_bypass_error_if_webhook_not_found(&self) -> bool {
pub fn should_acknowledge_webhook_for_resource_not_found_errors(&self) -> bool {

)))
} else {
Ok(services::api::ApplicationResponse::TextPlain(
"[accepted]".to_string(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no need to send "[accepted]" back in the response / any content body right?

state.clone(),
req_state,
req,
merchant_account.clone(),
key_store,
connector_name_or_mca_id,
merchant_connector_account,
connector.clone(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can try passing references here

let (application_response, webhooks_response_tracker, serialized_req) =
Box::pin(incoming_webhooks_core::<W>(

// Fetch the merchant connector account to get the webhooks source secret
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also please add the comment why the mca and connector are being fetched in wrapper instead of core?

| errors::ApiErrorResponse::PayoutNotFound
| errors::ApiErrorResponse::MandateNotFound
| errors::ApiErrorResponse::PaymentNotFound
| errors::ApiErrorResponse::RefundNotFound
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should add errors::ApiErrorResponse::AuthenticationNotFound error too

crates/router/src/utils.rs Outdated Show resolved Hide resolved
crates/router/src/core/webhooks/incoming.rs Outdated Show resolved Hide resolved
| ApiErrorResponse::PaymentNotFound
| ApiErrorResponse::RefundNotFound
| ApiErrorResponse::AuthenticationNotFound { .. } => Self::ResourceNotFound,
_ => Self::InternalError,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we map everything else to an internal error, or should we only map InternalServerError to InternalError and everything else to an Unknown error variant?

Comment on lines +411 to +414
vec![(
"x-http-code".to_string(),
Maskable::Masked(Secret::new("404".to_string())),
)],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need this custom header? If yes, then where would be monitoring this (Grafana dashboard, etc.)? Or can we have metrics instead?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-core Area: Core flows A-webhooks Area: Webhook flows C-refactor Category: Refactor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants