Subversion Repositories SmartDukaan

Rev

Rev 6137 | Rev 6228 | Go to most recent revision | Details | Compare with Previous | 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");
6090 anupam.sin 53
            rechargeResultUri = ConfigClient.getClient().get("recharge_success_url");
6058 anupam.sin 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());
6070 anupam.sin 69
        //byte[] result = decodeRecvdData(data1);
6058 anupam.sin 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));
6223 anupam.sin 103
            Long merchantPaymentId = paymentServiceClient.getClient().createPayment(rechargeOrder.getUserId(), rechargeOrder.getTotalAmount(), 8, rechargeOrder.getTransactionId(), true);
6058 anupam.sin 104
            //Update payment status as authorized
105
            paymentServiceClient.getClient().updatePaymentDetails(merchantPaymentId, "",
6137 anupam.sin 106
                    "", "0", "", "", "", "", "", PaymentStatus.SUCCESS, "", null);
6058 anupam.sin 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
 
125
    private boolean validatePaymentParams(double returnedAmount, Payment payment){
126
        if(!(payment != null && Math.abs(payment.getAmount() - returnedAmount) <= 0.50)){
127
            // We did not request this payment or the authorised amount is different.
128
            log.error("Checks and balance failed on returned data");
129
            return false;
130
        }
131
        return true;
132
    }
133
 
134
    private byte[] decodeRecvdData(StringBuffer data1) {
135
        for (int i = 0; i < data1.length(); i++) {
136
            if (data1.charAt(i) == ' ')
137
                data1.setCharAt(i, '+');
138
        }
139
 
140
        Base64 base64 = new Base64();
141
        byte[] data = base64.decode(data1.toString());
142
        RC4 rc4 = new RC4(accountKey);
143
        byte[] result = rc4.rc4(data);
144
        return result;
145
    }
146
 
147
    private String parseRecvdData(byte[] result) {
148
        ByteArrayInputStream byteIn = new ByteArrayInputStream(result, 0, result.length);
149
        BufferedReader reader = new BufferedReader(new InputStreamReader(byteIn));
150
        String recvString1 = "";
151
        String recvString = "";
152
        try {
153
            recvString1 = reader.readLine();
154
            int lineCount = 0;
155
            while (recvString1 != null) {
156
                lineCount++;
157
                if (lineCount > 705)
158
                    break;
159
                recvString += recvString1 + "\n";
160
                recvString1 = reader.readLine();
161
            }
162
        } catch (IOException e) {
163
            log.error("Unable to read from Ebs response", e);
164
        }
165
        recvString = recvString.replace("=&", "=--&");
166
        return recvString;
167
    }
168
 
169
    private void updatePaymentParams(String str){
170
        StringTokenizer st = new StringTokenizer(str, "=&");
171
        String key, value;
172
        while(st.hasMoreTokens()) {
173
            key = st.nextToken();
174
            value = st.nextToken();
175
            log.info("Key: " + key + ", Value: " + value);
176
            paymentParams.put(key, value);
177
        }
178
    }
179
 
180
    public String getRedirectUrl(){
181
        return this.redirectUrl;
182
    }
183
 
184
    public void setServletRequest(HttpServletRequest request) {
185
        this.request = request;
186
    }
187
 
188
    public Map<String, String> getPaymentParams() {
189
        return paymentParams;
190
    }
191
 
192
 
193
    public static String getRechargeResultUri() {
194
        return rechargeResultUri;
195
    }
196
 
197
    public String getRechargeOrderId() {
198
        return rechargeOrderId;
199
    }
200
 
201
    public void setRechargeOrderId(String rechargeOrderId) {
202
        this.rechargeOrderId = rechargeOrderId;
203
    }
204
 
205
}