billing: Use UserProfile.id to ascertain customer's email.

Previously, our Stripe webhook event handler code retrieved the
user's email from Stripe using the stripe.Customer.email attribute.
This led to situations such that whenever the email that Stripe had
did not correspond to a UserProfile in Zulip, the payment flow
failed since we couldn't find a UserProfile associated with the
given email.

Now, we pass in the UserProfile.id in the metadata to Stripe's
checkout Session object, so that we can fetch the correct email
in future Stripe requests.
This commit is contained in:
Eeshan Garg 2021-12-02 18:23:56 -05:00 committed by Tim Abbott
parent c894f50f3c
commit 88fd399bec
481 changed files with 11 additions and 4 deletions

View File

@ -12,7 +12,7 @@ from corporate.lib.stripe import (
update_or_create_stripe_customer,
)
from corporate.models import Event, PaymentIntent, Session
from zerver.models import get_user_by_delivery_email
from zerver.models import get_active_user_profile_by_id_in_realm, get_user_by_delivery_email
billing_logger = logging.getLogger("corporate.stripe")
@ -70,9 +70,10 @@ def handle_checkout_session_completed_event(
session.save()
stripe_setup_intent = stripe.SetupIntent.retrieve(stripe_session.setup_intent)
stripe_customer = stripe.Customer.retrieve(stripe_setup_intent.customer)
assert session.customer.realm is not None
user = get_user_by_delivery_email(stripe_customer.email, session.customer.realm)
user_id = stripe_session.metadata.get("user_id")
assert user_id is not None
user = get_active_user_profile_by_id_in_realm(user_id, session.customer.realm)
payment_method = stripe_setup_intent.payment_method
if session.type in [

Some files were not shown because too many files have changed in this diff Show More