RAZORPAY CODE BACKEND

Razorpay Integration - CDAC Learning

CDAC Learning - Razorpay Backend Integration

Date: February 05, 2025

Entity - BuyNow

package com.salman.dac.entity;

import jakarta.persistence.*;
import lombok.*;

@Entity
@Table(name = "buy_now")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class BuyNow {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToOne
    @JoinColumn(name = "user_id", nullable = false)
    private User user;

    @ManyToOne
    @JoinColumn(name = "module_id", nullable = false)
    private Module module;

    private int amount;
    private String razorpayOrderId;
    private String orderStatus;
    private String phno;
}

Controller - BuyNowController

package com.salman.dac.controller;

import org.springframework.web.bind.annotation.*;
import org.springframework.http.ResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import com.razorpay.RazorpayException;
import com.salman.dac.service.BuyNowService;

@RestController
@RequestMapping("/api/buy-now")
public class BuyNowController {
    @Autowired
    private BuyNowService buyNowService;
    
    @PostMapping("/create-order")
    public ResponseEntity createOrder(@RequestBody BuyNow order) {
        try {
            return ResponseEntity.ok(buyNowService.createOrder(order));
        } catch (RazorpayException e) {
            return ResponseEntity.status(500).body("Error: " + e.getMessage());
        }
    }
}

Service - BuyNowService

package com.salman.dac.service;

import com.razorpay.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class BuyNowService {
    @Transactional
    public BuyNow createOrder(BuyNow order) throws RazorpayException {
        RazorpayClient client = new RazorpayClient("SECRETE_KEY_ID", "SECRETE_KEY");
        JSONObject orderRequest = new JSONObject();
        orderRequest.put("amount", order.getAmount() * 100);
        orderRequest.put("currency", "INR");
        orderRequest.put("receipt", "receipt_" + System.currentTimeMillis());

        Order razorpayOrder = client.orders.create(orderRequest);
        order.setRazorpayOrderId(razorpayOrder.get("id"));
        order.setOrderStatus("CREATED");
        return order;
    }
}

Repository - BuyNowRepository

package com.salman.dac.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import com.salman.dac.entity.BuyNow;

public interface BuyNowRepository extends JpaRepository {
    BuyNow findByRazorpayOrderId(String orderId);
}

Configuration - RazorpayConfig

package com.salman.dac.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.razorpay.RazorpayClient;
import com.razorpay.RazorpayException;

@Configuration
public class RazorpayConfig {
    @Bean
    public RazorpayClient razorpayClient() throws RazorpayException {
        return new RazorpayClient("SECRETE_KEY_ID", "SECRETE_KEY");
    }
}

Application Properties

razorpay.key.id=SECRETE_KEY_ID
razorpay.secret.key=SECRETE_KEY

POM Dependency

<dependency>
    <groupId>com.razorpay</groupId>
    <artifactId>razorpay-java</artifactId>
    <version>1.4.3</version>
</dependency>

Comments

Popular posts from this blog

JWT Authentication with Spring Boot

RAZORPAY FRONTEND CODE