Skip to content

What Stripe API version 2022-11-15 breaks in your code

Stripe API version 2022-11-15 removed charges from PaymentIntent responses. How to find every line that reads it before you upgrade.

Stripe2 min read

Short answer

Moving from Stripe API version 2022-08-01 to 2022-11-15 removes charges from PaymentIntent responses and stops including refunds on a Charge unless you expand it. Code that reads paymentIntent.charges stops working. Read latest_charge instead.

Every Stripe request runs against one API version: your account's default, or the version your code sets with apiVersion. A newer version can remove or reshape fields, and nothing in your code changes to tell you. This post walks through one real upgrade, 2022-08-01 to 2022-11-15, with the GitGo CLI and a small TypeScript file.

What changed between the two versions

Three changes in 2022-11-15 touch PaymentIntents and Charges:

  • charges was removed from PaymentIntent responses. Code that reads it gets undefined.
  • latest_charge was added to PaymentIntent responses. Read it instead.
  • A Charge no longer includes refunds unless you expand it.

To list every change between two versions, run gitgo diff on their OpenAPI descriptions. It labels each change breaking, risky or info.

Where it breaks in your code

A diff tells you what changed, not which of your lines it affects. gitgo scan takes the same two descriptions and lists each line of your TypeScript, JavaScript or Python code that the change affects. Take this file:

src/responses.ts
import Stripe from 'stripe';

const stripe = new Stripe('sk_test', { apiVersion: '2022-08-01' });

export async function latestCharge(id: string) {
  const pi = await stripe.paymentIntents.retrieve(id);
  const chargeId = pi.charges.data[0].id;
  const { charges, latest_charge: latest } = await stripe.paymentIntents.retrieve(id);
  console.log(charges.total_count, latest);
  const status = (await stripe.paymentIntents.retrieve(id)).status;
  const list = await stripe.paymentIntents.list({ limit: 3 });
  for (const item of list.data) {
    console.log(item.charges.url);
  }
  return [chargeId, status];
}
Terminal
$ gitgo scan --from stripe-2022-08-01.json --to stripe-2022-11-15.json ./src
responses.ts:7:20  BREAKING  response-property-removed  $.charges in response 200 (application/json) was removed (GET /v1/payment_intents/{intent})
responses.ts:8:11  BREAKING  response-property-removed  $.charges in response 200 (application/json) was removed (GET /v1/payment_intents/{intent})
responses.ts:8:20  INFO  response-required-property-added  new required property $.latest_charge in response 200 (application/json) (GET /v1/payment_intents/{intent})
responses.ts:9:15  BREAKING  response-property-removed  $.charges in response 200 (application/json) was removed (GET /v1/payment_intents/{intent})
responses.ts:13:17  BREAKING  response-property-removed  $.data[].charges in response 200 (application/json) was removed (GET /v1/payment_intents)

5 impacts (4 breaking, 0 risky, 1 info) in 1 file; 6 files scanned, 0 skipped, 31 API usages; spec 2022-08-01 -> 2022-11-15

Each read of charges is listed with its line and column. A text search for charges would find these lines too, along with every unrelated charges in your codebase. The scan lists only reads of the field Stripe removed.

How to fix it

Replace each read of charges with latest_charge. It holds the ID of the most recent charge, or the full Charge object if you expand it. If you need every charge on a PaymentIntent, list them with stripe.charges.list({ payment_intent: id }).

Diff
-  const chargeId = pi.charges.data[0].id;
+  const chargeId = typeof pi.latest_charge === 'string' ? pi.latest_charge : pi.latest_charge?.id;
One way to fix line 7

Then run the scan again. When it reports no breaking impacts, change the apiVersion your client pins. A hosted GitGo service that writes these edits as a pull request is in development.

Try it on your code

Download the OpenAPI descriptions for your current version and the one you want, then scan the directory that calls Stripe. The CLI runs on your machine and sends nothing anywhere. See install the CLI.

Shell
gitgo scan --from stripe-current.json --to stripe-target.json ./src

Questions

What replaced charges on a Stripe PaymentIntent?
From API version 2022-11-15, PaymentIntent responses have latest_charge instead of charges. It holds the ID of the most recent charge, or the Charge object when expanded. To list every charge on a PaymentIntent, call the Charges list endpoint with the payment_intent parameter.
Does the Stripe 2022-11-15 change affect destructured fields?
Yes. const { charges } = await stripe.paymentIntents.retrieve(id) reads the removed field just as pi.charges does, so it breaks the same way.

Run it on your own code

The CLI is free and open source. It runs on your machine and sends nothing anywhere.