billing: Expand default_source object when retrieving customer.

This commit is contained in:
Vishnu Ks 2018-07-24 18:10:46 +05:30 committed by Rishi Gupta
parent 1225bfd6ee
commit b88613fd0b
3 changed files with 30 additions and 16 deletions

View File

@ -91,7 +91,7 @@ def catch_stripe_errors(func: CallableT) -> CallableT:
@catch_stripe_errors @catch_stripe_errors
def get_stripe_customer(stripe_customer_id: str) -> stripe.Customer: def get_stripe_customer(stripe_customer_id: str) -> stripe.Customer:
stripe_customer = stripe.Customer.retrieve(stripe_customer_id) stripe_customer = stripe.Customer.retrieve(stripe_customer_id, expand=["default_source"])
if PRINT_STRIPE_FIXTURE_DATA: if PRINT_STRIPE_FIXTURE_DATA:
print(''.join(['"retrieve_customer": ', str(stripe_customer), ','])) # nocoverage print(''.join(['"retrieve_customer": ', str(stripe_customer), ','])) # nocoverage
return stripe_customer return stripe_customer
@ -113,15 +113,6 @@ def extract_current_subscription(stripe_customer: stripe.Customer) -> Any:
return stripe_subscription return stripe_subscription
return None return None
@catch_stripe_errors
def payment_source(stripe_customer: stripe.Customer) -> Optional[stripe.Card]:
if stripe_customer.default_source is None:
return None # nocoverage -- no way to get here yet
for source in stripe_customer.sources.data:
if source.id == stripe_customer.default_source:
return source
raise AssertionError("Default source not in sources.")
@catch_stripe_errors @catch_stripe_errors
def do_create_customer_with_payment_source(user: UserProfile, stripe_token: str) -> stripe.Customer: def do_create_customer_with_payment_source(user: UserProfile, stripe_token: str) -> stripe.Customer:
realm = user.realm realm = user.realm

View File

@ -138,7 +138,31 @@
"account_balance": 0, "account_balance": 0,
"created": 1529990750, "created": 1529990750,
"currency": "usd", "currency": "usd",
"default_source": "card_1Ch9gVGh0CmXqmnwv94RombT", "default_source": {
"address_city": "Pacific",
"address_country": "United States",
"address_line1": "Under the sea",
"address_line1_check": "pass",
"address_line2": null,
"address_state": "FL",
"address_zip": "33333",
"address_zip_check": "pass",
"brand": "Visa",
"country": "US",
"customer": "cus_D7OT2jf5YAtZQL",
"cvc_check": "pass",
"dynamic_last4": null,
"exp_month": 3,
"exp_year": 2033,
"fingerprint": "6dAXT9VZvwro65EK",
"funding": "credit",
"id": "card_1Ch9gVGh0CmXqmnwv94RombT",
"last4": "4242",
"metadata": {},
"name": "Ada Starr",
"object": "card",
"tokenization_method": null
},
"delinquent": false, "delinquent": false,
"description": "zulip (Zulip Dev)", "description": "zulip (Zulip Dev)",
"discount": null, "discount": null,

View File

@ -27,8 +27,8 @@ from zerver.models import UserProfile, Realm
from zerver.views.push_notifications import validate_token from zerver.views.push_notifications import validate_token
from zilencer.lib.stripe import STRIPE_PUBLISHABLE_KEY, StripeError, \ from zilencer.lib.stripe import STRIPE_PUBLISHABLE_KEY, StripeError, \
do_create_customer_with_payment_source, do_subscribe_customer_to_plan, \ do_create_customer_with_payment_source, do_subscribe_customer_to_plan, \
get_stripe_customer, get_upcoming_invoice, payment_source, \ get_stripe_customer, get_upcoming_invoice, get_seat_count, \
get_seat_count, extract_current_subscription, sign_string, unsign_string extract_current_subscription, sign_string, unsign_string
from zilencer.models import RemotePushDeviceToken, RemoteZulipServer, \ from zilencer.models import RemotePushDeviceToken, RemoteZulipServer, \
Customer, Plan Customer, Plan
@ -249,9 +249,8 @@ def billing_home(request: HttpRequest) -> HttpResponse:
prorated_charges = 0 prorated_charges = 0
payment_method = None payment_method = None
source = payment_source(stripe_customer) if stripe_customer.default_source is not None:
if source is not None: payment_method = "Card ending in %(last4)s" % {'last4': stripe_customer.default_source.last4}
payment_method = "Card ending in %(last4)s" % {'last4': source.last4}
context.update({ context.update({
'plan_name': plan_name, 'plan_name': plan_name,