Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

webhooks/stripe: Use object field to determine display type.

sathwikshetty333f5f9a6667e2
diff --git a/zerver/webhooks/stripe/tests.py b/zerver/webhooks/stripe/tests.py
index 27553cc0e5c..b87a82ae08d 100644
--- a/zerver/webhooks/stripe/tests.py
+++ b/zerver/webhooks/stripe/tests.py
@@ -1,6 +1,8 @@
from unittest import mock
from unittest.mock import MagicMock, patch
+import orjson
+
from zerver.lib.test_classes import WebhookTestCase
@@ -25,6 +27,23 @@ class StripeHookTests(WebhookTestCase):
content_type="application/x-www-form-urlencoded",
)
+ def test_charge_dispute_created_pdp_prefix(self) -> None:
+ self.subscribe(self.test_user, self.channel_name)
+ payload = orjson.loads(self.get_body("charge_dispute_created"))
+ payload["data"]["object"]["id"] = "pdp_00000000000000"
+ msg = self.send_webhook_payload(
+ self.test_user,
+ self.url,
+ orjson.dumps(payload).decode(),
+ content_type="application/x-www-form-urlencoded",
+ )
+ self.assert_channel_message(
+ message=msg,
+ channel_name=self.channel_name,
+ topic_name="disputes",
+ content="[Dispute](https://dashboard.stripe.com/disputes/pdp_00000000000000) created. Current status: needs response.",
+ )
+
def test_charge_failed(self) -> None:
expected_topic_name = "charges"
expected_message = (
diff --git a/zerver/webhooks/stripe/view.py b/zerver/webhooks/stripe/view.py
index 2c5290bca66..db5e475072e 100644
--- a/zerver/webhooks/stripe/view.py
+++ b/zerver/webhooks/stripe/view.py
@@ -84,6 +84,10 @@ def topic_and_body(payload: WildValue) -> tuple[str, str]:
topic_name = customer_id
body = None
+ def charge_object_type(charge_id: str) -> str:
+ # Legacy ACH-style payments report object_type "charge" but use a "py_" id prefix.
+ return "payment" if charge_id.startswith("py_") else "charge"
+
def update_string(blacklist: Sequence[str] = []) -> str:
assert "previous_attributes" in payload["data"]
previous_attributes = set(payload["data"]["previous_attributes"].keys()).difference(
@@ -100,8 +104,11 @@ def topic_and_body(payload: WildValue) -> tuple[str, str]:
)
def default_body(update_blacklist: Sequence[str] = []) -> str:
+ id = object_["id"].tame(check_string)
+ type = object_["object"].tame(check_string)
body = "{resource} {verbed}".format(
- resource=linkified_id(object_["id"].tame(check_string)), verbed=event.replace("_", " ")
+ resource=linkified_id(id, type),
+ verbed=event.replace("_", " "),
)
if event == "updated":
return body + update_string(blacklist=update_blacklist)
@@ -127,8 +134,10 @@ def topic_and_body(payload: WildValue) -> tuple[str, str]:
if resource == "charge":
if not topic_name: # only in legacy fixtures
topic_name = "charges"
+ object_id = object_["id"].tame(check_string)
+ charge_type = charge_object_type(object_id)
body = "{resource} for {amount} {verbed}".format(
- resource=linkified_id(object_["id"].tame(check_string)),
+ resource=linkified_id(object_id, charge_type),
amount=amount_string(
object_["amount"].tame(check_int), object_["currency"].tame(check_string)
),
@@ -143,9 +152,13 @@ def topic_and_body(payload: WildValue) -> tuple[str, str]:
)
if resource == "refund":
topic_name = "refunds"
+ id = object_["id"].tame(check_string)
+ type = object_["object"].tame(check_string)
+ charge_id = object_["charge"].tame(check_string)
+ charge_type = charge_object_type(charge_id)
body = "A {resource} for a {charge} of {amount} was updated.".format(
- resource=linkified_id(object_["id"].tame(check_string), lower=True),
- charge=linkified_id(object_["charge"].tame(check_string), lower=True),
+ resource=linkified_id(id, type, lower=True),
+ charge=linkified_id(charge_id, charge_type, lower=True),
amount=amount_string(
object_["amount"].tame(check_int), object_["currency"].tame(check_string)
),
@@ -323,45 +336,43 @@ class DisplayInfo:
STRIPE_OBJECT_TYPES: dict[str, DisplayInfo] = {
# CORE RESOURCES
- "ch": DisplayInfo("Charge", "charges"),
- "cus": DisplayInfo("Customer", "customers"),
- "dp": DisplayInfo("Dispute", "disputes"),
- "du": DisplayInfo("Dispute", "disputes"),
+ "charge": DisplayInfo("Charge", "charges"),
+ "customer": DisplayInfo("Customer", "customers"),
+ "dispute": DisplayInfo("Dispute", "disputes"),
"file": DisplayInfo("File", "files"),
- "link": DisplayInfo("File link", "file_links"),
- "pi": DisplayInfo("Payment intent", "payment_intents"),
- "po": DisplayInfo("Payout", "payouts"),
- "prod": DisplayInfo("Product", "products"),
- "re": DisplayInfo("Refund", "refunds"),
- "tok": DisplayInfo("Token", "tokens"),
+ "file_link": DisplayInfo("File link", "file_links"),
+ "payment_intent": DisplayInfo("Payment intent", "payment_intents"),
+ "payout": DisplayInfo("Payout", "payouts"),
+ "product": DisplayInfo("Product", "products"),
+ "refund": DisplayInfo("Refund", "refunds"),
+ "token": DisplayInfo("Token", "tokens"),
# PAYMENT METHODS
# Payment methods have URL prefixes like /customers/cus_id/sources.
- "ba": DisplayInfo("Bank account"),
+ "bank_account": DisplayInfo("Bank account"),
"card": DisplayInfo("Card"),
- "src": DisplayInfo("Source"),
+ "source": DisplayInfo("Source"),
# BILLING
# Coupons have a configurable id, but the URL prefix is /coupons.
# It's not clear if discounts have a URL.
- "in": DisplayInfo("Invoice", "invoices"),
- "ii": DisplayInfo("Invoice item", "invoiceitems"),
+ "invoice": DisplayInfo("Invoice", "invoices"),
+ "invoiceitem": DisplayInfo("Invoice item", "invoiceitems"),
# Products are covered in core resources.
# Plans have a configurable id, though by default they are created with this pattern:
# 'plan': ('Plan', 'plans').
- "sub": DisplayInfo("Subscription", "subscriptions"),
- "si": DisplayInfo("Subscription item", "subscription_items"),
+ "subscription": DisplayInfo("Subscription", "subscriptions"),
+ "subscription_item": DisplayInfo("Subscription item", "subscription_items"),
# Usage records seem to have URL prefixes like /subscription_items/si_id/usage_record_summaries.
- "mbur": DisplayInfo("Usage record"),
+ "usage_record": DisplayInfo("Usage record"),
# UNDOCUMENTED
- "py": DisplayInfo("Payment", "payments"),
- "pyr": DisplayInfo("Refund", "refunds"),
+ "payment": DisplayInfo("Payment", "payments"),
# Connect, Fraud, Orders, etc not implemented.
}
-def linkified_id(id: str, lower: bool = False) -> str:
- display_info = STRIPE_OBJECT_TYPES[id.split("_", 1)[0]]
+def linkified_id(id: str, type: str, lower: bool = False) -> str:
+ display_info = STRIPE_OBJECT_TYPES[type]
name = display_info.name
- if lower: # nocoverage
+ if lower:
name = name.lower()
if display_info.url_prefix is None: # nocoverage
return name