feat(subscriptions): cancel/webhook/add_payment_callback sur get_current et customer_known
This commit is contained in:
@@ -109,7 +109,7 @@ def add_payment():
|
|||||||
def add_payment_callback():
|
def add_payment_callback():
|
||||||
session_id = request.args.get("id", "")
|
session_id = request.args.get("id", "")
|
||||||
cust = _customer_handle(current_user.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
|
sub_handle = row["frisbii_subscription_handle"] if row else None
|
||||||
if not sub_handle:
|
if not sub_handle:
|
||||||
return redirect("/compte/abonnement?paiement=succes")
|
return redirect("/compte/abonnement?paiement=succes")
|
||||||
@@ -132,7 +132,7 @@ def add_payment_callback():
|
|||||||
@subscriptions_bp.route("/subscriptions/cancel", methods=["POST"])
|
@subscriptions_bp.route("/subscriptions/cancel", methods=["POST"])
|
||||||
@login_required
|
@login_required
|
||||||
def cancel():
|
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"]:
|
if row is None or not row["frisbii_subscription_handle"]:
|
||||||
return "Aucun abonnement à résilier", 400
|
return "Aucun abonnement à résilier", 400
|
||||||
try:
|
try:
|
||||||
@@ -140,7 +140,7 @@ def cancel():
|
|||||||
except client.FrisbiiError:
|
except client.FrisbiiError:
|
||||||
logger.exception("Échec de résiliation Frisbii")
|
logger.exception("Échec de résiliation Frisbii")
|
||||||
return redirect("/compte/abonnement?error=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")
|
return redirect("/compte/abonnement?resiliation=ok")
|
||||||
|
|
||||||
|
|
||||||
@@ -152,7 +152,7 @@ def webhook():
|
|||||||
|
|
||||||
customer = payload.get("customer")
|
customer = payload.get("customer")
|
||||||
sub_handle = payload.get("subscription")
|
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
|
return "", 200 # rien à rapprocher
|
||||||
try:
|
try:
|
||||||
sub = client.get_subscription(sub_handle) if sub_handle else None
|
sub = client.get_subscription(sub_handle) if sub_handle else None
|
||||||
@@ -162,5 +162,5 @@ def webhook():
|
|||||||
if sub is None:
|
if sub is None:
|
||||||
return "", 200
|
return "", 200
|
||||||
status, current_period_end = webhooks.map_subscription(sub)
|
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
|
return "", 200
|
||||||
|
|||||||
@@ -84,10 +84,8 @@ def test_subscribe_requires_login(sub_app):
|
|||||||
|
|
||||||
def test_cancel_calls_api_and_marks_cancelled(logged_in_client, monkeypatch):
|
def test_cancel_calls_api_and_marks_cancelled(logged_in_client, monkeypatch):
|
||||||
client, uid = logged_in_client
|
client, uid = logged_in_client
|
||||||
db.create_pending(uid, "colibre-%d" % uid, "simple")
|
handle, _ = db.create_pending(uid, "colibre-%d" % uid, "simple")
|
||||||
db.update_from_webhook(
|
db.update_from_webhook(handle, "active", "2099-01-01T00:00:00+00:00")
|
||||||
"colibre-%d" % uid, "sub_42", "active", "2099-01-01T00:00:00+00:00"
|
|
||||||
)
|
|
||||||
monkeypatch.setattr(
|
monkeypatch.setattr(
|
||||||
frisbii_client,
|
frisbii_client,
|
||||||
"cancel_subscription",
|
"cancel_subscription",
|
||||||
@@ -96,7 +94,7 @@ def test_cancel_calls_api_and_marks_cancelled(logged_in_client, monkeypatch):
|
|||||||
resp = client.post("/subscriptions/cancel")
|
resp = client.post("/subscriptions/cancel")
|
||||||
assert resp.status_code == 302
|
assert resp.status_code == 302
|
||||||
assert "resiliation=ok" in resp.headers["Location"]
|
assert "resiliation=ok" in resp.headers["Location"]
|
||||||
row = db.get_by_user(uid)
|
row = db.get_current(uid)
|
||||||
assert row["status"] == "cancelled"
|
assert row["status"] == "cancelled"
|
||||||
assert row["current_period_end"] == "2099-02-01T00:00:00+00:00"
|
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()
|
auth_db.init_schema()
|
||||||
uid = auth_db.create_user("wh@ex.fr", "hash")
|
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(
|
monkeypatch.setattr(
|
||||||
frisbii_client,
|
frisbii_client,
|
||||||
"get_subscription",
|
"get_subscription",
|
||||||
@@ -124,14 +122,14 @@ def test_webhook_updates_subscription(sub_app, monkeypatch):
|
|||||||
"timestamp": "2026-06-25T10:00:00Z",
|
"timestamp": "2026-06-25T10:00:00Z",
|
||||||
"event_type": "subscription_created",
|
"event_type": "subscription_created",
|
||||||
"customer": "colibre-%d" % uid,
|
"customer": "colibre-%d" % uid,
|
||||||
"subscription": "sub_42",
|
"subscription": handle,
|
||||||
}
|
}
|
||||||
payload["signature"] = _sign("s3cr3t", payload["timestamp"], payload["id"])
|
payload["signature"] = _sign("s3cr3t", payload["timestamp"], payload["id"])
|
||||||
resp = sub_app.test_client().post("/frisbii/webhook", json=payload)
|
resp = sub_app.test_client().post("/frisbii/webhook", json=payload)
|
||||||
assert resp.status_code == 200
|
assert resp.status_code == 200
|
||||||
row = db.get_by_user(uid)
|
row = db.get_current(uid)
|
||||||
assert row["status"] == "active"
|
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):
|
def test_subscribe_skips_if_already_active(logged_in_client, monkeypatch):
|
||||||
|
|||||||
Reference in New Issue
Block a user