Subversion Repositories SmartDukaan

Rev

Rev 13288 | Rev 13374 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 13288 Rev 13372
Line 1... Line 1...
1
package in.shop2020.serving.controllers;
1
package in.shop2020.serving.controllers;
-
 
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
 
2
import java.security.MessageDigest;
15
import java.security.MessageDigest;
3
import java.security.NoSuchAlgorithmException;
16
import java.security.NoSuchAlgorithmException;
4
import java.util.List;
17
import java.util.List;
5
 
18
 
6
import org.apache.log4j.Logger;
19
import org.apache.log4j.Logger;
7
import org.apache.struts2.convention.annotation.InterceptorRef;
20
import org.apache.struts2.convention.annotation.InterceptorRef;
8
import org.apache.struts2.convention.annotation.InterceptorRefs;
21
import org.apache.struts2.convention.annotation.InterceptorRefs;
9
import org.apache.struts2.convention.annotation.Result;
22
import org.apache.struts2.convention.annotation.Result;
10
import org.apache.struts2.convention.annotation.Results;
23
import org.apache.struts2.convention.annotation.Results;
11
 
24
 
-
 
25
import com.google.gson.Gson;
12
import com.opensymphony.xwork2.ValidationAwareSupport;
26
import com.opensymphony.xwork2.ValidationAwareSupport;
13
 
27
 
14
import in.shop2020.config.ConfigException;
-
 
15
import in.shop2020.model.v1.order.LineItem;
-
 
16
import in.shop2020.model.v1.order.Order;
-
 
17
import in.shop2020.model.v1.order.Transaction;
-
 
18
import in.shop2020.payments.Attribute;
-
 
19
import in.shop2020.payments.Payment;
-
 
20
import in.shop2020.serving.services.IPaymentService;
-
 
21
import in.shop2020.thrift.clients.PaymentClient;
-
 
22
import in.shop2020.thrift.clients.TransactionClient;
-
 
23
import in.shop2020.thrift.clients.config.ConfigClient;
-
 
24
 
-
 
25
@SuppressWarnings("serial")
28
@SuppressWarnings("serial")
26
@InterceptorRefs({
29
@InterceptorRefs({
27
    @InterceptorRef("myDefault"),
30
    @InterceptorRef("myDefault"),
28
    @InterceptorRef("login")
31
    @InterceptorRef("login")
29
})
32
})
30
@Results({
33
@Results({
31
	@Result(name="shipping-redirect", type="redirectAction", 
34
	@Result(name="shipping-redirect", type="redirectAction", params = {"actionName" , "shipping"}),
32
    		params = {"actionName" , "shipping"})
35
	@Result(name="recharge-redirect", type="redirectAction", params = {"actionName" , "recharge"})
33
})
36
})
34
public class PayuPayController extends ValidationAwareSupport{
37
public class PayuPayController extends ValidationAwareSupport{
35
 
38
 
36
	private static Logger log = Logger.getLogger(Class.class);
39
	private static Logger log = Logger.getLogger(Class.class);
37
	
40
	
38
	private static String accountId;
41
	private static String accountId;
39
	
42
	
-
 
43
	
40
	private static String returnUrl = "http://local.shop2020.in:8080/payu-pay-response";
44
	private static String returnUrl = "/payu-pay-response";
-
 
45
 
-
 
46
	private static String cancelUrl = "/pay-options";
-
 
47
 
-
 
48
	
-
 
49
	private static String rechargeCancelUrl = "/recharge";
41
	
50
	
42
	private static String salt;
51
	private static String salt;
43
	
52
	
-
 
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
 
44
	static{
67
	static{
45
		try {
68
		try {
46
			accountId = ConfigClient.getClient().get("payu_account_id");
69
			accountId = ConfigClient.getClient().get("payu_account_id");
47
			returnUrl = ConfigClient.getClient().get("payu_return_url");
-
 
48
			salt = ConfigClient.getClient().get("payu_secret_key");
70
			salt = ConfigClient.getClient().get("payu_secret_key");
-
 
71
			returnUrl = ConfigClient.getClient().get("payu_return_url");
-
 
72
			cancelUrl = ConfigClient.getClient().get("pay_options_url");
-
 
73
			rechargeCancelUrl = ConfigClient.getClient().get("recharge_url");
-
 
74
			postActionUrl = ConfigClient.getClient().get("payu_post_url");
49
			//"https://test.payu.in/_payment";
75
			//"https://test.payu.in/_payment";
50
		} catch (ConfigException e) {
76
		} catch (ConfigException e) {
51
			log.error("Unable to get EBS payment configuration.");
77
			log.error("Unable to get PayU payment configuration.");
52
		}
78
		}
53
	}
79
	}
54
	
80
	
55
	private String id;
81
	private String id;
56
	
82
	
Line 89... Line 115...
89
		}
115
		}
90
		
116
		
91
		setDescription(order);
117
		setDescription(order);
92
		setPaymentOption(payment);
118
		setPaymentOption(payment);
93
		
119
		
-
 
120
		ppp = new PayuPayPojo();
-
 
121
		ppp.setKey(accountId);
-
 
122
		this.amount = payment.getAmount();
-
 
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
		
94
		this.amount = payment.getAmount();
145
		this.amount = payment.getAmount();
-
 
146
 
