9
2
Fork
You've already forked platform
2

Fix prepaid credit mint via Paddle custom_data #340

Open
vkobel wants to merge 5 commits from fix/paddle-customdata-credit-mint into main
pull from: fix/paddle-customdata-credit-mint
merge into: caution:main
caution:main
caution:feat/paddle-byoc-subscriptions
caution:feat/legal-publish-doc
caution:fix/issue-116-duplicate-ssh-key
caution:fix/issue-307-remove-apps
caution:fix/issue-265-http-port-private
caution:fix/issue-330-remove-build-binary
caution:feat/issue-164-account-id
caution:fix/issue-119-logout-confirmation
caution:feat/add-tofu-lockfile-cache
caution:feat/webauthn-discoverable-login
caution:docs/issue-339-install-instructions
caution:fix/issue-336-split-qr-login-tokens
caution:fix/verify-app-source-preflight
caution:fix/many-git-push-deploy
caution:fix/issue-334-email-verification-throttle
caution:fix/issue-273-billing-url
caution:fix/issue-333-final-usage-collection
caution:fix/issue-270-low-credit-email
caution:fix/issue-301-remove-auto-topup
caution:fix/issue-320-byoc-auth-first
caution:fix/issue-348-git-progress-checklist
caution:fix/issue-321-byoc-caution-hcl
caution:fix/issue-332-resource-id-suspension
caution:fix/eif-app-mode-normalization
caution:feat/cli-stagex-container
caution:fix/eif-s3-layer-cache
caution:fix/authz-role-checks
caution:fix/cli-hangs-unsupported-ssh-url
caution:feat/print-platform-commit-dashboard
caution:anton/feat/multi-user
caution:fix/config-domain-validation
caution:anton/support-ssh-destroy-get
caution:fix/cli-config-dir
caution:feat/publish-build-inputs
caution:feat/locksmith-polishing
caution:fix/procfile-run-command-no-split
caution:anton/byoc-require-login
caution:fix/hcl-init-template
caution:feat/egress-default-deny
caution:ryansquared/hcl
caution:quotas
caution:anton/feat/region-routing-resource-unavailable-alert
caution:fix/attestation-502-dashboard
caution:ryansquared/systemdize
caution:fix/verify-git-credential-hang
caution:anton/fix-low-credits-alert
caution:xenushka/rename-byoc
caution:ryansquared/add-download-eif
caution:add-debug-flag
caution:test/pr1-integration-harness
caution:anton/fix/only-pass-required-env-vars
caution:ryansquared/add-database-bucket

The Paddle credit flow credited an amount taken from client transaction metadata, never tied to what was actually paid. Move the decision server-side: the API records a credit_purchase_intents row (server-computed amount, keyed by transaction id) at creation; the webhook and callback credit that amount and refuse when no row exists. Metadata is display-only now.

Also adds UNIQUE on credit_ledger.paddle_transaction_id + ON CONFLICT DO NOTHING on every grant path, replacing a racy SELECT-then-INSERT.

Two unrelated bugs found while testing, fixed in their own commits: metering's get_user_usage query (syntax + GROUP BY + NUMERIC-as-f64), and a stale usage_records ref in the e2e test (now usage_ledger).

Reviewer:

migration 044 adds the table + constraint; the UNIQUE fails to apply if the DB holds duplicate paddle_transaction_id rows from the old race — run the dupe-check first. In-flight pre-deploy transactions have no intent row and won't auto-credit (reconcile manually).

What I tested already:

make test-e2e-billing against a real stack — 37/37, including four new cases: credits the intent amount and ignores a forged custom_data value, refuses with no intent row, credits once on duplicate delivery, and the UNIQUE/ON CONFLICT no-op.

