Subversion Repositories SmartDukaan

Rev

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