arlyon / async-stripe

Async (and blocking!) Rust bindings for the Stripe API
https://payments.rs
Apache License 2.0
467 stars 131 forks source link

Can't retrieve CheckoutSession using checkout_session_id query param #608

Closed sytzez closed 2 months ago

sytzez commented 2 months ago

Describe the bug

Sorry if there's already a solution to this problem, I haven't been able to find it, but I'm also quite new to using this crate.

In the Stripe documentation, it states that I should be able to retrieve the CheckoutSession when Stripe redirects the user to a page on my server using a session id query param in the URL.

I have tried using CheckoutSession::retrieve using an id of type String, extracted from the URL query. (I'm using axum by the way, but this should work similarly for any web server crate). However, I'm not able to construct a CheckoutSessionId from this String, and I've not found much documentation around these various ...Id types which exist in async-stripe.

For the time being, I've made a workaround to this problem by copy-pasting and slightly modifying the code from CheckoutSession::retrieve, but I believe there should be a better way.

My workaround:

    let response: stripe::Response<stripe::CheckoutSession> = STRIPE_CLIENT.get_query(
        &format!("/checkout/sessions/{my_checkout_session_id_str}"),
        HashMap::<String, String>::new(),
    );

To Reproduce

  1. Get the checkout_session_id from a Stripe redirected URL generated using {{CHECKOUT_SESSION_ID}}
  2. Try to retrieve the CheckoutSession using the checkout_session_id query param, which at this point is a String extracted by insert Rust web server crate

Expected behavior

I would expect there to be a way to create a CheckoutSessionId from a String or &str.

Code snippets

No response

OS

Ubuntu

Rust version

1.80.1

Library version

async-stripe 0.39.1

API version

2024-06-20

Additional context

No response

augustoccesar commented 2 months ago

Hello! Do you mean something like this?

let checkout_session_id = CheckoutSessionId::from_str("cs_test_xxxxxxx")?;
let checkout = CheckoutSession::retrieve(&client, &checkout_session_id, &[]);

Also, if interested, here is the code! https://github.com/arlyon/async-stripe/blob/f736d90ea0770f56c30e2c904a33858e4706bd1c/src/ids.rs#L497

https://github.com/arlyon/async-stripe/blob/f736d90ea0770f56c30e2c904a33858e4706bd1c/src/ids.rs#L92-L98

sytzez commented 2 months ago

That's exactly what I was looking for, thanks!