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.

billing: Create customer_has_credit_card_as_default_source function.

Vishnu KScdf683e36f91
diff --git a/corporate/lib/stripe.py b/corporate/lib/stripe.py
index 5dfd6886221..b1cd4b098ea 100644
--- a/corporate/lib/stripe.py
+++ b/corporate/lib/stripe.py
@@ -338,6 +338,19 @@ def do_replace_payment_source(
return updated_stripe_customer
+def stripe_customer_has_credit_card_as_default_source(stripe_customer: stripe.Customer) -> bool:
+ if not stripe_customer.default_source:
+ return False
+ return stripe_customer.default_source.object == "card"
+
+
+def customer_has_credit_card_as_default_source(customer: Customer) -> bool:
+ if not customer.stripe_customer_id:
+ return False
+ stripe_customer = stripe_get_customer(customer.stripe_customer_id)
+ return stripe_customer_has_credit_card_as_default_source(stripe_customer)
+
+
# event_time should roughly be timezone_now(). Not designed to handle
# event_times in the past or future
@transaction.atomic
diff --git a/corporate/tests/stripe_fixtures/customer_has_credit_card_as_default_source--Customer.create.1.json b/corporate/tests/stripe_fixtures/customer_has_credit_card_as_default_source--Customer.create.1.json
new file mode 100644
index 00000000000..611715f7d65
--- /dev/null
+++ b/corporate/tests/stripe_fixtures/customer_has_credit_card_as_default_source--Customer.create.1.json
@@ -0,0 +1,54 @@
+{
+ "account_balance": 0,
+ "address": null,
+ "balance": 0,
+ "created": 1000000000,
+ "currency": null,
+ "default_source": null,
+ "delinquent": false,
+ "description": "zulip (Zulip Dev)",
+ "discount": null,
+ "email": "[email protected]",
+ "id": "cus_NORMALIZED0001",
+ "invoice_prefix": "NORMA01",
+ "invoice_settings": {
+ "custom_fields": null,
+ "default_payment_method": null,
+ "footer": null
+ },
+ "livemode": false,
+ "metadata": {
+ "realm_id": "1",
+ "realm_str": "zulip"
+ },
+ "name": null,
+ "next_invoice_sequence": 1,
+ "object": "customer",
+ "phone": null,
+ "preferred_locales": [],
+ "shipping": null,
+ "sources": {
+ "data": [],
+ "has_more": false,
+ "object": "list",
+ "total_count": 0,
+ "url": "/v1/customers/cus_NORMALIZED0001/sources"
+ },
+ "subscriptions": {
+ "data": [],
+ "has_more": false,
+ "object": "list",
+ "total_count": 0,
+ "url": "/v1/customers/cus_NORMALIZED0001/subscriptions"
+ },
+ "tax_exempt": "none",
+ "tax_ids": {
+ "data": [],
+ "has_more": false,
+ "object": "list",
+ "total_count": 0,
+ "url": "/v1/customers/cus_NORMALIZED0001/tax_ids"
+ },
+ "tax_info": null,
+ "tax_info_verification": null
+}
diff --git a/corporate/tests/stripe_fixtures/customer_has_credit_card_as_default_source--Customer.create.2.json b/corporate/tests/stripe_fixtures/customer_has_credit_card_as_default_source--Customer.create.2.json
new file mode 100644
index 00000000000..20e83ef1555
--- /dev/null
+++ b/corporate/tests/stripe_fixtures/customer_has_credit_card_as_default_source--Customer.create.2.json
@@ -0,0 +1,80 @@
+{
+ "account_balance": 0,
+ "address": null,
+ "balance": 0,
+ "created": 1000000000,
+ "currency": null,
+ "default_source": "card_NORMALIZED00000000000001",
+ "delinquent": false,
+ "description": "zulip (Zulip Dev)",
+ "discount": null,
+ "email": "[email protected]",
+ "id": "cus_NORMALIZED0002",
+ "invoice_prefix": "NORMA02",
+ "invoice_settings": {
+ "custom_fields": null,
+ "default_payment_method": null,
+ "footer": null
+ },
+ "livemode": false,
+ "metadata": {
+ "realm_id": "1",
+ "realm_str": "zulip"
+ },
+ "name": null,
+ "next_invoice_sequence": 1,
+ "object": "customer",
+ "phone": null,
+ "preferred_locales": [],
+ "shipping": null,
+ "sources": {
+ "data": [
+ {
+ "address_city": "Pacific",
+ "address_country": "United States",
+ "address_line1": "Under the sea,",
+ "address_line1_check": "pass",
+ "address_line2": null,
+ "address_state": null,
+ "address_zip": "33333",
+ "address_zip_check": "pass",
+ "brand": "Visa",
+ "country": "US",
+ "customer": "cus_NORMALIZED0002",
+ "cvc_check": "pass",
+ "dynamic_last4": null,
+ "exp_month": 3,
+ "exp_year": 2033,
+ "fingerprint": "NORMALIZED000001",
+ "funding": "credit",
+ "id": "card_NORMALIZED00000000000001",
+ "last4": "4242",
+ "metadata": {},
+ "name": "Ada Starr",
+ "object": "card",
+ "tokenization_method": null
+ }
+ ],
+ "has_more": false,
+ "object": "list",
+ "total_count": 1,
+ "url": "/v1/customers/cus_NORMALIZED0002/sources"
+ },
+ "subscriptions": {
+ "data": [],
+ "has_more": false,
+ "object": "list",
+ "total_count": 0,
+ "url": "/v1/customers/cus_NORMALIZED0002/subscriptions"
+ },
+ "tax_exempt": "none",
+ "tax_ids": {
+ "data": [],
+ "has_more": false,
+ "object": "list",
+ "total_count": 0,
+ "url": "/v1/customers/cus_NORMALIZED0002/tax_ids"
+ },
+ "tax_info": null,
+ "tax_info_verification": null
+}
diff --git a/corporate/tests/stripe_fixtures/customer_has_credit_card_as_default_source--Customer.retrieve.1.json b/corporate/tests/stripe_fixtures/customer_has_credit_card_as_default_source--Customer.retrieve.1.json
new file mode 100644
index 00000000000..611715f7d65
--- /dev/null
+++ b/corporate/tests/stripe_fixtures/customer_has_credit_card_as_default_source--Customer.retrieve.1.json
@@ -0,0 +1,54 @@
+{
+ "account_balance": 0,
+ "address": null,
+ "balance": 0,
+ "created": 1000000000,
+ "currency": null,
+ "default_source": null,
+ "delinquent": false,
+ "description": "zulip (Zulip Dev)",
+ "discount": null,
+ "email": "[email protected]",
+ "id": "cus_NORMALIZED0001",
+ "invoice_prefix": "NORMA01",
+ "invoice_settings": {
+ "custom_fields": null,
+ "default_payment_method": null,
+ "footer": null
+ },
+ "livemode": false,
+ "metadata": {
+ "realm_id": "1",
+ "realm_str": "zulip"
+ },
+ "name": null,
+ "next_invoice_sequence": 1,
+ "object": "customer",
+ "phone": null,
+ "preferred_locales": [],
+ "shipping": null,
+ "sources": {
+ "data": [],
+ "has_more": false,
+ "object": "list",
+ "total_count": 0,
+ "url": "/v1/customers/cus_NORMALIZED0001/sources"
+ },
+ "subscriptions": {
+ "data": [],
+ "has_more": false,
+ "object": "list",
+ "total_count": 0,
+ "url": "/v1/customers/cus_NORMALIZED0001/subscriptions"
+ },
+ "tax_exempt": "none",
+ "tax_ids": {
+ "data": [],
+ "has_more": false,
+ "object": "list",
+ "total_count": 0,
+ "url": "/v1/customers/cus_NORMALIZED0001/tax_ids"
+ },
+ "tax_info": null,
+ "tax_info_verification": null
+}
diff --git a/corporate/tests/stripe_fixtures/customer_has_credit_card_as_default_source--Customer.retrieve.2.json b/corporate/tests/stripe_fixtures/customer_has_credit_card_as_default_source--Customer.retrieve.2.json
new file mode 100644
index 00000000000..b7644511337
--- /dev/null
+++ b/corporate/tests/stripe_fixtures/customer_has_credit_card_as_default_source--Customer.retrieve.2.json
@@ -0,0 +1,104 @@
+{
+ "account_balance": 0,
+ "address": null,
+ "balance": 0,
+ "created": 1000000000,
+ "currency": null,
+ "default_source": {
+ "address_city": "Pacific",
+ "address_country": "United States",
+ "address_line1": "Under the sea,",
+ "address_line1_check": "pass",
+ "address_line2": null,
+ "address_state": null,
+ "address_zip": "33333",
+ "address_zip_check": "pass",
+ "brand": "Visa",
+ "country": "US",
+ "customer": "cus_NORMALIZED0002",
+ "cvc_check": "pass",
+ "dynamic_last4": null,
+ "exp_month": 3,
+ "exp_year": 2033,
+ "fingerprint": "NORMALIZED000001",
+ "funding": "credit",
+ "id": "card_NORMALIZED00000000000001",
+ "last4": "4242",
+ "metadata": {},
+ "name": "Ada Starr",
+ "object": "card",
+ "tokenization_method": null
+ },
+ "delinquent": false,
+ "description": "zulip (Zulip Dev)",
+ "discount": null,
+ "email": "[email protected]",
+ "id": "cus_NORMALIZED0002",
+ "invoice_prefix": "NORMA02",
+ "invoice_settings": {
+ "custom_fields": null,
+ "default_payment_method": null,
+ "footer": null
+ },
+ "livemode": false,
+ "metadata": {
+ "realm_id": "1",
+ "realm_str": "zulip"
+ },
+ "name": null,
+ "next_invoice_sequence": 1,
+ "object": "customer",
+ "phone": null,
+ "preferred_locales": [],
+ "shipping": null,
+ "sources": {
+ "data": [
+ {
+ "address_city": "Pacific",
+ "address_country": "United States",
+ "address_line1": "Under the sea,",
+ "address_line1_check": "pass",
+ "address_line2": null,
+ "address_state": null,
+ "address_zip": "33333",
+ "address_zip_check": "pass",
+ "brand": "Visa",
+ "country": "US",
+ "customer": "cus_NORMALIZED0002",
+ "cvc_check": "pass",
+ "dynamic_last4": null,
+ "exp_month": 3,
+ "exp_year": 2033,
+ "fingerprint": "NORMALIZED000001",
+ "funding": "credit",
+ "id": "card_NORMALIZED00000000000001",
+ "last4": "4242",
+ "metadata": {},
+ "name": "Ada Starr",
+ "object": "card",
+ "tokenization_method": null
+ }
+ ],
+ "has_more": false,
+ "object": "list",
+ "total_count": 1,
+ "url": "/v1/customers/cus_NORMALIZED0002/sources"
+ },
+ "subscriptions": {
+ "data": [],
+ "has_more": false,
+ "object": "list",
+ "total_count": 0,
+ "url": "/v1/customers/cus_NORMALIZED0002/subscriptions"
+ },
+ "tax_exempt": "none",
+ "tax_ids": {
+ "data": [],
+ "has_more": false,
+ "object": "list",
+ "total_count": 0,
+ "url": "/v1/customers/cus_NORMALIZED0002/tax_ids"
+ },
+ "tax_info": null,
+ "tax_info_verification": null
+}
diff --git a/corporate/tests/stripe_fixtures/customer_has_credit_card_as_default_source--Token.create.1.json b/corporate/tests/stripe_fixtures/customer_has_credit_card_as_default_source--Token.create.1.json
new file mode 100644
index 00000000000..60d47536e7c
--- /dev/null
+++ b/corporate/tests/stripe_fixtures/customer_has_credit_card_as_default_source--Token.create.1.json
@@ -0,0 +1,33 @@
+{
+ "card": {
+ "address_city": "Pacific",
+ "address_country": "United States",
+ "address_line1": "Under the sea,",
+ "address_line1_check": "unchecked",
+ "address_line2": null,
+ "address_state": null,
+ "address_zip": "33333",
+ "address_zip_check": "unchecked",
+ "brand": "Visa",
+ "country": "US",
+ "cvc_check": "unchecked",
+ "dynamic_last4": null,
+ "exp_month": 3,
+ "exp_year": 2033,
+ "fingerprint": "NORMALIZED000001",
+ "funding": "credit",
+ "id": "card_NORMALIZED00000000000001",
+ "last4": "4242",
+ "metadata": {},
+ "name": "Ada Starr",
+ "object": "card",
+ "tokenization_method": null
+ },
+ "client_ip": "0.0.0.0",
+ "created": 1000000000,
+ "id": "tok_NORMALIZED00000000000001",
+ "livemode": false,
+ "object": "token",
+ "type": "card",
+ "used": false
+}
diff --git a/corporate/tests/test_stripe.py b/corporate/tests/test_stripe.py
index c6dccb95fb9..7a5eb1bdf43 100644
--- a/corporate/tests/test_stripe.py
+++ b/corporate/tests/test_stripe.py
@@ -29,6 +29,8 @@ from corporate.lib.stripe import (
attach_discount_to_realm,
catch_stripe_errors,
compute_plan_parameters,
+ customer_has_credit_card_as_default_source,
+ do_create_stripe_customer,
get_discount_for_realm,
get_latest_seat_count,
get_price_per_license,
@@ -40,6 +42,7 @@ from corporate.lib.stripe import (
next_month,
process_initial_upgrade,
sign_string,
+ stripe_customer_has_credit_card_as_default_source,
stripe_get_customer,
unsign_string,
update_billing_method_of_current_plan,
@@ -502,6 +505,7 @@ class StripeTest(StripeTestCase):
Customer.objects.get(realm=user.realm).stripe_customer_id
)
self.assertEqual(stripe_customer.default_source.id[:5], "card_")
+ self.assertTrue(stripe_customer_has_credit_card_as_default_source(stripe_customer))
self.assertEqual(stripe_customer.description, "zulip (Zulip Dev)")
self.assertEqual(stripe_customer.discount, None)
self.assertEqual(stripe_customer.email, user.email)
@@ -662,6 +666,7 @@ class StripeTest(StripeTestCase):
stripe_customer = stripe_get_customer(
Customer.objects.get(realm=user.realm).stripe_customer_id
)
+ self.assertFalse(stripe_customer_has_credit_card_as_default_source(stripe_customer))
# It can take a second for Stripe to attach the source to the customer, and in
# particular it may not be attached at the time stripe_get_customer is called above,
# causing test flakes.
@@ -2684,6 +2689,18 @@ class StripeTest(StripeTestCase):
self.assertEqual(realm_audit_log.acting_user, iago)
self.assertEqual(realm_audit_log.extra_data, str(expected_extra_data))
+ @mock_stripe()
+ def test_customer_has_credit_card_as_default_source(self, *mocks: Mock) -> None:
+ iago = self.example_user("iago")
+ customer = Customer.objects.create(realm=iago.realm)
+ self.assertFalse(customer_has_credit_card_as_default_source(customer))
+
+ customer = do_create_stripe_customer(iago)
+ self.assertFalse(customer_has_credit_card_as_default_source(customer))
+
+ customer = do_create_stripe_customer(iago, stripe_token=stripe_create_token().id)
+ self.assertTrue(customer_has_credit_card_as_default_source(customer))
+
class RequiresBillingAccessTest(ZulipTestCase):
def setUp(self) -> None: