File size: 4,482 Bytes
ae5e350
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33e275d
cf1da3a
33e275d
ae5e350
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93a3ca0
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
"""Payment API service"""
# TODO: Implement payment endpoints

from fastapi import FastAPI, Request, HTTPException
from pydantic import BaseModel
from datetime import datetime
import random
import uvicorn
import logging
from typing import Dict, Any

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

app = FastAPI(
    title="Invoice Payment API",
    description="Payment processing simulation for invoice automation",
    version="1.0.0",
    root_path="/spaces/PARTHA181098/InvoicePaymentAPI"

)

TRANSACTIONS: Dict[str, Dict[str, Any]] = {}
PAYMENT_METRICS = {
    'total_transactions': 0,
    'successful_payments': 0,
    'failed_payments': 0,
    'cancelled_payments': 0,
    'last_transaction_time': None
}


class PaymentRequest(BaseModel):
    order_id: str
    customer_name: str
    amount: float
    currency: str = 'USD'
    method: str = 'bank_transfer'
    recipient_account: str = 'default_account'
    due_date: str


class PaymentResponse(BaseModel):
    transaction_id: str
    status: str
    message: str
    processed_at: str


@app.get("/")
async def root():
    return {'message': 'Welcome to the Invoice Payment API', 'version': '1.0.0'}


@app.get("/health")
async def health_check():
    return {
        'status': 'healthy',
        'timestamp': datetime.utcnow().isoformat(),
        'uptime': 'OK'
    }


@app.post("/initiate_payment")
async def initiate_payment(payment_request: PaymentRequest):
    transaction_id = f"txn_{int(datetime.utcnow().timestamp())}_{random.randint(1000,9999)}"
    PAYMENT_METRICS['total_transactions'] += 1
    PAYMENT_METRICS['last_transaction_time'] = datetime.utcnow().isoformat()

    success_chance = 0.85
    if random.random() < success_chance:
        status = 'SUCCESS'
        message = f"Payment of {payment_request.amount} {payment_request.currency} successful."
        PAYMENT_METRICS['successful_payments'] += 1
    else:
        status = 'FAILED'
        message = 'Payment failed due to simulated gateway error.'
        PAYMENT_METRICS['failed_payments'] += 1

    transaction_data = {
        'transaction_id': transaction_id,
        'order_id': payment_request.order_id,
        'customer_name': payment_request.customer_name,
        'amount': payment_request.amount,
        'currency': payment_request.currency,
        'method': payment_request.method,
        'recipient_account': payment_request.recipient_account,
        'status': status,
        'message': message,
        'created_at': datetime.utcnow().isoformat(),
        'updated_at': datetime.utcnow().isoformat()
    }

    TRANSACTIONS[transaction_id] = transaction_data
    logger.info(f"Processed payment transaction {transaction_id} with status: {status}")

    return PaymentResponse(
        transaction_id=transaction_id,
        status=status,
        message=message,
        processed_at=transaction_data['updated_at']
    )


@app.get("/transaction/{transaction_id}")
async def get_transaction_status(transaction_id: str):
    transaction = TRANSACTIONS.get(transaction_id)
    if not transaction:
        raise HTTPException(status_code=404, detail="Transaction not found")
    return transaction


@app.post("/cancel_payment/{transaction_id}")
async def cancel_payment(transaction_id: str):
    transaction = TRANSACTIONS.get(transaction_id)
    if not transaction:
        raise HTTPException(status_code=400, detail="Cannot cancel a non-existent payment")

    if transaction['status'] in ['SUCCESS', 'FAILED']:
        raise HTTPException(status_code=400, detail="Cannot cancel a completed payment")

    transaction['status'] = 'CANCELLED'
    transaction['updated_at'] = datetime.utcnow().isoformat()
    PAYMENT_METRICS['cancelled_payments'] += 1
    logger.info(f"Cancelled payment transaction: {transaction_id}")
    return {
        'transaction_id': transaction_id,
        'status': 'CANCELLED',
        'message': 'Payment has been cancelled successfully'
    }


@app.get("/payment_methods")
async def get_payment_methods():
    return {
        'available_methods': [
            'bank_transfer',
            'credit_card',
            'paypal',
            'upi',
            'crypto'
        ],
        'default_method': 'bank_transfer'
    }


@app.get("/metrics")
async def get_metrics():
    return {
        'metrics': PAYMENT_METRICS,
        'active_transactions': len(TRANSACTIONS)
    }


# if __name__ == "__main__":
#     uvicorn.run(app, host="0.0.0.0", port=8001)