Sub / app /api /billing.py
Himankpro's picture
Upload 93 files
3a6c182 verified
Raw History Blame Contribute Delete
3.51 kB
"""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