<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Luis Eduardo Guerreiro Espindola</title>
    <description>The latest articles on DEV Community by Luis Eduardo Guerreiro Espindola (@luiseduardoge).</description>
    <link>https://dev.to/luiseduardoge</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1345337%2F63ec8382-baa6-4882-9b99-7baa578c314f.jpeg</url>
      <title>DEV Community: Luis Eduardo Guerreiro Espindola</title>
      <link>https://dev.to/luiseduardoge</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/luiseduardoge"/>
    <language>en</language>
    <item>
      <title>My First Paying Customer Stayed on the Free Plan. The Error Said Only: get</title>
      <dc:creator>Luis Eduardo Guerreiro Espindola</dc:creator>
      <pubDate>Wed, 25 Nov 2026 14:38:43 +0000</pubDate>
      <link>https://dev.to/luiseduardoge/my-first-paying-customer-stayed-on-the-free-plan-the-error-said-only-get-h2m</link>
      <guid>https://dev.to/luiseduardoge/my-first-paying-customer-stayed-on-the-free-plan-the-error-said-only-get-h2m</guid>
      <description>&lt;p&gt;The admin alert had one word in it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Stripe webhook failed: Error: get
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Not a stack trace, not a field name. Just &lt;code&gt;get&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It was 24 June 2026, and that alert belonged to the first real paying customer of&lt;br&gt;
a home-search subscription product I lead. They had paid for Pro. Stripe had taken the money. Our database still had&lt;br&gt;
them on Free, which meant no Pro alerts, which meant the one thing they had just paid for wasn't happening.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F5nqnxu8q4bs8cd85t3hm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F5nqnxu8q4bs8cd85t3hm.png" alt="Timeline: the customer pays for Pro; Stripe sends checkout.session.completed; the handler's obj.get() raises AttributeError and the alert says Error: get; the plan stays Free because no subscription ID was stored. Then the fix, to_dict() at the edge with tests built from a real stripe.Event, and the subscription replayed from Stripe" width="800" height="336"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The tests were green. They had been green the whole time. This is the story of why, and of the five lines that fixed it.&lt;/p&gt;
&lt;h2&gt;
  
  
  The handler that passed every test
&lt;/h2&gt;

&lt;p&gt;The webhook looked like most Stripe webhooks you will find in tutorials, simplified here:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nd"&gt;@router&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/webhooks/stripe"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;stripe_webhook&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;event&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stripe&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Webhook&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;construct_event&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;body&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"stripe-signature"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;settings&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stripe_webhook_secret&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"data"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"object"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"checkout.session.completed"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;customer_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"customer"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;subscription_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"subscription"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;activate_pro&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;subscription_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the tests built events the way most of us do, as plain dictionaries:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;_make_event&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event_type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;event_type&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"data"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"object"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Plain dicts have &lt;code&gt;.get()&lt;/code&gt;. So every test called &lt;code&gt;obj.get("customer")&lt;/code&gt; on a dict, got &lt;code&gt;"cus_123"&lt;/code&gt;, and passed.&lt;/p&gt;

&lt;p&gt;Production doesn't hand you a dict. &lt;code&gt;stripe.Webhook.construct_event()&lt;/code&gt; returns a &lt;code&gt;stripe.Event&lt;/code&gt;, and everything inside it is a&lt;br&gt;
&lt;code&gt;StripeObject&lt;/code&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  StripeObject stopped being a dict
&lt;/h2&gt;

