Skip to content

Commit

Permalink
VIPPS release fix (#702)
Browse files Browse the repository at this point in the history
Co-authored-by: Lea Raknes <[email protected]>
Co-authored-by: Mads Nylund <[email protected]>
Co-authored-by: Mads Nylund <[email protected]>
Co-authored-by: Thomas Svendal <[email protected]>
Co-authored-by: Martin Clementz <[email protected]>
Fixed flake8 (#672)
fixed typo (#691)
  • Loading branch information
thomsen85 authored Sep 18, 2023
1 parent 6a6df4f commit 7a1284f
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 42 deletions.
6 changes: 5 additions & 1 deletion app/content/views/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
8 changes: 7 additions & 1 deletion app/payment/util/payment_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])


Expand Down
64 changes: 27 additions & 37 deletions app/payment/views/vipps_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 0 additions & 2 deletions app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,5 +285,3 @@
CELERY_BROKER_URL = "amqp://guest:guest@rabbitmq:5672"
if ENVIRONMENT == EnvironmentOptions.LOCAL:
CELERY_TASK_ALWAYS_EAGER = False


2 changes: 1 addition & 1 deletion app/util/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 7a1284f

Please sign in to comment.