Subversion Repositories SmartDukaan

Rev

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