From 69862d62a75f54b82164502bd4a097409c34902c Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Mon, 29 Jan 2018 11:46:01 +0100 Subject: [PATCH] Change the database value type from Decimal to int --- config/template_config.ini | 10 +++++----- database.py | 1 - strings.py | 2 +- worker.py | 28 +++++++++++++++------------- 4 files changed, 21 insertions(+), 20 deletions(-) diff --git a/config/template_config.ini b/config/template_config.ini index 1b63567..5480e64 100644 --- a/config/template_config.ini +++ b/config/template_config.ini @@ -3,7 +3,7 @@ # Config file parameters [Config] ; Config file version. DO NOT EDIT THIS! -version = 9 +version = 10 ; Set this to no when you are done editing the file is_template = yes @@ -33,10 +33,10 @@ currency_exp = 2 [Credit Card] # Provider token: get the token at @BotFather credit_card_token = 123456789:YOUR_TOKEN_HERE_ -# Minimum wallet payment accepted -min_amount = 10 -# Maximum wallet payment accepted -max_amount = 100 +# Minimum wallet payment accepted (in miniumum currency units, $1.00 = 100 units) +min_amount = 1000 +# Maximum wallet payment accepted (in miniumum currency units, $1.00 = 100 units) +max_amount = 10000 # Make the user pay a fee when loading the wallet with a credit card # Credit card fee percentage (set to 0.0 to disable) fee_percentage = 2.9 diff --git a/database.py b/database.py index 24a33c3..afe1fb8 100644 --- a/database.py +++ b/database.py @@ -4,7 +4,6 @@ from sqlalchemy.orm import sessionmaker, relationship from sqlalchemy.ext.declarative import declarative_base import configloader import telegram -import decimal import strings from html import escape diff --git a/strings.py b/strings.py index 5cef9d0..aca8261 100644 --- a/strings.py +++ b/strings.py @@ -66,7 +66,7 @@ payment_invoice_description = "Pagando questa ricevuta verranno aggiunti {amount payment_invoice_label = "Ricarica" # Payment: label of the labeled price on the invoice -payment_invoice_fee_label = "Carta di credito" +payment_invoice_fee_label = "Supplemento carta" # Info: informazioni sul bot bot_info = 'Questo bot utilizza greed,' \ diff --git a/worker.py b/worker.py index 58644a2..804954b 100644 --- a/worker.py +++ b/worker.py @@ -1,7 +1,6 @@ import threading import typing import uuid - import telegram import strings import configloader @@ -9,7 +8,6 @@ import sys import queue as queuem import database as db import re -from decimal import Decimal class StopSignal: """A data class that should be sent to the worker when the conversation has to be stopped abnormally.""" @@ -234,28 +232,32 @@ class ChatWorker(threading.Thread): reply_markup=telegram.ReplyKeyboardMarkup(keyboard, one_time_keyboard=True)) # Wait until a valid amount is sent # TODO: check and debug the regex - selection = Decimal(self.__wait_for_regex(r"([0-9]{1,3}(?:[.,][0-9]{1,2})?)").replace(",", ".")) + selection = int(self.__wait_for_regex(r"([0-9]{1,3}(?:[.,][0-9]{1,2})?)").replace(".", "").replace(",", "")) * (10 ** int(configloader.config["Payments"]["currency_exp"])) # Ensure the amount is within the range - if selection > Decimal(configloader.config["Payments"]["max_amount"]): + if selection > int(configloader.config["Payments"]["max_amount"]): self.bot.send_message(self.chat.id, strings.error_payment_amount_over_max.format(max_amount=strings.currency_format_string.format(symbol=strings.currency_symbol, value=configloader.config["Payments"]["max_amount"]))) continue - elif selection < Decimal(configloader.config["Payments"]["min_amount"]): + elif selection < int(configloader.config["Payments"]["min_amount"]): self.bot.send_message(self.chat.id, strings.error_payment_amount_under_min.format(min_amount=strings.currency_format_string.format(symbol=strings.currency_symbol, value=configloader.config["Payments"]["min_amount"]))) continue break # Set the invoice active invoice payload self.invoice_payload = str(uuid.uuid4()) # Create the price array - prices = [telegram.LabeledPrice(label=strings.payment_invoice_label, amount=int(selection * (10 ** int(configloader.config["Payments"]["currency_exp"]))))] + prices = [telegram.LabeledPrice(label=strings.payment_invoice_label, amount=selection)] # If the user has to pay a fee when using the credit card, add it to the prices list - fee_percentage = Decimal(configloader.config["Credit Card"]["fee_percentage"]) - fee_fixed = Decimal(configloader.config["Credit Card"]["fee_fixed"]) - total_fee = (selection * fee_percentage + fee_fixed).quantize(Decimal("1.00")) - prices.append(telegram.LabeledPrice(label=strings.payment_invoice_fee_label, amount=int(total_fee))) + fee_percentage = float(configloader.config["Credit Card"]["fee_percentage"]) / 100 + fee_fixed = int(configloader.config["Credit Card"]["fee_fixed"]) + total_fee = int(selection * fee_percentage) + fee_fixed + if total_fee > 0: + prices.append(telegram.LabeledPrice(label=strings.payment_invoice_fee_label, amount=int(total_fee))) + else: + # Otherwise, set the fee to 0 to ensure no accidental discounts are applied + total_fee = 0 # The amount is valid, send the invoice self.bot.send_invoice(self.chat.id, title=strings.payment_invoice_title, - description=strings.payment_invoice_description.format(amount=strings.currency_format_string.format(symbol=strings.currency_symbol, value=selection)), + description=strings.payment_invoice_description.format(amount=strings.currency_format_string.format(symbol=strings.currency_symbol, value=selection / (10 ** int(configloader.config["Payments"]["currency_exp"])))), payload=self.invoice_payload, provider_token=configloader.config["Credit Card"]["credit_card_token"], start_parameter="tempdeeplink", # TODO: no idea on how deeplinks should work @@ -273,7 +275,7 @@ class ChatWorker(threading.Thread): successfulpayment = self.__wait_for_successfulpayment() # Create a new database transaction transaction = db.Transaction(user=self.user, - value=successfulpayment.total_amount - int(total_fee * (10 ** int(configloader.config["Payments"]["currency_exp"]))), + value=successfulpayment.total_amount - int(total_fee), provider="Credit Card", telegram_charge_id=successfulpayment.telegram_payment_charge_id, provider_charge_id=successfulpayment.provider_payment_charge_id) @@ -282,7 +284,7 @@ class ChatWorker(threading.Thread): transaction.payment_email = successfulpayment.order_info.email transaction.payment_phone = successfulpayment.order_info.phone_number # Add the credit to the user account - self.user.credit += (successfulpayment.total_amount - int(total_fee * (10 ** int(configloader.config["Payments"]["currency_exp"])))) + self.user.credit += successfulpayment.total_amount - total_fee # Add and commit the transaction self.session.add(transaction) self.session.commit()