stripe: Be more explicit about the valid set of event types.

Previously, the Stripe webhook code was riddled with implicit
assertions that there were exactly N event types within a given
category, and we handled the last one in a final `else` clause in the
block.  This was likely to cause confusing problems in the event that
we're missing an event type (or Stripe adds a new one).

We fix this by just adding a few more conditionals and raising the
standard "unexpected event type" exception for the others.
This commit is contained in:
Tim Abbott
2018-11-19 11:40:54 -08:00
parent 9afb1c3459
commit 1abb1447df

View File

@@ -92,6 +92,8 @@ def api_stripe_webhook(request: HttpRequest, user_profile: UserProfile,
elif event_type == "customer.subscription.updated": elif event_type == "customer.subscription.updated":
body_template = "The customer subscription with id **[{id}]({link})** was updated." body_template = "The customer subscription with id **[{id}]({link})** was updated."
body = body_template.format(id=object_id, link=link) body = body_template.format(id=object_id, link=link)
else:
raise UnexpectedWebhookEventType("Stripe", event_type)
else: else:
link = "https://dashboard.stripe.com/customers/{}".format(object_id) link = "https://dashboard.stripe.com/customers/{}".format(object_id)
body_template = "{beginning} customer with id **[{id}]({link})** {rest}." body_template = "{beginning} customer with id **[{id}]({link})** {rest}."
@@ -102,9 +104,11 @@ def api_stripe_webhook(request: HttpRequest, user_profile: UserProfile,
rest = "has been created" rest = "has been created"
else: else:
rest = "and email **{}** has been created".format(data_object['email']) rest = "and email **{}** has been created".format(data_object['email'])
else: elif event_type == "customer.deleted":
beginning = "A" beginning = "A"
rest = "has been deleted" rest = "has been deleted"
else:
raise UnexpectedWebhookEventType("Stripe", event_type)
body = body_template.format(beginning=beginning, id=object_id, link=link, rest=rest) body = body_template.format(beginning=beginning, id=object_id, link=link, rest=rest)
topic = "Customer {}".format(object_id) topic = "Customer {}".format(object_id)
@@ -130,9 +134,11 @@ def api_stripe_webhook(request: HttpRequest, user_profile: UserProfile,
elif event_type == "order.payment_succeeded": elif event_type == "order.payment_succeeded":
beginning = "An order payment on" beginning = "An order payment on"
end = "succeeded" end = "succeeded"
else: elif event_type == "order.updated":
beginning = "The" beginning = "The"
end = "been updated" end = "been updated"
else:
raise UnexpectedWebhookEventType("Stripe", event_type)
body = body_template.format(beginning=beginning, body = body_template.format(beginning=beginning,
id=object_id, id=object_id,
@@ -149,8 +155,10 @@ def api_stripe_webhook(request: HttpRequest, user_profile: UserProfile,
"for amount **{amount}** has {end}." "for amount **{amount}** has {end}."
if event_type == "transfer.failed": if event_type == "transfer.failed":
end = 'failed' end = 'failed'
else: elif event_type == "transfer.paid":
end = "been paid" end = "been paid"
else:
raise UnexpectedWebhookEventType('Stripe', event_type)
body = body_template.format( body = body_template.format(
description=data_object['description'], description=data_object['description'],
id=object_id, id=object_id,