From 7a1284fd4588840f7101657e34492dcb879c9de1 Mon Sep 17 00:00:00 2001 From: Thomas Svendal Date: Mon, 18 Sep 2023 21:46:28 +0200 Subject: [PATCH] VIPPS release fix (#702) Co-authored-by: Lea Raknes Co-authored-by: Mads Nylund Co-authored-by: Mads Nylund <73914541+MadsNyl@users.noreply.github.com> Co-authored-by: Thomas Svendal Co-authored-by: Martin Clementz Fixed flake8 (#672) fixed typo (#691) --- app/content/views/registration.py | 6 ++- app/payment/util/payment_utils.py | 8 +++- app/payment/views/vipps_callback.py | 64 ++++++++++++----------------- app/settings.py | 2 - app/util/exceptions.py | 2 +- 5 files changed, 40 insertions(+), 42 deletions(-) diff --git a/app/content/views/registration.py b/app/content/views/registration.py index 36ccef0ee..d899e2031 100644 --- a/app/content/views/registration.py +++ b/app/content/views/registration.py @@ -65,7 +65,11 @@ def create(self, request, *args, **kwargs): serializer, event=event, user=request.user ) - create_payment_order(event, request, registration) + try: + create_payment_order(event, request, registration) + except Exception as e: + registration.delete() + raise e registration_serializer = RegistrationSerializer( registration, context={"user": registration.user} diff --git a/app/payment/util/payment_utils.py b/app/payment/util/payment_utils.py index 03b82ffc2..96bd5ce1f 100644 --- a/app/payment/util/payment_utils.py +++ b/app/payment/util/payment_utils.py @@ -17,7 +17,13 @@ def get_new_access_token(): "Merchant-Serial-Number": settings.VIPPS_MERCHANT_SERIAL_NUMBER, } - response = requests.post(TOKEN_URL, headers=TOKEN_HEADERS).json() + response = requests.post(TOKEN_URL, headers=TOKEN_HEADERS) + + if response.status_code != 200: + raise Exception("Could not get access token") + + response = response.json() + return (response["expires_on"], response["access_token"]) diff --git a/app/payment/views/vipps_callback.py b/app/payment/views/vipps_callback.py index bdfc45ff4..63b95c607 100644 --- a/app/payment/views/vipps_callback.py +++ b/app/payment/views/vipps_callback.py @@ -2,49 +2,39 @@ import requests -from app.payment.exceptions import ( - VippsCallbackInternalServerException, - VippsForcePaymentException, -) from app.payment.models.order import Order from app.payment.util.payment_utils import get_new_access_token def vipps_callback(_request, order_id): - try: - access_token = get_new_access_token()[1] - url = f"{settings.VIPPS_ORDER_URL}{order_id}/details" - headers = { - "Content-Type": "application/json", - "Ocp-Apim-Subscription-Key": settings.VIPPS_SUBSCRIPTION_KEY, - "Authorization": "Bearer " + access_token, - "Merchant-Serial-Number": settings.VIPPS_MERCHANT_SERIAL_NUMBER, - } - res = requests.get(url, headers=headers) - json = res.json() - status = json["transactionLogHistory"][0]["operation"] - order = Order.objects.get(order_id=order_id) - order.status = status - order.save() - return status - except Exception: - raise VippsCallbackInternalServerException() + access_token = get_new_access_token()[1] + url = f"{settings.VIPPS_ORDER_URL}{order_id}/details" + headers = { + "Content-Type": "application/json", + "Ocp-Apim-Subscription-Key": settings.VIPPS_SUBSCRIPTION_KEY, + "Authorization": "Bearer " + access_token, + "Merchant-Serial-Number": settings.VIPPS_MERCHANT_SERIAL_NUMBER, + } + res = requests.get(url, headers=headers) + json = res.json() + status = json["transactionLogHistory"][0]["operation"] + order = Order.objects.get(order_id=order_id) + order.status = status + order.save() + return status def force_payment(order_id): - try: - access_token = get_new_access_token()[1] - url = f"{settings.VIPPS_FORCE_PAYMENT_URL}{order_id}/approve" - headers = { - "Content-Type": "application/json", - "Ocp-Apim-Subscription-Key": settings.VIPPS_SUBSCRIPTION_KEY, - "Authorization": "Bearer " + access_token, - "Merchant-Serial-Number": settings.VIPPS_MERCHANT_SERIAL_NUMBER, - } + access_token = get_new_access_token()[1] + url = f"{settings.VIPPS_FORCE_PAYMENT_URL}{order_id}/approve" + headers = { + "Content-Type": "application/json", + "Ocp-Apim-Subscription-Key": settings.VIPPS_SUBSCRIPTION_KEY, + "Authorization": "Bearer " + access_token, + "Merchant-Serial-Number": settings.VIPPS_MERCHANT_SERIAL_NUMBER, + } - res = requests.post(url, headers=headers) - status_code = res.status_code - json = res.json() - return (json, status_code) - except Exception: - raise VippsForcePaymentException() + res = requests.post(url, headers=headers) + status_code = res.status_code + json = res.json() + return (json, status_code) diff --git a/app/settings.py b/app/settings.py index 409e87772..a6b0176ac 100644 --- a/app/settings.py +++ b/app/settings.py @@ -285,5 +285,3 @@ CELERY_BROKER_URL = "amqp://guest:guest@rabbitmq:5672" if ENVIRONMENT == EnvironmentOptions.LOCAL: CELERY_TASK_ALWAYS_EAGER = False - - \ No newline at end of file diff --git a/app/util/exceptions.py b/app/util/exceptions.py index 57d8d2ca4..e4e08f664 100644 --- a/app/util/exceptions.py +++ b/app/util/exceptions.py @@ -22,7 +22,7 @@ def exception_handler(exc, context): if response: log_api_error(response, exc) else: - logger.error(f"Unhandled request exception: {traceback(exc)}") + logger.error(traceback.format_exc()) if not settings.DEBUG and not response: response = Response(