import httpx
import asyncio
async def payment_workflow(amount, vendor, account_number):
"""Process payment with human review for high-value transactions."""
async with httpx.AsyncClient() as client:
# Threshold for auto-approval
AUTO_APPROVAL_THRESHOLD = 1000
if amount > AUTO_APPROVAL_THRESHOLD:
# Request human review
response = await client.post(
"https://api.humancheck.dev/reviews",
headers={
"Authorization": "Bearer your-api-key-here",
"Content-Type": "application/json"
},
json={
"task_type": "payment",
"proposed_action": f"Process payment of ${amount:,.2f} to {vendor} (Account: {account_number})",
"agent_reasoning": f"Payment amount (${amount:,.2f}) exceeds auto-approval threshold of ${AUTO_APPROVAL_THRESHOLD:,.2f}",
"confidence_score": 0.9,
"urgency": "high" if amount > 5000 else "medium",
"metadata": {
"amount": amount,
"vendor": vendor,
"account_number": account_number,
"currency": "USD"
},
"blocking": True # Wait for decision
}
)
review = response.json()
# Get decision
decision = review.get("decision")
if not decision:
# Poll for decision
decision_response = await client.get(
f"https://api.humancheck.dev/reviews/{review['id']}/decision",
headers={"Authorization": "Bearer your-api-key-here"}
)
decision = decision_response.json()
# Process based on decision
if decision["decision_type"] == "approve":
# Process payment
result = await process_payment(amount, vendor, account_number)
return {"status": "completed", "transaction_id": result["id"]}
elif decision["decision_type"] == "modify":
# Extract modified amount from modified_action
modified_action = decision["modified_action"]
# Parse modified amount (simplified)
modified_amount = extract_amount(modified_action)
result = await process_payment(modified_amount, vendor, account_number)
return {"status": "completed", "transaction_id": result["id"], "modified": True}
else:
return {"status": "rejected", "reason": decision.get("notes")}
else:
# Auto-approve small payments
result = await process_payment(amount, vendor, account_number)
return {"status": "completed", "transaction_id": result["id"], "auto_approved": True}
# Helper function to extract amount from modified action
def extract_amount(action_string):
"""Extract amount from action string."""
import re
match = re.search(r'\$?([\d,]+\.?\d*)', action_string)
if match:
return float(match.group(1).replace(',', ''))
return None
# Usage
asyncio.run(payment_workflow(5000, "ACME Corp", "1234567890"))