Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

Commit

Permalink
feat: Make mobile IAP execute/ API atomic (#3995)
Browse files Browse the repository at this point in the history
  • Loading branch information
moeez96 authored Jul 5, 2023
1 parent a4298d2 commit 356f2fc
Showing 1 changed file with 22 additions and 26 deletions.
48 changes: 22 additions & 26 deletions ecommerce/extensions/iap/api/v1/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,36 +227,32 @@ def post(self, request):
logger.exception(LOGGER_EXECUTE_ALREADY_PURCHASED, request.user.username, basket_id)
return JsonResponse({'error': _(ERROR_ALREADY_PURCHASED)}, status=406)

with transaction.atomic():
try:
self.handle_payment(receipt, basket)
except GatewayError as exception:
logger.exception(LOGGER_EXECUTE_GATEWAY_ERROR, request.user.username, basket_id, str(exception))
return JsonResponse({'error': ERROR_DURING_PAYMENT_HANDLING}, status=400)
except KeyError as exception:
logger.exception(LOGGER_PAYMENT_FAILED_FOR_BASKET, basket_id, str(exception))
return JsonResponse({'error': ERROR_DURING_PAYMENT_HANDLING}, status=400)
except RedundantPaymentNotificationError:
logger.exception(LOGGER_EXECUTE_REDUNDANT_PAYMENT, request.user.username, basket_id)
return JsonResponse({'error': COURSE_ALREADY_PAID_ON_DEVICE}, status=409)
except PaymentError as exception:
logger.exception(LOGGER_EXECUTE_PAYMENT_ERROR, request.user.username, basket_id, str(exception))
return JsonResponse({'error': ERROR_DURING_PAYMENT_HANDLING}, status=400)

try:
order = self.create_order(request, basket)
except Exception as exception: # pylint: disable=broad-except
logger.exception(LOGGER_EXECUTE_ORDER_CREATION_FAILED, request.user.username, basket_id, str(exception))
return JsonResponse({'error': ERROR_DURING_ORDER_CREATION}, status=400)

try:
self.handle_post_order(order)
with transaction.atomic():
self.handle_payment(receipt, basket)
order = self.create_order(request, basket)
self.handle_post_order(order)
logger.info(LOGGER_EXECUTE_SUCCESSFUL, request.user.username, basket_id, self.payment_processor.NAME)
response = {'order_data': MobileOrderSerializer(order, context={'request': request}).data}
return JsonResponse(response, status=200)
except GatewayError as exception:
logger.exception(LOGGER_EXECUTE_GATEWAY_ERROR, request.user.username, basket_id, str(exception))
return JsonResponse({'error': ERROR_DURING_PAYMENT_HANDLING}, status=400)
except KeyError as exception:
logger.exception(LOGGER_PAYMENT_FAILED_FOR_BASKET, basket_id, str(exception))
return JsonResponse({'error': ERROR_DURING_PAYMENT_HANDLING}, status=400)
except RedundantPaymentNotificationError:
logger.exception(LOGGER_EXECUTE_REDUNDANT_PAYMENT, request.user.username, basket_id)
return JsonResponse({'error': COURSE_ALREADY_PAID_ON_DEVICE}, status=409)
except PaymentError as exception:
logger.exception(LOGGER_EXECUTE_PAYMENT_ERROR, request.user.username, basket_id, str(exception))
return JsonResponse({'error': ERROR_DURING_PAYMENT_HANDLING}, status=400)
except AttributeError:
self.log_order_placement_exception(basket.order_number, basket.id)
return JsonResponse({'error': ERROR_DURING_POST_ORDER_OP}, status=200)

logger.info(LOGGER_EXECUTE_SUCCESSFUL, request.user.username, basket_id, self.payment_processor.NAME)
return JsonResponse({'order_data': MobileOrderSerializer(order, context={'request': request}).data}, status=200)
except Exception as exception: # pylint: disable=broad-except
logger.exception(LOGGER_EXECUTE_ORDER_CREATION_FAILED, request.user.username, basket_id, str(exception))
return JsonResponse({'error': ERROR_DURING_ORDER_CREATION}, status=400)


class BaseRefund(APIView):
Expand Down

0 comments on commit 356f2fc

Please sign in to comment.