View as Markdown

Start and cancel a subscription#

The person approves the plan and its monthly price once; you charge it until they cancel.

Level 1: A person approves with a passkey.
Class write-hard: Hard to undo. The app creates the approval for that exact request, and it works once.
routes.append(route("POST", "/plans/{plan}/subscription", "plans:subscribe", cls="write-hard", level=1,
                    resource="plan:{plan}", amount_field="/monthly", summary="Subscribe to a plan",
                    body={"monthly": "the price per month, e.g. 12.00", "currency": "EUR"}))
routes.append(route("GET", "/subscription", "plans:read", level=1, resource="plan:*", summary="Your subscription"))
routes.append(route("DELETE", "/subscription", "plans:cancel", cls="write-hard", level=1, resource="plan:mine",
                    summary="Cancel your subscription"))
templates["plans:subscribe"] = "Subscribe to {scope} for {amount.max} {amount.currency} per month, until you cancel"
templates["plans:read"] = "See your subscription"
templates["plans:cancel"] = "Cancel your subscription"


@app.post("/plans/<plan>/subscription")
def subscribe(plan):
    # Approved once, with the price: your billing charges it every month (subscriptions.py) until it is cancelled.
    return subscriptions.start(g.allowedby.human_id, plan, request.json["monthly"]), 201


@app.get("/subscription")
def subscription():
    return subscriptions.get(g.allowedby.human_id) or ({"error": "no subscription"}, 404)


@app.delete("/subscription")
def cancel():
    return subscriptions.cancel(g.allowedby.human_id) or ({"error": "no subscription"}, 404)

Run in 8 languages on this version. See the results

What the test proves#

  • the person approves the plan and its monthly price, once: 201, charged
  • another price needs another approval
  • a month later it is charged again, and nobody is asked
  • the person approves the cancellation: it is never charged again