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

Axum Webhook handler doesn't compile #515

Open
itsbalamurali opened this issue Mar 4, 2024 · 1 comment
Open

Axum Webhook handler doesn't compile #515

itsbalamurali opened this issue Mar 4, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@itsbalamurali
Copy link

itsbalamurali commented Mar 4, 2024

Describe the bug

error[E0412]: cannot find type `AccountCapabilities` in this scope
   --> /Users/hmmm/.cargo/registry/src/index.crates.io-6f17d22bba15001f/async-stripe-0.34.1/src/resources/webhook_events.rs:428:25
    |
428 |     AccountCapabilities(AccountCapabilities),
    |                         ^^^^^^^^^^^^^^^^^^^ not found in this scope
    |
help: there is an enum variant `crate::EventObject::AccountCapabilities`; try using the variant's enum
    |
428 |     AccountCapabilities(crate::EventObject),
    |                         ~~~~~~~~~~~~~~~~~~

To Reproduce

Axum code:

use axum::extract::{FromRequest, Request};
use axum::http::{Response, StatusCode};
use axum::response::IntoResponse;
use stripe::{Event, EventObject, EventType};


struct StripeEvent(Event);

#[async_trait::async_trait]
impl<S, B> FromRequest<S, B> for StripeEvent
    where
        String: FromRequest<S, B>,
        B: Send + 'static,
        S: Send + Sync,
{
    type Rejection = Response<B>;

    async fn from_request(req: Request<B>, state: &S) -> Result<Self, Self::Rejection> {
        let signature = if let Some(sig) = req.headers().get("stripe-signature") {
            sig.to_owned()
        } else {
            return Err(StatusCode::BAD_REQUEST.into_response());
        };

        let payload =
            String::from_request(req, state).await.map_err(IntoResponse::into_response)?;

        Ok(Self(
            stripe::Webhook::construct_event(&payload, signature.to_str().unwrap(), "whsec_xxxxx")
                .map_err(|_| StatusCode::BAD_REQUEST.into_response())?,
        ))
    }
}

#[axum::debug_handler]
async fn handle_webhook(StripeEvent(event): StripeEvent) {
    match event.type_ {
        EventType::CheckoutSessionCompleted => {
            if let EventObject::CheckoutSession(session) = event.data.object {
                println!("Received checkout session completed webhook with id: {:?}", session.id);
            }
        }
        EventType::AccountUpdated => {
            if let EventObject::Account(account) = event.data.object {
                println!("Received account updated webhook for account: {:?}", account.id);
            }
        }
        _ => println!("Unknown event encountered in webhook: {:?}", event.type_),
    }
}

Cargo.toml

async-stripe = { version ="0.34", features = [
    "runtime-tokio-hyper-rustls-webpki",
    "uuid",
    "stream",
    "webhook-events"
], default-features = false }

Expected behavior

Expected to compile and work as expected

Code snippets

No response

OS

macOS

Rust version

1.76.0

Library version

async-stripe 0.34.1

API version

Latest

Additional context

No response

@itsbalamurali itsbalamurali added the bug Something isn't working label Mar 4, 2024
@WesleyAC
Copy link

I believe that as per #137, this can be fixed by enabling the connect feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants