Subversion Repositories SmartDukaan

Rev

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