Subversion Repositories SmartDukaan

Rev

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