| Line 3... |
Line 3... |
| 3 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
3 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
| 4 |
import com.spice.profitmandi.common.web.util.ResponseSender;
|
4 |
import com.spice.profitmandi.common.web.util.ResponseSender;
|
| 5 |
import com.spice.profitmandi.dao.entity.fofo.UpsellRazorpayPaymentStatus;
|
5 |
import com.spice.profitmandi.dao.entity.fofo.UpsellRazorpayPaymentStatus;
|
| 6 |
import com.spice.profitmandi.dao.repository.cs.AgentRecordingRepository;
|
6 |
import com.spice.profitmandi.dao.repository.cs.AgentRecordingRepository;
|
| 7 |
import com.spice.profitmandi.dao.repository.fofo.UpsellRazorpayPaymentStatusRepository;
|
7 |
import com.spice.profitmandi.dao.repository.fofo.UpsellRazorpayPaymentStatusRepository;
|
| - |
|
8 |
import com.spice.profitmandi.service.integrations.RazorpayPaymentService;
|
| 8 |
import com.spice.profitmandi.service.integrations.kommuno.KommunoService;
|
9 |
import com.spice.profitmandi.service.integrations.kommuno.KommunoService;
|
| 9 |
import com.spice.profitmandi.service.integrations.smartping.SmartPingService;
|
10 |
import com.spice.profitmandi.service.integrations.smartping.SmartPingService;
|
| 10 |
import com.spice.profitmandi.service.integrations.smartping.model.CallDetailModel;
|
11 |
import com.spice.profitmandi.service.integrations.smartping.model.CallDetailModel;
|
| 11 |
import com.spice.profitmandi.web.util.MVCResponseSender;
|
12 |
import com.spice.profitmandi.web.util.MVCResponseSender;
|
| 12 |
import org.apache.logging.log4j.LogManager;
|
13 |
import org.apache.logging.log4j.LogManager;
|
| Line 20... |
Line 21... |
| 20 |
import org.springframework.ui.Model;
|
21 |
import org.springframework.ui.Model;
|
| 21 |
import org.springframework.web.bind.annotation.ModelAttribute;
|
22 |
import org.springframework.web.bind.annotation.ModelAttribute;
|
| 22 |
import org.springframework.web.bind.annotation.RequestMapping;
|
23 |
import org.springframework.web.bind.annotation.RequestMapping;
|
| 23 |
import org.springframework.web.bind.annotation.RequestMethod;
|
24 |
import org.springframework.web.bind.annotation.RequestMethod;
|
| 24 |
|
25 |
|
| 25 |
import javax.crypto.Mac;
|
- |
|
| 26 |
import javax.crypto.spec.SecretKeySpec;
|
- |
|
| 27 |
import javax.servlet.http.HttpServletRequest;
|
26 |
import javax.servlet.http.HttpServletRequest;
|
| 28 |
import javax.transaction.Transactional;
|
27 |
import javax.transaction.Transactional;
|
| 29 |
import java.time.LocalDateTime;
|
28 |
import java.time.LocalDateTime;
|
| 30 |
import java.util.Base64;
|
- |
|
| 31 |
import java.util.Enumeration;
|
- |
|
| 32 |
import java.util.HashMap;
|
- |
|
| 33 |
import java.util.Map;
|
- |
|
| 34 |
|
29 |
|
| 35 |
@Controller
|
30 |
@Controller
|
| 36 |
@Transactional(rollbackOn = Throwable.class)
|
31 |
@Transactional(rollbackOn = Throwable.class)
|
| 37 |
public class WebHookController {
|
32 |
public class WebHookController {
|
| 38 |
|
33 |
|
| Line 90... |
Line 85... |
| 90 |
if (paymentId == null || razorpaySignature == null || orderId == null || insuranceAmount == null) {
|
85 |
if (paymentId == null || razorpaySignature == null || orderId == null || insuranceAmount == null) {
|
| 91 |
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Missing required parameters");
|
86 |
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Missing required parameters");
|
| 92 |
}
|
87 |
}
|
| 93 |
|
88 |
|
| 94 |
// Construct payload for signature verification
|
89 |
// Construct payload for signature verification
|
| 95 |
String payload = "razorpay_payment_id=" + paymentId + "&orderId=" + orderId + "&insuranceAmount=" + insuranceAmount;
|
90 |
String payload = orderId + "|" + paymentId;
|
| - |
|
91 |
String generated_signature = RazorpayPaymentService
|
| - |
|
92 |
.calculateRFC2104HMAC(payload, razorpaySecret);
|
| 96 |
|
93 |
|
| 97 |
// Verify the signature
|
- |
|
| 98 |
boolean isSignatureValid = verifySignature(payload, razorpaySignature);
|
94 |
if (!generated_signature.equals(razorpaySignature)) {
|
| 99 |
if (!isSignatureValid) {
|
- |
|
| 100 |
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Invalid signature");
|
95 |
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Invalid signature");
|
| - |
|
96 |
|
| 101 |
}
|
97 |
}
|
| 102 |
|
98 |
|
| - |
|
99 |
|
| 103 |
// Handle the payment status (you may need to add more logic depending on the status)
|
100 |
// Handle the payment status (you may need to add more logic depending on the status)
|
| 104 |
updatePaymentStatus(paymentId, "captured", orderId, insuranceAmount);
|
101 |
updatePaymentStatus(paymentId, "captured", orderId, insuranceAmount);
|
| 105 |
|
102 |
|
| 106 |
return ResponseEntity.ok("Payment successful for ID: " + paymentId);
|
103 |
return ResponseEntity.ok("Payment successful for ID: " + paymentId);
|
| 107 |
|
104 |
|
| Line 110... |
Line 107... |
| 110 |
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error processing callback");
|
107 |
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error processing callback");
|
| 111 |
}
|
108 |
}
|
| 112 |
}
|
109 |
}
|
| 113 |
|
110 |
|
| 114 |
|
111 |
|
| 115 |
private boolean verifySignature(String payload, String razorpaySignature) throws Exception {
|
- |
|
| 116 |
String actualSignature = HmacSHA256(payload, razorpaySecret);
|
- |
|
| 117 |
return actualSignature.equals(razorpaySignature);
|
- |
|
| 118 |
}
|
- |
|
| 119 |
|
- |
|
| 120 |
private String HmacSHA256(String data, String key) throws Exception {
|
- |
|
| 121 |
Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
|
- |
|
| 122 |
SecretKeySpec secret_key = new SecretKeySpec(key.getBytes(), "HmacSHA256");
|
- |
|
| 123 |
sha256_HMAC.init(secret_key);
|
- |
|
| 124 |
return Base64.getEncoder().encodeToString(sha256_HMAC.doFinal(data.getBytes()));
|
- |
|
| 125 |
}
|
- |
|
| 126 |
|
- |
|
| 127 |
private Map<String, String> getHeadersInfo(HttpServletRequest request) {
|
- |
|
| 128 |
Map<String, String> map = new HashMap<>();
|
- |
|
| 129 |
Enumeration headerNames = request.getHeaderNames();
|
- |
|
| 130 |
while (headerNames.hasMoreElements()) {
|
- |
|
| 131 |
String key = (String) headerNames.nextElement();
|
- |
|
| 132 |
String value = request.getHeader(key);
|
- |
|
| 133 |
map.put(key, value);
|
- |
|
| 134 |
}
|
- |
|
| 135 |
return map;
|
- |
|
| 136 |
}
|
- |
|
| 137 |
|
- |
|
| 138 |
private void updatePaymentStatus(String paymentId, String status, String orderId, String amount) throws ProfitMandiBusinessException {
|
112 |
private void updatePaymentStatus(String paymentId, String status, String orderId, String amount) throws ProfitMandiBusinessException {
|
| 139 |
UpsellRazorpayPaymentStatus upsellRazorpayPaymentStatus = new UpsellRazorpayPaymentStatus();
|
113 |
UpsellRazorpayPaymentStatus upsellRazorpayPaymentStatus = new UpsellRazorpayPaymentStatus();
|
| 140 |
upsellRazorpayPaymentStatus.setCreatedTimestamp(LocalDateTime.now());
|
114 |
upsellRazorpayPaymentStatus.setCreatedTimestamp(LocalDateTime.now());
|
| 141 |
upsellRazorpayPaymentStatus.setOrderId(Integer.parseInt(orderId));
|
115 |
upsellRazorpayPaymentStatus.setOrderId(Integer.parseInt(orderId));
|
| 142 |
upsellRazorpayPaymentStatus.setPaymentId(paymentId);
|
116 |
upsellRazorpayPaymentStatus.setPaymentId(paymentId);
|