95
		this.billingDetails = new ContactDetails(order.getCustomer_name(),
147
		this.billingDetails = new ContactDetails(ppp.getFirstname(),
96
				order.getCustomer_email(), order.getCustomer_address1(), order.getCustomer_address2(),
148
				order.getCustomer_email(), order.getCustomer_address1()+" "+ order.getCustomer_address2(),
97
				order.getCustomer_city(), order.getCustomer_state(),
149
				order.getCustomer_city(), order.getCustomer_state(),
98
				order.getCustomer_pincode(), "India",
150
				order.getCustomer_pincode(), "India",
99
				order.getCustomer_mobilenumber());
151
				order.getCustomer_mobilenumber());
100
		
-
 
101
		log.info(billingDetails);
152
		log.info(billingDetails);
102
		
153
		
-
 
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
		
103
		return "show";
166
		return "index";
104
	}
167
	}
-
 
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
    }
105
 
253
 
106
	public String getDescription(){
254
	public String getDescription(){
107
		if(this.description.length() >= 255)
255
		if(this.description.length() >= 255)
108
			return this.description.substring(0, 255).trim();
256
			return this.description.substring(0, 255).trim();
109
		else
257
		else
Line 167... Line 315...
167
		return paymentOption;
315
		return paymentOption;
168
	}
316
	}
169
 
317
 
170
	public String getSecureHash() throws NoSuchAlgorithmException{
318
	public String getSecureHash() throws NoSuchAlgorithmException{
171
		String pass = accountId + "|" + id + "|" + amount  + "|" + getDescription() + "|" +  billingDetails.getName() + "|" + billingDetails.getEmail() + "|||||||||||" + salt;
319
		String pass = accountId + "|" + id + "|" + amount  + "|" + getDescription() + "|" +  billingDetails.getName() + "|" + billingDetails.getEmail() + "|||||||||||" + salt;
-
 
320
		System.out.println("accountId|id|amount|getDescription()|billingDetails.getName()|billingDetails.getEmail()|||||||||||salt");
172
		System.out.println(pass);
321
		System.out.println(pass);
173
		MessageDigest md = MessageDigest.getInstance("SHA-512");
322
		MessageDigest md = MessageDigest.getInstance("SHA-512");
174
		md.update(pass.getBytes(), 0, pass.getBytes().length);
323
		md.update(pass.getBytes(), 0, pass.getBytes().length);
175
		byte[] mdbytes = md.digest();
324
		byte[] mdbytes = md.digest();
176
		//	convert the byte to hex format method
325
		//	convert the byte to hex format method
Line 183... Line 332...
183
	
332
	
184
	public ContactDetails getBillingDetails() {
333
	public ContactDetails getBillingDetails() {
185
		return billingDetails;
334
		return billingDetails;
186
	}
335
	}
187
 
336
 
-
 
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
 
188
	public static class ContactDetails{
354
	public static class ContactDetails{
189
		private String name;
355
		private String name;
190
		private String email;
356
		private String email;
191
		private String address1;
-
 
192
		private String address2;
357
		private String address;
193
		private String city;
358
		private String city;
194
		private String state;
359
		private String state;
195
		private String postalCode;
360
		private String postalCode;
196
		private String country;
361
		private String country;
197
		private String phone;
362
		private String phone;
198
		
363
		
199
		public ContactDetails(String name, String email, String address1, String address2,
364
		public ContactDetails(String name, String email, String address,
200
				String city, String state, String postalCode, String country,
365
				String city, String state, String postalCode, String country,
201
				String phone) {
366
				String phone) {
202
			this.name = name;
367
			this.name = name;
203
			this.email = email;
368
			this.email = email;
204
			this.address1 = address1;
-
 
205
			this.address2 = address2;
369
			this.address = address;
206
			this.city = city;
370
			this.city = city;
207
			this.state = state;
371
			this.state = state;
208
			this.postalCode = postalCode;
372
			this.postalCode = postalCode;
209
			this.country = country;
373
			this.country = country;
210
			this.phone = phone;
374
			this.phone = phone;
211
		}
375
		}
212
 
376
 
213
		@Override
377
		@Override
214
		public String toString() {
378
		public String toString() {
215
			return "ContactDetails [name=" + name + ", email=" + email
379
			return "ContactDetails [name=" + name + ", email=" + email
216
					+ ", address1=" + address1 + ",  address2=" + address2 + ",city=" + city + ", state="
380
					+ ", address=" + address + ", city=" + city + ", state="
217
					+ state + ", postalCode=" + postalCode + ", country="
381
					+ state + ", postalCode=" + postalCode + ", country="
218
					+ country + ", phone=" + phone + "]";
382
					+ country + ", phone=" + phone + "]";
219
		}
383
		}
220
		
384
		
221
		public String getName() {
385
		public String getName() {
Line 224... Line 388...
224
 
388
 
225
		public String getEmail() {
389
		public String getEmail() {
226
			return email;
390
			return email;
227
		}
391
		}
228
 
392
 
229
		public String getAddress1() {
393
		public String getAddress() {
230
			return address1;
-
 
231
		}
-
 
232
 
-
 
233
		public String getAddress2() {
-
 
234
			return address2;
394
			return address;
235
		}
395
		}
236
 
396
 
237
		public String getCity() {
397
		public String getCity() {
238
			return city;
398
			return city;
239
		}
399
		}
Line 252... Line 412...
252
 
412
 
253
		public String getPhone() {
413
		public String getPhone() {
254
			return phone;
414
			return phone;
255
		}
415
		}
256
	}
416
	}
-
 
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
	}
257
}
581
}