Subversion Repositories SmartDukaan

Rev

Rev 35624 | Rev 35660 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
32916 amit.gupta 1
package com.spice.profitmandi.web.controller;
2
 
35624 ranu 3
import com.fasterxml.jackson.databind.ObjectMapper;
33715 ranu 4
import com.razorpay.Utils;
33672 ranu 5
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
33581 ranu 6
import com.spice.profitmandi.common.web.util.ResponseSender;
33715 ranu 7
import com.spice.profitmandi.dao.entity.fofo.Customer;
8
import com.spice.profitmandi.dao.entity.fofo.FofoOrder;
33672 ranu 9
import com.spice.profitmandi.dao.entity.fofo.UpsellRazorpayPaymentStatus;
33595 ranu 10
import com.spice.profitmandi.dao.repository.cs.AgentRecordingRepository;
33715 ranu 11
import com.spice.profitmandi.dao.repository.fofo.CustomerRepository;
12
import com.spice.profitmandi.dao.repository.fofo.FofoOrderRepository;
33672 ranu 13
import com.spice.profitmandi.dao.repository.fofo.UpsellRazorpayPaymentStatusRepository;
34481 ranu 14
import com.spice.profitmandi.service.integrations.kommuno.RecordingService;
32929 amit.gupta 15
import com.spice.profitmandi.service.integrations.smartping.SmartPingService;
16
import com.spice.profitmandi.service.integrations.smartping.model.CallDetailModel;
34481 ranu 17
import com.spice.profitmandi.service.integrations.smartping.model.PushCallLogModel;
32916 amit.gupta 18
import com.spice.profitmandi.web.util.MVCResponseSender;
19
import org.apache.logging.log4j.LogManager;
20
import org.apache.logging.log4j.Logger;
33715 ranu 21
import org.json.JSONObject;
32916 amit.gupta 22
import org.springframework.beans.factory.annotation.Autowired;
33672 ranu 23
import org.springframework.beans.factory.annotation.Value;
24
import org.springframework.http.HttpStatus;
33581 ranu 25
import org.springframework.http.MediaType;
26
import org.springframework.http.ResponseEntity;
32916 amit.gupta 27
import org.springframework.stereotype.Controller;
35624 ranu 28
import org.springframework.transaction.annotation.Transactional;
32916 amit.gupta 29
import org.springframework.ui.Model;
35654 ranu 30
import org.springframework.web.bind.annotation.*;
32916 amit.gupta 31
 
32
import javax.servlet.http.HttpServletRequest;
33
 
