diff --git a/zerver/webhooks/stripe/fixtures/customer_subscription_created_no_nickname.json b/zerver/webhooks/stripe/fixtures/customer_subscription_created_no_nickname.json new file mode 100644 index 0000000000..10cbb46084 --- /dev/null +++ b/zerver/webhooks/stripe/fixtures/customer_subscription_created_no_nickname.json @@ -0,0 +1,102 @@ +{ + "id": "evt_1DeFYZDEQaroqDjsX811NXeq", + "object": "event", + "api_version": "2018-11-08", + "created": 1544074671, + "data": { + "object": { + "id": "sub_E6STM5w5EX3K28", + "object": "subscription", + "application_fee_percent": null, + "billing": "send_invoice", + "billing_cycle_anchor": 1544074670, + "cancel_at_period_end": false, + "canceled_at": null, + "created": 1544074670, + "current_period_end": 1575610670, + "current_period_start": 1544074670, + "customer": "cus_00000000000000", + "days_until_due": 7, + "default_source": null, + "discount": null, + "ended_at": null, + "items": { + "object": "list", + "data": [ + { + "id": "si_E6STR3cU0bwWFG", + "object": "subscription_item", + "created": 1544074671, + "metadata": { + }, + "plan": { + "id": "plan_E6SQ6RAtmLVtzg", + "object": "plan", + "active": true, + "aggregate_usage": null, + "amount": 100, + "billing_scheme": "per_unit", + "created": 1544074513, + "currency": "usd", + "interval": "year", + "interval_count": 1, + "livemode": true, + "metadata": { + }, + "nickname": null, + "product": "prod_E6SPZ5RKfKClNL", + "tiers": null, + "tiers_mode": null, + "transform_usage": null, + "trial_period_days": null, + "usage_type": "licensed" + }, + "quantity": 800, + "subscription": "sub_E6STM5w5EX3K28" + } + ], + "has_more": false, + "total_count": 1, + "url": "/v1/subscription_items?subscription=sub_E6STM5w5EX3K28" + }, + "livemode": true, + "metadata": { + }, + "plan": { + "id": "plan_E6SQ6RAtmLVtzg", + "object": "plan", + "active": true, + "aggregate_usage": null, + "amount": 100, + "billing_scheme": "per_unit", + "created": 1544074513, + "currency": "usd", + "interval": "year", + "interval_count": 1, + "livemode": true, + "metadata": { + }, + "nickname": null, + "product": "prod_E6SPZ5RKfKClNL", + "tiers": null, + "tiers_mode": null, + "transform_usage": null, + "trial_period_days": null, + "usage_type": "licensed" + }, + "quantity": 800, + "start": 1544074670, + "status": "active", + "tax_percent": null, + "trial_end": null, + "trial_start": null + } + }, + "livemode": true, + "pending_webhooks": 1, + "request": { + "id": "req_J85F0AkACDDTBU", + "idempotency_key": null + }, + "type": "customer.subscription.created" +} diff --git a/zerver/webhooks/stripe/tests.py b/zerver/webhooks/stripe/tests.py index 3be4d39038..800f682666 100644 --- a/zerver/webhooks/stripe/tests.py +++ b/zerver/webhooks/stripe/tests.py @@ -111,6 +111,20 @@ Billing method: send invoice""" content_type="application/x-www-form-urlencoded", ) + def test_customer_subscription_created_no_nickname(self) -> None: + expected_topic = "cus_00000000000000" + expected_message = """\ +[Subscription](https://dashboard.stripe.com/subscriptions/sub_E6STM5w5EX3K28) created +Plan: https://dashboard.stripe.com/plans/plan_E6SQ6RAtmLVtzg +Quantity: 800 +Billing method: send invoice""" + self.check_webhook( + "customer_subscription_created_no_nickname", + expected_topic, + expected_message, + content_type="application/x-www-form-urlencoded", + ) + def test_customer_subscription_deleted(self) -> None: expected_topic = "cus_00000000000000" expected_message = ( diff --git a/zerver/webhooks/stripe/view.py b/zerver/webhooks/stripe/view.py index 9b14e04049..a5bf75d95c 100644 --- a/zerver/webhooks/stripe/view.py +++ b/zerver/webhooks/stripe/view.py @@ -196,10 +196,16 @@ def topic_and_body(payload: WildValue) -> Tuple[str, str]: ) if event == "created": if object_["plan"]: - body += "\nPlan: [{plan_nickname}](https://dashboard.stripe.com/plans/{plan_id})".format( - plan_nickname=object_["plan"]["nickname"].tame(check_string), - plan_id=object_["plan"]["id"].tame(check_string), - ) + nickname = object_["plan"]["nickname"].tame(check_none_or(check_string)) + if nickname is not None: + body += "\nPlan: [{plan_nickname}](https://dashboard.stripe.com/plans/{plan_id})".format( + plan_nickname=object_["plan"]["nickname"].tame(check_string), + plan_id=object_["plan"]["id"].tame(check_string), + ) + else: + body += "\nPlan: https://dashboard.stripe.com/plans/{plan_id}".format( + plan_id=object_["plan"]["id"].tame(check_string), + ) if object_["quantity"]: body += "\nQuantity: {}".format(object_["quantity"].tame(check_int)) if "billing" in object_: # nocoverage