&lt;p&gt;For years &lt;code&gt;StripeObject&lt;/code&gt; was a &lt;code&gt;dict&lt;/code&gt; subclass, so &lt;code&gt;.get()&lt;/code&gt; just worked and nobody had a reason to notice the difference.&lt;br&gt;
In stripe-python 15.0 it stopped being one. I checked each version against the installed package:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;14.0.0:&lt;/strong&gt; &lt;code&gt;StripeObject&lt;/code&gt; is a &lt;code&gt;dict&lt;/code&gt; subclass, and &lt;code&gt;obj.get("status")&lt;/code&gt; works.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;15.0.0:&lt;/strong&gt; not a &lt;code&gt;dict&lt;/code&gt; any more, and &lt;code&gt;obj.get("status")&lt;/code&gt; raises &lt;code&gt;AttributeError: get&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;16.0.0 and 17.1.0:&lt;/strong&gt; still not a &lt;code&gt;dict&lt;/code&gt;, and the error finally explains itself:
&lt;code&gt;AttributeError: 'get' is a dict method, but a Subscription is not a dict. Use .to_dict() to convert it.&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Newer versions explain themselves. 15.0 doesn't, and that's where the one-word alert comes from. &lt;code&gt;StripeObject&lt;/code&gt; resolves&lt;br&gt;
unknown attributes through &lt;code&gt;__getattr__&lt;/code&gt;, which looks the name up as a key: &lt;code&gt;obj.get&lt;/code&gt; becomes &lt;code&gt;obj["get"]&lt;/code&gt;. There is no&lt;br&gt;
&lt;code&gt;"get"&lt;/code&gt; key in a checkout session, so the &lt;code&gt;KeyError&lt;/code&gt; is re-raised as &lt;code&gt;AttributeError("get")&lt;/code&gt;, and &lt;code&gt;str()&lt;/code&gt; of that&lt;br&gt;
exception is the bare word &lt;code&gt;get&lt;/code&gt;. My alert printed &lt;code&gt;str(exc)&lt;/code&gt;. That's the whole message.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fi3rkmlr93ewotwq2x11m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fi3rkmlr93ewotwq2x11m.png" alt="Five steps in stripe-python 15.0: obj.get(&amp;quot;customer&amp;quot;) goes to StripeObject.__getattr__(&amp;quot;get&amp;quot;), which looks up the key &amp;quot;get&amp;quot;; the missing key becomes AttributeError(&amp;quot;get&amp;quot;), and the alert prints str(exc): Stripe webhook failed: Error: get. From 16.0 the error reads: 'get' is a dict method, but a Session is not a dict. Use .to_dict() to convert it" width="800" height="464"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is the trap in a few lines you can run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;stripe&lt;/span&gt;

&lt;span class="n"&gt;session&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"object"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"checkout.session"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"customer"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"cus_123"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;event&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stripe&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;construct_from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"evt_1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"object"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"event"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
     &lt;span class="s2"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"checkout.session.completed"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
     &lt;span class="s2"&gt;"data"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"object"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"sk_test_fake"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;object&lt;/span&gt;

&lt;span class="nb"&gt;isinstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# False&lt;/span&gt;
&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"customer"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;        &lt;span class="c1"&gt;# 'cus_123'  item access works&lt;/span&gt;
&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;customer&lt;/span&gt;           &lt;span class="c1"&gt;# 'cus_123'  attribute access works&lt;/span&gt;
&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"customer"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="c1"&gt;# AttributeError: get  (15.0)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Item access works, attribute access works, and the one method every Python developer reaches for doesn't.&lt;/p&gt;

&lt;h2&gt;
  
  
  The second trap was hiding in the same file
&lt;/h2&gt;

&lt;p&gt;While fixing it I found a handler that had the opposite problem. &lt;code&gt;customer.subscription.updated&lt;/code&gt; used attribute-style access with a&lt;br&gt;
default:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;getattr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"status"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On a real &lt;code&gt;StripeObject&lt;/code&gt; that returns the status. On the plain dict the tests used, &lt;code&gt;getattr(some_dict, "status", None)&lt;/code&gt;&lt;br&gt;
doesn't read keys at all, so it returns the default, &lt;code&gt;None&lt;/code&gt;, every time, without an error.&lt;/p&gt;

&lt;p&gt;So the billing router held two handlers, and each one was correct in only one world:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;.get()&lt;/code&gt; handlers passed the tests and crashed in production.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;getattr()&lt;/code&gt; handlers worked in production and were quietly wrong in the tests.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The tests weren't testing the code that runs. They were testing a version of it that receives a different type.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F7ne3fwgocz07w3k5mk8s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F7ne3fwgocz07w3k5mk8s.png" alt="Two columns: in tests, a plain dict makes .get() work and getattr() silently return the default; in production a StripeObject makes .get() raise AttributeError and getattr() work; a to_dict() step at the webhook edge makes both columns the same" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  The fix: one shape, chosen at the edge
&lt;/h2&gt;

&lt;p&gt;The fix is small. Convert the SDK object to plain Python once, right after verifying the signature, and let every&lt;br&gt;
handler see the same shape the tests use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;collections&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;abc&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;Mapping&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;typing&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;Any&lt;/span&gt;

&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;stripe&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;StripeObject&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;to_plain&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;StripeObject&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Mapping&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Any&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Any&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="s2"&gt;"""Plain, recursively converted dict for a Stripe object."""&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;isinstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;StripeObject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_dict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# recursive by default&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;isinstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Mapping&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kt"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;TypeError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"not a StripeObject: {type(obj).__name__}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;to_dict()&lt;/code&gt; is public and recursive, so nested objects (line items, the invoice's subscription details) become plain&lt;br&gt;
dicts and lists too. After that, &lt;code&gt;.get()&lt;/code&gt; is correct everywhere, and I changed the &lt;code&gt;getattr()&lt;/code&gt; handler to &lt;code&gt;.get()&lt;/code&gt; as well&lt;br&gt;
so there is one access style in the file.&lt;/p&gt;

&lt;p&gt;Then the part that actually prevents the next one: the tests stopped using hand-made dicts for events. They build the same&lt;br&gt;
class production receives:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;_real_event&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;evt&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;stripe&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Event&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;stripe&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;construct_from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;evt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"sk_test_fake"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_handler_works_on_a_real_stripe_event&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tmp_path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="s2"&gt;"""A paid checkout must put the customer on the paid plan."""&lt;/span&gt;
    &lt;span class="n"&gt;store&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Store&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tmp_path&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="s2"&gt;"t.db"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;paid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;checkout_session&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;payment_status&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"paid"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;real&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;_real_event&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;event&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"checkout.session.completed"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;paid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="nf"&gt;handle_event&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;store&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;real&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"created"&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;sub&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;store&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;subscriptions_for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"cus_123"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="n"&gt;sub&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"status"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"active"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That test fails on the old handler and passes on the new one. The dict-based tests never could have told the difference.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fixing the customer, not just the code
&lt;/h2&gt;

&lt;p&gt;Deploying the fix didn't fix the customer. Their checkout event had failed, so we never stored their Stripe subscription&lt;br&gt;
ID, and our "sync my plan" button needed exactly that ID to ask Stripe for the current state. The customer couldn't&lt;br&gt;
repair it themselves.&lt;/p&gt;

&lt;p&gt;I replayed their state from Stripe: looked up the customer's subscription in Stripe and wrote it to our database, the&lt;br&gt;
way the webhook should have.&lt;/p&gt;

&lt;p&gt;If your webhook has failed for real customers, check that your recovery path doesn't depend on data the failed webhook&lt;br&gt;
was supposed to write.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I do with Stripe webhooks now
&lt;/h2&gt;

&lt;p&gt;I rebuilt the whole pattern as a small public demo, &lt;a href="https://github.com/LuisEduardoGE/fastapi-stripe-webhooks" rel="noopener noreferrer"&gt;fastapi-stripe-webhooks&lt;/a&gt;&lt;br&gt;
(25 tests, no Stripe keys needed). The short version:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Normalize at the edge.&lt;/strong&gt; &lt;code&gt;to_dict()&lt;/code&gt; once, right after &lt;code&gt;construct_event&lt;/code&gt;. No handler ever touches a &lt;code&gt;StripeObject&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Test with the real class.&lt;/strong&gt; &lt;code&gt;stripe.Event.construct_from(...)&lt;/code&gt; in fixtures, not dicts.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Verify the raw body.&lt;/strong&gt; &lt;code&gt;await request.body()&lt;/code&gt;, never parsed and re-serialized JSON, or the signature check fails.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Store before you acknowledge.&lt;/strong&gt; Keep an inbox keyed by &lt;code&gt;event.id&lt;/code&gt;: it makes retries idempotent, and a handler crash
leaves a &lt;code&gt;failed&lt;/code&gt; row you can re-run instead of a lost event.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Answer fast, work later.&lt;/strong&gt; Return &lt;code&gt;200&lt;/code&gt; once the event is stored; do the work in a background job.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Log the exception type, not just &lt;code&gt;str(exc)&lt;/code&gt;.&lt;/strong&gt; &lt;code&gt;AttributeError: get&lt;/code&gt; says far more than &lt;code&gt;get&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fi54w1ilmpl42rtn8ey2l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fi54w1ilmpl42rtn8ey2l.png" alt="Webhook path: Stripe sends the event to POST /webhooks/stripe, which reads the raw body, verifies the signature with construct_event(), converts it with to_plain(), stores it in an events inbox keyed by event.id and answers 200. In the background, process_event() runs derive_change() on plain dicts and updates the subscriptions table; failed rows are re-run by process_unfinished(). Tests build events with stripe.Event.construct_from()" width="800" height="464"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The bug was one method call. What it cost was the first impression of the first person who trusted us with money.&lt;br&gt;
Since then, every Stripe test I write starts from a real &lt;code&gt;stripe.Event&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Have you been caught by an SDK object that looks like a dict but isn't? I'd like to hear which library got you.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Luis Eduardo G. Espindola: full-stack developer (Python and TypeScript) in Belgium.&lt;/em&gt;\&lt;br&gt;
&lt;em&gt;Code: &lt;a href="https://github.com/LuisEduardoGE/fastapi-stripe-webhooks" rel="noopener noreferrer"&gt;github.com/LuisEduardoGE/fastapi-stripe-webhooks&lt;/a&gt;&lt;/em&gt;\&lt;br&gt;
&lt;em&gt;Elsewhere: &lt;a href="https://github.com/LuisEduardoGE" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt; · &lt;a href="https://www.linkedin.com/in/luisgespindola/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>stripe</category>
      <category>fastapi</category>
      <category>testing</category>
    </item>
  </channel>
</rss>
