Subversion Repositories SmartDukaan

Rev

Rev 6070 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
6058 anupam.sin 1
package in.shop2020.serving.controllers;
2
 
3
import in.shop2020.config.ConfigException;
4
import in.shop2020.model.v1.order.RechargeOrder;
5
import in.shop2020.model.v1.order.RechargeOrderStatus;
6
import in.shop2020.model.v1.order.TransactionServiceException;
7
import in.shop2020.payments.Payment;
8
import in.shop2020.payments.PaymentException;
9
import in.shop2020.payments.PaymentStatus;
10
import in.shop2020.serving.utils.ebs.Base64;
11
import in.shop2020.serving.utils.ebs.RC4;
12
import in.shop2020.thrift.clients.PaymentClient;
13
import in.shop2020.thrift.clients.TransactionClient;
14
import in.shop2020.thrift.clients.UserClient;
15
import in.shop2020.thrift.clients.config.ConfigClient;
16
 
17
import java.io.BufferedReader;
18
import java.io.ByteArrayInputStream;
19
import java.io.IOException;
20
import java.io.InputStreamReader;
21
import java.util.Map;
22
import java.util.StringTokenizer;
23
import java.util.TreeMap;
24
 
25
import javax.servlet.http.HttpServletRequest;
26
 
27
import org.apache.log4j.Logger;
28
import org.apache.thrift.TException;
29
 
