Spaces:
Running
Running
File size: 12,326 Bytes
de3cb16 |
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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 |
"""
Razorpay Payment Service - Modular, plug-and-play payment integration.
This module provides a complete Razorpay integration that can be easily
moved to another project with minimal changes.
Features:
- Create Razorpay orders for credit purchases
- Verify payment signatures (HMAC SHA256)
- Process webhook events
- Configurable credit packages
Usage:
from services.razorpay_service import RazorpayService, CREDIT_PACKAGES
# Initialize
service = RazorpayService() # Uses environment variables
# Or with explicit credentials
service = RazorpayService(
key_id="rzp_test_xxx",
key_secret="secret",
webhook_secret="webhook_secret"
)
# Create order
order = service.create_order(
amount_paise=9900,
transaction_id="txn_abc123",
notes={"user_id": "user123"}
)
# Verify payment
is_valid = service.verify_payment_signature(
order_id="order_xxx",
payment_id="pay_xxx",
signature="signature"
)
Environment Variables:
RAZORPAY_KEY_ID: Your Razorpay Key ID
RAZORPAY_KEY_SECRET: Your Razorpay Key Secret
RAZORPAY_WEBHOOK_SECRET: Webhook secret for signature verification
"""
import os
import hmac
import hashlib
import logging
from typing import Optional, Dict, Any, List
from dataclasses import dataclass
from enum import Enum
import razorpay
logger = logging.getLogger(__name__)
# =============================================================================
# Credit Packages Configuration
# =============================================================================
@dataclass
class CreditPackage:
"""Represents a purchasable credit package."""
id: str
name: str
credits: int
amount_paise: int # Amount in paise (INR × 100)
currency: str = "INR"
@property
def amount_rupees(self) -> float:
"""Get amount in rupees."""
return self.amount_paise / 100
def to_dict(self) -> Dict[str, Any]:
"""Convert to dictionary for API responses."""
return {
"id": self.id,
"name": self.name,
"credits": self.credits,
"amount_paise": self.amount_paise,
"amount_rupees": self.amount_rupees,
"currency": self.currency
}
# Available credit packages
CREDIT_PACKAGES: Dict[str, CreditPackage] = {
"starter": CreditPackage(
id="starter",
name="Starter Pack",
credits=100,
amount_paise=9900 # ₹99
),
"standard": CreditPackage(
id="standard",
name="Standard Pack",
credits=500,
amount_paise=44900 # ₹449
),
"pro": CreditPackage(
id="pro",
name="Pro Pack",
credits=1000,
amount_paise=79900 # ₹799
)
}
def get_package(package_id: str) -> Optional[CreditPackage]:
"""Get a credit package by ID."""
return CREDIT_PACKAGES.get(package_id.lower())
def list_packages() -> List[Dict[str, Any]]:
"""List all available credit packages."""
return [pkg.to_dict() for pkg in CREDIT_PACKAGES.values()]
# =============================================================================
# Payment Status Enum
# =============================================================================
class PaymentStatus(str, Enum):
"""Payment transaction statuses."""
CREATED = "created"
AUTHORIZED = "authorized"
CAPTURED = "captured"
PAID = "paid"
FAILED = "failed"
REFUNDED = "refunded"
# =============================================================================
# Razorpay Service
# =============================================================================
class RazorpayServiceError(Exception):
"""Base exception for Razorpay service errors."""
pass
class RazorpayConfigError(RazorpayServiceError):
"""Raised when Razorpay is not properly configured."""
pass
class RazorpayOrderError(RazorpayServiceError):
"""Raised when order creation fails."""
pass
class RazorpayVerificationError(RazorpayServiceError):
"""Raised when payment verification fails."""
pass
class RazorpayService:
"""
Modular Razorpay payment service.
This service handles all Razorpay interactions including:
- Order creation
- Payment signature verification
- Webhook signature verification
The service can be initialized with explicit credentials or will
automatically use environment variables.
"""
def __init__(
self,
key_id: Optional[str] = None,
key_secret: Optional[str] = None,
webhook_secret: Optional[str] = None
):
"""
Initialize Razorpay service.
Args:
key_id: Razorpay Key ID (or RAZORPAY_KEY_ID env var)
key_secret: Razorpay Key Secret (or RAZORPAY_KEY_SECRET env var)
webhook_secret: Webhook secret (or RAZORPAY_WEBHOOK_SECRET env var)
"""
self.key_id = key_id or os.getenv("RAZORPAY_KEY_ID")
self.key_secret = key_secret or os.getenv("RAZORPAY_KEY_SECRET")
self.webhook_secret = webhook_secret or os.getenv("RAZORPAY_WEBHOOK_SECRET")
if not self.key_id or not self.key_secret:
raise RazorpayConfigError(
"Razorpay credentials not configured. "
"Set RAZORPAY_KEY_ID and RAZORPAY_KEY_SECRET environment variables."
)
# Initialize Razorpay client
self._client = razorpay.Client(auth=(self.key_id, self.key_secret))
logger.info("Razorpay service initialized")
@property
def is_configured(self) -> bool:
"""Check if the service is properly configured."""
return bool(self.key_id and self.key_secret)
def create_order(
self,
amount_paise: int,
transaction_id: str,
currency: str = "INR",
notes: Optional[Dict[str, str]] = None
) -> Dict[str, Any]:
"""
Create a Razorpay order for payment.
Args:
amount_paise: Amount in paise (₹1 = 100 paise)
transaction_id: Your internal transaction/receipt ID
currency: Currency code (default: INR)
notes: Optional notes to attach to the order
Returns:
Razorpay order response containing order_id
Raises:
RazorpayOrderError: If order creation fails
"""
try:
order_data = {
"amount": amount_paise,
"currency": currency,
"receipt": transaction_id,
"notes": notes or {}
}
order = self._client.order.create(data=order_data)
logger.info(
f"Created Razorpay order: {order['id']} "
f"for amount: {amount_paise} paise, "
f"receipt: {transaction_id}"
)
return order
except razorpay.errors.BadRequestError as e:
logger.error(f"Razorpay order creation failed (bad request): {e}")
raise RazorpayOrderError(f"Invalid order data: {e}")
except razorpay.errors.ServerError as e:
logger.error(f"Razorpay server error: {e}")
raise RazorpayOrderError(f"Razorpay server error: {e}")
except Exception as e:
logger.error(f"Unexpected error creating Razorpay order: {e}")
raise RazorpayOrderError(f"Failed to create order: {e}")
def verify_payment_signature(
self,
order_id: str,
payment_id: str,
signature: str
) -> bool:
"""
Verify Razorpay payment signature.
This MUST be called after receiving payment confirmation from the
client to ensure the payment is authentic.
Args:
order_id: Razorpay order ID
payment_id: Razorpay payment ID
signature: Razorpay signature from checkout response
Returns:
True if signature is valid
Raises:
RazorpayVerificationError: If signature verification fails
"""
try:
# Generate expected signature
message = f"{order_id}|{payment_id}"
expected_signature = hmac.new(
self.key_secret.encode('utf-8'),
message.encode('utf-8'),
hashlib.sha256
).hexdigest()
# Constant-time comparison to prevent timing attacks
is_valid = hmac.compare_digest(expected_signature, signature)
if is_valid:
logger.info(f"Payment signature verified: order={order_id}, payment={payment_id}")
else:
logger.warning(f"Invalid payment signature: order={order_id}, payment={payment_id}")
return is_valid
except Exception as e:
logger.error(f"Error verifying payment signature: {e}")
raise RazorpayVerificationError(f"Signature verification failed: {e}")
def verify_webhook_signature(
self,
body: bytes,
signature: str
) -> bool:
"""
Verify Razorpay webhook signature.
Use this to authenticate incoming webhook requests.
Args:
body: Raw request body bytes
signature: X-Razorpay-Signature header value
Returns:
True if webhook signature is valid
"""
if not self.webhook_secret:
logger.warning("Webhook secret not configured, skipping verification")
return False
try:
expected_signature = hmac.new(
self.webhook_secret.encode('utf-8'),
body,
hashlib.sha256
).hexdigest()
is_valid = hmac.compare_digest(expected_signature, signature)
if not is_valid:
logger.warning("Invalid webhook signature received")
return is_valid
except Exception as e:
logger.error(f"Error verifying webhook signature: {e}")
return False
def fetch_payment(self, payment_id: str) -> Dict[str, Any]:
"""
Fetch payment details from Razorpay.
Args:
payment_id: Razorpay payment ID
Returns:
Payment details from Razorpay
"""
try:
payment = self._client.payment.fetch(payment_id)
return payment
except Exception as e:
logger.error(f"Error fetching payment {payment_id}: {e}")
raise RazorpayServiceError(f"Failed to fetch payment: {e}")
def fetch_order(self, order_id: str) -> Dict[str, Any]:
"""
Fetch order details from Razorpay.
Args:
order_id: Razorpay order ID
Returns:
Order details from Razorpay
"""
try:
order = self._client.order.fetch(order_id)
return order
except Exception as e:
logger.error(f"Error fetching order {order_id}: {e}")
raise RazorpayServiceError(f"Failed to fetch order: {e}")
# =============================================================================
# Module-level convenience functions
# =============================================================================
_service_instance: Optional[RazorpayService] = None
def get_razorpay_service() -> RazorpayService:
"""
Get or create a singleton Razorpay service instance.
Returns:
RazorpayService instance
Raises:
RazorpayConfigError: If Razorpay is not configured
"""
global _service_instance
if _service_instance is None:
_service_instance = RazorpayService()
return _service_instance
def is_razorpay_configured() -> bool:
"""Check if Razorpay credentials are configured in environment."""
return bool(
os.getenv("RAZORPAY_KEY_ID") and
os.getenv("RAZORPAY_KEY_SECRET")
)
|