Skip to content

Commit 014f769

Browse files
authored
Create payment_utils.py
1 parent 1ae369a commit 014f769

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import uuid
2+
import logging
3+
from decimal import Decimal
4+
5+
logger = logging.getLogger(__name__)
6+
7+
def generate_order_id():
8+
"""Generate a unique order ID using UUID."""
9+
order_id = str(uuid.uuid4())
10+
logger.debug("Generated order ID: %s", order_id)
11+
return order_id
12+
13+
def format_amount(amount, currency):
14+
"""Format the amount for display or API requests."""
15+
try:
16+
if currency == "USD":
17+
formatted = "${:,.2f}".format(Decimal(amount))
18+
else:
19+
formatted = "{:,.2f} {}".format(Decimal(amount), currency)
20+
logger.debug("Formatted amount: %s", formatted)
21+
return formatted
22+
except Exception as e:
23+
logger.error("Error formatting amount: %s", e)
24+
raise ValueError("Invalid amount format")
25+
26+
def log_payment_event(event_type, payment_data):
27+
"""Log payment events for auditing purposes."""
28+
logger.info("Payment Event: %s | Data: %s", event_type, payment_data)
29+
30+
def calculate_total_amount(items):
31+
"""Calculate the total amount from a list of items."""
32+
try:
33+
total = sum(Decimal(item['price']) * Decimal(item['quantity']) for item in items)
34+
logger.debug("Calculated total amount: %s", total)
35+
return total
36+
except Exception as e:
37+
logger.error("Error calculating total amount: %s", e)
38+
raise ValueError("Invalid items for total calculation")
39+
40+
def validate_payment_data(payment_data):
41+
"""Validate payment data structure."""
42+
required_fields = ['amount', 'currency', 'method']
43+
for field in required_fields:
44+
if field not in payment_data:
45+
logger.error("Missing required field: %s", field)
46+
raise ValueError(f"Missing required field: {field}")
47+
logger.debug("Payment data validated: %s", payment_data)

0 commit comments

Comments
 (0)