Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
6060 rajveer 1
package in.shop2020.serving.controllers;
2
 
3
import in.shop2020.config.ConfigException;
4
import in.shop2020.datalogger.EventType;
5
import in.shop2020.payments.Attribute;
6
import in.shop2020.payments.Payment;
7
import in.shop2020.payments.PaymentException;
8
import in.shop2020.payments.PaymentStatus;
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
import in.shop2020.utils.DataLogger;
15
 
16
import java.security.MessageDigest;
17
import java.security.NoSuchAlgorithmException;
18
import java.util.ArrayList;
19
import java.util.List;
20
import java.util.Map;
21
import java.util.TreeMap;
22
 
23
import javax.servlet.http.HttpServletRequest;
24
 
25
import org.apache.log4j.Logger;
26
import org.apache.thrift.TException;
27
 
28
@SuppressWarnings("serial")
29
public class PayuPayResponseController extends BaseController{
30
 
31
	private static Logger log = Logger.getLogger(Class.class);
32
 
33
	private static final String BANK_REF_ID = "BankRefId";
34
	private static final String UNMAPPED_STATUS = "UnMappedStatus";
35
 
36
	private static String successUrl;
37
	private static String errorUrl;
38
 
39
	/**
40
	 * The secret key used to decode RC4 encoded data.
41
	 */
42
	private static String accountKey = "C0Dr8m";
43
 
44
	private static String salt = "3sf0jURk";
45
 
46
	private String redirectUrl;
47
 
48
	static{
49
		try {
50
			successUrl = ConfigClient.getClient().get("ebs_success_url");
51
			errorUrl = ConfigClient.getClient().get("ebs_error_url");
52
			accountKey = ConfigClient.getClient().get("payu_secret_key");
53
		} catch (ConfigException e) {
54
			log.error("Unable to get success and error usr info from config server.");
55
		}
56
	}
57
 
58
	private Map<String, String> paymentParams = new TreeMap<String, String>();
59
 
60
	public String create() {
61
//		String gatewayTxnId = request.getParameter("mihpayid");
62
//		String status = request.getParameter("status");
63
//		String key = request.getParameter("key");
64
//		String mode = request.getParameter("mode");
65
//		String txnid = request.getParameter("txnid");
66
//		String amount = request.getParameter("amount");
67
//		String hash  = request.getParameter("hash");
68
//		String bank_ref_num  = request.getParameter("bank_ref_num");
69
//		String PG_TYPE  = request.getParameter("PG_TYPE");
70
//		String Error  = request.getParameter("Error");
71
//		String unmappedstatus  = request.getParameter("unmappedstatus");
72
//		
73
		updatePaymentParams(request.getParameterMap());
74
 
75
		PaymentClient paymentServiceClient = null;
76
		TransactionClient transactionServiceClient = null;
77
		UserClient userServiceClient = null;
78
		try {
79
			paymentServiceClient = new PaymentClient();
80
			transactionServiceClient = new TransactionClient();
81
			userServiceClient = new UserClient();
82
		} catch (Exception e) {
83
			log.error("Unable to initialize one of the clients", e);
84
		}
85
 
86
 
87
		long merchantPaymentId = Long.parseLong(paymentParams.get("txnid"));
88
		String gatewayPaymentId = paymentParams.get("mihpayid");
89
		double amount = Double.parseDouble(paymentParams.get("amount"));
90
		String gatewayTxnStatus = paymentParams.get("status");
91
		String hash = paymentParams.get("hash");
92
		String bankRefId = paymentParams.get("bank_ref_num");
93
		String unmappedStatus  = paymentParams.get("unmappedstatus");
94
 
95
 
96
		List<Attribute> attributes = new ArrayList<Attribute>();
97
		attributes.add(new Attribute(BANK_REF_ID, bankRefId));
98
		attributes.add(new Attribute(UNMAPPED_STATUS, unmappedStatus));
99
 
100
		Payment payment = null;
101
		Long txnId = null;
102
		try {
103
			payment = paymentServiceClient.getClient().getPayment(merchantPaymentId);
104
			txnId = payment.getMerchantTxnId();
105
		} catch (PaymentException e1) {
106
			log.error("Payment exception. It is serious, check merchant payment id + " + merchantPaymentId, e1);
107
		} catch (TException e1) {
108
			log.error("Thrift exception. Check payment id "+ merchantPaymentId, e1);
109
		}
110
 
111
		if(!validatePaymentParams(amount, payment, hash)){
112
			this.redirectUrl = errorUrl + "?paymentId=" + merchantPaymentId;
113
			return "index";
114
		}
115
 
116
		if(gatewayTxnStatus.equalsIgnoreCase("SUCCESS")){
117
			//Update payment status as authorized if payment is authorized.
118
			try {
119
				paymentServiceClient.getClient().updatePaymentDetails(merchantPaymentId, gatewayPaymentId,
120
						"", gatewayTxnStatus, "Transaction Authorized at PG", "", "", "", "", PaymentStatus.AUTHORIZED, "", attributes);
121
			} catch (PaymentException e) {
122
				log.error("Unable to mark the payment as authorized", e);
123
			} catch (TException e) {
124
			    log.error("Unable to mark the payment as authorized", e);
125
			}
126
 
127
 
128
			CommonPaymentService.processSuccessfulTxn(txnId, userServiceClient, transactionServiceClient, false);
129
            this.redirectUrl = successUrl + "?paymentId=" + merchantPaymentId;	
130
 
131
 
132
		}else{
133
			try {
134
				paymentServiceClient.getClient().updatePaymentDetails(merchantPaymentId, gatewayPaymentId,
135
						"", gatewayTxnStatus, "Payment Failed at PG", "", "", "", "", PaymentStatus.FAILED, "", attributes);
136
			} catch (PaymentException e) {
137
			    log.error("Unable to mark the payment as failed", e);
138
			} catch (TException e) {
139
			    log.error("Unable to mark the payment as failed", e);
140
			}
141
 
142
			CommonPaymentService.processFailedTxn(txnId, transactionServiceClient);
143
			DataLogger.logData(EventType.PAYMENT_FAILURE, getSessionId(), userinfo.getUserId(), userinfo.getEmail(), Long.toString(merchantPaymentId), gatewayPaymentId,
144
                    gatewayTxnStatus, "Payment Failed at PG");
145
 
146
			this.redirectUrl = errorUrl + "?paymentId=" + merchantPaymentId;
147
		}
148
 
149
		log.info("User will be redirected to: " + this.redirectUrl);
150
		return "index";
151
	}
152
 
153
	private boolean validatePaymentParams(double returnedAmount, Payment payment, String hash){
154
		if(!(payment != null && Math.abs(payment.getAmount() - returnedAmount) <= 0.50 && hash.equals(getSecureHash()))){
155
			// We did not request this payment or the authorised amount is different.
156
			log.error("Checks and balance failed on returned data");
157
			return false;
158
		}
159
		return true;
160
	}
161
 
162
 
163
	public String getSecureHash(){
164
		try{
165
			String pass = salt + "|||||||||||" + paymentParams.get("email") + "|" +  paymentParams.get("firstname") + "|" + paymentParams.get("productinfo") + "|" + paymentParams.get("amount") + "|" + paymentParams.get("txnid") + "|" + accountKey;
166
			System.out.println(pass);
167
			MessageDigest md = MessageDigest.getInstance("SHA-512");
168
			md.update(pass.getBytes(), 0, pass.getBytes().length);
169
			byte[] mdbytes = md.digest();
170
			//	convert the byte to hex format method
171
			StringBuffer sb = new StringBuffer();
172
			for (int i = 0; i < mdbytes.length; i++) {
173
				sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
174
			}
175
			return sb.toString();
176
		}catch(NoSuchAlgorithmException nsae){
177
			log.error("No such algorithm exception");
178
			return null;
179
		}
180
	}
181
 
182
	private void updatePaymentParams(Map map){
183
		for(Object key : map.keySet()){
184
		    String keyStr = (String)key;
185
		    String[] vals = (String[])map.get(keyStr);
186
		    String value = vals[0];
187
		    System.out.println("Key " + (String)key + "     :    " + value);
188
		    paymentParams.put(keyStr, value);
189
		}
190
	}
191
 
192
	public String getRedirectUrl(){
193
		return this.redirectUrl;
194
	}
195
 
196
	@Override
197
	public void setServletRequest(HttpServletRequest request) {
198
		this.request = request;
199
	}
200
 
201
	public Map<String, String> getPaymentParams() {
202
		return paymentParams;
203
	}
204
}