Subversion Repositories SmartDukaan

Rev

Rev 33602 | Rev 33682 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 33602 Rev 33672
Line 1... Line 1...
1
package com.spice.profitmandi.web.controller;
1
package com.spice.profitmandi.web.controller;
2
 
2
 
-
 
3
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
3
import com.spice.profitmandi.common.web.util.ResponseSender;
4
import com.spice.profitmandi.common.web.util.ResponseSender;
-
 
5
import com.spice.profitmandi.dao.entity.fofo.UpsellRazorpayPaymentStatus;
4
import com.spice.profitmandi.dao.repository.cs.AgentRecordingRepository;
6
import com.spice.profitmandi.dao.repository.cs.AgentRecordingRepository;
-
 
7
import com.spice.profitmandi.dao.repository.fofo.UpsellRazorpayPaymentStatusRepository;
5
import com.spice.profitmandi.service.integrations.kommuno.KommunoService;
8
import com.spice.profitmandi.service.integrations.kommuno.KommunoService;
6
import com.spice.profitmandi.service.integrations.smartping.SmartPingService;
9
import com.spice.profitmandi.service.integrations.smartping.SmartPingService;
7
import com.spice.profitmandi.service.integrations.smartping.model.CallDetailModel;
10
import com.spice.profitmandi.service.integrations.smartping.model.CallDetailModel;
8
import com.spice.profitmandi.web.util.MVCResponseSender;
11
import com.spice.profitmandi.web.util.MVCResponseSender;
9
import org.apache.logging.log4j.LogManager;
12
import org.apache.logging.log4j.LogManager;
10
import org.apache.logging.log4j.Logger;
13
import org.apache.logging.log4j.Logger;
-
 
14
import org.json.JSONObject;
11
import org.springframework.beans.factory.annotation.Autowired;
15
import org.springframework.beans.factory.annotation.Autowired;
-
 
16
import org.springframework.beans.factory.annotation.Value;
-
 
17
import org.springframework.http.HttpStatus;
12
import org.springframework.http.MediaType;
18
import org.springframework.http.MediaType;
13
import org.springframework.http.ResponseEntity;
19
import org.springframework.http.ResponseEntity;
14
import org.springframework.stereotype.Controller;
20
import org.springframework.stereotype.Controller;
15
import org.springframework.ui.Model;
21
import org.springframework.ui.Model;
16
import org.springframework.web.bind.annotation.ModelAttribute;
22
import org.springframework.web.bind.annotation.ModelAttribute;
-
 
23
import org.springframework.web.bind.annotation.RequestBody;
17
import org.springframework.web.bind.annotation.RequestMapping;
24
import org.springframework.web.bind.annotation.RequestMapping;
18
import org.springframework.web.bind.annotation.RequestMethod;
25
import org.springframework.web.bind.annotation.RequestMethod;
19
 
26
 
-
 
27
import javax.crypto.Mac;
-
 
28
import javax.crypto.spec.SecretKeySpec;
20
import javax.servlet.http.HttpServletRequest;
29
import javax.servlet.http.HttpServletRequest;
21
import javax.transaction.Transactional;
30
import javax.transaction.Transactional;
-
 
31
import java.time.LocalDateTime;
-
 
32
import java.util.Base64;
-
 
33
import java.util.Enumeration;
-
 
34
import java.util.HashMap;
-
 
35
import java.util.Map;
22
 
36
 
