Skip to content

Take payments through Rotur payment requests - #31

Merged
Mistium merged 3 commits into
mainfrom
rotur-app-payments-v2
Oct 3, 2026
Merged

Mistium merged 3 commits into
mainfrom
rotur-app-payments-v2

Conversation

@Mistium

@Mistium Mistium commented Oct 2, 2026 •

Copy link
Copy Markdown
Member

Purchases, donations and refunds move to Rotur payment requests (https://docs.rotur.dev/build-an-app/payments). MistWarp no longer needs anyone's credits:transfer, and no longer pays sellers out of its own account.

Before

  • The editor transferred credits to MistWarp's account with the person's own token (which held credits:transfer), then mistwarp-api searched the transfer history for it.
  • mistwarp-api then paid the seller, collaborators and remix parents from MistWarp's account with MISTWARP_ROTUR_TOKEN. Payouts that failed were queued in data/pending-payouts.json, with admin routes to retry them.

Now

  1. Intent: the editor asks mistwarp-api for an intent. mistwarp-api asks Rotur, with the app's client ID and secret, for a payment request. The request's reference is the purchase key (mwbuy_, mwgame_, mwdonate_ or mwrefund_). The answer includes Rotur's approveUrl.
  2. Approval: the person approves it on rotur.dev with their password. For sales, the request carries splits, so Rotur pays the creator, collaborators, the remixed project's creator and MistWarp's fee in one go (rotur/api#83). MistWarp's share goes to MISTWARP_ROTUR_USER, or else the app's owner.
  3. Delivery: payment.completed marks the key paid and delivers projects and game products. The editor's confirm call can do the same before the webhook arrives, by asking Rotur whether the request was paid. Whichever comes second finds the purchase already delivered and answers yes.
  4. Donations and refunds: a donation is delivered when its comment is posted with donation: {key}. A refund has its own intent (POST /projects/:id/products/:product/refund/intent): the creator pays the buyer back, then revokes with refundKey.

Changes to the API

  • Intents now answer {key, requestId, approveUrl, amount, splits}.
  • Confirm calls take {key}.
  • /payouts and the pending-payout admin stats are gone.

Depends on

  • rotur/api#83, for splits on payment requests. Until it's deployed, Rotur refuses requests with splits, so sales can't start. Donations and refunds still work.
  • rotur/site#117, for showing the shares on the approve page.
  • The editor's payments PR (Pay through Rotur payment requests scratch-gui#108), which opens Rotur's approve window and sends {key}. Merge and deploy the two together. The old editor's transfer-then-confirm flow won't work against this.

Rebased on main, now that #25 is merged.

Still on the old keys

  • COMMERCE_SERVICE_KEY: still needed for bounty awards and the groups integration.
  • MISTWARP_ROTUR_TOKEN: still needed for notifications and /profile lookups.

Bounties need a Rotur change before they can move (escrow, or holding a payment request until a winner is picked).

Review fixes

  • One purchase, one request. Starting the same purchase again (same buyer, item, price and splits) while its Rotur request has more than a minute left hands back that request and its approveUrl, instead of making a second one that could also be paid. It's only reused while Rotur still says pending, so after declining, the person can try again straight away. The check and the new request happen under a lock for that purchase.
  • Old clients are refused. The purchase, game product and donation intents answer 409 client_outdated, "MistWarp was updated, reload this page", when the body has no returnUrl key. Editor tabs opened before the deploy, and packaged games exported before scratch-gui#108, would otherwise pay the old way with nothing delivered. The editor and current packaged games always send returnUrl, even when it's empty.
  • Refunds pay back what was paid. The amount comes from the buyer's purchase record, not the current price. Game product records now store the price at the intent. A product that was granted or claimed for free has nothing to refund, so it can only be revoked without one.
  • A failed revoke keeps the refund key, which is only forgotten once removeGameProductEntitlement succeeds, so the revoke can be retried.
  • Splits are saved on the purchase key at the intent and used at delivery for the sale record, instead of being worked out again.
  • No fee account, no sale. If mistwarpFeeAccount() can't be found (Rotur unreachable and MISTWARP_ROTUR_USER unset), the intent answers 503 "MistWarp can't take payments right now" instead of the creator getting MistWarp's share.
  • The payer is never in the splits. If the buyer is the remixed project's owner or the fee account, their share goes to the next person in the splits (the one before, if they're last), since Rotur refuses splits that include the payer. The intent answers with the splits actually sent.

Before deploying

  • Check that data/pending-payouts.json is empty, or has nothing still owed. This PR removes the payouts queue and its retry, so anything left in it would never be paid.

Testing

  • Unit tests: rotur-payments.test.osl is new and covers:

    • the fee account;
    • request bodies with and without splits;
    • the invalid_return_url retry;
    • refusals;
    • paid checks against the reference;
    • the webhook marking keys paid and delivering only sales;
    • forgetting keys.

    rotur-webhooks.test.osl covers payment.completed. All 29 OSL test files pass, including new cases for reusing an open request, a request about to expire, and payers in the splits.

  • End to end: run against real Rotur (claw built from rotur/api#83) and this build, in a private network namespace, twice in a row. 29 of 29 checks pass:

    • Alice buys Bob's 10-credit project. The request splits 9 to Bob and 1 to MistWarp. Confirming before paying gets 402, and Bob can't confirm her purchase. She approves on Rotur, confirming delivers the project, and the balances end at 90, 9 and 1. Confirming twice doesn't deliver twice.
    • A 4-credit game product is delivered by a signed payment.completed with no confirm. A forged signature is refused, and confirming afterwards answers yes.
    • A 5-credit donation comment is refused before paying, posted after, and its key can't be used twice.
    • Revoking a paid product needs a paid refund from Bob. Alice can't start one. Afterwards Alice no longer owns the product, and the final balances are 85, 13.6 and 1.4.

    Claw only delivers webhooks to public https addresses, so the e2e signs payment.completed itself, in claw's exact format and with a real request ID. Rotur's own delivery to api.mistwarp.org wasn't tested.

No visible surface in this repo; the editor's payments PR carries the screenshots.

Buying a project or game product, donating with a comment and refunding
a game product now ask Rotur for a payment request with the app's
credentials. The person approves it on rotur.dev, Rotur shares the
credits between everyone the sale pays, and payment.completed (or the
editor's confirm, whichever comes first) delivers the purchase.

This replaces checking transfers people made with MistWarp's
credits:transfer token, and paying sellers out of MistWarp's own
account, so the pending payouts queue and its admin routes go.
…accepts

- Starting the same purchase again while its Rotur request is still open
  hands back that request, so paying twice can't happen.
- A refund pays back what the buyer paid, from their purchase record, and
  a failed revoke keeps the refund key so it can be tried again.
- The splits are saved with the purchase and used when it's delivered.
- Without the fee account a sale doesn't start, instead of the creator
  getting MistWarp's share.
- The payer is dropped from the splits, with their share going to the next
  person, since Rotur refuses splits that pay the payer.
@Mistium
Mistium force-pushed the rotur-app-payments-v2 branch from 8a453a9 to 33d8339 Compare October 2, 2026 17:33
@Mistium
Mistium changed the base branch from rotur-app-bans to main October 2, 2026 17:33
…e at a time

- An open request is reused only while Rotur still has it pending, so a
  declined one doesn't stop the person trying again until it expires.
- Purchase, game product and donation intents answer 409 "MistWarp was
  updated, reload this page" when the body has no returnUrl, as from an
  editor tab or packaged game made before payment requests. Those would
  pay the old way, which nothing delivers any more.
- Looking for an open request and making a new one happen under a lock
  for that purchase, so two clicks can't make two requests.
@Mistium
Mistium merged commit a427d50 into main Oct 3, 2026
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