Rev 33672 | Rev 33684 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
package com.spice.profitmandi.web.controller;import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;import com.spice.profitmandi.common.web.util.ResponseSender;import com.spice.profitmandi.dao.entity.fofo.UpsellRazorpayPaymentStatus;import com.spice.profitmandi.dao.repository.cs.AgentRecordingRepository;import com.spice.profitmandi.dao.repository.fofo.UpsellRazorpayPaymentStatusRepository;import com.spice.profitmandi.service.integrations.kommuno.KommunoService;import com.spice.profitmandi.service.integrations.smartping.SmartPingService;import com.spice.profitmandi.service.integrations.smartping.model.CallDetailModel;import com.spice.profitmandi.web.util.MVCResponseSender;import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;import org.json.JSONObject;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.http.HttpStatus;import org.springframework.http.MediaType;import org.springframework.http.ResponseEntity;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.ModelAttribute;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import javax.crypto.Mac;import javax.crypto.spec.SecretKeySpec;import javax.servlet.http.HttpServletRequest;import javax.transaction.Transactional;import java.time.LocalDateTime;import java.util.Base64;import java.util.Enumeration;import java.util.HashMap;import java.util.Map;@Controller@Transactional(rollbackOn = Throwable.class)public class WebHookController {@AutowiredMVCResponseSender mvcResponseSender;@Autowiredprivate ResponseSender<?> responseSender;private static final Logger LOGGER = LogManager.getLogger(WebHookController.class);@AutowiredSmartPingService smartPingService;@AutowiredKommunoService kommunoService;@AutowiredAgentRecordingRepository agentRecordingRepository;@RequestMapping(value = "/click2call/report-handler", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)public ResponseEntity<?> click2callReportHandlerPost(HttpServletRequest request, Model model, @ModelAttribute CallDetailModel callDetail) throws Exception {LOGGER.info("first event call detail {}", callDetail);kommunoService.updateAgentRecording(callDetail);return responseSender.ok(true);}@RequestMapping(value = "/click2call/report-handler/recording-url", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)public ResponseEntity<?> click2callReportHandlerUpdateRecordingUrlPost(HttpServletRequest request, Model model, @ModelAttribute CallDetailModel callDetail) throws Exception {LOGGER.info("update call detail {}", callDetail);kommunoService.updateAgentRecordingUrl(callDetail);return responseSender.ok(true);}@Value("${razorpay.account.keySecret}")private String razorpaySecret;@Autowiredprivate UpsellRazorpayPaymentStatusRepository upsellRazorpayPaymentStatusRepository;@RequestMapping(value = "/upsellPayment/callback", method = RequestMethod.POST)public ResponseEntity<String> handleCallback(HttpServletRequest request, @RequestBody String payload) {try {LOGGER.info("webhookcalled {}", 1);// Verify the callback requestMap<String, String> headers = getHeadersInfo(request);String razorpaySignature = headers.get("x-razorpay-signature");boolean isSignatureValid = verifySignature(payload, razorpaySignature);if (!isSignatureValid) {return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Invalid signature");}// Parse the payloadJSONObject jsonObject = new JSONObject(payload);String paymentId = jsonObject.getString("razorpay_payment_id");String status = jsonObject.getString("status");JSONObject notes = jsonObject.getJSONObject("notes");String orderId = notes.getString("orderId");String orderItemId = notes.getString("orderItemId");String planId = notes.getString("planId");JSONObject customer = jsonObject.getJSONObject("customer");String customerName = notes.getString("name");String customerMobile = notes.getString("contact");String customerEmail = notes.getString("email");String insuranceAmount = notes.getString("insuranceAmount");// Process the payment statusif ("captured".equals(status)) {updatePaymentStatus(paymentId, "captured", orderId, insuranceAmount);throw new Exception("Payment successful for ID: " + paymentId);} else {updatePaymentStatus(paymentId, "failed", orderId, insuranceAmount);throw new Exception("Payment failed for ID: " + paymentId);}} catch (Exception e) {e.printStackTrace();return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error processing callback");}}private boolean verifySignature(String payload, String razorpaySignature) throws Exception {String actualSignature = HmacSHA256(payload, razorpaySecret);return actualSignature.equals(razorpaySignature);}private String HmacSHA256(String data, String key) throws Exception {Mac sha256_HMAC = Mac.getInstance("HmacSHA256");SecretKeySpec secret_key = new SecretKeySpec(key.getBytes(), "HmacSHA256");sha256_HMAC.init(secret_key);return Base64.getEncoder().encodeToString(sha256_HMAC.doFinal(data.getBytes()));}private Map<String, String> getHeadersInfo(HttpServletRequest request) {Map<String, String> map = new HashMap<>();Enumeration headerNames = request.getHeaderNames();while (headerNames.hasMoreElements()) {String key = (String) headerNames.nextElement();String value = request.getHeader(key);map.put(key, value);}return map;}private void updatePaymentStatus(String paymentId, String status, String orderId, String amount) throws ProfitMandiBusinessException {UpsellRazorpayPaymentStatus upsellRazorpayPaymentStatus = new UpsellRazorpayPaymentStatus();upsellRazorpayPaymentStatus.setCreatedTimestamp(LocalDateTime.now());upsellRazorpayPaymentStatus.setOrderId(Integer.parseInt(orderId));upsellRazorpayPaymentStatus.setPaymentId(paymentId);upsellRazorpayPaymentStatus.setPaymentStatus(status);upsellRazorpayPaymentStatus.setPayment(Float.parseFloat(amount));upsellRazorpayPaymentStatusRepository.persist(upsellRazorpayPaymentStatus);}}