webhooks/stripe: Support customer.subscription.update events.

This commit is contained in:
Eeshan Garg 2018-11-19 13:19:57 -03:30 committed by Tim Abbott
parent ba640bf89d
commit 9afb1c3459
3 changed files with 140 additions and 2 deletions

View File

@ -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"
}
}
}
}

View File

@ -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."

View File

@ -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}."