Subversion Repositories SmartDukaan

Rev

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