From 9afb1c3459296194c2fd90c7d23ad93b71c79aa8 Mon Sep 17 00:00:00 2001 From: Eeshan Garg Date: Mon, 19 Nov 2018 13:19:57 -0330 Subject: [PATCH] webhooks/stripe: Support customer.subscription.update events. --- .../customer_subscription_updated.json | 129 ++++++++++++++++++ zerver/webhooks/stripe/tests.py | 7 + zerver/webhooks/stripe/view.py | 6 +- 3 files changed, 140 insertions(+), 2 deletions(-) create mode 100644 zerver/webhooks/stripe/fixtures/customer_subscription_updated.json diff --git a/zerver/webhooks/stripe/fixtures/customer_subscription_updated.json b/zerver/webhooks/stripe/fixtures/customer_subscription_updated.json new file mode 100644 index 0000000000..d02781c087 --- /dev/null +++ b/zerver/webhooks/stripe/fixtures/customer_subscription_updated.json @@ -0,0 +1,129 @@ +{ + "created":1326853478, + "livemode":false, + "id":"customer.subscription.updated_00000000000000", + "type":"customer.subscription.updated", + "object":"event", + "request":null, + "pending_webhooks":1, + "api_version":"2018-02-28", + "data":{ + "object":{ + "id":"sub_00000000000000", + "object":"subscription", + "application_fee_percent":null, + "billing":"charge_automatically", + "billing_cycle_anchor":1542644734, + "cancel_at_period_end":false, + "canceled_at":null, + "created":1542644734, + "current_period_end":1545236734, + "current_period_start":1542644734, + "customer":"cus_00000000000000", + "days_until_due":null, + "default_source":null, + "discount":null, + "ended_at":null, + "items":{ + "object":"list", + "data":[ + { + "id":"si_00000000000000", + "object":"subscription_item", + "created":1542644735, + "metadata":{ + + }, + "plan":{ + "id":"gold_00000000000000", + "object":"plan", + "active":true, + "aggregate_usage":null, + "amount":2000, + "billing_scheme":"per_unit", + "created":1394782612, + "currency":"cad", + "interval":"month", + "interval_count":1, + "livemode":false, + "metadata":{ + + }, + "nickname":null, + "product":"prod_00000000000000", + "tiers":null, + "tiers_mode":null, + "transform_usage":null, + "trial_period_days":null, + "usage_type":"licensed" + }, + "quantity":1, + "subscription":"sub_00000000000000" + } + ], + "has_more":false, + "total_count":1, + "url":"/v1/subscription_items?subscription=sub_E0G5y2VdPVOB4o" + }, + "livemode":false, + "metadata":{ + + }, + "plan":{ + "id":"gold_00000000000000", + "object":"plan", + "active":true, + "aggregate_usage":null, + "amount":2000, + "billing_scheme":"per_unit", + "created":1394782612, + "currency":"cad", + "interval":"month", + "interval_count":1, + "livemode":false, + "metadata":{ + + }, + "nickname":null, + "product":"prod_00000000000000", + "tiers":null, + "tiers_mode":null, + "transform_usage":null, + "trial_period_days":null, + "usage_type":"licensed" + }, + "quantity":1, + "start":1542644734, + "status":"active", + "tax_percent":null, + "trial_end":null, + "trial_start":null + }, + "previous_attributes":{ + "plan":{ + "id":"OLD_00000000000000", + "object":"plan", + "active":true, + "aggregate_usage":null, + "amount":2000, + "billing_scheme":"per_unit", + "created":1542644734, + "currency":"cad", + "interval":"month", + "interval_count":1, + "livemode":false, + "metadata":{ + + }, + "nickname":null, + "product":"prod_00000000000000", + "tiers":null, + "tiers_mode":null, + "transform_usage":null, + "trial_period_days":null, + "usage_type":"licensed", + "name":"Old plan" + } + } + } +} diff --git a/zerver/webhooks/stripe/tests.py b/zerver/webhooks/stripe/tests.py index 0108d42c60..1adf539e2b 100644 --- a/zerver/webhooks/stripe/tests.py +++ b/zerver/webhooks/stripe/tests.py @@ -81,6 +81,13 @@ class StripeHookTests(WebhookTestCase): self.send_and_test_stream_message('customer_subscription_deleted', expected_topic, expected_message, content_type="application/x-www-form-urlencoded") + def test_customer_subscription_updated(self) -> None: + expected_topic = u"Customer sub_00000000000000" + expected_message = u"The customer subscription with id **[sub_00000000000000](https://dashboard.stripe.com/subscriptions/sub_00000000000000)** was updated." + self.send_and_test_stream_message('customer_subscription_updated', + expected_topic, expected_message, + content_type="application/x-www-form-urlencoded") + def test_customer_subscription_trial_will_end(self) -> None: expected_topic = u"Customer sub_00000000000000" expected_message = u"The customer subscription trial with id **[sub_00000000000000](https://dashboard.stripe.com/subscriptions/sub_00000000000000)** will end in 3 days." diff --git a/zerver/webhooks/stripe/view.py b/zerver/webhooks/stripe/view.py index 5d1ed2f862..60ee21ddcd 100644 --- a/zerver/webhooks/stripe/view.py +++ b/zerver/webhooks/stripe/view.py @@ -80,7 +80,7 @@ def api_stripe_webhook(request: HttpRequest, user_profile: UserProfile, body_template = "The customer subscription with id **[{id}]({link})** was deleted." body = body_template.format(id=object_id, link=link) - else: # customer.subscription.trial_will_end + elif event_type == "customer.subscription.trial_will_end": DAY = 60 * 60 * 24 # seconds in a day # days_left should always be three according to # https://stripe.com/docs/api/python#event_types, but do the @@ -89,7 +89,9 @@ def api_stripe_webhook(request: HttpRequest, user_profile: UserProfile, body_template = ("The customer subscription trial with id" " **[{id}]({link})** will end in {days} days.") body = body_template.format(id=object_id, link=link, days=days_left) - + elif event_type == "customer.subscription.updated": + body_template = "The customer subscription with id **[{id}]({link})** was updated." + body = body_template.format(id=object_id, link=link) else: link = "https://dashboard.stripe.com/customers/{}".format(object_id) body_template = "{beginning} customer with id **[{id}]({link})** {rest}."