Bhaskar-Guthula-137/RazorpayIntegration_NextJS_Razorpay_Cloudflare

Razorpay Integration using Next js 15, React 19, Razorpay SDK, Cloudflare Workers

โ˜… 0Forks 0TypeScriptGitHub โ†—Compare

Project website โ†—

cloudflare-workersnextjs15razorpay-payment-gatewayreact19tailwindcss-v4

README

๐Ÿ’ณ Razorpay Payment Integration with Next.js

A modern, production-ready payment integration demo built with Next.js 15, Razorpay, and deployed on Cloudflare Workers using OpenNext. This project demonstrates secure payment processing with a clean, responsive UI and seamless user experience.

๐Ÿš€ Live Demo

View Live Demo โ†’

๐Ÿ“ธ Screenshots

Homepage Payment Initialization Payment Completed
Homepage Payment Initialization Payment Completed

โœจ Features

  • โœ… Secure Payment Processing - Integration with Razorpay's payment gateway
  • โœ… Modern UI/UX - Clean, responsive design with smooth animations
  • โœ… Real-time Feedback - Loading states and interactive button effects
  • โœ… Edge Deployment - Deployed on Cloudflare Workers for global low-latency
  • โœ… Type-Safe - Built with TypeScript for robust code quality
  • โœ… Server-Side Order Creation - Secure API routes for payment order generation
  • โœ… Responsive Design - Works seamlessly across all device sizes

๐Ÿ› ๏ธ Tech Stack

๐Ÿ—๏ธ Project Structure

razorpay-next-app/
โ”œโ”€โ”€ src/
โ”‚   โ””โ”€โ”€ app/
โ”‚       โ”œโ”€โ”€ api/
โ”‚       โ”‚   โ””โ”€โ”€ create-order/
โ”‚       โ”‚       โ””โ”€โ”€ route.ts          # API endpoint for order creation
โ”‚       โ”œโ”€โ”€ globals.css               # Global styles
โ”‚       โ”œโ”€โ”€ layout.tsx                # Root layout
โ”‚       โ””โ”€โ”€ page.tsx                  # Homepage with payment button
โ”œโ”€โ”€ public/                           # Static assets & screenshots
โ”œโ”€โ”€ package.json
โ”œโ”€โ”€ tsconfig.json
โ”œโ”€โ”€ wrangler.jsonc                    # Cloudflare configuration
โ””โ”€โ”€ README.md

๐Ÿšฆ Getting Started

Prerequisites

  • Node.js 18+ or Bun
  • Razorpay account (Sign up here)
  • Cloudflare account (for deployment)

Installation

  1. Clone the repository

    git clone https://github.com/yourusername/razorpay-next-app.git
    cd razorpay-next-app
  2. Install dependencies

    npm install
    # or
    bun install
  3. Set up environment variables

    Create a .env file in the root directory:

    NEXT_PUBLIC_RAZORPAY_KEY_ID=your_razorpay_key_id

    Create a .dev.vars file for local Cloudflare development:

    RAZORPAY_KEY_ID=your_razorpay_key_id
    RAZORPAY_KEY_SECRET=your_razorpay_key_secret

    Note: Get your API keys from Razorpay Dashboard

  4. Run the development server

    npm run dev
    # or
    bun dev

    Open http://localhost:3000 to view the app.

๐Ÿ“ฆ Deployment

Deploy to Cloudflare Workers

  1. Build and deploy

    npm run deploy
    # or
    bun run deploy
  2. Set environment variables in Cloudflare

    npx wrangler secret put RAZORPAY_KEY_ID
    npx wrangler secret put RAZORPAY_KEY_SECRET

Preview locally with Cloudflare runtime

npm run preview
# or
bun run preview

๐Ÿ” Security Best Practices

  • โœ… API keys are stored securely in environment variables
  • โœ… Payment order creation happens server-side
  • โœ… Amount validation on the backend
  • โœ… Unique receipt IDs generated using nanoid
  • โœ… HTTPS-only in production

๐ŸŽจ Key Implementation Details

Payment Flow

  1. User clicks "Pay Now" button
  2. Frontend sends amount to /api/create-order
  3. Server creates a Razorpay order with unique receipt ID
  4. Razorpay checkout modal opens with order details
  5. User completes payment
  6. Payment success handler is triggered

API Route (/api/create-order)

// Secure server-side order creation
const order = await razorpay.orders.create({
  amount: amount,        // in paise
  currency: "INR",
  receipt: "receipt_" + nanoid(8)
});

Frontend Integration

// Razorpay checkout initialization
const rzp1 = new window.Razorpay({
  key: process.env.NEXT_PUBLIC_RAZORPAY_KEY_ID,
  amount: order.amount,
  currency: "INR",
  order_id: orderId,
  handler: () => console.log("Payment successful!")
});
rzp1.open();

๐Ÿ“ Available Scripts

Command Description
npm run dev Start development server with Turbopack
npm run build Build for production
npm run start Start production server
npm run lint Run ESLint
npm run preview Preview with Cloudflare runtime locally
npm run deploy Build and deploy to Cloudflare Workers
npm run cf-typegen Generate Cloudflare environment types

๐Ÿค Contributing

Contributions, issues, and feature requests are welcome! Feel free to check the issues page.

๐Ÿ“„ License

This project is open source and available under the MIT License.

๐Ÿ‘จโ€๐Ÿ’ป Author

Bhaskar G

๐Ÿ™ Acknowledgments


Built with โค๏ธ using Next.js and Razorpay

Contributors

Bhaskar-Guthula-137

Issues