| Line 18... |
Line 18... |
| 18 |
import org.springframework.http.MediaType;
|
18 |
import org.springframework.http.MediaType;
|
| 19 |
import org.springframework.http.ResponseEntity;
|
19 |
import org.springframework.http.ResponseEntity;
|
| 20 |
import org.springframework.stereotype.Controller;
|
20 |
import org.springframework.stereotype.Controller;
|
| 21 |
import org.springframework.ui.Model;
|
21 |
import org.springframework.ui.Model;
|
| 22 |
import org.springframework.web.bind.annotation.ModelAttribute;
|
22 |
import org.springframework.web.bind.annotation.ModelAttribute;
|
| 23 |
import org.springframework.web.bind.annotation.RequestBody;
|
- |
|
| 24 |
import org.springframework.web.bind.annotation.RequestMapping;
|
23 |
import org.springframework.web.bind.annotation.RequestMapping;
|
| 25 |
import org.springframework.web.bind.annotation.RequestMethod;
|
24 |
import org.springframework.web.bind.annotation.RequestMethod;
|
| 26 |
|
25 |
|
| 27 |
import javax.crypto.Mac;
|
26 |
import javax.crypto.Mac;
|
| 28 |
import javax.crypto.spec.SecretKeySpec;
|
27 |
import javax.crypto.spec.SecretKeySpec;
|
| Line 75... |
Line 74... |
| 75 |
@Value("${razorpay.account.keySecret}")
|
74 |
@Value("${razorpay.account.keySecret}")
|
| 76 |
private String razorpaySecret;
|
75 |
private String razorpaySecret;
|
| 77 |
@Autowired
|
76 |
@Autowired
|
| 78 |
private UpsellRazorpayPaymentStatusRepository upsellRazorpayPaymentStatusRepository;
|
77 |
private UpsellRazorpayPaymentStatusRepository upsellRazorpayPaymentStatusRepository;
|
| 79 |
|
78 |
|
| 80 |
@RequestMapping(value = "/upsellPayment/callback", method = RequestMethod.POST)
|
79 |
@RequestMapping(value = "/upsellPayment/callback", method = RequestMethod.GET)
|
| 81 |
public ResponseEntity<String> handleCallback(HttpServletRequest request, @RequestBody String payload) {
|
80 |
public ResponseEntity<String> handleCallback(HttpServletRequest request) {
|
| 82 |
try {
|
81 |
try {
|
| 83 |
LOGGER.info("webhookcalled {}", 1);
|
82 |
LOGGER.info("webhook called {}", 1);
|
| - |
|
83 |
|
| 84 |
// Verify the callback request
|
84 |
// Verify the callback request
|
| 85 |
Map<String, String> headers = getHeadersInfo(request);
|
85 |
Map<String, String> headers = getHeadersInfo(request);
|
| 86 |
String razorpaySignature = headers.get("x-razorpay-signature");
|
86 |
String razorpaySignature = headers.get("x-razorpay-signature");
|
| - |
|
87 |
|
| - |
|
88 |
// Retrieve the payload from query parameters
|
| - |
|
89 |
String paymentId = request.getParameter("razorpay_payment_id");
|
| - |
|
90 |
String status = request.getParameter("status");
|
| - |
|
91 |
|
| - |
|
92 |
// Assuming notes are passed as individual parameters
|
| - |
|
93 |
String orderId = request.getParameter("orderId");
|
| - |
|
94 |
String insuranceAmount = request.getParameter("insuranceAmount");
|
| - |
|
95 |
|
| - |
|
96 |
// Verify that all necessary parameters are received
|
| - |
|
97 |
if (paymentId == null || status == null || orderId == null || insuranceAmount == null) {
|
| - |
|
98 |
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Missing required parameters");
|
| - |
|
99 |
}
|
| - |
|
100 |
|
| - |
|
101 |
// Construct payload for signature verification
|
| - |
|
102 |
JSONObject payloadJson = new JSONObject();
|
| - |
|
103 |
payloadJson.put("razorpay_payment_id", paymentId);
|
| - |
|
104 |
payloadJson.put("status", status);
|
| - |
|
105 |
|
| - |
|
106 |
JSONObject notes = new JSONObject();
|
| - |
|
107 |
notes.put("orderId", orderId);
|
| - |
|
108 |
notes.put("insuranceAmount", insuranceAmount);
|
| - |
|
109 |
payloadJson.put("notes", notes);
|
| - |
|
110 |
|
| - |
|
111 |
String payload = payloadJson.toString();
|
| 87 |
boolean isSignatureValid = verifySignature(payload, razorpaySignature);
|
112 |
boolean isSignatureValid = verifySignature(payload, razorpaySignature);
|
| 88 |
|
113 |
|
| 89 |
if (!isSignatureValid) {
|
114 |
if (!isSignatureValid) {
|
| 90 |
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Invalid signature");
|
115 |
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Invalid signature");
|
| 91 |
}
|
116 |
}
|
| 92 |
|
117 |
|
| 93 |
// Parse the payload
|
- |
|
| 94 |
JSONObject jsonObject = new JSONObject(payload);
|
- |
|
| 95 |
String paymentId = jsonObject.getString("razorpay_payment_id");
|
- |
|
| 96 |
String status = jsonObject.getString("status");
|
- |
|
| 97 |
|
- |
|
| 98 |
JSONObject notes = jsonObject.getJSONObject("notes");
|
- |
|
| 99 |
String orderId = notes.getString("orderId");
|
- |
|
| 100 |
String orderItemId = notes.getString("orderItemId");
|
- |
|
| 101 |
String planId = notes.getString("planId");
|
- |
|
| 102 |
|
- |
|
| 103 |
JSONObject customer = jsonObject.getJSONObject("customer");
|
- |
|
| 104 |
String customerName = notes.getString("name");
|
- |
|
| 105 |
String customerMobile = notes.getString("contact");
|
- |
|
| 106 |
String customerEmail = notes.getString("email");
|
- |
|
| 107 |
String insuranceAmount = notes.getString("insuranceAmount");
|
- |
|
| 108 |
|
- |
|
| 109 |
// Process the payment status
|
118 |
// Process the payment status
|
| 110 |
if ("captured".equals(status)) {
|
119 |
if ("captured".equals(status)) {
|
| 111 |
updatePaymentStatus(paymentId, "captured", orderId, insuranceAmount);
|
120 |
updatePaymentStatus(paymentId, "captured", orderId, insuranceAmount);
|
| 112 |
throw new Exception("Payment successful for ID: " + paymentId);
|
121 |
return ResponseEntity.ok("Payment successful for ID: " + paymentId);
|
| 113 |
|
- |
|
| 114 |
} else {
|
122 |
} else {
|
| 115 |
updatePaymentStatus(paymentId, "failed", orderId, insuranceAmount);
|
123 |
updatePaymentStatus(paymentId, "failed", orderId, insuranceAmount);
|
| 116 |
throw new Exception("Payment failed for ID: " + paymentId);
|
124 |
return ResponseEntity.ok("Payment failed for ID: " + paymentId);
|
| 117 |
|
- |
|
| 118 |
}
|
125 |
}
|
| 119 |
|
- |
|
| 120 |
} catch (Exception e) {
|
126 |
} catch (Exception e) {
|
| 121 |
e.printStackTrace();
|
127 |
e.printStackTrace();
|
| 122 |
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error processing callback");
|
128 |
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error processing callback");
|
| 123 |
}
|
129 |
}
|
| 124 |
}
|
130 |
}
|
| 125 |
|
131 |
|
| - |
|
132 |
|
| 126 |
private boolean verifySignature(String payload, String razorpaySignature) throws Exception {
|
133 |
private boolean verifySignature(String payload, String razorpaySignature) throws Exception {
|
| 127 |
String actualSignature = HmacSHA256(payload, razorpaySecret);
|
134 |
String actualSignature = HmacSHA256(payload, razorpaySecret);
|
| 128 |
return actualSignature.equals(razorpaySignature);
|
135 |
return actualSignature.equals(razorpaySignature);
|
| 129 |
}
|
136 |
}
|
| 130 |
|
137 |
|