Skip to content

About

Soroban smart contracts for recurring subscription payments on Stellar: capped, interval-bound, revocable.

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Stellar Subscriptions Contracts

Soroban smart contracts for recurring subscription payments on Stellar — Stripe Billing, native to Stellar.

A subscriber authorizes a spending cap and a billing interval once. A merchant can then pull a fixed amount each interval, but never more than authorized and never after the subscriber cancels.

Table of Contents

Try it in ten minutes

  1. Clone and test:
   git clone https://github.com/stellar-sub/stellar-subscriptions-contracts
   cd stellar-subscriptions-contracts
   make test
  1. Read an invariant test: Open contracts/subscription/src/test/invariant_cap.rs to see how the contract enforces "never charge more than authorized."

  2. Verify on Testnet:

   make testnet-deploy
   make testnet-integration

Next: See CONTRIBUTING.md to add features without weakening the invariants.


The security model

The subscriber grants a bounded, revocable authorization; the merchant pulls within it. Three invariants must always hold, and each has tests that prove it:

  1. CAP — the total ever charged never exceeds the authorized total_cap.
  2. INTERVAL — at most one charge per billing interval, even for the right merchant.
  3. REVOCATION — once the subscriber cancels, no charge succeeds, ever.

Those tests were written before the implementation. A property-based fuzz test also checks all three after every step of random charge, pause, resume and cancel sequences. See docs/subscription.md for how each one is enforced.

Contracts

Contract Path Role
Subscription contracts/subscription Authorization, charging, pause / resume / cancel. Enforces the invariants. Docs
Plan contracts/plan Merchants' reusable billing terms. Docs
Registry contracts/registry Index of plans and subscriptions with aggregate stats. Docs
flowchart LR
    S[Subscriber] -- subscribe / pause / resume / cancel --> SUB[Subscription]
    M[Merchant] -- create_plan --> PLAN[Plan]
    M -- charge --> SUB
    SUB -- get_plan at subscribe --> PLAN
    SUB -- transfer_from --> T[(Token)]
    SUB -- register / update --> REG[Registry]
    PLAN -- register_plan --> REG
Loading

Only the subscriber can pause, resume or cancel. Only the merchant can charge. The admin can link the three contracts once, before first use, and nothing else.

Quick start

# Prerequisites: Rust (pinned by rust-toolchain.toml) and the stellar CLI
make test                 # every contract's test suite, including the invariant tests
make build                # wasm via stellar contract build
make check                # everything CI runs: test, clippy, fmt, wasm build
make deploy               # deploy + initialize + link on Testnet
make testnet-deploy       # deploy on Testnet with DEMO=1 defaults
make testnet-integration  # source examples/env.sh and verify invariants on Testnet

Testnet

Two convenience make targets wrap the end-to-end Testnet deployment and live invariant verification workflow:

  • make testnet-deploy — runs scripts/deploy.sh with sensible Testnet defaults (NETWORK=testnet, IDENTITY=subs-deployer, DEMO=1). It builds the contracts, deploys and initializes plan, registry, and subscription, performs the one-shot contract linking, seeds the demo identities/plan/subscription, and writes the deployment variables to .deployments/testnet.env.

    make testnet-deploy
    # Override defaults if needed:
    make testnet-deploy NETWORK=testnet IDENTITY=my-deployer DEMO=1
  • make testnet-integration — sources examples/env.sh to load and validate the deployed contract IDs and demo identities from .deployments/<network>.env, then invokes scripts/verify-testnet.sh to prove the CAP, INTERVAL, and REVOCATION invariants on-chain against the live Testnet deployment.

    make testnet-integration

Layout

contracts/
  subscription/   core subscription + authorization logic
  plan/           merchant billing plans
  registry/       index of plans and subscriptions
docs/             per-contract reference
  subscription.md  contract reference and charge() walkthrough
  plan.md          plan contract reference
  registry.md      registry contract reference
  errors.md        how to read a failed transaction and map it to an error code
scripts/deploy.sh deploy, initialize and link all three
DEPLOYMENTS.md    live contract ids

Deployments

Testnet contract ids are in DEPLOYMENTS.md.

FAQ

My merchant says charges are failing — what do I do?

Symptoms. The merchant's charge call fails with TransferFailed (see docs/errors.md for how to read the failed transaction).

Cause. The subscriber's token allowance has expired. charge pulls funds via transfer_from, which requires a live allowance from the subscriber to the subscription contract. Allowances are time-bounded, so once the allowance lapses every subsequent charge fails with TransferFailed — even though the subscription itself is still active and within its total_cap.

Fix. The subscriber calls refresh_allowance on the subscription contract to re-grant the token allowance. The next charge then succeeds as normal.

Nothing is lost. A failed charge makes no state changes: no balance moves, the charged total is unchanged, and the billing interval is not consumed. Once the allowance is refreshed, the merchant can charge again on the normal schedule — no back-charges or manual reconciliation are needed.

Note to maintainers: issue #239

Issue #239 (re-calling initialize with the same admin is rejected) is already covered on main. initialize returns AlreadyInitialized whenever an admin is stored, and initialize_twice_is_rejected in contracts/subscription/src/test/validation.rs (also in the subscription test for a different admin), contracts/plan/src/test.rs and contracts/registry/src/test.rs calls try_initialize(&s.admin) with the original admin. No code change is needed.

Sister repositories

Contributing

See CONTRIBUTING.md, in particular the section on adding features without weakening the invariants.

Handsoff notes

  • #238: Test: initialize called twice on registry and plan contracts returns AlreadyInitialized

Note to maintainers: register_subscription signature (#251)

The subscription contract's RegistryInterface::register_subscription (contracts/subscription/src/interfaces.rs) and the registry's register_subscription (contracts/registry/src/lib.rs) both take subscription_id, subscriber, merchant, token, so the signatures are aligned on main. The end-to-end subscribe-with-registry path is covered by the_registry_mirrors_the_full_lifecycle in contracts/subscription/src/test/integration.rs. The only change made is removing duplicated subscriber/merchant fields in the registry's SubscriptionRecord literal inside register_subscription.

About

Soroban smart contracts for recurring subscription payments on Stellar: capped, interval-bound, revocable.

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages