From 1baddbf0a48c4639f0b2292a764ad23cc15fa8b5 Mon Sep 17 00:00:00 2001 From: Fabian Krueger <fabian.krueger@fau.de> Date: Fri, 20 Sep 2019 09:35:26 +0200 Subject: [PATCH] update backend: now supports a charge function --- store/backend.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/store/backend.py b/store/backend.py index c3682d1..5d4c9d5 100644 --- a/store/backend.py +++ b/store/backend.py @@ -226,3 +226,30 @@ class ChargeLogic: return charges + """ + Executes the charge logic. + @param user_id: id of the user that charges + @param amount: the amount of money to be charged + @param token: the unique token got by getToken() + """ + @staticmethod + def charge(user_id, amount, token): + try: + with transaction.atomic(): + user = list(User.objects.filter(id=user_id))[0] + charge_id = ChargeLogic.__createChargeTuple(user, amount, token) + ChargeLogic.__updateUserMoney(user, amount) + except IntegrityError: + charge = list(Charge.objects.filter(token=token).values('id'))[0] + return charge['id'] + return charge_id + + @staticmethod + def __createChargeTuple(user, amount, token): + charge = Charge(token=token, amount=amount, annullated=False, user_id=user.id) + charge.save() + return charge.id + + @staticmethod + def __updateUserMoney(user, amount): + user.updateMoney(amount) -- GitLab