The Paddle credit flow credited an amount taken from client transaction metadata, never tied to what was actually paid. Move the decision server-side: the API records a credit_purchase_intents row (server-computed amount, keyed by transaction id) at creation; the webhook and callback credit that amount and refuse when no row exists. Metadata is display-only now. Also adds UNIQUE on credit_ledger.paddle_transaction_id + ON CONFLICT DO NOTHING on every grant path, replacing a racy SELECT-then-INSERT. Two unrelated bugs found while testing, fixed in their own commits: metering's get_user_usage query (syntax + GROUP BY + NUMERIC-as-f64), and a stale usage_records ref in the e2e test (now usage_ledger). ### Reviewer: migration 044 adds the table + constraint; the UNIQUE fails to apply if the DB holds duplicate paddle_transaction_id rows from the old race — run the dupe-check first. In-flight pre-deploy transactions have no intent row and won't auto-credit (reconcile manually). ### What I tested already: make test-e2e-billing against a real stack — 37/37, including four new cases: credits the intent amount and ignores a forged custom_data value, refuses with no intent row, credits once on duplicate delivery, and the UNIQUE/ON CONFLICT no-op.
The Paddle transaction.completed webhook took the credited amount from
client-supplied transaction metadata. The signature was verified but the value
was not tied to anything the server controlled, so the granted credit could
diverge from what was actually paid. The /billing/credits/purchase callback had
the same gap.
Move the value decision server-side. The API records a credit_purchase_intents
row (server-computed amount, keyed by transaction id) when it creates the
transaction; the webhook and the callback both credit that recorded amount and
refuse when no row exists. Client metadata is retained for display only.
Make one-payment-one-grant durable: add UNIQUE on
credit_ledger.paddle_transaction_id and switch every grant path to
ON CONFLICT DO NOTHING, replacing a racy SELECT-then-INSERT.
Reviewer: migration 044 adds the table + constraint; the UNIQUE will fail if the
DB already holds duplicate transaction rows, so run the dupe-check first.
In-flight transactions created before this lands have no intent row and won't
auto-credit (reconcile manually). e2e steps 34-37 cover the new behaviour.
GET /api/usage/:user_id was broken three ways and always failed: a trailing
comma before FROM, a SELECT DISTINCT ... GROUP BY that also projected
non-grouped columns, and decoding NUMERIC columns (quantity, base_unit_cost_usd,
margin_percent) as f64 — which panics the worker. Drop the comma and the
DISTINCT/GROUP BY (the handler already rolls rows up in Rust) and cast the
numeric columns to float8 so the existing decode works.
test_billing.sh counted rows from usage_records, which no longer exists in a
fresh schema (the metering usage path moved to usage_ledger in migration 033).
Under set -o pipefail the missing-relation error aborted the suite at step 6.
Point both counts at usage_ledger.
vkobel force-pushed fix/paddle-customdata-credit-mint from 77b2abb4bf to e39208395f 2026年07月01日 14:35:53 +02:00 Compare
@ -36,6 +36,10 @@
# 28. Credit code redemption
# 29. Duplicate code redemption rejected
# 30. Invalid code rejected
# 34. Credit purchase webhook uses server-authoritative intent amount
Owner
Copy link

not really an issue but a few numbers were skipped here

not really an issue but a few numbers were skipped here
This pull request has changes conflicting with the target branch.
  • src/metering/src/webhooks.rs
  • tests/e2e/test_billing.sh
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin fix/paddle-customdata-credit-mint:fix/paddle-customdata-credit-mint
git switch fix/paddle-customdata-credit-mint

Merge

Merge the changes and update on Forgejo.
git switch main
git merge --no-ff fix/paddle-customdata-credit-mint
git switch fix/paddle-customdata-credit-mint
git rebase main
git switch main
git merge --ff-only fix/paddle-customdata-credit-mint
git switch fix/paddle-customdata-credit-mint
git rebase main
git switch main
git merge --no-ff fix/paddle-customdata-credit-mint
git switch main
git merge --squash fix/paddle-customdata-credit-mint
git switch main
git merge --ff-only fix/paddle-customdata-credit-mint
git switch main
git merge fix/paddle-customdata-credit-mint
git push origin main
Sign in to join this conversation.
No reviewers
Labels
Clear labels
Compat/Breaking
Breaking change that won't be backward compatible
Component/Enclave-Builder
This issue affects Enclave Builder (git-push and CLI)
Component/Infrastructure
This issue affects generic Caution hosted infra components
Deploy/BYOC
Relates to Caution Bring-your-own-compute
Deploy/Full-managed
Relates to Caution fully managed cloud
Deploy/Self-hosted
Relates to Caution deployed on self-hosted infra
Interface/API
This issue affects the API
Interface/CLI
This issue affects the command line interface
Interface/Git
This issue affects the Git `push` interface
Interface/Web
This issue affects the web dashboard
Kind/Bug
Something is not working
Kind/Documentation
Documentation changes
Kind/Enhancement
Improve existing functionality
Kind/Feature
New functionality
Kind/Security
This is security issue
Kind/Testing
Issue or pull request related to testing
Priority
Critical
The priority is critical
Priority
High
The priority is high
Priority
Low
The priority is low
Priority
Medium
The priority is medium
Reviewed
Confirmed
Issue has been confirmed
Reviewed
Duplicate
This issue or pull request already exists
Reviewed
Invalid
Invalid issue
Reviewed
Won't Fix
This issue won't be fixed
Status
Abandoned
Somebody has started to work on this but abandoned work
Status
Blocked
Something is blocking this issue or pull request
Status
Need More Info
Feedback is required to reproduce issue or to continue work
Milestone
Clear milestone
No items
No milestone
Projects
Clear projects
No items
No project
Assignees
Clear assignees
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
caution/platform!340
Reference in a new issue
caution/platform
No description provided.
Delete branch "fix/paddle-customdata-credit-mint"

Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?