arlyon/async-stripe

Axum Webhook handler doesn't compile

Opened this issue · 1 comments

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

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