Fix: obtaining stripe_customer in landing flow

This commit is contained in:
PCoder 2017-10-29 21:36:12 +01:00
parent 63eb7fc0e2
commit af1690b846

View file

@ -403,8 +403,7 @@ class PaymentOrderView(FormView):
return HttpResponseRedirect(reverse('datacenterlight:index')) return HttpResponseRedirect(reverse('datacenterlight:index'))
HostingUtils.clear_items_from_list( HostingUtils.clear_items_from_list(
request.session, request.session,
['token', 'billing_address_data', 'card_id', 'customer', ['token', 'card_id', 'customer', 'user']
'user']
) )
if 'token' in request.session: if 'token' in request.session:
del request.session['token'] del request.session['token']
@ -609,9 +608,13 @@ class OrderConfirmationView(DetailView):
'brand': card_details_response['brand'], 'brand': card_details_response['brand'],
'card_id': card_details_response['card_id'] 'card_id': card_details_response['card_id']
} }
ucd = UserCardDetail.contains( s_cus = None
request.user.stripecustomer, card_details_response try:
) s_cus = StripeCustomer.objects.get(stripe_id=stripe_api_cus_id)
except StripeCustomer.DoesNotExist:
pass
if s_cus:
ucd = UserCardDetail.get_user_card_details(s_cus, card_details_response)
if not ucd: if not ucd:
acc_result = stripe_utils.associate_customer_card( acc_result = stripe_utils.associate_customer_card(
stripe_api_cus_id, request.session['token'], stripe_api_cus_id, request.session['token'],
@ -631,8 +634,9 @@ class OrderConfirmationView(DetailView):
'msg_title': str(_('Error.')), 'msg_title': str(_('Error.')),
'msg_body': str( 'msg_body': str(
_('There was a payment related error.' _('There was a payment related error.'
' On close of this popup, you will be redirected back to' ' On close of this popup, you will be '
' the payment page.')) 'redirected back to the payment page.')
)
} }
logger.error( logger.error(
"Card association failed. Error {error}".format( "Card association failed. Error {error}".format(
@ -641,6 +645,12 @@ class OrderConfirmationView(DetailView):
) )
return HttpResponse(json.dumps(response), return HttpResponse(json.dumps(response),
content_type="application/json") content_type="application/json")
else:
if not ucd.preferred:
UserCardDetail.set_default_card(
stripe_api_cus_id=stripe_api_cus_id,
stripe_source_id=ucd.card_id
)
else: else:
card_id = request.session.get('card_id') card_id = request.session.get('card_id')
user_card_detail = UserCardDetail.objects.get(id=card_id) user_card_detail = UserCardDetail.objects.get(id=card_id)
@ -745,13 +755,13 @@ class OrderConfirmationView(DetailView):
billing_address_data.update({ billing_address_data.update({
'user': custom_user.id 'user': custom_user.id
}) })
if 'token' in request.session: if 'token' in request.session and s_cus is not None:
ucd = UserCardDetail.get_or_create_user_card_detail( ucd = UserCardDetail.get_or_create_user_card_detail(
stripe_customer=self.request.user.stripecustomer, stripe_customer=s_cus,
card_details=card_details_dict card_details=card_details_response
) )
UserCardDetail.save_default_card_local( UserCardDetail.save_default_card_local(
self.request.user.stripecustomer.stripe_id, s_cus.stripe_id,
ucd.card_id ucd.card_id
) )
user = { user = {