All files / lib/razorpay client.ts

93.93% Statements 62/66
80% Branches 8/10
100% Functions 3/3
93.93% Lines 62/66

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 1071x                 1x   1x 1x 1x       1x         1x   1x   1x 1x 1x         1x 4x 4x           1x 1x 1x 1x 1x               1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x         1x 4x 1x 1x 1x   3x 3x 3x 3x   3x 3x         1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
/**
 * Razorpay Client Configuration
 *
 * Server-side only - never import in client components
 */
 
import Razorpay from 'razorpay';
import crypto from 'crypto';
 
const RAZORPAY_CONFIGURED = !!process.env.RAZORPAY_KEY_ID && !!process.env.RAZORPAY_KEY_SECRET;
 
if (!RAZORPAY_CONFIGURED) {
  console.warn('Razorpay credentials not configured. Payment features will be disabled.');
}
 
// Only create the Razorpay instance if credentials are configured
// Using a placeholder key to avoid initialization errors during build
const razorpayInstance = RAZORPAY_CONFIGURED
  ? new Razorpay({
      key_id: process.env.RAZORPAY_KEY_ID!,
      key_secret: process.env.RAZORPAY_KEY_SECRET!,
    })
  : null;
 
export const razorpay = razorpayInstance as Razorpay;
 
export const RAZORPAY_KEY_ID = process.env.NEXT_PUBLIC_RAZORPAY_KEY_ID || '';
export const RAZORPAY_KEY_SECRET = process.env.RAZORPAY_KEY_SECRET || '';
export const RAZORPAY_WEBHOOK_SECRET = process.env.RAZORPAY_WEBHOOK_SECRET || '';
 
/**
 * Check if Razorpay is configured
 */
export function isRazorpayConfigured(): boolean {
  return !!(process.env.RAZORPAY_KEY_ID && process.env.RAZORPAY_KEY_SECRET);
}
 
/**
 * Subscription plan IDs from Razorpay dashboard
 * Update these after creating plans in Razorpay
 */
export const RAZORPAY_PLANS = {
  basic: process.env.RAZORPAY_PLAN_BASIC || '',
  pro: process.env.RAZORPAY_PLAN_PRO || '',
  premium: process.env.RAZORPAY_PLAN_PREMIUM || '',
} as const;
 
export type PlanType = keyof typeof RAZORPAY_PLANS;
 
/**
 * Verify Razorpay payment signature
 * This ensures the payment response hasn't been tampered with
 */
export function verifyPaymentSignature(
  orderId: string,
  paymentId: string,
  signature: string
): boolean {
  const body = orderId + '|' + paymentId;
  const expectedSignature = crypto
    .createHmac('sha256', RAZORPAY_KEY_SECRET)
    .update(body)
    .digest('hex');
  return expectedSignature === signature;
}
 
/**
 * Verify Razorpay webhook signature
 */
export function verifyWebhookSignature(body: string, signature: string): boolean {
  if (!RAZORPAY_WEBHOOK_SECRET) {
    console.warn('Webhook secret not configured');
    return false;
  }
 
  const expectedSignature = crypto
    .createHmac('sha256', RAZORPAY_WEBHOOK_SECRET)
    .update(body)
    .digest('hex');
 
  return expectedSignature === signature;
}
 
/**
 * Pricing configuration (amounts in paise)
 */
export const PRICING = {
  basic: {
    amount: 29900, // ₹299
    currency: 'INR',
    name: 'Basic',
    description: 'ProofID Basic Plan - 1 Year',
  },
  pro: {
    amount: 49900, // ₹499
    currency: 'INR',
    name: 'Pro',
    description: 'ProofID Pro Plan - 1 Year',
  },
  premium: {
    amount: 99900, // ₹999
    currency: 'INR',
    name: 'Premium',
    description: 'ProofID Premium Plan - 1 Year',
  },
} as const;