Subversion Repositories SmartDukaan

Rev

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

Rev 33693 Rev 33715
Line 1... Line 1...
1
package com.spice.profitmandi.web.controller;
1
package com.spice.profitmandi.web.controller;
2
 
2
 
-
 
3
import com.razorpay.Utils;
3
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
4
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
4
import com.spice.profitmandi.common.web.util.ResponseSender;
5
import com.spice.profitmandi.common.web.util.ResponseSender;
-
 
6
import com.spice.profitmandi.dao.entity.fofo.Customer;
-
 
7
import com.spice.profitmandi.dao.entity.fofo.FofoOrder;
5
import com.spice.profitmandi.dao.entity.fofo.UpsellRazorpayPaymentStatus;
8
import com.spice.profitmandi.dao.entity.fofo.UpsellRazorpayPaymentStatus;
6
import com.spice.profitmandi.dao.repository.cs.AgentRecordingRepository;
9
import com.spice.profitmandi.dao.repository.cs.AgentRecordingRepository;
-
 
10
import com.spice.profitmandi.dao.repository.fofo.CustomerRepository;
-
 
11
import com.spice.profitmandi.dao.repository.fofo.FofoOrderRepository;
7
import com.spice.profitmandi.dao.repository.fofo.UpsellRazorpayPaymentStatusRepository;
12
import com.spice.profitmandi.dao.repository.fofo.UpsellRazorpayPaymentStatusRepository;
8
import com.spice.profitmandi.service.integrations.RazorpayPaymentService;
-
 
9
import com.spice.profitmandi.service.integrations.kommuno.KommunoService;
13
import com.spice.profitmandi.service.integrations.kommuno.KommunoService;
10
import com.spice.profitmandi.service.integrations.smartping.SmartPingService;
14
import com.spice.profitmandi.service.integrations.smartping.SmartPingService;
11
import com.spice.profitmandi.service.integrations.smartping.model.CallDetailModel;
15
import com.spice.profitmandi.service.integrations.smartping.model.CallDetailModel;
12
import com.spice.profitmandi.web.util.MVCResponseSender;
16
import com.spice.profitmandi.web.util.MVCResponseSender;
13
import org.apache.logging.log4j.LogManager;
17
import org.apache.logging.log4j.LogManager;
14
import org.apache.logging.log4j.Logger;
18
import org.apache.logging.log4j.Logger;
-
 
19
import org.json.JSONObject;
15
import org.springframework.beans.factory.annotation.Autowired;
20
import org.springframework.beans.factory.annotation.Autowired;
16
import org.springframework.beans.factory.annotation.Value;
21
import org.springframework.beans.factory.annotation.Value;
17
import org.springframework.http.HttpStatus;
22
import org.springframework.http.HttpStatus;
18
import org.springframework.http.MediaType;
23
import org.springframework.http.MediaType;
19
import org.springframework.http.ResponseEntity;
24
import org.springframework.http.ResponseEntity;
Line 23... Line 28...
23
import org.springframework.web.bind.annotation.RequestMapping;
28
import org.springframework.web.bind.annotation.RequestMapping;
24
import org.springframework.web.bind.annotation.RequestMethod;
29
import org.springframework.web.bind.annotation.RequestMethod;
25
 
30
 
26
import javax.servlet.http.HttpServletRequest;
31
import javax.servlet.http.HttpServletRequest;
27
import javax.transaction.Transactional;
32
import javax.transaction.Transactional;
28
import java.time.LocalDateTime;
-
 
29
 
33
 