30
public class WalletOnlyPaymentController {
31
    private static Logger log = Logger.getLogger(Class.class);
32
 
33
    private static final String FLAG_KEY = "IsFlagged";
34
    private static final String TXN_KEY = "TransactionID";
35
    private static final String AUTH_TXN_ID = "AuthTxnId";
36
 
37
    private static String successUrl;
38
    private static String errorUrl;
39
    private static String rechargeResultUri;
40
    private RechargeOrder rechargeOrder = null;
41
 
42
    /**
43
     * The secret key used to decode RC4 encoded data.
44
     */
45
    private static String accountKey;
46
 
47
    private String redirectUrl;
48
 
49
    static{
50
        try {
51
            successUrl = ConfigClient.getClient().get("ebs_success_url");
52
            errorUrl = ConfigClient.getClient().get("ebs_error_url");
53
            setRechargeResultUri(ConfigClient.getClient().get("recharge_success_url"));
54
            accountKey = ConfigClient.getClient().get("ebs_secret_key");
55
        } catch (ConfigException e) {
56
            log.error("Unable to get success and error usr info from config server.");
57
        }
58
    }
59
 
60
    private Map<String, String> paymentParams = new TreeMap<String, String>();
61
 
62
    private String rechargeOrderId;
63
 
64
    private HttpServletRequest request;
65
 
66
    public String index() {
67
        StringBuffer data1 = new StringBuffer(rechargeOrderId);
68
        log.info("Received rechargeORder Id : " + data1.toString());
69
        byte[] result = decodeRecvdData(data1);
70
 
71
        //String recvString = parseRecvdData(result);
72
        //updatePaymentParams(recvString);
73
 
74
        PaymentClient paymentServiceClient = null;
75
        TransactionClient transactionServiceClient = null;
76
        UserClient userServiceClient = null;
77
        try {
78
            paymentServiceClient = new PaymentClient();
79
            transactionServiceClient = new TransactionClient();
80
            userServiceClient = new UserClient();
81
        } catch (Exception e) {
82
            log.error("Unable to initialize one of the clients", e);
83
        }
84
 
85
 
86
        //long merchantPaymentId = Long.parseLong(paymentParams.get("MerchantRefNo"));
87
        //String gatewayPaymentId = paymentParams.get("PaymentID");
88
        //double amount = Double.parseDouble(paymentParams.get("Amount"));
89
//        String isFlagged = paymentParams.get(FLAG_KEY);
90
//        String gatewayTxnStatus = paymentParams.get("ResponseCode");
91
//        String description = paymentParams.get("ResponseMessage");
92
//        String authTxnId = paymentParams.get(TXN_KEY);
93
 
94
        Payment payment = null;
95
        Long txnId = null;
96
        try {
97
            //payment = paymentServiceClient.getClient().getPayment(merchantPaymentId);
98
            //txnId = payment.getMerchantTxnId();
99
            /**
100
             * CREATE PAYMENT
101
             */
102
            rechargeOrder = transactionServiceClient.getClient().getRechargeOrder(Long.parseLong(rechargeOrderId));
103
            Long merchantPaymentId = paymentServiceClient.getClient().createPayment(rechargeOrder.getUserId(), 0, 8, rechargeOrder.getTransactionId(), true);
104
            //Update payment status as authorized
105
            paymentServiceClient.getClient().updatePaymentDetails(merchantPaymentId, "",
106
                    "", "0", "", "", "", "", "", PaymentStatus.AUTHORIZED, "", null);
107
            transactionServiceClient.getClient().updateRechargeOrderStatus(rechargeOrder.getId(),  RechargeOrderStatus.PAYMENT_SUCCESSFUL);
108
            this.redirectUrl = rechargeResultUri + "?paymentId=" + merchantPaymentId;
109
        } catch (PaymentException e) {
110
            log.error("Unable to mark the payment as authorized", e);
111
        } catch (TException e) {
112
            log.error("Unable to mark the payment as authorized", e);
113
        } catch (TransactionServiceException e) {
114
            // TODO Auto-generated catch block
115
            log.error("Unable to recharge");
116
        }
117
 
118
            //DataLogger.logData(EventType.PAYMENT_FAILURE, getSessionId(), userinfo.getUserId(), userinfo.getEmail(), Long.toString(merchantPaymentId), gatewayPaymentId,
119
            //        gatewayTxnStatus, description);
120
        log.info("User will be redirected to: " + this.redirectUrl);
121
        return "index";
122
    }
123
 
124
    private static void setRechargeResultUri(String string) {
125
        // TODO Auto-generated method stub
126
 
127
    }
128
 
129
    private boolean validatePaymentParams(double returnedAmount, Payment payment){
130
        if(!(payment != null && Math.abs(payment.getAmount() - returnedAmount) <= 0.50)){
131
            // We did not request this payment or the authorised amount is different.
132
            log.error("Checks and balance failed on returned data");
133
            return false;
134
        }
135
        return true;
136
    }
137
 
138
    private byte[] decodeRecvdData(StringBuffer data1) {
139
        for (int i = 0; i < data1.length(); i++) {
140
            if (data1.charAt(i) == ' ')
141
                data1.setCharAt(i, '+');
142
        }
143
 
144
        Base64 base64 = new Base64();
145
        byte[] data = base64.decode(data1.toString());
146
        RC4 rc4 = new RC4(accountKey);
147
        byte[] result = rc4.rc4(data);
148
        return result;
149
    }
150
 
151
    private String parseRecvdData(byte[] result) {
152
        ByteArrayInputStream byteIn = new ByteArrayInputStream(result, 0, result.length);
153
        BufferedReader reader = new BufferedReader(new InputStreamReader(byteIn));
154
        String recvString1 = "";
155
        String recvString = "";
156
        try {
157
            recvString1 = reader.readLine();
158
            int lineCount = 0;
159
            while (recvString1 != null) {
160
                lineCount++;
161
                if (lineCount > 705)
162
                    break;
163
                recvString += recvString1 + "\n";
164
                recvString1 = reader.readLine();
165
            }
166
        } catch (IOException e) {
167
            log.error("Unable to read from Ebs response", e);
168
        }
169
        recvString = recvString.replace("=&", "=--&");
170
        return recvString;
171
    }
172
 
173
    private void updatePaymentParams(String str){
174
        StringTokenizer st = new StringTokenizer(str, "=&");
175
        String key, value;
176
        while(st.hasMoreTokens()) {
177
            key = st.nextToken();
178
            value = st.nextToken();
179
            log.info("Key: " + key + ", Value: " + value);
180
            paymentParams.put(key, value);
181
        }
182
    }
183
 
184
    public String getRedirectUrl(){
185
        return this.redirectUrl;
186
    }
187
 
188
    public void setServletRequest(HttpServletRequest request) {
189
        this.request = request;
190
    }
191
 
192
    public Map<String, String> getPaymentParams() {
193
        return paymentParams;
194
    }
195
 
196
 
197
    public static String getRechargeResultUri() {
198
        return rechargeResultUri;
199
    }
200
 
201
    public String getRechargeOrderId() {
202
        return rechargeOrderId;
203
    }
204
 
205
    public void setRechargeOrderId(String rechargeOrderId) {
206
        this.rechargeOrderId = rechargeOrderId;
207
    }
208
 
209
}