Subversion Repositories SmartDukaan

Rev

Rev 13288 | Rev 13374 | 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;
13372 amit.gupta 2
import in.shop2020.config.ConfigException;
3
import in.shop2020.model.v1.order.LineItem;
4
import in.shop2020.model.v1.order.Order;
5
import in.shop2020.model.v1.order.RechargeOrder;
6
import in.shop2020.model.v1.order.Transaction;
7
import in.shop2020.payments.Attribute;
8
import in.shop2020.payments.Payment;
9
import in.shop2020.serving.services.IPaymentService;
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
 
6060 rajveer 15
import java.security.MessageDigest;
16
import java.security.NoSuchAlgorithmException;
17
import java.util.List;
18
 
19
import org.apache.log4j.Logger;
20
import org.apache.struts2.convention.annotation.InterceptorRef;
21
import org.apache.struts2.convention.annotation.InterceptorRefs;
22
import org.apache.struts2.convention.annotation.Result;
23
import org.apache.struts2.convention.annotation.Results;
24
 
13372 amit.gupta 25
import com.google.gson.Gson;
6060 rajveer 26
import com.opensymphony.xwork2.ValidationAwareSupport;
27
 
28
@SuppressWarnings("serial")
13288 amit.gupta 29
@InterceptorRefs({
30
    @InterceptorRef("myDefault"),
31
    @InterceptorRef("login")
32
})
6060 rajveer 33
@Results({
13372 amit.gupta 34
	@Result(name="shipping-redirect", type="redirectAction", params = {"actionName" , "shipping"}),
35
	@Result(name="recharge-redirect", type="redirectAction", params = {"actionName" , "recharge"})
6060 rajveer 36
})
37
public class PayuPayController extends ValidationAwareSupport{
38
 
39
	private static Logger log = Logger.getLogger(Class.class);
40
 
6062 rajveer 41
	private static String accountId;
6060 rajveer 42
 
43
 
13372 amit.gupta 44
	private static String returnUrl = "/payu-pay-response";
45
 
46
	private static String cancelUrl = "/pay-options";
47
 
48
 
49
	private static String rechargeCancelUrl = "/recharge";
50
 
6062 rajveer 51
	private static String salt;
6060 rajveer 52
 
13372 amit.gupta 53
	private static String postActionUrl;
54
 
55
	private String phone;//This is used for recharge orders of value less than 1000.
56
	private String resultJson;//This is used for recharge orders of value less than 1000.
57
	private PayuPayPojo ppp; 
58
 
59
	public PayuPayPojo getPpp() {
60
		return ppp;
61
	}
62
 
63
	public void setPpp(PayuPayPojo ppp) {
64
		this.ppp = ppp;
65
	}
66
 
6060 rajveer 67
	static{
68
		try {
69
			accountId = ConfigClient.getClient().get("payu_account_id");
13372 amit.gupta 70
			salt = ConfigClient.getClient().get("payu_secret_key");
6060 rajveer 71
			returnUrl = ConfigClient.getClient().get("payu_return_url");
13372 amit.gupta 72
			cancelUrl = ConfigClient.getClient().get("pay_options_url");
73
			rechargeCancelUrl = ConfigClient.getClient().get("recharge_url");
74
			postActionUrl = ConfigClient.getClient().get("payu_post_url");
6060 rajveer 75
			//"https://test.payu.in/_payment";
76
		} catch (ConfigException e) {
13372 amit.gupta 77
			log.error("Unable to get PayU payment configuration.");
6060 rajveer 78
		}
79
	}
80
 
81
	private String id;
82
 
83
	private String paymentOption = null;
84
 
85
	private StringBuilder description;
86
 
87
	private double amount;
88
 
89
	private ContactDetails billingDetails;
90
 
91
	public String show(){
92
		PaymentClient paymentServiceClient = null;
93
		Payment payment = null;
94
		try {
95
			long paymentId = Long.parseLong(this.id);
96
			paymentServiceClient = new PaymentClient();
97
			payment = paymentServiceClient.getClient().getPayment(paymentId);
98
		} catch (Exception e) {
99
			log.error("Error while getting payment client", e);
100
			addActionError("We are experiencing some problems. Please try later.");
101
			return "shipping-redirect";
102
		}
103
 
104
		Order order = null;
105
		try {
106
			long txnId = payment.getMerchantTxnId();
107
			TransactionClient transactionServiceClient = new TransactionClient();
108
			in.shop2020.model.v1.order.TransactionService.Client txnClient = transactionServiceClient.getClient();
109
			Transaction transaction = txnClient.getTransaction(txnId);
110
			order = transaction.getOrders().get(0);
111
		} catch (Exception e) {
112
			log.error("Error while getting transaction information", e);
113
			addActionError("We are experiencing some problems. Please try later.");
114
			return "shipping-redirect";
115
		}
116
 
117
		setDescription(order);
118
		setPaymentOption(payment);
119
 
13372 amit.gupta 120
		ppp = new PayuPayPojo();
121
		ppp.setKey(accountId);
6060 rajveer 122
		this.amount = payment.getAmount();
13372 amit.gupta 123
		ppp.setAmount(payment.getAmount() + "");
124
		ppp.setEmail(order.getCustomer_email());
125
		ppp.setPhone(order.getCustomer_mobilenumber());
126
		ppp.setAddress1(order.getCustomer_address1());
127
		ppp.setAddress2(order.getCustomer_address2());
128
		ppp.setCity(order.getCustomer_city());
129
		ppp.setState(order.getCustomer_state());
130
		ppp.setProductinfo(this.getDescription());
131
		ppp.setZipcode(order.getCustomer_pincode());
132
		ppp.setPg("Wallet");
133
		ppp.setBankcode("payuw");
134
		ppp.setSurl(returnUrl);
135
		ppp.setFurl(returnUrl);
136
		ppp.setCurl(cancelUrl);
137
		String[] name = order.getCustomer_name().split(" ");
138
		ppp.setFirstname(name[0]);
139
		if(name.length==2){
140
			ppp.setLastname(name[1]);
141
		}
142
		ppp.setTxnid(getId());
143
		ppp.setCountry("India");
144
 
145
		this.amount = payment.getAmount();
146
 
147
		this.billingDetails = new ContactDetails(ppp.getFirstname(),
148
				order.getCustomer_email(), order.getCustomer_address1()+" "+ order.getCustomer_address2(),
6060 rajveer 149
				order.getCustomer_city(), order.getCustomer_state(),
150
				order.getCustomer_pincode(), "India",
151
				order.getCustomer_mobilenumber());
152
		log.info(billingDetails);
153
 
13372 amit.gupta 154
		ppp.setPostActionUrl(postActionUrl);
155
 
156
		try {
157
			ppp.setHash(getSecureHash());
158
		} catch (NoSuchAlgorithmException e) {
159
			log.error("Error while evaluating secure hash", e);
160
			addActionError("We are experiencing some problems. Please try later.");
161
			return "shipping-redirect";
162
		}
163
 
164
		setResultJson(new Gson().toJson(ppp));
165
 
166
		return "index";
6060 rajveer 167
	}
13372 amit.gupta 168
 
169
	/**
170
	 * This method is used for Recharge payments. It is being called by RechargePaymentController.
171
	 * @return
172
	 */
173
	public String edit(){
174
        PaymentClient paymentServiceClient = null;
175
        Payment payment = null;
176
        try {
177
            long paymentId = Long.parseLong(this.id);
178
            paymentServiceClient = new PaymentClient();
179
            payment = paymentServiceClient.getClient().getPayment(paymentId);
180
        } catch (Exception e) {
181
        	addActionError("We are experiencing some problems. Please try later.");
182
            log.error("Error while getting payment client", e);
183
            return "recharge-redirect";
184
        }
185
 
186
        RechargeOrder rechargeOrder = null;
187
        try {
188
            long txnId = payment.getMerchantTxnId();
189
            TransactionClient transactionServiceClient = new TransactionClient();
190
            in.shop2020.model.v1.order.TransactionService.Client txnClient = transactionServiceClient.getClient();
191
            rechargeOrder = txnClient.getRechargeOrdersForTransaction(txnId);
192
        } catch (Exception e) {
193
            log.error("Error while getting transaction information", e);
194
            addActionError("We are experiencing some problems. Please try later.");
195
            log.error("Error while getting payment client", e);
196
            return "recharge-redirect";
197
        }
198
 
199
        String desc = "Recharge for Rs. " + rechargeOrder.getTotalAmount() + ", operator : " + rechargeOrder.getOperatorId();
200
        this.description = new StringBuilder(desc);
201
        setPaymentOption(payment);
202
 
203
        this.amount = payment.getAmount();
204
        if(getPhone() == null || getPhone().isEmpty()) {
205
            UserClient userClient;
206
            in.shop2020.model.v1.user.Address address = null;
207
            try {
208
                userClient = new UserClient();
209
                long addressId = userClient.getClient().getDefaultAddressId(rechargeOrder.getUserId());
210
                address = userClient.getClient().getAddressById(addressId);
211
                this.billingDetails = new ContactDetails(address.getName().split("@")[0],
212
                        rechargeOrder.getUserEmailId(), address.getLine1(),
213
                        address.getCity(), address.getState(),
214
                        address.getPin(), "IND",
215
                        address.getPhone());
216
            } catch (Exception e) {
217
                log.error("Unable to get address to put in billing details for User : " + rechargeOrder.getUserId());
218
                e.printStackTrace();
219
            }
220
        } else {
221
        this.billingDetails = new ContactDetails(rechargeOrder.getUserEmailId().split("@")[0],
222
                rechargeOrder.getUserEmailId(), id,
223
                id, id,
224
                id, "IND",
225
                getPhone());
226
        }
227
        log.info("Billing details of recharge order " + rechargeOrder.getDisplayId() + " : " + billingDetails);
228
        PayuPayPojo epp = new PayuPayPojo();
229
		epp.setKey(getAccountId());
230
		epp.setTxnid(getId());
231
		epp.setBankcode("payuw");
232
		epp.setPg("Wallet");
233
		String[] name = billingDetails.getName().split(" ");
234
		epp.setFirstname(name[0]);
235
		epp.setEmail(billingDetails.getEmail());
236
		epp.setPhone(billingDetails.getPhone());
237
 
238
		epp.setSurl(returnUrl);
239
		epp.setFurl(returnUrl);
240
		epp.setCurl(rechargeCancelUrl);
241
 
242
		epp.setAmount(getAmount() + "");
243
		epp.setProductinfo(getDescription());
244
		try {
245
			epp.setHash(getSecureHash());
246
		} catch (Exception e) {
247
			log.error("Could not set securehash" );
248
		}
249
		epp.setPostActionUrl(postActionUrl);
250
		setResultJson(new Gson().toJson(epp));
251
        return "index";
252
    }
6060 rajveer 253
 
254
	public String getDescription(){
255
		if(this.description.length() >= 255)
256
			return this.description.substring(0, 255).trim();
257
		else
258
			return this.description.toString().trim();
259
	}
260
 
261
	private void setDescription(Order order){
262
		StringBuilder descriptionBuilder = new StringBuilder(255);
263
		for(LineItem line: order.getLineitems()){
264
			if(line.getBrand() != null){
265
				descriptionBuilder.append(line.getBrand() + " ");
266
			}
267
			if(line.getModel_name() != null){
268
				descriptionBuilder.append(line.getModel_name() + " "); 
269
			}
270
			if(line.getModel_number() != null){
271
				descriptionBuilder.append(line.getModel_number() + " ");
272
			}
273
			if(line.getColor() != null){
274
				descriptionBuilder.append(line.getColor() + " ");
275
			}
276
		}
277
		String desc = descriptionBuilder.toString();
278
		desc = desc.replaceAll("[^a-zA-Z0-9\\s\\-\\@\\/\\.]", "");
279
		this.description = new StringBuilder(desc);
280
	}
281
 
282
	public void setId(String id) {
283
		this.id = id;
284
	}
285
 
286
	public String getId() {
287
		return id;
288
	}
289
 
290
	public String getAccountId() {
291
		return accountId;
292
	}
293
 
294
	public static String getReturnUrl() {
295
		return returnUrl;
296
	}
297
 
298
	public double getAmount() {
299
		return amount;
300
	}
301
 
302
	public void setPaymentOption(Payment payment) {
303
		this.paymentOption = null;
304
		List<Attribute> attributes = payment.getAttributes();
305
		if(attributes == null)
306
			return;
307
		for(Attribute attr : attributes){
308
			if(attr.getName().equals(IPaymentService.PAYMENT_METHOD))
309
				this.paymentOption = attr.getValue();
310
		}
311
	}
312
 
313
 
314
	public String getPaymentOption(){
315
		return paymentOption;
316
	}
317
 
318
	public String getSecureHash() throws NoSuchAlgorithmException{
319
		String pass = accountId + "|" + id + "|" + amount  + "|" + getDescription() + "|" +  billingDetails.getName() + "|" + billingDetails.getEmail() + "|||||||||||" + salt;
13372 amit.gupta 320
		System.out.println("accountId|id|amount|getDescription()|billingDetails.getName()|billingDetails.getEmail()|||||||||||salt");
6060 rajveer 321
		System.out.println(pass);
322
		MessageDigest md = MessageDigest.getInstance("SHA-512");
323
		md.update(pass.getBytes(), 0, pass.getBytes().length);
324
		byte[] mdbytes = md.digest();
325
		//	convert the byte to hex format method
326
		StringBuffer sb = new StringBuffer();
327
		for (int i = 0; i < mdbytes.length; i++) {
328
			sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
329
		}
330
		return sb.toString();
331
	}
332
 
333
	public ContactDetails getBillingDetails() {
334
		return billingDetails;
335
	}
336
 
13372 amit.gupta 337
	public void setPhone(String phone) {
338
		this.phone = phone;
339
	}
340
 
341
	public String getPhone() {
342
		return phone;
343
	}
344
 
345
    public void setResultJson(String resultJson) {
346
		this.resultJson = resultJson;
347
	}
348
 
349
	public String getResultJson() {
350
		log.info(resultJson);
351
		return resultJson;
352
	}
353
 
6060 rajveer 354
	public static class ContactDetails{
355
		private String name;
356
		private String email;
13372 amit.gupta 357
		private String address;
6060 rajveer 358
		private String city;
359
		private String state;
360
		private String postalCode;
361
		private String country;
362
		private String phone;
363
 
13372 amit.gupta 364
		public ContactDetails(String name, String email, String address,
6060 rajveer 365
				String city, String state, String postalCode, String country,
366
				String phone) {
367
			this.name = name;
368
			this.email = email;
13372 amit.gupta 369
			this.address = address;
6060 rajveer 370
			this.city = city;
371
			this.state = state;
372
			this.postalCode = postalCode;
373
			this.country = country;
374
			this.phone = phone;
375
		}
376
 
377
		@Override
378
		public String toString() {
379
			return "ContactDetails [name=" + name + ", email=" + email
13372 amit.gupta 380
					+ ", address=" + address + ", city=" + city + ", state="
6060 rajveer 381
					+ state + ", postalCode=" + postalCode + ", country="
382
					+ country + ", phone=" + phone + "]";
383
		}
384
 
385
		public String getName() {
386
			return name;
387
		}
388
 
389
		public String getEmail() {
390
			return email;
391
		}
392
 
13372 amit.gupta 393
		public String getAddress() {
394
			return address;
6060 rajveer 395
		}
396
 
397
		public String getCity() {
398
			return city;
399
		}
400
 
401
		public String getState() {
402
			return state;
403
		}
404
 
405
		public String getPostalCode() {
406
			return postalCode;
407
		}
408
 
409
		public String getCountry() {
410
			return country;
411
		}
412
 
413
		public String getPhone() {
414
			return phone;
415
		}
416
	}
13372 amit.gupta 417
 
418
	public class PayuPayPojo {
419
		public String getKey() {
420
			return key;
421
		}
422
		public void setKey(String key) {
423
			this.key = key;
424
		}
425
		public String getTxnid() {
426
			return txnid;
427
		}
428
		public void setTxnid(String txnid) {
429
			this.txnid = txnid;
430
		}
431
		public String getAmount() {
432
			return amount;
433
		}
434
		public void setAmount(String amount) {
435
			this.amount = amount;
436
		}
437
		public String getProductinfo() {
438
			return productinfo;
439
		}
440
		public void setProductinfo(String productinfo) {
441
			this.productinfo = productinfo;
442
		}
443
		public String getFirstname() {
444
			return firstname;
445
		}
446
		public void setFirstname(String firstname) {
447
			this.firstname = firstname;
448
		}
449
		public String getEmail() {
450
			return email;
451
		}
452
		public void setEmail(String email) {
453
			this.email = email;
454
		}
455
		public String getLastname() {
456
			return lastname;
457
		}
458
		public void setLastname(String lastname) {
459
			this.lastname = lastname;
460
		}
461
		public String getAddress1() {
462
			return address1;
463
		}
464
		public void setAddress1(String address1) {
465
			this.address1 = address1;
466
		}
467
		public String getAddress2() {
468
			return address2;
469
		}
470
		public void setAddress2(String address2) {
471
			this.address2 = address2;
472
		}
473
		public String getCity() {
474
			return city;
475
		}
476
		public void setCity(String city) {
477
			this.city = city;
478
		}
479
		public String getState() {
480
			return state;
481
		}
482
		public void setState(String state) {
483
			this.state = state;
484
		}
485
		public String getCountry() {
486
			return country;
487
		}
488
		public void setCountry(String country) {
489
			this.country = country;
490
		}
491
		public String getZipcode() {
492
			return zipcode;
493
		}
494
		public void setZipcode(String zipcode) {
495
			this.zipcode = zipcode;
496
		}
497
		public String getSurl() {
498
			return surl;
499
		}
500
		public void setSurl(String surl) {
501
			this.surl = surl;
502
		}
503
		public String getFurl() {
504
			return furl;
505
		}
506
		public void setFurl(String furl) {
507
			this.furl = furl;
508
		}
509
		public String getCurl() {
510
			return curl;
511
		}
512
		public void setCurl(String curl) {
513
			this.curl = curl;
514
		}
515
		public String getHash() {
516
			return hash;
517
		}
518
		public void setHash(String hash) {
519
			this.hash = hash;
520
		}
521
		public String getPg() {
522
			return pg;
523
		}
524
		public void setPg(String pg) {
525
			this.pg = pg;
526
		}
527
		public String getCodurl() {
528
			return codurl;
529
		}
530
		public void setCodurl(String codurl) {
531
			this.codurl = codurl;
532
		}
533
		public String getDrop_category() {
534
			return drop_category;
535
		}
536
		public void setDrop_category(String drop_category) {
537
			this.drop_category = drop_category;
538
		}
539
		public void setPhone(String phone) {
540
			this.phone = phone;
541
		}
542
		public String getPhone() {
543
			return phone;
544
		}
545
		public void setBankcode(String bankcode) {
546
			this.bankcode = bankcode;
547
		}
548
		public String getBankcode() {
549
			return bankcode;
550
		}
551
		public void setPostActionUrl(String postActionUrl) {
552
			this.postActionUrl = postActionUrl;
553
		}
554
		public String getPostActionUrl() {
555
			return postActionUrl;
556
		}
557
		private String key;
558
		private String txnid;
559
		private String amount;
560
		private String productinfo;
561
		private String firstname;
562
		private String email;
563
		private String lastname;
564
		private String address1;
565
		private String address2;
566
		private String city;
567
		private String state;
568
		private String country;
569
		private String zipcode;
570
		private String surl;
571
		private String furl;
572
		private String curl;
573
		private String hash;
574
		private String pg;
575
		private String codurl;
576
		private String drop_category;
577
		private String phone;
578
		private String bankcode;
579
		private String postActionUrl;
580
	}
6060 rajveer 581
}