File size: 3,506 Bytes
3a6c182
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Razorpay + coupon unlock for /api/pipeline/start."""

from flask import request, jsonify

from . import billing_bp
from ..config import Config
from ..services import billing_service
from ..utils.logger import get_logger

logger = get_logger("mirofish.api.billing")


@billing_bp.route("/status", methods=["GET"])
def billing_status():
    if not Config.billing_enabled():
        return jsonify(
            {
                "success": True,
                "data": {
                    "enabled": False,
                    "key_id": None,
                    "amount": None,
                    "currency": None,
                },
            }
        )
    data = {
        "enabled": True,
        "key_id": Config.RAZORPAY_KEY_ID or None,
        "amount": Config.RAZORPAY_AMOUNT_PAISE if Config.RAZORPAY_KEY_SECRET else None,
        "currency": Config.RAZORPAY_CURRENCY if Config.RAZORPAY_KEY_SECRET else None,
        "coupons_enabled": bool(Config.payment_coupon_set()),
    }
    return jsonify({"success": True, "data": data})


@billing_bp.route("/create-order", methods=["POST"])
def billing_create_order():
    try:
        if not (Config.RAZORPAY_KEY_ID and Config.RAZORPAY_KEY_SECRET):
            return jsonify({"success": False, "error": "Razorpay is not configured"}), 503
        order = billing_service.create_razorpay_order()
        return jsonify({"success": True, "data": order})
    except Exception as e:
        logger.error("create-order: %s", e)
        return jsonify({"success": False, "error": str(e)}), 500


@billing_bp.route("/verify-payment", methods=["POST"])
def billing_verify_payment():
    try:
        if not (Config.RAZORPAY_KEY_ID and Config.RAZORPAY_KEY_SECRET):
            return jsonify({"success": False, "error": "Razorpay is not configured"}), 503
        body = request.get_json(silent=True) or {}
        oid = (body.get("razorpay_order_id") or "").strip()
        pid = (body.get("razorpay_payment_id") or "").strip()
        sig = (body.get("razorpay_signature") or "").strip()
        if not oid or not pid or not sig:
            return jsonify({"success": False, "error": "Missing payment fields"}), 400
        if not billing_service.verify_razorpay_payment_signature(oid, pid, sig):
            return jsonify({"success": False, "error": "Invalid payment signature"}), 400
        token = billing_service.issue_simulation_payment_token(via="razorpay")
        return jsonify({"success": True, "data": {"simulation_payment_token": token}})
    except Exception as e:
        logger.error("verify-payment: %s", e)
        return jsonify({"success": False, "error": str(e)}), 500


@billing_bp.route("/apply-coupon", methods=["POST"])
def billing_apply_coupon():
    try:
        if not Config.payment_coupon_set():
            return jsonify({"success": False, "error": "Coupons are not configured"}), 503
        body = request.get_json(silent=True) or {}
        code = body.get("code") or body.get("coupon") or ""
        if not billing_service.coupon_valid(str(code)):
            return jsonify({"success": False, "error": "Invalid coupon code"}), 400
        token = billing_service.issue_simulation_payment_token(via="coupon")
        return jsonify({"success": True, "data": {"simulation_payment_token": token}})
    except Exception as e:
        logger.error("apply-coupon: %s", e)
        return jsonify({"success": False, "error": str(e)}), 500