Hey there, fellow developer! Ready to dive into the world of online payments with Razorpay? You're in the right place. Let's walk through integrating Razorpay's API into your Java project using their nifty razorpay-java package. Buckle up, and let's get coding!
Razorpay is a powerhouse when it comes to payment gateways in India. Their API is robust, well-documented, and now, with their Java package, it's easier than ever to integrate into your projects. We'll be using the razorpay-java package to make our lives easier and our code cleaner.
Before we jump in, make sure you've got:
First things first, let's add the razorpay-java dependency to your project. If you're using Maven, add this to your pom.xml:
<dependency> <groupId>com.razorpay</groupId> <artifactId>razorpay-java</artifactId> <version>1.3.9</version> </dependency>
For Gradle users, add this to your build.gradle:
implementation 'com.razorpay:razorpay-java:1.3.9'
Now, let's initialize the Razorpay client:
import com.razorpay.*; RazorpayClient razorpay = new RazorpayClient("key_id", "key_secret");
Time to create an order! Here's how you do it:
JSONObject orderRequest = new JSONObject(); orderRequest.put("amount", 50000); // amount in paise orderRequest.put("currency", "INR"); orderRequest.put("receipt", "txn_123456"); Order order = razorpay.orders.create(orderRequest); String orderId = order.get("id");
Easy peasy, right? Now you've got an order ID to use on your checkout page.
After the payment is made, you'll need to verify the signature and capture the payment:
String paymentId = request.getParameter("razorpay_payment_id"); String orderId = request.getParameter("razorpay_order_id"); String signature = request.getParameter("razorpay_signature"); try { Utils.verifyPaymentSignature(params, "secret"); razorpay.payments.capture(paymentId, amount); } catch (RazorpayException e) { System.out.println("Error: " + e.getMessage()); }
Need to refund a payment? No sweat:
JSONObject refundRequest = new JSONObject(); refundRequest.put("amount", 100); refundRequest.put("speed", "normal"); Refund refund = razorpay.payments.refund(paymentId, refundRequest);
Webhooks are your friends for real-time updates. Set up an endpoint in your application to handle Razorpay events:
@PostMapping("/razorpay-webhook") public ResponseEntity<String> handleWebhook(@RequestBody String payload) { // Verify webhook signature // Process the event return ResponseEntity.ok().build(); }
Always wrap your Razorpay API calls in try-catch blocks to handle RazorpayException. And remember, keep your API keys secret and never expose them in client-side code!
Razorpay provides a test mode for you to play around without real transactions. Use it liberally before going live. Here's a quick test case to get you started:
@Test public void testOrderCreation() { // Set up test environment RazorpayClient razorpay = new RazorpayClient("test_key_id", "test_key_secret"); // Create a test order JSONObject orderRequest = new JSONObject(); orderRequest.put("amount", 1000); orderRequest.put("currency", "INR"); orderRequest.put("receipt", "test_receipt"); try { Order order = razorpay.orders.create(orderRequest); assertNotNull(order.get("id")); } catch (RazorpayException e) { fail("Order creation failed: " + e.getMessage()); } }
And there you have it! You're now equipped to integrate Razorpay into your Java project like a pro. Remember, the Razorpay documentation is your best friend for more advanced features and edge cases.
Happy coding, and may your transactions always be successful! 🚀💰