Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
20112 aman.kumar 1
package in.shop2020.serving.controllers;
2
 
3
import in.shop2020.model.v1.order.LineItem;
4
import in.shop2020.model.v1.order.Order;
5
import in.shop2020.model.v1.order.Transaction;
6
import in.shop2020.payments.Attribute;
7
import in.shop2020.payments.Payment;
8
import in.shop2020.serving.controllers.PayuPayController.ContactDetails;
9
import in.shop2020.serving.services.IPaymentService;
10
import in.shop2020.thrift.clients.PaymentClient;
11
import in.shop2020.thrift.clients.TransactionClient;
12
 
13
import java.math.BigInteger;
14
import java.security.MessageDigest;
15
import java.util.HashMap;
16
import java.util.Iterator;
17
import java.util.List;
18
import java.util.Map;
19
import java.util.TreeMap;
20
 
21
import org.apache.log4j.Logger;
22
 
23
import com.opensymphony.xwork2.ValidationAwareSupport;
24
 
25
public class HdfcPayController extends ValidationAwareSupport {
26
	static String SECURE_SECRET = "16da8a86fcf626ed37045d1fb9714d0f"; // hdfc gateway secret key
27
	private static Logger log = Logger.getLogger(Class.class); 
28
	// key;
29
	private final static String V3URL = "https://secure.ebs.in/pg/ma/payment/request";
30
	private ContactDetails billingDetails;
31
	private String paymentOption = null;
32
	private double amount;
33
	private String accountId = "21137";
34
	private StringBuilder description;
20115 aman.kumar 35
	private final String CHANNEL = "10";
20112 aman.kumar 36
	private final static String CURRENCY = "INR";
37
	private final static String RETURNURL = "http://www.shop2020.in/hdfc-pay-response";
38
	private final static String MODE = "LIVE";
39
	private final static String ALGO = "MD5";
40
	private final static String COUNTRY = "IND";
41
	private final static String PAGEID = "1";
42
	private String id;
43
	private HDFCPayPojo hpp;
44
 
45
	public HDFCPayPojo getHpp() {
46
		return hpp;
47
	}
48
 
49
	public void setHpp(HDFCPayPojo hpp) {
50
		this.hpp = hpp;
51
	}
52
 
53
	public void setId(String id) {
54
		this.id = id;
55
	}
56
 
57
	public String getId() {
58
		return id;
59
	}
60
 
61
	// hdfc api code. DO NOT CHANGE
62
	private String md5(String str) throws Exception {
63
		MessageDigest m = MessageDigest.getInstance("MD5");
64
 
65
		byte[] data = str.getBytes();
66
 
67
		m.update(data, 0, data.length);
68
 
69
		BigInteger i = new BigInteger(1, m.digest());
70
 
71
		String hash = String.format("%1$032X", i);
72
 
73
		return hash;
74
	}
75
 
76
	public String show() {
77
		String md5HashData = SECURE_SECRET;
78
		PaymentClient paymentServiceClient = null;
79
		Payment payment = null;
80
		try {
81
			long paymentId = Long.parseLong(this.id);
82
			paymentServiceClient = new PaymentClient();
83
			payment = paymentServiceClient.getClient().getPayment(paymentId);
84
		} catch (Exception e) {
85
			log.error("Error while getting payment client", e);
86
			addActionError("We are experiencing some problems. Please try later.");
87
			return "shipping-redirect";
88
		}
89
 
90
		Order order = null;
91
		try {
92
			long txnId = payment.getMerchantTxnId();
93
			TransactionClient transactionServiceClient = new TransactionClient();
94
			in.shop2020.model.v1.order.TransactionService.Client txnClient = transactionServiceClient
95
					.getClient();
96
			Transaction transaction = txnClient.getTransaction(txnId);
97
			order = transaction.getOrders().get(0);
98
		} catch (Exception e) {
99
			log.error("Error while getting transaction information", e);
100
			addActionError("We are experiencing some problems. Please try later.");
101
			return "shipping-redirect";
102
		}
103
		try{
104
 
105
		setDescription(order);
106
		setPaymentOption(payment);
107
		this.amount = payment.getAmount();
108
		// set all the fields in pojo
109
		hpp = new HDFCPayPojo(accountId, CHANNEL, getId(), "", PAGEID, CURRENCY,
110
				RETURNURL, MODE, ALGO, amount + "", getDescription(),
111
				order.getCustomer_name(), order.getCustomer_address1() + " "
112
						+ order.getCustomer_address2(),
113
				order.getCustomer_city(), order.getCustomer_state(), COUNTRY,
114
				order.getCustomer_pincode(), order.getCustomer_email(),
115
				order.getCustomer_mobilenumber(), order.getCustomer_name(),
116
				order.getCustomer_address1() + " "
117
						+ order.getCustomer_address2(),
118
				order.getCustomer_city(), order.getCustomer_state(), COUNTRY,
119
				order.getCustomer_pincode(), order.getCustomer_mobilenumber());
120
 
121
		hpp.setPostUrl(V3URL);
122
		// hpp.setAccountId(accountId);
123
 
124
		/* HDFC GATEWAY CODE STARTS-- CHECK CAREFULLY BEFORE CHANGE*/
125
		HashMap testMap = new HashMap();
126
		testMap.put("V3URL", V3URL);
127
		testMap.put("account_id", accountId + "");
128
		testMap.put("channel", CHANNEL);
129
		testMap.put("page_id", "1");
130
		testMap.put("return_url", RETURNURL);
131
		testMap.put("currency", CURRENCY);
132
		testMap.put("mode", MODE);
133
		testMap.put("algo", ALGO);
134
		testMap.put("reference_no", hpp.getTxnid());
135
		testMap.put("amount", hpp.getAmount());
136
		testMap.put("description", hpp.getDescription());
137
		testMap.put("name", hpp.getBillName());
138
		testMap.put("address", hpp.getAddress());
139
		testMap.put("city", hpp.getCity());
140
		testMap.put("state", hpp.getState());
141
		testMap.put("postal_code", hpp.getZipcode());
142
		testMap.put("country", hpp.getCountry());
143
		testMap.put("email", hpp.getEmail());
144
		testMap.put("phone", hpp.getPhone());
145
		testMap.put("ship_name", hpp.getShipName());
146
		testMap.put("ship_address", hpp.getShipAddress());
147
		testMap.put("ship_city", hpp.getShipCity());
148
		testMap.put("ship_state", hpp.getShipState());
149
		testMap.put("ship_country", hpp.getShipCountry());
150
		testMap.put("ship_phone", hpp.getShipPhone());
151
		testMap.put("ship_postal_code", hpp.getShipZipcode());
152
		testMap.put("submit", "SUBMIT");
153
		// Sort the HashMap
154
		Map requestFields = new TreeMap(testMap);
155
 
156
		String V3URL = (String) requestFields.remove("V3URL");
157
		requestFields.remove("submit");
158
		for (Iterator i = requestFields.keySet().iterator(); i.hasNext();) {
159
 
160
			String key = (String) i.next();
161
			String value = (String) requestFields.get(key);
162
			md5HashData += "|" + value;
163
		}
164
 
165
		String hvalue = ALGO;
166
 
167
		String hashedvalue = "";
168
 
169
		if (hvalue.equals("MD5")) {
170
			hashedvalue = md5(md5HashData);
171
		}
172
 
173
		/* HDFC GATEWAY CODE ENDS*/
174
 
175
		hpp.setHash(hashedvalue);
176
		}catch (Exception e) {
177
			log.error("Error while creating HDFCPayPojo information", e);
178
			addActionError("We are experiencing some problems. Please try later.");
179
			return "shipping-redirect";
180
		}
181
		return "show";
182
	}
183
 
184
	public String getDescription() {
185
		if (this.description.length() >= 255)
186
			return this.description.substring(0, 255).trim();
187
		else
188
			return this.description.toString().trim();
189
	}
190
 
191
	private void setDescription(Order order) {
192
		StringBuilder descriptionBuilder = new StringBuilder(255);
193
		for (LineItem line : order.getLineitems()) {
194
			if (line.getBrand() != null) {
195
				descriptionBuilder.append(line.getBrand() + " ");
196
			}
197
			if (line.getModel_name() != null) {
198
				descriptionBuilder.append(line.getModel_name() + " ");
199
			}
200
			if (line.getModel_number() != null) {
201
				descriptionBuilder.append(line.getModel_number() + " ");
202
			}
203
			if (line.getColor() != null) {
204
				descriptionBuilder.append(line.getColor() + " ");
205
			}
206
		}
207
		String desc = descriptionBuilder.toString();
208
		desc = desc.replaceAll("[^a-zA-Z0-9\\s\\-\\@\\/\\.]", "");
209
		this.description = new StringBuilder(desc);
210
	}
211
 
212
	public void setPaymentOption(Payment payment) {
213
		this.paymentOption = null;
214
		List<Attribute> attributes = payment.getAttributes();
215
		if (attributes == null)
216
			return;
217
		for (Attribute attr : attributes) {
218
			if (attr.getName().equals(IPaymentService.PAYMENT_METHOD))
219
				this.paymentOption = attr.getValue();
220
		}
221
	}
222
 
223
	public String getPaymentOption() {
224
		return paymentOption;
225
	}
226
 
227
	public class HDFCPayPojo {
228
 
229
		private String postUrl;
230
		private String accountId;
231
		private String channel;
232
		private String txnid;
233
		private String hash;
234
		private String pageId;
235
		private String currency;
236
		private String returnUrl;
237
		private String mode;
238
		private String algo;
239
		private String amount;
240
		private String description;
241
		private String billName;
242
		private String address;
243
		private String city;
244
		private String state;
245
		private String country;
246
		private String zipcode;
247
		private String email;
248
		private String phone;
249
		private String shipName;
250
		private String shipAddress;
251
		private String shipCity;
252
		private String shipState;
253
		private String shipCountry;
254
		private String shipZipcode;
255
		private String shipPhone;
256
 
257
		public HDFCPayPojo(String accountId, String channel, String txnid,
258
				String hash, String pageId, String currency, String returnUrl,
259
				String mode, String algo, String amount, String description,
260
				String billName, String address, String city, String state,
261
				String country, String zipcode, String email, String phone,
262
				String shipName, String shipAddress, String shipCity,
263
				String shipState, String shipCountry, String shipZipcode,
264
				String shipPhone) {
265
			super();
266
			this.accountId = accountId;
267
			this.channel = channel;
268
			this.txnid = txnid;
269
			this.hash = hash;
270
			this.pageId = pageId;
271
			this.currency = currency;
272
			this.returnUrl = returnUrl;
273
			this.mode = mode;
274
			this.algo = algo;
275
			this.amount = amount;
276
			this.description = description;
277
			this.billName = billName;
278
			this.address = address;
279
			this.city = city;
280
			this.state = state;
281
			this.country = country;
282
			this.zipcode = zipcode;
283
			this.email = email;
284
			this.phone = phone;
285
			this.shipName = shipName;
286
			this.shipAddress = shipAddress;
287
			this.shipCity = shipCity;
288
			this.shipState = shipState;
289
			this.shipCountry = shipCountry;
290
			this.shipZipcode = shipZipcode;
291
			this.shipPhone = shipPhone;
292
		}
293
 
294
		public String getAccountId() {
295
			return accountId;
296
		}
297
 
298
		public void setAccountId(String accountId) {
299
			this.accountId = accountId;
300
		}
301
 
302
		public String getChannel() {
303
			return channel;
304
		}
305
 
306
		public void setChannel(String channel) {
307
			this.channel = channel;
308
		}
309
 
310
		public String getTxnid() {
311
			return txnid;
312
		}
313
 
314
		public void setTxnid(String txnid) {
315
			this.txnid = txnid;
316
		}
317
 
318
		public String getHash() {
319
			return hash;
320
		}
321
 
322
		public void setHash(String hash) {
323
			this.hash = hash;
324
		}
325
 
326
		public String getPageId() {
327
			return pageId;
328
		}
329
 
330
		public void setPageId(String pageId) {
331
			this.pageId = pageId;
332
		}
333
 
334
		public String getCurrency() {
335
			return currency;
336
		}
337
 
338
		public void setCurrency(String currency) {
339
			this.currency = currency;
340
		}
341
 
342
		public String getReturnUrl() {
343
			return returnUrl;
344
		}
345
 
346
		public void setReturnUrl(String returnUrl) {
347
			this.returnUrl = returnUrl;
348
		}
349
 
350
		public String getMode() {
351
			return mode;
352
		}
353
 
354
		public void setMode(String mode) {
355
			this.mode = mode;
356
		}
357
 
358
		public String getAlgo() {
359
			return algo;
360
		}
361
 
362
		public void setAlgo(String algo) {
363
			this.algo = algo;
364
		}
365
 
366
		public String getAmount() {
367
			return amount;
368
		}
369
 
370
		public void setAmount(String amount) {
371
			this.amount = amount;
372
		}
373
 
374
		public String getDescription() {
375
			return description;
376
		}
377
 
378
		public void setDescription(String description) {
379
			this.description = description;
380
		}
381
 
382
		public String getBillName() {
383
			return billName;
384
		}
385
 
386
		public void setBillName(String billName) {
387
			this.billName = billName;
388
		}
389
 
390
		public String getAddress() {
391
			return address;
392
		}
393
 
394
		public void setAddress(String address) {
395
			this.address = address;
396
		}
397
 
398
		public String getCity() {
399
			return city;
400
		}
401
 
402
		public void setCity(String city) {
403
			this.city = city;
404
		}
405
 
406
		public String getState() {
407
			return state;
408
		}
409
 
410
		public void setState(String state) {
411
			this.state = state;
412
		}
413
 
414
		public String getCountry() {
415
			return country;
416
		}
417
 
418
		public void setCountry(String country) {
419
			this.country = country;
420
		}
421
 
422
		public String getZipcode() {
423
			return zipcode;
424
		}
425
 
426
		public void setZipcode(String zipcode) {
427
			this.zipcode = zipcode;
428
		}
429
 
430
		public String getEmail() {
431
			return email;
432
		}
433
 
434
		public void setEmail(String email) {
435
			this.email = email;
436
		}
437
 
438
		public String getPhone() {
439
			return phone;
440
		}
441
 
442
		public void setPhone(String phone) {
443
			this.phone = phone;
444
		}
445
 
446
		public String getShipName() {
447
			return shipName;
448
		}
449
 
450
		public void setShipName(String shipName) {
451
			this.shipName = shipName;
452
		}
453
 
454
		public String getShipAddress() {
455
			return shipAddress;
456
		}
457
 
458
		public void setShipAddress(String shipAddress) {
459
			this.shipAddress = shipAddress;
460
		}
461
 
462
		public String getShipCity() {
463
			return shipCity;
464
		}
465
 
466
		public void setShipCity(String shipCity) {
467
			this.shipCity = shipCity;
468
		}
469
 
470
		public String getShipState() {
471
			return shipState;
472
		}
473
 
474
		public void setShipState(String shipState) {
475
			this.shipState = shipState;
476
		}
477
 
478
		public String getShipCountry() {
479
			return shipCountry;
480
		}
481
 
482
		public void setShipCountry(String shipCountry) {
483
			this.shipCountry = shipCountry;
484
		}
485
 
486
		public String getShipZipcode() {
487
			return shipZipcode;
488
		}
489
 
490
		public void setShipZipcode(String shipZipcode) {
491
			this.shipZipcode = shipZipcode;
492
		}
493
 
494
		public String getShipPhone() {
495
			return shipPhone;
496
		}
497
 
498
		public void setShipPhone(String shipPhone) {
499
			this.shipPhone = shipPhone;
500
		}
501
 
502
		public String getPostUrl() {
503
			return postUrl;
504
		}
505
 
506
		public void setPostUrl(String postUrl) {
507
			this.postUrl = postUrl;
508
		}
509
 
510
	}
511
}