Subversion Repositories SmartDukaan

Rev

Rev 3616 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3616 chandransh 1
package in.shop2020.serving.controllers;
2
 
3
import in.shop2020.config.ConfigException;
4
import in.shop2020.payments.Attribute;
5
import in.shop2020.payments.Payment;
6
import in.shop2020.payments.PaymentException;
7
import in.shop2020.payments.PaymentStatus;
8
import in.shop2020.payments.PaymentService.Client;
9
import in.shop2020.serving.services.CommonPaymentService;
10
import in.shop2020.thrift.clients.PaymentClient;
11
import in.shop2020.thrift.clients.TransactionClient;
12
import in.shop2020.thrift.clients.UserClient;
13
import in.shop2020.thrift.clients.config.ConfigClient;
14
 
15
import java.io.IOException;
16
 
17
import javax.servlet.http.HttpServletRequest;
18
 
19
import org.apache.log4j.Logger;
20
import org.apache.struts2.interceptor.ServletRequestAware;
21
import org.apache.thrift.TException;
22
 
23
public class HdfcEmiPayResponseController implements ServletRequestAware{
24
 
25
    /**
26
     * Enum of all statuses that can be returned by the HDFC gateway
27
     * 
28
     * @author Chandranshu
29
     * 
30
     */
31
    private enum PaymentReturnStatus{
32
        APPROVED("APPROVED"),
33
        NOT_APPROVED("NOT APPROVED"),
34
        CAPTURED("CAPTURED"),
35
        NOT_CAPTURED ("NOT CAPTURED"),
36
        CANCELLED ("CANCELLED"),
37
        DENIED_BY_RISK("DENIED BY RISK"),
38
        HOST_TIMEOUT("HOST TIMEOUT");
39
        private String value;
40
        PaymentReturnStatus(String value) {
41
            this.value = value;
42
        }
43
        public String value(){
44
            return this.value;
45
        }
46
    }
47
 
48
    HttpServletRequest request;
49
 
50
    //private static Logger log = LoggerFactory.getLogger(HdfcPayResponseController.class);
51
    private static Logger log = Logger.getLogger(Class.class);
52
 
53
    public static String successUrl;
54
    public static String errorUrl;
55
 
56
    public static String AMOUNT = "amt";
57
    public static String TRACKID = "trackid";
58
    public static String RESULT = "result";
59
    public static String AUTH = "auth";
60
    public static String TRANID = "tranid";
61
    public static String PAYMENTID = "paymentid";
62
    public static String REF = "ref";
63
    public static String POSTDATE = "postdate";
64
    public static String ERROR = "Error";
65
    public static String ERRORTEXT = "ErrorText";
66
    public static String UDF5 = "udf5"; 
67
 
68
    static{
69
        try {
70
            successUrl = ConfigClient.getClient().get("emi_payment_success_url");
71
            errorUrl = ConfigClient.getClient().get("emi_payment_error_url");
72
        } catch (ConfigException e) {
73
            log.error("Unable to get success and error usr info from config server.");
74
        }
75
    }
76
 
77
    String redirectUrl;
78
 
79
    public HdfcEmiPayResponseController() {
80
 
81
    }
82
 
83
    /**
84
     * Sets the redirectUrl instance variable which is used in the view to
85
     * redirect the customer to the success or failure page.
86
     * 
87
     * @return index in all cases.
88
     * @throws IOException
89
     * @throws SecurityException
90
     */
91
    public String create() throws IOException, SecurityException{
92
        log.info("Inside hdfc pay response Create");
93
 
94
        PaymentClient paymentServiceClient = null;
95
        TransactionClient transactionServiceClient = null;
96
        UserClient userServiceClient = null;
97
        try{
98
            paymentServiceClient = new PaymentClient();
99
            transactionServiceClient = new TransactionClient();
100
            userServiceClient = new UserClient();
101
        }catch(Exception e){
102
            //Nothing to worry. lets move forward
103
            log.error("Unable to initialize one of the clients", e);
104
        }
105
 
106
        Long txnId = null;
107
 
108
        String paymentId = request.getParameter(PAYMENTID);
109
        String result = request.getParameter(RESULT);
110
        String trackId = request.getParameter(TRACKID);
111
        long merchantPaymentId = Long.parseLong(trackId);
112
        String amount = request.getParameter(AMOUNT);
113
        String errorText = request.getParameter(ERRORTEXT);
114
 
115
        //FIXME dump them somewhere
116
        String udf5=request.getParameter(UDF5);
117
        //FIXME hdfc is sending comma separated amount, which is very disappointing. May be we get more surprises moving forward.
118
        amount= amount.replace(",", "");
119
 
120
        //Setting redirect URL to the error URL value by default.
121
        this.redirectUrl = errorUrl + "?paymentId=" + merchantPaymentId;
122
 
123
        Client paymentClient = paymentServiceClient.getClient();
124
        Payment payment = null;
125
        try {
126
            payment = paymentClient.getPayment(merchantPaymentId);
127
            txnId = payment.getMerchantTxnId();
128
 
129
            if(!validatePaymentParams(paymentId, amount, udf5, payment))
130
                return "index";
131
        } catch (PaymentException e1) {
132
            log.error("Payment exception. It is serious, check merchant payment id + " + merchantPaymentId, e1);
133
        } catch (TException e1) {
134
            log.error("Thrift exception. Check payment id "+ merchantPaymentId, e1);
135
        }
136
 
137
        if (result != null && result.trim().equals(PaymentReturnStatus.APPROVED.value())) {
138
            log.info("Payment " + merchantPaymentId + " authorized successfully. Updating the database.");
139
            String description = "Payment authorized";
140
            updatePaymentDetails(merchantPaymentId, description, PaymentStatus.AUTHORIZED, request, paymentClient);
141
 
4246 rajveer 142
            CommonPaymentService.processSuccessfulTxn(txnId, userServiceClient, transactionServiceClient, false);
3616 chandransh 143
 
144
            this.redirectUrl = successUrl + "?paymentId=" + merchantPaymentId;
145
        } else {
146
            updatePaymentDetails(merchantPaymentId, errorText, PaymentStatus.FAILED, request, paymentClient);
147
 
148
            CommonPaymentService.processFailedTxn(txnId, transactionServiceClient);
149
 
150
            this.redirectUrl = errorUrl + "?paymentId=" + merchantPaymentId;
151
        }
152
 
153
        return "index";
154
    }
155
 
156
    private boolean validatePaymentParams(String paymentId, String amount, String udf5, Payment payment) {
157
        long merchantPaymentId = payment.getPaymentId();
158
        String dbUdf5="";
159
        double dbAmount = payment.getAmount();
160
        for(Attribute attribute: payment.getAttributes()){
161
            if(attribute.getName().trim().equalsIgnoreCase(UDF5)){
162
                dbUdf5 = attribute.getValue();
163
            }
164
        }
165
        // verify 3 things:  udf5, amount and paymentid 
166
        double returnedAmount = Double.parseDouble(amount);
167
        log.info(paymentId+ ":"+ payment.getGatewayPaymentId() + "\n" + returnedAmount + ":" + dbAmount + "\n" + dbUdf5 + ":" + udf5 );
168
        if(!(paymentId.equalsIgnoreCase(payment.getGatewayPaymentId()) && (Math.abs(dbAmount - returnedAmount) <= 0.50) && udf5.equalsIgnoreCase(dbUdf5))){
169
            log.error("Checks and balance failed on returned data");
170
            this.redirectUrl =  errorUrl + "?paymentId="+merchantPaymentId;
171
            return false;
172
        }
173
        return true;
174
    }
175
 
176
    private void updatePaymentDetails(long merchantPaymentId, String message, PaymentStatus status, HttpServletRequest req, Client paymentClient) {
177
        String paymentId = request.getParameter(PAYMENTID);
178
        String result = request.getParameter(RESULT);
179
        String postdate = request.getParameter(POSTDATE);
180
        String tranId = request.getParameter(TRANID);
181
        String auth = request.getParameter(AUTH);
182
        String ref = request.getParameter(REF);
183
 
184
        String sessionId = request.getSession().getId();
185
        String errorNo = request.getParameter(ERROR);
186
        try {
187
            paymentClient.updatePaymentDetails(merchantPaymentId, paymentId, sessionId, result, message, tranId, auth, ref, errorNo, status, postdate, null);
188
        } catch (PaymentException e1) {
189
            log.error("Unable to update payment details in our database.", e1);
190
        } catch (TException e1) {
191
            log.error("Unable to update payment details in our database. Thrift exception.", e1);
192
        }
193
    }
194
 
195
    public String getRedirectUrl(){
196
        return this.redirectUrl;
197
    }
198
 
199
    @Override
200
    public void setServletRequest(HttpServletRequest request) {
201
        this.request = request;
202
        for(Object param: request.getParameterMap().keySet()){
203
            log.info("PARAMS: " + param + " = "+ request.getParameter((String)param));
204
        }
205
    }
206
}