mirror of
https://github.com/Steffo99/greed.git
synced 2024-11-25 15:24:17 +00:00
Change the database value type from Decimal to int
This commit is contained in:
parent
ad38019afb
commit
69862d62a7
4 changed files with 21 additions and 20 deletions
|
@ -3,7 +3,7 @@
|
||||||
# Config file parameters
|
# Config file parameters
|
||||||
[Config]
|
[Config]
|
||||||
; Config file version. DO NOT EDIT THIS!
|
; Config file version. DO NOT EDIT THIS!
|
||||||
version = 9
|
version = 10
|
||||||
; Set this to no when you are done editing the file
|
; Set this to no when you are done editing the file
|
||||||
is_template = yes
|
is_template = yes
|
||||||
|
|
||||||
|
@ -33,10 +33,10 @@ currency_exp = 2
|
||||||
[Credit Card]
|
[Credit Card]
|
||||||
# Provider token: get the token at @BotFather
|
# Provider token: get the token at @BotFather
|
||||||
credit_card_token = 123456789:YOUR_TOKEN_HERE_
|
credit_card_token = 123456789:YOUR_TOKEN_HERE_
|
||||||
# Minimum wallet payment accepted
|
# Minimum wallet payment accepted (in miniumum currency units, $1.00 = 100 units)
|
||||||
min_amount = 10
|
min_amount = 1000
|
||||||
# Maximum wallet payment accepted
|
# Maximum wallet payment accepted (in miniumum currency units, $1.00 = 100 units)
|
||||||
max_amount = 100
|
max_amount = 10000
|
||||||
# Make the user pay a fee when loading the wallet with a credit card
|
# Make the user pay a fee when loading the wallet with a credit card
|
||||||
# Credit card fee percentage (set to 0.0 to disable)
|
# Credit card fee percentage (set to 0.0 to disable)
|
||||||
fee_percentage = 2.9
|
fee_percentage = 2.9
|
||||||
|
|
|
@ -4,7 +4,6 @@ from sqlalchemy.orm import sessionmaker, relationship
|
||||||
from sqlalchemy.ext.declarative import declarative_base
|
from sqlalchemy.ext.declarative import declarative_base
|
||||||
import configloader
|
import configloader
|
||||||
import telegram
|
import telegram
|
||||||
import decimal
|
|
||||||
import strings
|
import strings
|
||||||
from html import escape
|
from html import escape
|
||||||
|
|
||||||
|
|
|
@ -66,7 +66,7 @@ payment_invoice_description = "Pagando questa ricevuta verranno aggiunti {amount
|
||||||
payment_invoice_label = "Ricarica"
|
payment_invoice_label = "Ricarica"
|
||||||
|
|
||||||
# Payment: label of the labeled price on the invoice
|
# 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
|
# Info: informazioni sul bot
|
||||||
bot_info = 'Questo bot utilizza <a href="https://github.com/Steffo99/greed">greed</a>,' \
|
bot_info = 'Questo bot utilizza <a href="https://github.com/Steffo99/greed">greed</a>,' \
|
||||||
|
|
26
worker.py
26
worker.py
|
@ -1,7 +1,6 @@
|
||||||
import threading
|
import threading
|
||||||
import typing
|
import typing
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
import telegram
|
import telegram
|
||||||
import strings
|
import strings
|
||||||
import configloader
|
import configloader
|
||||||
|
@ -9,7 +8,6 @@ import sys
|
||||||
import queue as queuem
|
import queue as queuem
|
||||||
import database as db
|
import database as db
|
||||||
import re
|
import re
|
||||||
from decimal import Decimal
|
|
||||||
|
|
||||||
class StopSignal:
|
class StopSignal:
|
||||||
"""A data class that should be sent to the worker when the conversation has to be stopped abnormally."""
|
"""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))
|
reply_markup=telegram.ReplyKeyboardMarkup(keyboard, one_time_keyboard=True))
|
||||||
# Wait until a valid amount is sent
|
# Wait until a valid amount is sent
|
||||||
# TODO: check and debug the regex
|
# 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
|
# 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"])))
|
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
|
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"])))
|
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
|
continue
|
||||||
break
|
break
|
||||||
# Set the invoice active invoice payload
|
# Set the invoice active invoice payload
|
||||||
self.invoice_payload = str(uuid.uuid4())
|
self.invoice_payload = str(uuid.uuid4())
|
||||||
# Create the price array
|
# 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
|
# 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_percentage = float(configloader.config["Credit Card"]["fee_percentage"]) / 100
|
||||||
fee_fixed = Decimal(configloader.config["Credit Card"]["fee_fixed"])
|
fee_fixed = int(configloader.config["Credit Card"]["fee_fixed"])
|
||||||
total_fee = (selection * fee_percentage + fee_fixed).quantize(Decimal("1.00"))
|
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)))
|
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
|
# The amount is valid, send the invoice
|
||||||
self.bot.send_invoice(self.chat.id,
|
self.bot.send_invoice(self.chat.id,
|
||||||
title=strings.payment_invoice_title,
|
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,
|
payload=self.invoice_payload,
|
||||||
provider_token=configloader.config["Credit Card"]["credit_card_token"],
|
provider_token=configloader.config["Credit Card"]["credit_card_token"],
|
||||||
start_parameter="tempdeeplink", # TODO: no idea on how deeplinks should work
|
start_parameter="tempdeeplink", # TODO: no idea on how deeplinks should work
|
||||||
|
@ -273,7 +275,7 @@ class ChatWorker(threading.Thread):
|
||||||
successfulpayment = self.__wait_for_successfulpayment()
|
successfulpayment = self.__wait_for_successfulpayment()
|
||||||
# Create a new database transaction
|
# Create a new database transaction
|
||||||
transaction = db.Transaction(user=self.user,
|
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",
|
provider="Credit Card",
|
||||||
telegram_charge_id=successfulpayment.telegram_payment_charge_id,
|
telegram_charge_id=successfulpayment.telegram_payment_charge_id,
|
||||||
provider_charge_id=successfulpayment.provider_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_email = successfulpayment.order_info.email
|
||||||
transaction.payment_phone = successfulpayment.order_info.phone_number
|
transaction.payment_phone = successfulpayment.order_info.phone_number
|
||||||
# Add the credit to the user account
|
# 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
|
# Add and commit the transaction
|
||||||
self.session.add(transaction)
|
self.session.add(transaction)
|
||||||
self.session.commit()
|
self.session.commit()
|
||||||
|
|
Loading…
Reference in a new issue