Subversion Repositories SmartDukaan

Rev

Rev 6432 | Go to most recent revision | Details | Last modification | View Log | RSS feed

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