From 8fd202f7b48b98042b6b8ceea031e285a6f48ceb Mon Sep 17 00:00:00 2001 From: KOSASIH Date: Sun, 1 Dec 2024 20:15:26 +0700 Subject: [PATCH] Create payment_gateway.py --- src/payment/payment_gateway.py | 50 ++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/payment/payment_gateway.py diff --git a/src/payment/payment_gateway.py b/src/payment/payment_gateway.py new file mode 100644 index 000000000..0691e61bb --- /dev/null +++ b/src/payment/payment_gateway.py @@ -0,0 +1,50 @@ +import uuid +import time +from cryptography.fernet import Fernet +from notification import Notification + +class PaymentGateway: + def __init__(self): + self.payments = {} # payment_id -> Payment + self.notification_service = Notification() + + def initiate_payment(self, amount, currency, payer, receiver, recurring=False, interval=None): + payment_id = str(uuid.uuid4()) + payment = Payment(payment_id, amount, currency, payer, receiver, recurring, interval) + self.payments[payment_id] = payment + self.notification_service.send_notification(payer, f"Payment initiated: {payment_id} for {amount} {currency} from {payer} to {receiver}.") + print(f"Payment initiated: {payment_id} for {amount} {currency} from {payer} to {receiver}.") + return payment_id + + def get_payment_status(self, payment_id): + payment = self.payments.get(payment_id) + if payment: + return payment.status + return "Payment not found." + + def complete_payment(self, payment_id): + payment = self.payments.get(payment_id) + if payment and payment.status == "Pending": + payment.status = "Completed" + payment.timestamp = time.time() + self.notification_service.send_notification(payment.receiver, f"Payment {payment_id} completed.") + print(f"Payment {payment_id} completed.") + else: + print(f"Payment {payment_id} cannot be completed or does not exist.") + + def process_recurring_payments(self): + for payment in self.payments.values(): + if payment.recurring and payment.status == "Pending": + self.complete_payment(payment.payment_id) + +class Payment: + def __init__(self, payment_id, amount, currency, payer, receiver, recurring=False, interval=None): + self.payment_id = payment_id + self.amount = amount + self.currency = currency + self.payer = payer + self.receiver = receiver + self.status = "Pending" + self.timestamp = None + self.recurring = recurring + self.interval = interval # e.g., "monthly", "weekly"