You are reviewing a payment processing system that handles transactions for both standard credit cards and virtual credit cards. The system must enforce various business rules before approving or declining payments. Your task is to implement the payment processing logic that correctly evaluates these rules and returns the appropriate status code.
The system needs to handle two payment methods:
The account_info dictionary contains:
available_credit (float): Current available credit limitaccount_status (string): Either "active" or "suspended"daily_limit (float): Maximum daily spending amount (for virtual cards)transactions_today (int): Number of transactions processed todayExample 1:
Input: amount = 150.00 payment_method = "credit_card" account_info = { "available_credit": 500.00, "account_status": "active", "daily_limit": 1000.00, "transactions_today": 2 } Output: "APPROVED" Explanation: Account is active, sufficient credit available, and all rules pass for a credit card payment.
Example 2:
Input: amount = 75.00 payment_method = "virtual_card" account_info = { "available_credit": 500.00, "account_status": "active", "daily_limit": 1000.00, "transactions_today": 10 } Output: "DECLINED_LIMIT_EXCEEDED" Explanation: Virtual card has reached the daily transaction limit of 10 transactions.
Example 3:
Input: amount = 50.00 payment_method = "credit_card" account_info = { "available_credit": 300.00, "account_status": "suspended", "daily_limit": 1000.00, "transactions_today": 3 } Output: "DECLINED_ACCOUNT_SUSPENDED" Explanation: Account is suspended, so payment is declined immediately regardless of other factors.
Hint 1: Order of Operations Process validation rules in a specific order. Account status should be checked first, then credit availability, then payment-method-specific rules. This ensures proper error priority and matches real-world payment processing logic.
Hint 2: Virtual Card Rules Virtual cards have two additional restrictions beyond standard credit cards: a maximum number of daily transactions (10) and a daily spending limit. Both must be checked, and either one failing should result in the same decline code.
Hint 3: Account Info Structure The account_info dictionary contains all necessary data. You don't need to track state across calls - each call is independent with the current state provided in account_info.
Full Solution `` Explanation:
The solution implements a cascading validation system following payment processing best practices:
Account Status Check: First priority is ensuring the account is active. Suspended accounts cannot process any transactions regardless of other factors.
Credit Availability: Second check verifies sufficient funds. This applies to both payment methods and prevents overdraft situations.
Virtual Card Restrictions: Virtual cards have additional security measures:
- Transaction count limit (10 per day) prevents abuse through many small transactions
- Daily spending limit caps total exposure from a single virtual card
Return Logic: The function returns the first applicable decline reason or "APPROVED" if all checks pass.
Time Complexity: O(1) - All operations are constant time dictionary lookups and comparisons.
Space Complexity: O(1) - Only uses a fixed amount of variables regardless of input size.
This design prioritizes security and risk management by checking critical factors first (account status) before more granular rules (spending limits).
def processPayment(amount, payment_method, account_info):
"""
Process a payment transaction based on business rules.
Args:
amount: Payment amount in dollars
payment_method: Either "credit_card" or "virtual_card"
account_info: Dictionary containing account details
Returns:
Status code string indicating approval or decline reason
"""
# Rule 1: Check account status first
if account_info["account_status"] != "active":
return "DECLINED_ACCOUNT_SUSPENDED"
# Rule 2: Check available credit
if amount > account_info["available_credit"]:
return "DECLINED_INSUFFICIENT_CREDIT"
# Rules 3 & 4: Additional validation for virtual cards
if payment_method == "virtual_card":
# Check daily transaction count limit
if account_info["transactions_today"] >= 10:
return "DECLINED_LIMIT_EXCEEDED"
# Check daily spending limit
if amount > account_info["daily_limit"]:
return "DECLINED_LIMIT_EXCEEDED"
# All checks passed
return "APPROVED"