From 23f395ee8a2e7d97dd862297427f721167c0e8b3 Mon Sep 17 00:00:00 2001 From: Pavlo Zhuk Date: Sat, 4 Apr 2020 23:37:03 +0300 Subject: [PATCH] allow refill on insufficient funds during checkout --- config/template_config.ini | 3 ++- worker.py | 16 +++++++++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/config/template_config.ini b/config/template_config.ini index 149eca7..c9e107e 100644 --- a/config/template_config.ini +++ b/config/template_config.ini @@ -74,7 +74,8 @@ phone_required = yes full_order_info = no ; Payment presets would be suggested to the user as buttons, when making credit card transaction payment_presets = 10.00,25.00,50.00,100.00 - +; Allow balance refill during the order checkout in case of unsufficient balance +refill_on_checkout = yes # Exception reporting settings [Error Reporting] diff --git a/worker.py b/worker.py index c2716ea..5d81b93 100644 --- a/worker.py +++ b/worker.py @@ -484,14 +484,20 @@ class ChatWorker(threading.Thread): order_id=order.order_id) self.session.add(order_item) # Ensure the user has enough credit to make the purchase - if self.user.credit - self.__get_cart_value(cart) < 0: + credit_required = self.__get_cart_value(cart) - self.user.credit + # Notify user In case of insufficient credit + if credit_required > 0: self.bot.send_message(self.chat.id, strings.error_not_enough_credit) + # Suggest payment for missing credit value if configuration allows refill + if configloader.config["Appearance"]["refill_on_checkout"] == 'yes': + self.__make_payment(utils.Price(credit_required)) + # If afer requested payment credit is still insufficient (either payment failure or cancel) + if self.user.credit < self.__get_cart_value(cart): # Rollback all the changes self.session.rollback() - return - - # User has credit and valid order, perform transaction now - self.__order_transaction(order=order, value=-int(self.__get_cart_value(cart))) + else: + # User has credit and valid order, perform transaction now + self.__order_transaction(order=order, value=-int(self.__get_cart_value(cart))) @staticmethod def __get_cart_value(cart):