wyyerd / stripe-rs

Rust API bindings for the Stripe HTTP API.
Apache License 2.0
223 stars 88 forks source link

Parsing a Charge #123

Closed ThouCheese closed 4 years ago

ThouCheese commented 4 years ago

Im migrating my payment implementation to use the PaymentIntent and PaymentMethod API's, and I'm running into some trouble with the webhook. The webhook posts an event to my web server. The event in turn contains a payment_intent object, which contains a list of charges. One of these charges is formatted as follows:

{
  "id": "py_1G8uzFCMiKy60bCcGlnX5i81",
  "object": "charge",
  "amount": 1000,
  /* extra fields omitted */
}

The object is structured according to how a charge should be structured, save for the format of the id. This causes stripe-rust to emit an error instead of parsing the charge. I'm not sure if this is an error with stripe-rust or stripe. However, the checkout works perfectly if I change the impl FromStr for ChargeId to the following:

impl std::str::FromStr for ChargeId {
    type Err = ParseIdError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if !s.starts_with("ch_") && !s.starts_with("py_") {
            Err(ParseIdError {
                typename: "ChargeId",
                expected: "id to start with \"ch_\" or \"py_\"",
            })
        } else {
            Ok(ChargeId(s.into()))
        }
    }
}

I'm holding off on submitting a pull request, because this code is generated by a macro and I'm not sure how you guys would prefer to see this fixed. Thanks!

kestred commented 4 years ago

Fixed in https://github.com/wyyerd/stripe-rs/commit/0aeb9a6551413239b96d94289f84d067eea4b963 specifically to generally handle this kind of issue when stripe developers decide to use this API pattern.

kestred commented 4 years ago

This has been published to crates.io as version 0.12.1.