Subversion Repositories SmartDukaan

Rev

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