Subversion Repositories SmartDukaan

Rev

Rev 33688 | Rev 33693 | 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
 
33672 ranu 3
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
33581 ranu 4
import com.spice.profitmandi.common.web.util.ResponseSender;
33672 ranu 5
import com.spice.profitmandi.dao.entity.fofo.UpsellRazorpayPaymentStatus;
33595 ranu 6
import com.spice.profitmandi.dao.repository.cs.AgentRecordingRepository;
33672 ranu 7
import com.spice.profitmandi.dao.repository.fofo.UpsellRazorpayPaymentStatusRepository;
33689 ranu 8
import com.spice.profitmandi.service.integrations.RazorpayPaymentService;
33602 ranu 9
import com.spice.profitmandi.service.integrations.kommuno.KommunoService;
32929 amit.gupta 10
import com.spice.profitmandi.service.integrations.smartping.SmartPingService;
11
import com.spice.profitmandi.service.integrations.smartping.model.CallDetailModel;
32916 amit.gupta 12
import com.spice.profitmandi.web.util.MVCResponseSender;
13
import org.apache.logging.log4j.LogManager;
14
import org.apache.logging.log4j.Logger;
15
import org.springframework.beans.factory.annotation.Autowired;
33672 ranu 16
import org.springframework.beans.factory.annotation.Value;
17
import org.springframework.http.HttpStatus;
33581 ranu 18
import org.springframework.http.MediaType;
19
import org.springframework.http.ResponseEntity;
32916 amit.gupta 20
import org.springframework.stereotype.Controller;
21
import org.springframework.ui.Model;
33591 ranu 22
import org.springframework.web.bind.annotation.ModelAttribute;
32916 amit.gupta 23
import org.springframework.web.bind.annotation.RequestMapping;
24
import org.springframework.web.bind.annotation.RequestMethod;
25
 
26
import javax.servlet.http.HttpServletRequest;
27
import javax.transaction.Transactional;
33672 ranu 28
import java.time.LocalDateTime;
32916 amit.gupta 29
 
30
@Controller
31
@Transactional(rollbackOn = Throwable.class)
32
public class WebHookController {
33
 
34
    @Autowired
35
    MVCResponseSender mvcResponseSender;
36
 
33581 ranu 37
    @Autowired
38
    private ResponseSender<?> responseSender;
39
 
32916 amit.gupta 40
    private static final Logger LOGGER = LogManager.getLogger(WebHookController.class);
41
 
32919 amit.gupta 42
    @Autowired
32929 amit.gupta 43
    SmartPingService smartPingService;
32916 amit.gupta 44
 
33595 ranu 45
    @Autowired
33602 ranu 46
    KommunoService kommunoService;
47
 
48
    @Autowired
33595 ranu 49
    AgentRecordingRepository agentRecordingRepository;
50
 
33590 ranu 51
    @RequestMapping(value = "/click2call/report-handler", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
33591 ranu 52
    public ResponseEntity<?> click2callReportHandlerPost(HttpServletRequest request, Model model, @ModelAttribute CallDetailModel callDetail) throws Exception {
33581 ranu 53
        LOGGER.info("first event call detail {}", callDetail);
33602 ranu 54
        kommunoService.updateAgentRecording(callDetail);
33581 ranu 55
 
56
        return responseSender.ok(true);
32917 amit.gupta 57
    }
33581 ranu 58
 
59
 
33590 ranu 60
    @RequestMapping(value = "/click2call/report-handler/recording-url", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
33591 ranu 61
    public ResponseEntity<?> click2callReportHandlerUpdateRecordingUrlPost(HttpServletRequest request, Model model, @ModelAttribute CallDetailModel callDetail) throws Exception {
33581 ranu 62
        LOGGER.info("update call detail {}", callDetail);
33602 ranu 63
        kommunoService.updateAgentRecordingUrl(callDetail);
33581 ranu 64
        return responseSender.ok(true);
65
    }
33672 ranu 66
 
67
 
68
    @Value("${razorpay.account.keySecret}")
69
    private String razorpaySecret;
70
    @Autowired
71
    private UpsellRazorpayPaymentStatusRepository upsellRazorpayPaymentStatusRepository;
72
 
33684 ranu 73
    @RequestMapping(value = "/upsellPayment/callback", method = RequestMethod.GET)
74
    public ResponseEntity<String> handleCallback(HttpServletRequest request) {
33672 ranu 75
        try {
33688 ranu 76
            LOGGER.info("Webhook called with query parameters");
33684 ranu 77
 
33688 ranu 78
            // Retrieve the Razorpay parameters from the query string
33684 ranu 79
            String paymentId = request.getParameter("razorpay_payment_id");
33688 ranu 80
            String razorpaySignature = request.getParameter("razorpay_signature");
33684 ranu 81
            String orderId = request.getParameter("orderId");
82
            String insuranceAmount = request.getParameter("insuranceAmount");
83
 
33688 ranu 84
            // Check for required parameters
85
            if (paymentId == null || razorpaySignature == null || orderId == null || insuranceAmount == null) {
33684 ranu 86
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Missing required parameters");
87
            }
88
 
89
            // Construct payload for signature verification
33689 ranu 90
            String payload = orderId + "|" + paymentId;
91
            String generated_signature = RazorpayPaymentService
92
                    .calculateRFC2104HMAC(payload, razorpaySecret);
33684 ranu 93
 
33689 ranu 94
            if (!generated_signature.equals(razorpaySignature)) {
33672 ranu 95
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Invalid signature");
33689 ranu 96
 
33672 ranu 97
            }
98
 
33689 ranu 99
 
33688 ranu 100
            // Handle the payment status (you may need to add more logic depending on the status)
101
            updatePaymentStatus(paymentId, "captured", orderId, insuranceAmount);
102
 
103
            return ResponseEntity.ok("Payment successful for ID: " + paymentId);
104
 
33672 ranu 105
        } catch (Exception e) {
106
            e.printStackTrace();
107
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error processing callback");
108
        }
109
    }
110
 
33684 ranu 111
 
33672 ranu 112
    private void updatePaymentStatus(String paymentId, String status, String orderId, String amount) throws ProfitMandiBusinessException {
113
        UpsellRazorpayPaymentStatus upsellRazorpayPaymentStatus = new UpsellRazorpayPaymentStatus();
114
        upsellRazorpayPaymentStatus.setCreatedTimestamp(LocalDateTime.now());
115
        upsellRazorpayPaymentStatus.setOrderId(Integer.parseInt(orderId));
116
        upsellRazorpayPaymentStatus.setPaymentId(paymentId);
117
        upsellRazorpayPaymentStatus.setPaymentStatus(status);
33682 ranu 118
        upsellRazorpayPaymentStatus.setPayment(Float.parseFloat(amount));
33672 ranu 119
        upsellRazorpayPaymentStatusRepository.persist(upsellRazorpayPaymentStatus);
120
    }
32916 amit.gupta 121
}