Subversion Repositories SmartDukaan

Rev

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