From d081cf7b78c2face11dcf287b1fc487bbdb3d951 Mon Sep 17 00:00:00 2001 From: Rishi Gupta Date: Wed, 22 Aug 2018 18:40:38 -0700 Subject: [PATCH] billing: Add transaction.atomic() for Customer creation and logging. --- zilencer/lib/stripe.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/zilencer/lib/stripe.py b/zilencer/lib/stripe.py index bfdbe423f7..58b902c281 100644 --- a/zilencer/lib/stripe.py +++ b/zilencer/lib/stripe.py @@ -146,16 +146,17 @@ def do_create_customer_with_payment_source(user: UserProfile, stripe_token: str) if PRINT_STRIPE_FIXTURE_DATA: print(''.join(['"create_customer": ', str(stripe_customer), ','])) # nocoverage event_time = timestamp_to_datetime(stripe_customer.created) - RealmAuditLog.objects.create( - realm=user.realm, acting_user=user, event_type=RealmAuditLog.STRIPE_CUSTOMER_CREATED, - event_time=event_time) - RealmAuditLog.objects.create( - realm=user.realm, acting_user=user, event_type=RealmAuditLog.STRIPE_CARD_ADDED, - event_time=event_time) - Customer.objects.create( - realm=realm, - stripe_customer_id=stripe_customer.id, - billing_user=user) + with transaction.atomic(): + RealmAuditLog.objects.create( + realm=user.realm, acting_user=user, event_type=RealmAuditLog.STRIPE_CUSTOMER_CREATED, + event_time=event_time) + RealmAuditLog.objects.create( + realm=user.realm, acting_user=user, event_type=RealmAuditLog.STRIPE_CARD_ADDED, + event_time=event_time) + Customer.objects.create( + realm=realm, + stripe_customer_id=stripe_customer.id, + billing_user=user) return stripe_customer @catch_stripe_errors