def add(cls, user: User, new_payment): """ Adds a new payment to the given user. :param user: User object :param new_payment: The new payment to be added to the user :return: A brand new payment """ new_payment["status"] = "PENDIENTE" etomin_number = "" for card in user.cards: if card.json()["_id"] == new_payment["id_card"]: etomin_number = user.cards[user.cards.index(card)].token payment = cls(**new_payment) params = { "public_key": os.environ.get("ETOMIN_PB_KEY"), "transaction": { "payer": { "email": user.email }, "order": { "external_reference": payment._id }, "payment": { "amount": new_payment.get("amount"), "payment_method": new_payment.get("payment_method"), "currency": CURRENCY, "payment_country": PAYMENT_COUNTRY, "token": etomin_number }, "payment_pending": False, "device_session_id": "" }, "test": True } auth = requests.get(URL) if auth.status_code == 200: obj = json.loads(auth.text) params["transaction"]["device_session_id"] = obj["session_id"] charge = requests.post(URL_CHARGE, params={}, data=json.dumps(params), headers=HEADERS) obj_charge = json.loads(charge.text) if obj_charge.get("error") == '0': payment.status = "APROBADO" payment.id_reference = obj_charge.get("authorization_code") payment.etomin_number = obj_charge.get("card_token") new_payment["status"] = "APROBADO" user.change_user_balance(-new_payment.get("amount")) user.payments.append(payment) user.update_mongo() return new_payment else: payment.status = "RECHAZADO" payment.etomin_number = etomin_number user.payments.append(payment) user.update_mongo() raise PaymentFailedException(obj_charge.get("message"))
def put(self): """ Updates the balance to the user given a new balance :return: Confirmation message """ try: data = ChangeUserBalance.parser.parse_args() balance_to_change = data['balance'] user_id = get_jwt_identity() user = UserModel.get_by_id(user_id) updated_user = UserModel.change_user_balance( user, balance_to_change) return Response( success=True, message="Balance del usuario exitosamente actualizado".format( updated_user.balance)).json(), 200 except UserException as e: return Response(message=e.message).json(), 400