Subversion Repositories SmartDukaan

Rev

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