34
@Controller
35458 amit 35
@Transactional(rollbackFor = Throwable.class)
32916 amit.gupta 36
public class WebHookController {
37
 
38
    @Autowired
39
    MVCResponseSender mvcResponseSender;
40
 
33581 ranu 41
    @Autowired
42
    private ResponseSender<?> responseSender;
43
 
33715 ranu 44
    @Autowired
45
    private FofoOrderRepository fofoOrderRepository;
46
 
47
    @Autowired
48
    private CustomerRepository customerRepository;
49
 
32916 amit.gupta 50
    private static final Logger LOGGER = LogManager.getLogger(WebHookController.class);
51
 
32919 amit.gupta 52
    @Autowired
32929 amit.gupta 53
    SmartPingService smartPingService;
32916 amit.gupta 54
 
33595 ranu 55
    @Autowired
34481 ranu 56
    RecordingService recordingService;
33602 ranu 57
 
58
    @Autowired
33595 ranu 59
    AgentRecordingRepository agentRecordingRepository;
60
 
33581 ranu 61
 
35654 ranu 62
    /*
35624 ranu 63
     * URL: POST /click2call/report-handler/recording-url
64
     * Content-Type: application/x-www-form-urlencoded
65
     * Third-party: Knowlarity
66
     */
33590 ranu 67
    @RequestMapping(value = "/click2call/report-handler/recording-url", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
33591 ranu 68
    public ResponseEntity<?> click2callReportHandlerUpdateRecordingUrlPost(HttpServletRequest request, Model model, @ModelAttribute CallDetailModel callDetail) throws Exception {
35624 ranu 69
        // Log raw parameters from Knowlarity before processing, so we can see exactly what they sent
70
        LOGGER.info("recording-url raw params from Knowlarity: {}", request.getParameterMap());
71
        try {
72
            LOGGER.info("update call detail (mapped): {}", callDetail);
73
            recordingService.updateAgentRecordingUrl(callDetail);
74
        } catch (Exception e) {
75
            LOGGER.error("Error processing recording-url webhook. Raw params: {}", request.getParameterMap(), e);
76
        }
33581 ranu 77
        return responseSender.ok(true);
78
    }
33672 ranu 79
 
35624 ranu 80
    /**
81
     * Knowlarity Click2Call Webhook - Push Call Log.
82
     */
35654 ranu 83
    @RequestMapping(value = "/click2call/push-call-log-handler/{srNumber}", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
84
    public ResponseEntity<?> click2callPushLogHandler(HttpServletRequest request, @RequestBody String rawBody, @PathVariable String srNumber) throws Exception {
35624 ranu 85
        // Log raw JSON body from Knowlarity before processing, so we can see exactly what they sent
86
        LOGGER.info("push-call-log raw JSON from Knowlarity: {}", rawBody);
87
        try {
88
            ObjectMapper objectMapper = new ObjectMapper();
89
            PushCallLogModel pushCallLogModel = objectMapper.readValue(rawBody, PushCallLogModel.class);
90
            LOGGER.info("update call detail - push log (mapped): {}", pushCallLogModel);
91
            recordingService.updateAgentCallLog(pushCallLogModel);
92
        } catch (Exception e) {
93
            LOGGER.error("Error processing push-call-log webhook. Raw JSON: {}", rawBody, e);
94
        }
34530 ranu 95
        return responseSender.ok("true");
34481 ranu 96
    }
33672 ranu 97
 
34481 ranu 98
 
33672 ranu 99
    @Value("${razorpay.account.keySecret}")
100
    private String razorpaySecret;
101
    @Autowired
102
    private UpsellRazorpayPaymentStatusRepository upsellRazorpayPaymentStatusRepository;
103
 
33684 ranu 104
    @RequestMapping(value = "/upsellPayment/callback", method = RequestMethod.GET)
33715 ranu 105
    public ResponseEntity<?> handleCallback(HttpServletRequest request) {
33672 ranu 106
        try {
33688 ranu 107
            // Retrieve the Razorpay parameters from the query string
33684 ranu 108
            String paymentId = request.getParameter("razorpay_payment_id");
33688 ranu 109
            String razorpaySignature = request.getParameter("razorpay_signature");
33693 ranu 110
            String paymentLinkId = request.getParameter("razorpay_payment_link_id");
33715 ranu 111
            String paymentLinkStatus = request.getParameter("razorpay_payment_link_status");
112
            String paymentLinkReferenceId = request.getParameter("razorpay_payment_link_reference_id");
33693 ranu 113
 
33684 ranu 114
            String orderId = request.getParameter("orderId");
115
            String insuranceAmount = request.getParameter("insuranceAmount");
116
 
33688 ranu 117
            // Check for required parameters
118
            if (paymentId == null || razorpaySignature == null || orderId == null || insuranceAmount == null) {
33684 ranu 119
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Missing required parameters");
120
            }
121
 
33715 ranu 122
            JSONObject options = new JSONObject();
123
            options.put("payment_link_reference_id", paymentLinkReferenceId);
124
            options.put("razorpay_payment_id", paymentId);
125
            options.put("payment_link_status", paymentLinkStatus);
126
            options.put("payment_link_id", paymentLinkId);
127
            options.put("razorpay_signature", razorpaySignature);
33684 ranu 128
 
33715 ranu 129
            boolean status = Utils.verifyPaymentLink(options, razorpaySecret);
33693 ranu 130
 
33715 ranu 131
            LOGGER.info("status signature {}", status);
132
 
133
            if (!status) {
33672 ranu 134
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Invalid signature");
135
            }
33715 ranu 136
            FofoOrder fofoOrder = fofoOrderRepository.selectByOrderId(Integer.parseInt(orderId));
137
            Customer customer = customerRepository.selectById(fofoOrder.getCustomerId());
138
            // Handle the payment status (you may need to add more logic depending on the status)
139
            updatePaymentStatus(paymentId, paymentLinkReferenceId, "captured", orderId, insuranceAmount);
33672 ranu 140
 
33715 ranu 141
            // Construct the HTML response
142
            String htmlResponse = "<html>" +
143
                    "<head>" +
144
                    "<style>" +
145
                    "  .container { background-color: #f0f0f0; padding: 20px; text-align: center; border-radius: 8px;max-width:600px;width:auto;margin:10px auto; }" +
146
                    "  .success-icon { color: green; font-size:30px; margin-right: 10px;border-radius: 50%;border: 2px solid;padding: 1px 7px; }" +
147
                    "  .message { font-size: 18px; margin-top: 10px; }" +
148
                    "</style>" +
149
                    "</head>" +
150
                    "<body>" +
151
                    "  <div class='container'>" +
152
                    "    <span class='success-icon'>&#10003;</span>" +
153
                    "    <div class='message'>" +
154
                    "      Hi " + customer.getFirstName() + ",<br>" +
155
                    "      Your payment of " + insuranceAmount + " was successfully completed.<br>" +
156
                    "      Your payment ID is: " + paymentId + "." +
157
                    "    </div>" +
158
                    "  </div>" +
159
                    "</body>" +
160
                    "</html>";
33689 ranu 161
 
33715 ranu 162
            return ResponseEntity.ok().contentType(MediaType.TEXT_HTML).body(htmlResponse);
163
 
33672 ranu 164
        } catch (Exception e) {
165
            e.printStackTrace();
33715 ranu 166
 
33672 ranu 167
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error processing callback");
168
        }
169
    }
170
 
33684 ranu 171
 
33715 ranu 172
    private void updatePaymentStatus(String paymentId, String paymentLinkReferenceId, String status, String orderId, String amount) throws ProfitMandiBusinessException {
173
        int id = Integer.parseInt(paymentLinkReferenceId.replaceFirst("^0+(?!$)", ""));
174
        UpsellRazorpayPaymentStatus upsellRazorpayPaymentStatus = upsellRazorpayPaymentStatusRepository.selectById(id);
33672 ranu 175
        upsellRazorpayPaymentStatus.setPaymentId(paymentId);
33715 ranu 176
        upsellRazorpayPaymentStatus.setReferenceId(paymentLinkReferenceId);
33672 ranu 177
        upsellRazorpayPaymentStatus.setPaymentStatus(status);
33682 ranu 178
        upsellRazorpayPaymentStatus.setPayment(Float.parseFloat(amount));
33672 ranu 179
    }
32916 amit.gupta 180
}