The before code processes payments without saving any record. The after code adds a TransactionHistory class that stores each payment with details and timestamp. The PaymentProcessor uses this to save every transaction, enabling retrieval of user transaction history.
### Before: No transaction history tracking
class PaymentProcessor:
def process_payment(self, user_id, amount):
# Process payment logic
print(f"Processed payment of {amount} for user {user_id}")
### After: With transaction history tracking
from datetime import datetime
class TransactionHistory:
def __init__(self):
self.records = []
def add_record(self, user_id, amount, status):
record = {
'user_id': user_id,
'amount': amount,
'status': status,
'timestamp': datetime.utcnow().isoformat() + 'Z'
}
self.records.append(record)
def get_history(self, user_id):
return [r for r in self.records if r['user_id'] == user_id]
class PaymentProcessor:
def __init__(self, history):
self.history = history
def process_payment(self, user_id, amount):
# Process payment logic
status = 'success' # Assume success for example
self.history.add_record(user_id, amount, status)
print(f"Processed payment of {amount} for user {user_id}")
# Usage
history = TransactionHistory()
processor = PaymentProcessor(history)
processor.process_payment('user123', 100)
print(history.get_history('user123'))