diff --git a/store/backend.py b/store/backend.py index c3682d197be2f815076a4d3c0f3f170967339639..5d4c9d5186bc9288ec534c965c9c5f6093ff0dc6 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)