Fix issue : stripe user created every time for unauthenticated user

This commit is contained in:
M.Ravi 2017-09-28 14:46:56 +02:00
parent e110fd10f2
commit 07aaa20efe

View file

@ -379,14 +379,40 @@ class PaymentOrderView(FormView):
email=this_user.get('email'), email=this_user.get('email'),
token=token) token=token)
else: else:
user_email = form.cleaned_data.get('email')
user_name = form.cleaned_data.get('name')
this_user = { this_user = {
'email': form.cleaned_data.get('email'), 'email': user_email,
'name': form.cleaned_data.get('name') 'name': user_name
} }
customer = StripeCustomer.create_stripe_api_customer( try:
email=this_user.get('email'), custom_user = CustomUser.objects.get(email=user_email)
token=token, customer = StripeCustomer.objects.filter(
customer_name=form.cleaned_data.get('name')) user_id=custom_user.id).first()
if customer is None:
logger.debug(
("User {email} is already registered with us."
"But, StripeCustomer does not exist for {email}."
"Hence, creating a new StripeCustomer.").format(
email=user_email
)
)
customer = StripeCustomer.create_stripe_api_customer(
email=user_email,
token=token,
customer_name=user_name)
except CustomUser.DoesNotExist:
logger.debug(
("StripeCustomer does not exist for {email}."
"Hence, creating a new StripeCustomer.").format(
email=user_email
)
)
customer = StripeCustomer.create_stripe_api_customer(
email=user_email,
token=token,
customer_name=user_name)
request.session['billing_address_data'] = form.cleaned_data request.session['billing_address_data'] = form.cleaned_data
request.session['user'] = this_user request.session['user'] = this_user
# Get or create stripe customer # Get or create stripe customer
@ -499,7 +525,7 @@ class OrderConfirmationView(DetailView):
stripe_subscription_obj = subscription_result.get('response_object') stripe_subscription_obj = subscription_result.get('response_object')
# Check if the subscription was approved and is active # Check if the subscription was approved and is active
if stripe_subscription_obj is None or \ if stripe_subscription_obj is None or \
stripe_subscription_obj.status != 'active': stripe_subscription_obj.status != 'active':
msg = subscription_result.get('error') msg = subscription_result.get('error')
messages.add_message(self.request, messages.ERROR, msg, messages.add_message(self.request, messages.ERROR, msg,
extra_tags='failed_payment') extra_tags='failed_payment')