| 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;
|
| 33672 |
ranu |
14 |
import org.json.JSONObject;
|
| 32916 |
amit.gupta |
15 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 33672 |
ranu |
16 |
import org.springframework.beans.factory.annotation.Value;
|
|
|
17 |
import org.springframework.http.HttpStatus;
|
| 33581 |
ranu |
18 |
import org.springframework.http.MediaType;
|
|
|
19 |
import org.springframework.http.ResponseEntity;
|
| 32916 |
amit.gupta |
20 |
import org.springframework.stereotype.Controller;
|
|
|
21 |
import org.springframework.ui.Model;
|
| 33591 |
ranu |
22 |
import org.springframework.web.bind.annotation.ModelAttribute;
|
| 32916 |
amit.gupta |
23 |
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
24 |
import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
25 |
|
| 33672 |
ranu |
26 |
import javax.crypto.Mac;
|
|
|
27 |
import javax.crypto.spec.SecretKeySpec;
|
| 32916 |
amit.gupta |
28 |
import javax.servlet.http.HttpServletRequest;
|
|
|
29 |
import javax.transaction.Transactional;
|
| 33672 |
ranu |
30 |
import java.time.LocalDateTime;
|
|
|
31 |
import java.util.Base64;
|
|
|
32 |
import java.util.Enumeration;
|
|
|
33 |
import java.util.HashMap;
|
|
|
34 |
import java.util.Map;
|
| 32916 |
amit.gupta |
35 |
|
|
|
36 |
@Controller
|
|
|
37 |
@Transactional(rollbackOn = Throwable.class)
|
|
|
38 |
public class WebHookController {
|
|
|
39 |
|
|
|
40 |
@Autowired
|
|
|
41 |
MVCResponseSender mvcResponseSender;
|
|
|
42 |
|
| 33581 |
ranu |
43 |
@Autowired
|
|
|
44 |
private ResponseSender<?> responseSender;
|
|
|
45 |
|
| 32916 |
amit.gupta |
46 |
private static final Logger LOGGER = LogManager.getLogger(WebHookController.class);
|
|
|
47 |
|
| 32919 |
amit.gupta |
48 |
@Autowired
|
| 32929 |
amit.gupta |
49 |
SmartPingService smartPingService;
|
| 32916 |
amit.gupta |
50 |
|
| 33595 |
ranu |
51 |
@Autowired
|
| 33602 |
ranu |
52 |
KommunoService kommunoService;
|
|
|
53 |
|
|
|
54 |
@Autowired
|
| 33595 |
ranu |
55 |
AgentRecordingRepository agentRecordingRepository;
|
|
|
56 |
|
| 33590 |
ranu |
57 |
@RequestMapping(value = "/click2call/report-handler", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
|
| 33591 |
ranu |
58 |
public ResponseEntity<?> click2callReportHandlerPost(HttpServletRequest request, Model model, @ModelAttribute CallDetailModel callDetail) throws Exception {
|
| 33581 |
ranu |
59 |
LOGGER.info("first event call detail {}", callDetail);
|
| 33602 |
ranu |
60 |
kommunoService.updateAgentRecording(callDetail);
|
| 33581 |
ranu |
61 |
|
|
|
62 |
return responseSender.ok(true);
|
| 32917 |
amit.gupta |
63 |
}
|
| 33581 |
ranu |
64 |
|
|
|
65 |
|
| 33590 |
ranu |
66 |
@RequestMapping(value = "/click2call/report-handler/recording-url", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
|
| 33591 |
ranu |
67 |
public ResponseEntity<?> click2callReportHandlerUpdateRecordingUrlPost(HttpServletRequest request, Model model, @ModelAttribute CallDetailModel callDetail) throws Exception {
|
| 33581 |
ranu |
68 |
LOGGER.info("update call detail {}", callDetail);
|
| 33602 |
ranu |
69 |
kommunoService.updateAgentRecordingUrl(callDetail);
|
| 33581 |
ranu |
70 |
return responseSender.ok(true);
|
|
|
71 |
}
|
| 33672 |
ranu |
72 |
|
|
|
73 |
|
|
|
74 |
@Value("${razorpay.account.keySecret}")
|
|
|
75 |
private String razorpaySecret;
|
|
|
76 |
@Autowired
|
|
|
77 |
private UpsellRazorpayPaymentStatusRepository upsellRazorpayPaymentStatusRepository;
|
|
|
78 |
|
| 33684 |
ranu |
79 |
@RequestMapping(value = "/upsellPayment/callback", method = RequestMethod.GET)
|
|
|
80 |
public ResponseEntity<String> handleCallback(HttpServletRequest request) {
|
| 33672 |
ranu |
81 |
try {
|
| 33684 |
ranu |
82 |
LOGGER.info("webhook called {}", 1);
|
|
|
83 |
|
| 33672 |
ranu |
84 |
// Verify the callback request
|
|
|
85 |
Map<String, String> headers = getHeadersInfo(request);
|
|
|
86 |
String razorpaySignature = headers.get("x-razorpay-signature");
|
| 33684 |
ranu |
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();
|
| 33672 |
ranu |
112 |
boolean isSignatureValid = verifySignature(payload, razorpaySignature);
|
|
|
113 |
|
|
|
114 |
if (!isSignatureValid) {
|
|
|
115 |
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Invalid signature");
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
// Process the payment status
|
|
|
119 |
if ("captured".equals(status)) {
|
|
|
120 |
updatePaymentStatus(paymentId, "captured", orderId, insuranceAmount);
|
| 33684 |
ranu |
121 |
return ResponseEntity.ok("Payment successful for ID: " + paymentId);
|
| 33672 |
ranu |
122 |
} else {
|
|
|
123 |
updatePaymentStatus(paymentId, "failed", orderId, insuranceAmount);
|
| 33684 |
ranu |
124 |
return ResponseEntity.ok("Payment failed for ID: " + paymentId);
|
| 33672 |
ranu |
125 |
}
|
|
|
126 |
} catch (Exception e) {
|
|
|
127 |
e.printStackTrace();
|
|
|
128 |
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error processing callback");
|
|
|
129 |
}
|
|
|
130 |
}
|
|
|
131 |
|
| 33684 |
ranu |
132 |
|
| 33672 |
ranu |
133 |
private boolean verifySignature(String payload, String razorpaySignature) throws Exception {
|
|
|
134 |
String actualSignature = HmacSHA256(payload, razorpaySecret);
|
|
|
135 |
return actualSignature.equals(razorpaySignature);
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
private String HmacSHA256(String data, String key) throws Exception {
|
|
|
139 |
Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
|
|
|
140 |
SecretKeySpec secret_key = new SecretKeySpec(key.getBytes(), "HmacSHA256");
|
|
|
141 |
sha256_HMAC.init(secret_key);
|
|
|
142 |
return Base64.getEncoder().encodeToString(sha256_HMAC.doFinal(data.getBytes()));
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
private Map<String, String> getHeadersInfo(HttpServletRequest request) {
|
|
|
146 |
Map<String, String> map = new HashMap<>();
|
|
|
147 |
Enumeration headerNames = request.getHeaderNames();
|
|
|
148 |
while (headerNames.hasMoreElements()) {
|
|
|
149 |
String key = (String) headerNames.nextElement();
|
|
|
150 |
String value = request.getHeader(key);
|
|
|
151 |
map.put(key, value);
|
|
|
152 |
}
|
|
|
153 |
return map;
|
|
|
154 |
}
|
|
|
155 |
|
|
|
156 |
private void updatePaymentStatus(String paymentId, String status, String orderId, String amount) throws ProfitMandiBusinessException {
|
|
|
157 |
UpsellRazorpayPaymentStatus upsellRazorpayPaymentStatus = new UpsellRazorpayPaymentStatus();
|
|
|
158 |
upsellRazorpayPaymentStatus.setCreatedTimestamp(LocalDateTime.now());
|
|
|
159 |
upsellRazorpayPaymentStatus.setOrderId(Integer.parseInt(orderId));
|
|
|
160 |
upsellRazorpayPaymentStatus.setPaymentId(paymentId);
|
|
|
161 |
upsellRazorpayPaymentStatus.setPaymentStatus(status);
|
| 33682 |
ranu |
162 |
upsellRazorpayPaymentStatus.setPayment(Float.parseFloat(amount));
|
| 33672 |
ranu |
163 |
upsellRazorpayPaymentStatusRepository.persist(upsellRazorpayPaymentStatus);
|
|
|
164 |
}
|
| 32916 |
amit.gupta |
165 |
}
|