Fix: obtaining stripe_customer in landing flow
This commit is contained in:
parent
63eb7fc0e2
commit
af1690b846
1 changed files with 46 additions and 36 deletions
|
@ -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,38 +608,49 @@ 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)
|
||||||
if not ucd:
|
except StripeCustomer.DoesNotExist:
|
||||||
acc_result = stripe_utils.associate_customer_card(
|
pass
|
||||||
stripe_api_cus_id, request.session['token'],
|
if s_cus:
|
||||||
set_as_default=True
|
ucd = UserCardDetail.get_user_card_details(s_cus, card_details_response)
|
||||||
)
|
if not ucd:
|
||||||
if acc_result['response_object'] is None:
|
acc_result = stripe_utils.associate_customer_card(
|
||||||
msg = acc_result.get('error')
|
stripe_api_cus_id, request.session['token'],
|
||||||
messages.add_message(
|
set_as_default=True
|
||||||
self.request, messages.ERROR, msg,
|
|
||||||
extra_tags='failed_payment'
|
|
||||||
)
|
)
|
||||||
response = {
|
if acc_result['response_object'] is None:
|
||||||
'status': False,
|
msg = acc_result.get('error')
|
||||||
'redirect': "{url}#{section}".format(
|
messages.add_message(
|
||||||
url=reverse('datacenterlight:payment'),
|
self.request, messages.ERROR, msg,
|
||||||
section='payment_error'),
|
extra_tags='failed_payment'
|
||||||
'msg_title': str(_('Error.')),
|
)
|
||||||
'msg_body': str(
|
response = {
|
||||||
_('There was a payment related error.'
|
'status': False,
|
||||||
' On close of this popup, you will be redirected back to'
|
'redirect': "{url}#{section}".format(
|
||||||
' the payment page.'))
|
url=reverse('datacenterlight:payment'),
|
||||||
}
|
section='payment_error'),
|
||||||
logger.error(
|
'msg_title': str(_('Error.')),
|
||||||
"Card association failed. Error {error}".format(
|
'msg_body': str(
|
||||||
error=acc_result['error']
|
_('There was a payment related error.'
|
||||||
|
' On close of this popup, you will be '
|
||||||
|
'redirected back to the payment page.')
|
||||||
|
)
|
||||||
|
}
|
||||||
|
logger.error(
|
||||||
|
"Card association failed. Error {error}".format(
|
||||||
|
error=acc_result['error']
|
||||||
|
)
|
||||||
|
)
|
||||||
|
return HttpResponse(json.dumps(response),
|
||||||
|
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
|
||||||
)
|
)
|
||||||
)
|
|
||||||
return HttpResponse(json.dumps(response),
|
|
||||||
content_type="application/json")
|
|
||||||
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 = {
|
||||||
|
|
Loading…
Reference in a new issue