| 32916 |
amit.gupta |
1 |
package com.spice.profitmandi.web.controller;
|
|
|
2 |
|
| 33672 |
ranu |
3 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
| 33581 |
ranu |
4 |
import com.spice.profitmandi.common.web.util.ResponseSender;
|
| 33672 |
ranu |
5 |
import com.spice.profitmandi.dao.entity.fofo.UpsellRazorpayPaymentStatus;
|
| 33595 |
ranu |
6 |
import com.spice.profitmandi.dao.repository.cs.AgentRecordingRepository;
|
| 33672 |
ranu |
7 |
import com.spice.profitmandi.dao.repository.fofo.UpsellRazorpayPaymentStatusRepository;
|
| 33602 |
ranu |
8 |
import com.spice.profitmandi.service.integrations.kommuno.KommunoService;
|
| 32929 |
amit.gupta |
9 |
import com.spice.profitmandi.service.integrations.smartping.SmartPingService;
|
|
|
10 |
import com.spice.profitmandi.service.integrations.smartping.model.CallDetailModel;
|
| 32916 |
amit.gupta |
11 |
import com.spice.profitmandi.web.util.MVCResponseSender;
|
|
|
12 |
import org.apache.logging.log4j.LogManager;
|
|
|
13 |
import org.apache.logging.log4j.Logger;
|
|
|
14 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 33672 |
ranu |
15 |
import org.springframework.beans.factory.annotation.Value;
|
|
|
16 |
import org.springframework.http.HttpStatus;
|
| 33581 |
ranu |
17 |
import org.springframework.http.MediaType;
|
|
|
18 |
import org.springframework.http.ResponseEntity;
|
| 32916 |
amit.gupta |
19 |
import org.springframework.stereotype.Controller;
|
|
|
20 |
import org.springframework.ui.Model;
|
| 33591 |
ranu |
21 |
import org.springframework.web.bind.annotation.ModelAttribute;
|
| 32916 |
amit.gupta |
22 |
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
23 |
import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
24 |
|
| 33672 |
ranu |
25 |
import javax.crypto.Mac;
|
|
|
26 |
import javax.crypto.spec.SecretKeySpec;
|
| 32916 |
amit.gupta |
27 |
import javax.servlet.http.HttpServletRequest;
|
|
|
28 |
import javax.transaction.Transactional;
|
| 33672 |
ranu |
29 |
import java.time.LocalDateTime;
|
|
|
30 |
import java.util.Base64;
|
|
|
31 |
import java.util.Enumeration;
|
|
|
32 |
import java.util.HashMap;
|
|
|
33 |
import java.util.Map;
|
| 32916 |
amit.gupta |
34 |
|
|
|
35 |
@Controller
|
|
|
36 |
@Transactional(rollbackOn = Throwable.class)
|
|
|
37 |
public class WebHookController {
|
|
|
38 |
|
|
|
39 |
@Autowired
|
|
|
40 |
MVCResponseSender mvcResponseSender;
|
|
|
41 |
|
| 33581 |
ranu |
42 |
@Autowired
|
|
|
43 |
private ResponseSender<?> responseSender;
|
|
|
44 |
|
| 32916 |
amit.gupta |
45 |
private static final Logger LOGGER = LogManager.getLogger(WebHookController.class);
|
|
|
46 |
|
| 32919 |
amit.gupta |
47 |
@Autowired
|
| 32929 |
amit.gupta |
48 |
SmartPingService smartPingService;
|
| 32916 |
amit.gupta |
49 |
|
| 33595 |
ranu |
50 |
@Autowired
|
| 33602 |
ranu |
51 |
KommunoService kommunoService;
|
|
|
52 |
|
|
|
53 |
@Autowired
|
| 33595 |
ranu |
54 |
AgentRecordingRepository agentRecordingRepository;
|
|
|
55 |
|
| 33590 |
ranu |
56 |
@RequestMapping(value = "/click2call/report-handler", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
|
| 33591 |
ranu |
57 |
public ResponseEntity<?> click2callReportHandlerPost(HttpServletRequest request, Model model, @ModelAttribute CallDetailModel callDetail) throws Exception {
|
| 33581 |
ranu |
58 |
LOGGER.info("first event call detail {}", callDetail);
|
| 33602 |
ranu |
59 |
kommunoService.updateAgentRecording(callDetail);
|
| 33581 |
ranu |
60 |
|
|
|
61 |
return responseSender.ok(true);
|
| 32917 |
amit.gupta |
62 |
}
|
| 33581 |
ranu |
63 |
|
|
|
64 |
|
| 33590 |
ranu |
65 |
@RequestMapping(value = "/click2call/report-handler/recording-url", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
|
| 33591 |
ranu |
66 |
public ResponseEntity<?> click2callReportHandlerUpdateRecordingUrlPost(HttpServletRequest request, Model model, @ModelAttribute CallDetailModel callDetail) throws Exception {
|
| 33581 |
ranu |
67 |
LOGGER.info("update call detail {}", callDetail);
|
| 33602 |
ranu |
68 |
kommunoService.updateAgentRecordingUrl(callDetail);
|
| 33581 |
ranu |
69 |
return responseSender.ok(true);
|
|
|
70 |
}
|
| 33672 |
ranu |
71 |
|
|
|
72 |
|
|
|
73 |
@Value("${razorpay.account.keySecret}")
|
|
|
74 |
private String razorpaySecret;
|
|
|
75 |
@Autowired
|
|
|
76 |
private UpsellRazorpayPaymentStatusRepository upsellRazorpayPaymentStatusRepository;
|
|
|
77 |
|
| 33684 |
ranu |
78 |
@RequestMapping(value = "/upsellPayment/callback", method = RequestMethod.GET)
|
|
|
79 |
public ResponseEntity<String> handleCallback(HttpServletRequest request) {
|
| 33672 |
ranu |
80 |
try {
|
| 33688 |
ranu |
81 |
LOGGER.info("Webhook called with query parameters");
|
| 33684 |
ranu |
82 |
|
| 33688 |
ranu |
83 |
// Retrieve the Razorpay parameters from the query string
|
| 33684 |
ranu |
84 |
String paymentId = request.getParameter("razorpay_payment_id");
|
| 33688 |
ranu |
85 |
String razorpaySignature = request.getParameter("razorpay_signature");
|
| 33684 |
ranu |
86 |
String orderId = request.getParameter("orderId");
|
|
|
87 |
String insuranceAmount = request.getParameter("insuranceAmount");
|
|
|
88 |
|
| 33688 |
ranu |
89 |
// Check for required parameters
|
|
|
90 |
if (paymentId == null || razorpaySignature == null || orderId == null || insuranceAmount == null) {
|
| 33684 |
ranu |
91 |
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Missing required parameters");
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
// Construct payload for signature verification
|
| 33688 |
ranu |
95 |
String payload = "razorpay_payment_id=" + paymentId + "&orderId=" + orderId + "&insuranceAmount=" + insuranceAmount;
|
| 33684 |
ranu |
96 |
|
| 33688 |
ranu |
97 |
// Verify the signature
|
| 33672 |
ranu |
98 |
boolean isSignatureValid = verifySignature(payload, razorpaySignature);
|
|
|
99 |
if (!isSignatureValid) {
|
|
|
100 |
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Invalid signature");
|
|
|
101 |
}
|
|
|
102 |
|
| 33688 |
ranu |
103 |
// Handle the payment status (you may need to add more logic depending on the status)
|
|
|
104 |
updatePaymentStatus(paymentId, "captured", orderId, insuranceAmount);
|
|
|
105 |
|
|
|
106 |
return ResponseEntity.ok("Payment successful for ID: " + paymentId);
|
|
|
107 |
|
| 33672 |
ranu |
108 |
} catch (Exception e) {
|
|
|
109 |
e.printStackTrace();
|
|
|
110 |
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error processing callback");
|
|
|
111 |
}
|
|
|
112 |
}
|
|
|
113 |
|
| 33684 |
ranu |
114 |
|
| 33672 |
ranu |
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 {
|
|
|
139 |
UpsellRazorpayPaymentStatus upsellRazorpayPaymentStatus = new UpsellRazorpayPaymentStatus();
|
|
|
140 |
upsellRazorpayPaymentStatus.setCreatedTimestamp(LocalDateTime.now());
|
|
|
141 |
upsellRazorpayPaymentStatus.setOrderId(Integer.parseInt(orderId));
|
|
|
142 |
upsellRazorpayPaymentStatus.setPaymentId(paymentId);
|
|
|
143 |
upsellRazorpayPaymentStatus.setPaymentStatus(status);
|
| 33682 |
ranu |
144 |
upsellRazorpayPaymentStatus.setPayment(Float.parseFloat(amount));
|
| 33672 |
ranu |
145 |
upsellRazorpayPaymentStatusRepository.persist(upsellRazorpayPaymentStatus);
|
|
|
146 |
}
|
| 32916 |
amit.gupta |
147 |
}
|