23
@Controller
37
@Controller
24
@Transactional(rollbackOn = Throwable.class)
38
@Transactional(rollbackOn = Throwable.class)
25
public class WebHookController {
39
public class WebHookController {
26
 
40
 
Line 54... Line 68...
54
    public ResponseEntity<?> click2callReportHandlerUpdateRecordingUrlPost(HttpServletRequest request, Model model, @ModelAttribute CallDetailModel callDetail) throws Exception {
68
    public ResponseEntity<?> click2callReportHandlerUpdateRecordingUrlPost(HttpServletRequest request, Model model, @ModelAttribute CallDetailModel callDetail) throws Exception {
55
        LOGGER.info("update call detail {}", callDetail);
69
        LOGGER.info("update call detail {}", callDetail);
56
        kommunoService.updateAgentRecordingUrl(callDetail);
70
        kommunoService.updateAgentRecordingUrl(callDetail);
57
        return responseSender.ok(true);
71
        return responseSender.ok(true);
58
    }
72
    }
-
 
73
 
-
 
74
 
-
 
75
    @Value("${razorpay.account.keySecret}")
-
 
76
    private String razorpaySecret;
-
 
77
    @Autowired
-
 
78
    private UpsellRazorpayPaymentStatusRepository upsellRazorpayPaymentStatusRepository;
-
 
79
 
-
 
80
    @RequestMapping(value = "/upsellPayment/callback", method = RequestMethod.POST)
-
 
81
    public ResponseEntity<String> handleCallback(HttpServletRequest request, @RequestBody String payload) {
-
 
82
        try {
-
 
83
            // Verify the callback request
-
 
84
            Map<String, String> headers = getHeadersInfo(request);
-
 
85
            String razorpaySignature = headers.get("x-razorpay-signature");
-
 
86
            boolean isSignatureValid = verifySignature(payload, razorpaySignature);
-
 
87
 
-
 
88
            if (!isSignatureValid) {
-
 
89
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Invalid signature");
-
 
90
            }
-
 
91
 
-
 
92
            // Parse the payload
-
 
93
            JSONObject jsonObject = new JSONObject(payload);
-
 
94
            String paymentId = jsonObject.getString("razorpay_payment_id");
-
 
95
            String status = jsonObject.getString("status");
-
 
96
 
-
 
97
            JSONObject notes = jsonObject.getJSONObject("notes");
-
 
98
            String orderId = notes.getString("orderId");
-
 
99
            String orderItemId = notes.getString("orderItemId");
-
 
100
            String planId = notes.getString("planId");
-
 
101
 
-
 
102
            JSONObject customer = jsonObject.getJSONObject("customer");
-
 
103
            String customerName = notes.getString("name");
-
 
104
            String customerMobile = notes.getString("contact");
-
 
105
            String customerEmail = notes.getString("email");
-
 
106
            String insuranceAmount = notes.getString("insuranceAmount");
-
 
107
 
-
 
108
            // Process the payment status
-
 
109
            if ("captured".equals(status)) {
-
 
110
                updatePaymentStatus(paymentId, "captured", orderId, insuranceAmount);
-
 
111
                throw new Exception("Payment successful for ID: " + paymentId);
-
 
112
 
-
 
113
            } else {
-
 
114
                updatePaymentStatus(paymentId, "failed", orderId, insuranceAmount);
-
 
115
                throw new Exception("Payment failed for ID: " + paymentId);
-
 
116
 
-
 
117
            }
-
 
118
 
-
 
119
        } catch (Exception e) {
-
 
120
            e.printStackTrace();
-
 
121
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error processing callback");
-
 
122
        }
-
 
123
    }
-
 
124
 
-
 
125
    private boolean verifySignature(String payload, String razorpaySignature) throws Exception {
-
 
126
        String actualSignature = HmacSHA256(payload, razorpaySecret);
-
 
127
        return actualSignature.equals(razorpaySignature);
-
 
128
    }
-
 
129
 
-
 
130
    private String HmacSHA256(String data, String key) throws Exception {
-
 
131
        Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
-
 
132
        SecretKeySpec secret_key = new SecretKeySpec(key.getBytes(), "HmacSHA256");
-
 
133
        sha256_HMAC.init(secret_key);
-
 
134
        return Base64.getEncoder().encodeToString(sha256_HMAC.doFinal(data.getBytes()));
-
 
135
    }
-
 
136
 
-
 
137
    private Map<String, String> getHeadersInfo(HttpServletRequest request) {
-
 
138
        Map<String, String> map = new HashMap<>();
-
 
139
        Enumeration headerNames = request.getHeaderNames();
-
 
140
        while (headerNames.hasMoreElements()) {
-
 
141
            String key = (String) headerNames.nextElement();
-
 
142
            String value = request.getHeader(key);
-
 
143
            map.put(key, value);
-
 
144
        }
-
 
145
        return map;
-
 
146
    }
-
 
147
 
-
 
148
    private void updatePaymentStatus(String paymentId, String status, String orderId, String amount) throws ProfitMandiBusinessException {
-
 
149
        UpsellRazorpayPaymentStatus upsellRazorpayPaymentStatus = new UpsellRazorpayPaymentStatus();
-
 
150
        upsellRazorpayPaymentStatus.setCreatedTimestamp(LocalDateTime.now());
-
 
151
        upsellRazorpayPaymentStatus.setOrderId(Integer.parseInt(orderId));
-
 
152
        upsellRazorpayPaymentStatus.setPaymentId(paymentId);
-
 
153
        upsellRazorpayPaymentStatus.setPaymentStatus(status);
-
 
154
        upsellRazorpayPaymentStatusRepository.persist(upsellRazorpayPaymentStatus);
-
 
155
    }
59
}
156
}