Subversion Repositories SmartDukaan

Rev

Rev 6060 | Rev 13287 | 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;
2
import java.security.MessageDigest;
3
import java.security.NoSuchAlgorithmException;
4
import java.util.List;
5
 
6
import org.apache.log4j.Logger;
7
import org.apache.struts2.convention.annotation.InterceptorRef;
8
import org.apache.struts2.convention.annotation.InterceptorRefs;
9
import org.apache.struts2.convention.annotation.Result;
10
import org.apache.struts2.convention.annotation.Results;
11
 
12
import com.opensymphony.xwork2.ValidationAwareSupport;
13
 
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")
26
@InterceptorRefs({
27
    @InterceptorRef("myDefault"),
28
    @InterceptorRef("login")
29
})
30
@Results({
31
	@Result(name="shipping-redirect", type="redirectAction", 
32
    		params = {"actionName" , "shipping"})
33
})
34
public class PayuPayController extends ValidationAwareSupport{
35
 
36
	private static Logger log = Logger.getLogger(Class.class);
37
 
6062 rajveer 38
	private static String accountId;
6060 rajveer 39
 
40
	private static String returnUrl = "http://local.shop2020.in:8080/payu-pay-response";
41
 
6062 rajveer 42
	private static String salt;
6060 rajveer 43
 
44
	static{
45
		try {
46
			accountId = ConfigClient.getClient().get("payu_account_id");
47
			returnUrl = ConfigClient.getClient().get("payu_return_url");
6062 rajveer 48
			salt = ConfigClient.getClient().get("payu_secret_key");
6060 rajveer 49
			//"https://test.payu.in/_payment";
50
		} catch (ConfigException e) {
51
			log.error("Unable to get EBS payment configuration.");
52
		}
53
	}
54
 
55
	private String id;
56
 
57
	private String paymentOption = null;
58
 
59
	private StringBuilder description;
60
 
61
	private double amount;
62
 
63
	private ContactDetails billingDetails;
64
 
65
	public String show(){
66
		PaymentClient paymentServiceClient = null;
67
		Payment payment = null;
68
		try {
69
			long paymentId = Long.parseLong(this.id);
70
			paymentServiceClient = new PaymentClient();
71
			payment = paymentServiceClient.getClient().getPayment(paymentId);
72
		} catch (Exception e) {
73
			log.error("Error while getting payment client", e);
74
			addActionError("We are experiencing some problems. Please try later.");
75
			return "shipping-redirect";
76
		}
77
 
78
		Order order = null;
79
		try {
80
			long txnId = payment.getMerchantTxnId();
81
			TransactionClient transactionServiceClient = new TransactionClient();
82
			in.shop2020.model.v1.order.TransactionService.Client txnClient = transactionServiceClient.getClient();
83
			Transaction transaction = txnClient.getTransaction(txnId);
84
			order = transaction.getOrders().get(0);
85
		} catch (Exception e) {
86
			log.error("Error while getting transaction information", e);
87
			addActionError("We are experiencing some problems. Please try later.");
88
			return "shipping-redirect";
89
		}
90
 
91
		setDescription(order);
92
		setPaymentOption(payment);
93
 
94
		this.amount = payment.getAmount();
95
		this.billingDetails = new ContactDetails(order.getCustomer_name(),
96
				order.getCustomer_email(), order.getCustomer_address1(), order.getCustomer_address2(),
97
				order.getCustomer_city(), order.getCustomer_state(),
98
				order.getCustomer_pincode(), "India",
99
				order.getCustomer_mobilenumber());
100
 
101
		log.info(billingDetails);
102
 
103
		return "show";
104
	}
105
 
106
	public String getDescription(){
107
		if(this.description.length() >= 255)
108
			return this.description.substring(0, 255).trim();
109
		else
110
			return this.description.toString().trim();
111
	}
112
 
113
	private void setDescription(Order order){
114
		StringBuilder descriptionBuilder = new StringBuilder(255);
115
		for(LineItem line: order.getLineitems()){
116
			if(line.getBrand() != null){
117
				descriptionBuilder.append(line.getBrand() + " ");
118
			}
119
			if(line.getModel_name() != null){
120
				descriptionBuilder.append(line.getModel_name() + " "); 
121
			}
122
			if(line.getModel_number() != null){
123
				descriptionBuilder.append(line.getModel_number() + " ");
124
			}
125
			if(line.getColor() != null){
126
				descriptionBuilder.append(line.getColor() + " ");
127
			}
128
		}
129
		String desc = descriptionBuilder.toString();
130
		desc = desc.replaceAll("[^a-zA-Z0-9\\s\\-\\@\\/\\.]", "");
131
		this.description = new StringBuilder(desc);
132
	}
133
 
134
	public void setId(String id) {
135
		this.id = id;
136
	}
137
 
138
	public String getId() {
139
		return id;
140
	}
141
 
142
	public String getAccountId() {
143
		return accountId;
144
	}
145
 
146
	public static String getReturnUrl() {
147
		return returnUrl;
148
	}
149
 
150
	public double getAmount() {
151
		return amount;
152
	}
153
 
154
	public void setPaymentOption(Payment payment) {
155
		this.paymentOption = null;
156
		List<Attribute> attributes = payment.getAttributes();
157
		if(attributes == null)
158
			return;
159
		for(Attribute attr : attributes){
160
			if(attr.getName().equals(IPaymentService.PAYMENT_METHOD))
161
				this.paymentOption = attr.getValue();
162
		}
163
	}
164
 
165
 
166
	public String getPaymentOption(){
167
		return paymentOption;
168
	}
169
 
170
	public String getSecureHash() throws NoSuchAlgorithmException{
171
		String pass = accountId + "|" + id + "|" + amount  + "|" + getDescription() + "|" +  billingDetails.getName() + "|" + billingDetails.getEmail() + "|||||||||||" + salt;
172
		System.out.println(pass);
173
		MessageDigest md = MessageDigest.getInstance("SHA-512");
174
		md.update(pass.getBytes(), 0, pass.getBytes().length);
175
		byte[] mdbytes = md.digest();
176
		//	convert the byte to hex format method
177
		StringBuffer sb = new StringBuffer();
178
		for (int i = 0; i < mdbytes.length; i++) {
179
			sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
180
		}
181
		return sb.toString();
182
	}
183
 
184
	public ContactDetails getBillingDetails() {
185
		return billingDetails;
186
	}
187
 
188
	public static class ContactDetails{
189
		private String name;
190
		private String email;
191
		private String address1;
192
		private String address2;
193
		private String city;
194
		private String state;
195
		private String postalCode;
196
		private String country;
197
		private String phone;
198
 
199
		public ContactDetails(String name, String email, String address1, String address2,
200
				String city, String state, String postalCode, String country,
201
				String phone) {
202
			this.name = name;
203
			this.email = email;
204
			this.address1 = address1;
205
			this.address2 = address2;
206
			this.city = city;
207
			this.state = state;
208
			this.postalCode = postalCode;
209
			this.country = country;
210
			this.phone = phone;
211
		}
212
 
213
		@Override
214
		public String toString() {
215
			return "ContactDetails [name=" + name + ", email=" + email
216
					+ ", address1=" + address1 + ",  address2=" + address2 + ",city=" + city + ", state="
217
					+ state + ", postalCode=" + postalCode + ", country="
218
					+ country + ", phone=" + phone + "]";
219
		}
220
 
221
		public String getName() {
222
			return name;
223
		}
224
 
225
		public String getEmail() {
226
			return email;
227
		}
228
 
229
		public String getAddress1() {
230
			return address1;
231
		}
232
 
233
		public String getAddress2() {
234
			return address2;
235
		}
236
 
237
		public String getCity() {
238
			return city;
239
		}
240
 
241
		public String getState() {
242
			return state;
243
		}
244
 
245
		public String getPostalCode() {
246
			return postalCode;
247
		}
248
 
249
		public String getCountry() {
250
			return country;
251
		}
252
 
253
		public String getPhone() {
254
			return phone;
255
		}
256
	}
257
}