diff --git a/src/subscriptions/client.py b/src/subscriptions/client.py index cab5248..3e851ac 100644 --- a/src/subscriptions/client.py +++ b/src/subscriptions/client.py @@ -155,3 +155,13 @@ def get_payment_info_url(sub_handle: str, accept_url: str, cancel_url: str) -> s def get_plan(plan_handle: str) -> dict: return _call("GET", f"/v1/plan/{plan_handle}") + + +def change_subscription( + sub_handle: str, plan_handle: str, timing: str = "renewal" +) -> dict: + return _call( + "PUT", + f"/v1/subscription/{sub_handle}", + json={"timing": timing, "plan": plan_handle}, + ) diff --git a/tests/subscriptions/test_client.py b/tests/subscriptions/test_client.py index e86d8c9..afd8ef0 100644 --- a/tests/subscriptions/test_client.py +++ b/tests/subscriptions/test_client.py @@ -131,3 +131,18 @@ def test_get_payment_info_url_merges_existing_query_string(fake_httpx): assert "locale=fr" in url assert "accept_url=https%3A%2F%2Fapp%2Fok" in url assert "cancel_url=https%3A%2F%2Fapp%2Fko" in url + + +def test_change_subscription_sends_timing_and_plan(fake_httpx): + fake_httpx["queue"].append(fake_httpx["Response"](200, {"handle": "abo-1-1"})) + client.change_subscription("abo-1-1", "plan_soutien", timing="renewal") + call = fake_httpx["calls"][0] + assert call["method"] == "PUT" + assert call["url"] == "https://api.test/v1/subscription/abo-1-1" + assert call["json"] == {"timing": "renewal", "plan": "plan_soutien"} + + +def test_change_subscription_immediate_timing(fake_httpx): + fake_httpx["queue"].append(fake_httpx["Response"](200, {"handle": "abo-1-2"})) + client.change_subscription("abo-1-2", "plan_simple", timing="immediate") + assert fake_httpx["calls"][0]["json"]["timing"] == "immediate"