30
@Controller
34
@Controller
31
@Transactional(rollbackOn = Throwable.class)
35
@Transactional(rollbackOn = Throwable.class)
32
public class WebHookController {
36
public class WebHookController {
33
 
37
 
Line 35... Line 39...
35
    MVCResponseSender mvcResponseSender;
39
    MVCResponseSender mvcResponseSender;
36
 
40
 
37
    @Autowired
41
    @Autowired
38
    private ResponseSender<?> responseSender;
42
    private ResponseSender<?> responseSender;
39
 
43
 
-
 
44
    @Autowired
-
 
45
    private FofoOrderRepository fofoOrderRepository;
-
 
46
 
-
 
47
    @Autowired
-
 
48
    private CustomerRepository customerRepository;
-
 
49
 
40
    private static final Logger LOGGER = LogManager.getLogger(WebHookController.class);
50
    private static final Logger LOGGER = LogManager.getLogger(WebHookController.class);
41
 
51
 
42
    @Autowired
52
    @Autowired
43
    SmartPingService smartPingService;
53
    SmartPingService smartPingService;
44
 
54
 
Line 69... Line 79...
69
    private String razorpaySecret;
79
    private String razorpaySecret;
70
    @Autowired
80
    @Autowired
71
    private UpsellRazorpayPaymentStatusRepository upsellRazorpayPaymentStatusRepository;
81
    private UpsellRazorpayPaymentStatusRepository upsellRazorpayPaymentStatusRepository;
72
 
82
 
73
    @RequestMapping(value = "/upsellPayment/callback", method = RequestMethod.GET)
83
    @RequestMapping(value = "/upsellPayment/callback", method = RequestMethod.GET)
74
    public ResponseEntity<String> handleCallback(HttpServletRequest request) {
84
    public ResponseEntity<?> handleCallback(HttpServletRequest request) {
75
        try {
85
        try {
76
 
-
 
77
            // Retrieve the Razorpay parameters from the query string
86
            // Retrieve the Razorpay parameters from the query string
78
            String paymentId = request.getParameter("razorpay_payment_id");
87
            String paymentId = request.getParameter("razorpay_payment_id");
79
            String razorpaySignature = request.getParameter("razorpay_signature");
88
            String razorpaySignature = request.getParameter("razorpay_signature");
80
            String paymentLinkId = request.getParameter("razorpay_payment_link_id");
89
            String paymentLinkId = request.getParameter("razorpay_payment_link_id");
81
            String paymentLinkStatus = request.getParameter("payment_link_status");
90
            String paymentLinkStatus = request.getParameter("razorpay_payment_link_status");
82
            String paymentLinkReferenceId = request.getParameter("payment_link_reference_id");
91
            String paymentLinkReferenceId = request.getParameter("razorpay_payment_link_reference_id");
83
 
92
 
84
            String orderId = request.getParameter("orderId");
93
            String orderId = request.getParameter("orderId");
85
            String insuranceAmount = request.getParameter("insuranceAmount");
94
            String insuranceAmount = request.getParameter("insuranceAmount");
86
 
95
 
87
            // Check for required parameters
96
            // Check for required parameters
88
            if (paymentId == null || razorpaySignature == null || orderId == null || insuranceAmount == null) {
97
            if (paymentId == null || razorpaySignature == null || orderId == null || insuranceAmount == null) {
89
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Missing required parameters");
98
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Missing required parameters");
90
            }
99
            }
91
 
100
 
-
 
101
            JSONObject options = new JSONObject();
92
            String generated_signature = RazorpayPaymentService
102
            options.put("payment_link_reference_id", paymentLinkReferenceId);
93
                    .calculateRFC2104HMAC(paymentLinkId + "|" + paymentId, razorpaySecret);
103
            options.put("razorpay_payment_id", paymentId);
94
            LOGGER.info("generated_signature {}", generated_signature);
104
            options.put("payment_link_status", paymentLinkStatus);
95
            if (generated_signature.equals(razorpaySignature)) {
105
            options.put("payment_link_id", paymentLinkId);
96
                // Handle the payment status (you may need to add more logic depending on the status)
106
            options.put("razorpay_signature", razorpaySignature);
-
 
107
 
97
                updatePaymentStatus(paymentId, "captured", orderId, insuranceAmount);
108
            boolean status = Utils.verifyPaymentLink(options, razorpaySecret);
98
 
109
 
99
                return ResponseEntity.ok("Payment successful for ID: " + paymentId);
110
            LOGGER.info("status signature {}", status);
100
 
111
 
101
            } else {
112
            if (!status) {
102
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Invalid signature");
113
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Invalid signature");
103
            }
114
            }
-
 
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);
-
 
119
 
-
 
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>";
104
 
140
 
-
 
141
            return ResponseEntity.ok().contentType(MediaType.TEXT_HTML).body(htmlResponse);
105
 
142
 
106
        } catch (Exception e) {
143
        } catch (Exception e) {
107
            e.printStackTrace();
144
            e.printStackTrace();
-
 
145
 
108
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error processing callback");
146
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error processing callback");
109
        }
147
        }
110
    }
148
    }
111
 
149
 
112
 
150
 
113
    private void updatePaymentStatus(String paymentId, String status, String orderId, String amount) throws ProfitMandiBusinessException {
151
    private void updatePaymentStatus(String paymentId, String paymentLinkReferenceId, String status, String orderId, String amount) throws ProfitMandiBusinessException {
114
        UpsellRazorpayPaymentStatus upsellRazorpayPaymentStatus = new UpsellRazorpayPaymentStatus();
-
 
115
        upsellRazorpayPaymentStatus.setCreatedTimestamp(LocalDateTime.now());
152
        int id = Integer.parseInt(paymentLinkReferenceId.replaceFirst("^0+(?!$)", ""));
116
        upsellRazorpayPaymentStatus.setOrderId(Integer.parseInt(orderId));
153
        UpsellRazorpayPaymentStatus upsellRazorpayPaymentStatus = upsellRazorpayPaymentStatusRepository.selectById(id);
117
        upsellRazorpayPaymentStatus.setPaymentId(paymentId);
154
        upsellRazorpayPaymentStatus.setPaymentId(paymentId);
-
 
155
        upsellRazorpayPaymentStatus.setReferenceId(paymentLinkReferenceId);
118
        upsellRazorpayPaymentStatus.setPaymentStatus(status);
156
        upsellRazorpayPaymentStatus.setPaymentStatus(status);
119
        upsellRazorpayPaymentStatus.setPayment(Float.parseFloat(amount));
157
        upsellRazorpayPaymentStatus.setPayment(Float.parseFloat(amount));
120
        upsellRazorpayPaymentStatusRepository.persist(upsellRazorpayPaymentStatus);
-
 
121
    }
158
    }
122
}
159
}