From ad38019afb1e78f121da038bf79418889fe39e75 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Mon, 15 Jan 2018 10:16:04 +0100 Subject: [PATCH] Add support for credit card user fees --- config/template_config.ini | 15 ++++++++++----- database.py | 2 +- strings.py | 3 +++ worker.py | 13 ++++++++++--- 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/config/template_config.ini b/config/template_config.ini index 5f2b4b6..1b63567 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 = 8 +version = 9 ; Set this to no when you are done editing the file is_template = yes @@ -37,9 +37,14 @@ credit_card_token = 123456789:YOUR_TOKEN_HERE_ min_amount = 10 # Maximum wallet payment accepted max_amount = 100 -# Require the name of the credit card owner +# 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 +# Credit card fee fixed (in miniumum currency units, $1.00 = 100 units) (set to 0 to disable) +fee_fixed = 30 +# Require the name of the user name_required = yes -# Require the email of the credit card owner +# Require the email of the user email_required = yes -# Require the phone number of the credit card owner -phone_required = yes +# Require the phone number of the user +phone_required = yes \ No newline at end of file diff --git a/database.py b/database.py index ed415b7..24a33c3 100644 --- a/database.py +++ b/database.py @@ -42,7 +42,7 @@ class User(TableDeclarativeBase): self.last_name = telegram_chat.last_name self.username = telegram_chat.username # The starting wallet value is 0 - self.credit = decimal.Decimal("0") + self.credit = 0 def __str__(self): """Describe the user in the best way possible given the available data.""" diff --git a/strings.py b/strings.py index e1d1f18..5cef9d0 100644 --- a/strings.py +++ b/strings.py @@ -65,6 +65,9 @@ payment_invoice_description = "Pagando questa ricevuta verranno aggiunti {amount # Payment: label of the labeled price on the invoice payment_invoice_label = "Ricarica" +# Payment: label of the labeled price on the invoice +payment_invoice_fee_label = "Carta di credito" + # Info: informazioni sul bot bot_info = 'Questo bot utilizza greed,' \ ' un framework di @Steffo per i pagamenti su Telegram rilasciato sotto la' \ diff --git a/worker.py b/worker.py index a512d37..58644a2 100644 --- a/worker.py +++ b/worker.py @@ -245,6 +245,13 @@ class ChatWorker(threading.Thread): 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"]))))] + # 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))) # The amount is valid, send the invoice self.bot.send_invoice(self.chat.id, title=strings.payment_invoice_title, @@ -253,7 +260,7 @@ class ChatWorker(threading.Thread): provider_token=configloader.config["Credit Card"]["credit_card_token"], start_parameter="tempdeeplink", # TODO: no idea on how deeplinks should work currency=configloader.config["Payments"]["currency"], - prices=[telegram.LabeledPrice(label=strings.payment_invoice_label, amount=int(selection * (10 ** int(configloader.config["Payments"]["currency_exp"]))))], + prices=prices, need_name=configloader.config["Credit Card"]["name_required"] == "yes", need_email=configloader.config["Credit Card"]["email_required"] == "yes", need_phone_number=configloader.config["Credit Card"]["phone_required"] == "yes") @@ -266,7 +273,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, + value=successfulpayment.total_amount - int(total_fee * (10 ** int(configloader.config["Payments"]["currency_exp"]))), provider="Credit Card", telegram_charge_id=successfulpayment.telegram_payment_charge_id, provider_charge_id=successfulpayment.provider_payment_charge_id) @@ -275,7 +282,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 + self.user.credit += (successfulpayment.total_amount - int(total_fee * (10 ** int(configloader.config["Payments"]["currency_exp"])))) # Add and commit the transaction self.session.add(transaction) self.session.commit()