How to Add CCAvenue Payment Gateway in Spring Boot

11/28/20232 min read

person browsing on white monitor
person browsing on white monitor

Integrating a payment gateway into your Spring Boot application is essential for enabling secure and convenient online transactions. CCAvenue is a popular payment gateway in India that offers a wide range of payment options and robust security features. In this tutorial, we will guide you through the process of integrating the CCAvenue payment gateway into your Spring Boot application.

Step 1: Sign Up for CCAvenue

The first step is to sign up for a CCAvenue merchant account. Visit the CCAvenue website and follow the registration process. Once you have successfully registered, you will receive the necessary credentials to access the CCAvenue dashboard.

Step 2: Add CCAvenue Dependency

In your Spring Boot project, open the pom.xml file and add the CCAvenue dependency:

<dependency>
    <groupId>com.ccavenue</groupId>
    <artifactId>ccavenue-java</artifactId>
    <version>1.0.0</version>
</dependency>

Step 3: Configure CCAvenue Parameters

In your application.properties file, add the following CCAvenue configuration parameters:

ccavenue.merchant.id=YOUR_MERCHANT_ID
ccavenue.access.code=YOUR_ACCESS_CODE
ccavenue.working.key=YOUR_WORKING_KEY
ccavenue.redirect.url=YOUR_REDIRECT_URL
ccavenue.cancel.url=YOUR_CANCEL_URL
ccavenue.currency=INR

Step 4: Create Payment Controller

Create a new controller class to handle payment requests:

@RestController
@RequestMapping("/payment")
public class PaymentController {

    @Autowired
    private CCAvenueService ccAvenueService;

    @PostMapping("/pay")
    public String initiatePayment(@RequestBody PaymentRequest paymentRequest) {
        // Process payment request and generate order ID
        String orderId = ccAvenueService.generateOrderId(paymentRequest.getAmount());

        // Generate payment form HTML
        String paymentForm = ccAvenueService.generatePaymentForm(orderId, paymentRequest.getAmount());

        return paymentForm;
    }
}

Step 5: Implement CCAvenueService

Create a service class to handle CCAvenue integration:

@Service
public class CCAvenueService {

    @Value("${ccavenue.merchant.id}")
    private String merchantId;

    @Value("${ccavenue.access.code}")
    private String accessCode;

    @Value("${ccavenue.working.key}")
    private String workingKey;

    @Value("${ccavenue.redirect.url}")
    private String redirectUrl;

    @Value("${ccavenue.cancel.url}")
    private String cancelUrl;

    @Value("${ccavenue.currency}")
    private String currency;

    public String generateOrderId(BigDecimal amount) {
        // Generate a unique order ID based on your application logic
        // ...

        return orderId;
    }

    public String generatePaymentForm(String orderId, BigDecimal amount) {
        // Generate the payment form HTML using CCAvenue parameters
        // ...

        return paymentForm;
    }
}

Step 6: Create Payment Request Model

Create a model class to represent the payment request:

public class PaymentRequest {

    private BigDecimal amount;

    // Getters and setters
}

Step 7: Test the Integration

Start your Spring Boot application and test the integration by sending a payment request to the /payment/pay endpoint. You should receive the CCAvenue payment form HTML in the response. Embed this form in your website or application to enable users to make payments via CCAvenue.

Congratulations! You have successfully integrated the CCAvenue payment gateway into your Spring Boot application. Users can now make secure and convenient online payments using CCAvenue.

Remember to test the integration thoroughly and handle any errors or exceptions that may occur during the payment process. CCAvenue provides extensive documentation and support to help you troubleshoot and resolve any issues.