Subversion Repositories SmartDukaan

Rev

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