From 872fc33dd7833240efd802275536c63e8c88f587 Mon Sep 17 00:00:00 2001 From: Colin Maudry Date: Wed, 1 Jul 2026 16:41:57 +0200 Subject: [PATCH] feat(subscriptions): cancel/webhook/add_payment_callback sur get_current et customer_known --- src/subscriptions/routes.py | 10 +++++----- tests/subscriptions/test_routes.py | 16 +++++++--------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/src/subscriptions/routes.py b/src/subscriptions/routes.py index 9fd2a0a..5d32046 100644 --- a/src/subscriptions/routes.py +++ b/src/subscriptions/routes.py @@ -109,7 +109,7 @@ def add_payment(): def add_payment_callback(): session_id = request.args.get("id", "") cust = _customer_handle(current_user.id) - row = db.get_by_user(current_user.id) + row = db.get_current(current_user.id) sub_handle = row["frisbii_subscription_handle"] if row else None if not sub_handle: return redirect("/compte/abonnement?paiement=succes") @@ -132,7 +132,7 @@ def add_payment_callback(): @subscriptions_bp.route("/subscriptions/cancel", methods=["POST"]) @login_required def cancel(): - row = db.get_by_user(current_user.id) + row = db.get_current(current_user.id) if row is None or not row["frisbii_subscription_handle"]: return "Aucun abonnement à résilier", 400 try: @@ -140,7 +140,7 @@ def cancel(): except client.FrisbiiError: logger.exception("Échec de résiliation Frisbii") return redirect("/compte/abonnement?error=frisbii") - db.set_cancelled(current_user.id, sub.get("expires")) + db.set_cancelled(row["id"], sub.get("expires")) return redirect("/compte/abonnement?resiliation=ok") @@ -152,7 +152,7 @@ def webhook(): customer = payload.get("customer") sub_handle = payload.get("subscription") - if not customer or db.get_by_customer(customer) is None: + if not customer or not db.customer_known(customer): return "", 200 # rien à rapprocher try: sub = client.get_subscription(sub_handle) if sub_handle else None @@ -162,5 +162,5 @@ def webhook(): if sub is None: return "", 200 status, current_period_end = webhooks.map_subscription(sub) - db.update_from_webhook(customer, sub_handle, status, current_period_end) + db.update_from_webhook(sub_handle, status, current_period_end) return "", 200 diff --git a/tests/subscriptions/test_routes.py b/tests/subscriptions/test_routes.py index ec653aa..2259e82 100644 --- a/tests/subscriptions/test_routes.py +++ b/tests/subscriptions/test_routes.py @@ -84,10 +84,8 @@ def test_subscribe_requires_login(sub_app): def test_cancel_calls_api_and_marks_cancelled(logged_in_client, monkeypatch): client, uid = logged_in_client - db.create_pending(uid, "colibre-%d" % uid, "simple") - db.update_from_webhook( - "colibre-%d" % uid, "sub_42", "active", "2099-01-01T00:00:00+00:00" - ) + handle, _ = db.create_pending(uid, "colibre-%d" % uid, "simple") + db.update_from_webhook(handle, "active", "2099-01-01T00:00:00+00:00") monkeypatch.setattr( frisbii_client, "cancel_subscription", @@ -96,7 +94,7 @@ def test_cancel_calls_api_and_marks_cancelled(logged_in_client, monkeypatch): resp = client.post("/subscriptions/cancel") assert resp.status_code == 302 assert "resiliation=ok" in resp.headers["Location"] - row = db.get_by_user(uid) + row = db.get_current(uid) assert row["status"] == "cancelled" assert row["current_period_end"] == "2099-02-01T00:00:00+00:00" @@ -113,7 +111,7 @@ def test_webhook_updates_subscription(sub_app, monkeypatch): auth_db.init_schema() uid = auth_db.create_user("wh@ex.fr", "hash") - db.create_pending(uid, "colibre-%d" % uid, "simple") + handle, _ = db.create_pending(uid, "colibre-%d" % uid, "simple") monkeypatch.setattr( frisbii_client, "get_subscription", @@ -124,14 +122,14 @@ def test_webhook_updates_subscription(sub_app, monkeypatch): "timestamp": "2026-06-25T10:00:00Z", "event_type": "subscription_created", "customer": "colibre-%d" % uid, - "subscription": "sub_42", + "subscription": handle, } payload["signature"] = _sign("s3cr3t", payload["timestamp"], payload["id"]) resp = sub_app.test_client().post("/frisbii/webhook", json=payload) assert resp.status_code == 200 - row = db.get_by_user(uid) + row = db.get_current(uid) assert row["status"] == "active" - assert row["frisbii_subscription_handle"] == "sub_42" + assert row["frisbii_subscription_handle"] == handle def test_subscribe_skips_if_already_active(logged_in_client, monkeypatch):