Skip to content

Fix custom-amount retries failing with a Stripe idempotency error - #146

Merged
joaquimds merged 2 commits into
masterfrom
fix/custom-amount-price-reuse
Nov 11, 2026
Merged

joaquimds merged 2 commits into
masterfrom
fix/custom-amount-price-reuse

Conversation

@joaquimds

Copy link
Copy Markdown
Member

Problem

When a member's first payment on a custom-amount tier was declined, every retry in the same form session failed before any card was charged. The error was a Stripe IdempotencyException: "Keys for idempotent requests can only be used with the same parameters they were first used with."

The retry correctly reused the subscription idempotency key. But getOrCreatePriceForProduct compared Stripe's integer unit_amount with a float ($amount * 100), so === never matched. Each request created a new Price, the subscription request changed, and Stripe rejected it. Production logs from the last month show 0 reused recurring prices, 88 newly created ones, and 30 failed retries across 6 members.

Fix

In getOrCreatePriceForProduct:

  • Round the amount to an integer number of minor units ((int) round($amount * 100), which also handles amounts like 3.30)
  • Lowercase the currency, as Stripe stores it
  • Read every page of price search results, not just the first 10, since these products hold hundreds of prices
  • Only reuse a recurring price with the same interval, because the donation product is shared across tiers

getOrCreateOneTimePriceForProduct now reads every page too.

Tests

New tests in StripePriceReuseTest run the real lookup against an in-memory fake of the Stripe API, including a regression test that submits the same custom-amount join twice and checks that both subscription requests are identical. 7 of the 8 tests fail without the fix.

The existing SubscriptionIdempotencyKeyTest checked that the key was stable, but not that the request it protects was.

Not covered

  • After a decline, a retry now replays the original subscription, whose payment is waiting for a new payment method. Someone should check the frontend confirms that payment with the new card.
  • Existing single-use prices and incomplete subscriptions in Stripe are left as they are.

Version bumped to 1.4.47 with a changelog entry.

🤖 Generated with Claude Code

joaquimds and others added 2 commits November 11, 2026 10:36
getOrCreatePriceForProduct compared Stripe's integer unit_amount with a
float, so it never matched an existing price and created a new one on
every request. A retry in the same form session reused the subscription
idempotency key, but sent a different price, so Stripe rejected it
before any card was charged (RE-174).

- Round the amount to an integer number of minor units
- Lowercase the currency, as Stripe stores it
- Read every page of price search results, not just the first 10
- Only reuse a recurring price with the same interval

Bump version to 1.4.47.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@joaquimds
joaquimds merged commit 67be5c7 into master Nov 11, 2026
6 checks passed
@joaquimds
joaquimds deleted the fix/custom-amount-price-reuse branch November 11, 2026 10:50
joaquimds added a commit that referenced this pull request Nov 11, 2026
Since #146 the plugin sends Stripe the plan's currency in lowercase.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
conatus pushed a commit that referenced this pull request Nov 11, 2026
Saving again now reuses the Stripe price of a plan priced in pence, so the
test is no longer skipped, and the docs no longer list it as a known
problem.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BL2bFrTk84JMy3CPAsCTV9
conatus pushed a commit that referenced this pull request Nov 12, 2026
StripePriceReuseTest (from #146) arrived with the merge of master. With
phpcs and PHPStan now covering the tests:
- Put the log stub's empty method bodies on their own lines (phpcbf).
- Drop its CamelCaps phpcs:ignore comments; phpcs.xml excludes that rule
  for tests.
- Reset Stripe's HTTP client to its default CurlClient rather than null,
  which the SDK's signature doesn't allow, as StripeInvoicePaidTest does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BL2bFrTk84JMy3CPAsCTV9
@linear-code

linear-code Bot commented Nov 12, 2026

Copy link
Copy Markdown

JOIN-166

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant