Subversion Repositories SmartDukaan

Rev

Rev 6442 | Rev 6808 | Go to most recent revision | Details | Compare with Previous | 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;
6442 rajveer 5
import java.util.Map;
6390 rajveer 6
 
7
import org.apache.log4j.Logger;
8
import org.apache.struts2.convention.annotation.InterceptorRef;
9
import org.apache.struts2.convention.annotation.InterceptorRefs;
10
import org.apache.struts2.convention.annotation.Result;
11
import org.apache.struts2.convention.annotation.Results;
12
 
13
import com.opensymphony.xwork2.ValidationAwareSupport;
14
 
15
import in.shop2020.config.ConfigException;
16
import in.shop2020.model.v1.order.LineItem;
17
import in.shop2020.model.v1.order.Order;
18
import in.shop2020.model.v1.order.Transaction;
19
import in.shop2020.payments.Attribute;
20
import in.shop2020.payments.Payment;
21
import in.shop2020.serving.services.IPaymentService;
22
import in.shop2020.thrift.clients.PaymentClient;
23
import in.shop2020.thrift.clients.TransactionClient;
24
import in.shop2020.thrift.clients.config.ConfigClient;
25
 
26
@SuppressWarnings("serial")
27
@InterceptorRefs({
28
    @InterceptorRef("myDefault"),
29
    @InterceptorRef("login")
30
})
31
@Results({
32
	@Result(name="shipping-redirect", type="redirectAction", 
33
    		params = {"actionName" , "shipping"})
34
})
35
public class InnovitiPayController extends ValidationAwareSupport{
36
 
37
	private static Logger log = Logger.getLogger(Class.class);
38
 
39
	private static String accountId;
40
 
6806 rajveer 41
	private static String returnUrl;
6390 rajveer 42
 
43
	private static String salt;
44
 
6806 rajveer 45
	private static String gatewayUrl;
46
 
47
	private static String subAccountId;
48
 
6390 rajveer 49
	static{
50
		try {
51
			accountId = ConfigClient.getClient().get("innoviti_account_id");
52
			returnUrl = ConfigClient.getClient().get("innoviti_return_url");
53
			salt = ConfigClient.getClient().get("innoviti_secret_key");
6806 rajveer 54
			subAccountId = ConfigClient.getClient().get("innoviti_sub_account_id");
55
			returnUrl = ConfigClient.getClient().get("innoviti_gateway_url");
6390 rajveer 56
		} catch (ConfigException e) {
57
			log.error("Unable to get Innoviti payment configuration.");
58
		}
59
	}
60
 
61
	private String id;
62
 
63
	private String paymentOption = null;
64
 
65
	private StringBuilder description;
66
 
67
	private double amount;
68
 
6442 rajveer 69
	private double emiAmount = 0;
70
 
6390 rajveer 71
	private ContactDetails billingDetails;
72
 
73
	public String show(){
74
		PaymentClient paymentServiceClient = null;
75
		Payment payment = null;
76
		try {
77
			long paymentId = Long.parseLong(this.id);
78
			paymentServiceClient = new PaymentClient();
79
			payment = paymentServiceClient.getClient().getPayment(paymentId);
80
		} catch (Exception e) {
81
			log.error("Error while getting payment client", e);
82
			addActionError("We are experiencing some problems. Please try later.");
83
			return "shipping-redirect";
84
		}
85
 
86
		Order order = null;
87
		try {
88
			long txnId = payment.getMerchantTxnId();
89
			TransactionClient transactionServiceClient = new TransactionClient();
90
			in.shop2020.model.v1.order.TransactionService.Client txnClient = transactionServiceClient.getClient();
91
			Transaction transaction = txnClient.getTransaction(txnId);
92
			order = transaction.getOrders().get(0);
6442 rajveer 93
			Map<Long, Double> miscCharges = txnClient.getMiscCharges(txnId);
94
			System.out.println(miscCharges);
95
			if(miscCharges != null & !miscCharges.isEmpty()){
96
				emiAmount = miscCharges.get(1L);
97
			}
6390 rajveer 98
		} catch (Exception e) {
99
			log.error("Error while getting transaction information", e);
100
			addActionError("We are experiencing some problems. Please try later.");
101
			return "shipping-redirect";
102
		}
103
 
104
		setDescription(order);
105
		setPaymentOption(payment);
106
 
107
		this.amount = payment.getAmount();
108
		this.billingDetails = new ContactDetails(order.getCustomer_name(),
109
				order.getCustomer_email(), order.getCustomer_address1(), order.getCustomer_address2(),
110
				order.getCustomer_city(), order.getCustomer_state(),
111
				order.getCustomer_pincode(), "India",
112
				order.getCustomer_mobilenumber());
113
 
114
		log.info(billingDetails);
115
 
116
		return "show";
117
	}
118
 
119
	public String getDescription(){
120
		if(this.description.length() >= 255)
121
			return this.description.substring(0, 255).trim();
122
		else
123
			return this.description.toString().trim();
124
	}
125
 
126
	private void setDescription(Order order){
127
		StringBuilder descriptionBuilder = new StringBuilder(255);
128
		for(LineItem line: order.getLineitems()){
129
			if(line.getBrand() != null){
130
				descriptionBuilder.append(line.getBrand() + " ");
131
			}
132
			if(line.getModel_name() != null){
133
				descriptionBuilder.append(line.getModel_name() + " "); 
134
			}
135
			if(line.getModel_number() != null){
136
				descriptionBuilder.append(line.getModel_number() + " ");
137
			}
138
			if(line.getColor() != null){
139
				descriptionBuilder.append(line.getColor() + " ");
140
			}
141
		}
142
		String desc = descriptionBuilder.toString();
143
		desc = desc.replaceAll("[^a-zA-Z0-9\\s\\-\\@\\/\\.]", "");
144
		this.description = new StringBuilder(desc);
145
	}
146
 
147
	public void setId(String id) {
148
		this.id = id;
149
	}
150
 
151
	public String getId() {
152
		return id;
153
	}
154
 
155
	public String getAccountId() {
156
		return accountId;
157
	}
158
 
6806 rajveer 159
	public String getSubAccountId() {
160
		return subAccountId;
161
	}
162
 
6390 rajveer 163
	public static String getReturnUrl() {
164
		return returnUrl;
165
	}
166
 
6806 rajveer 167
	public String getGatewayUrl(){
168
		return gatewayUrl;
169
	}
170
 
6390 rajveer 171
	public double getAmount() {
172
		return amount;
173
	}
174
 
175
	public void setPaymentOption(Payment payment) {
176
		this.paymentOption = null;
177
		List<Attribute> attributes = payment.getAttributes();
178
		if(attributes == null)
179
			return;
180
		for(Attribute attr : attributes){
181
			if(attr.getName().equals(IPaymentService.PAYMENT_METHOD))
182
				this.paymentOption = attr.getValue();
183
		}
184
	}
185
 
186
 
187
	public String getPaymentOption(){
188
		return paymentOption;
189
	}
190
 
191
	public String getProcessingCode(){
6442 rajveer 192
		return paymentOption;
6390 rajveer 193
	}
194
 
6442 rajveer 195
	public String isCtx(){
196
		return "YES";
197
	}
198
 
199
	public double getCtxProcessingFee(){
200
		return emiAmount;
201
	}
202
 
203
	public double getCtxInterest(){
204
		return ProceedToPayController.getInterestRate(Long.parseLong(paymentOption));
205
	}
206
 
6390 rajveer 207
	public String getSecureHash() throws NoSuchAlgorithmException{
208
//		MD5(orderId "|" merchantId "|" subMerchantId "|" amt "|" cur "|" proSku "|" Cname "|" mobile "|" emailId "|" redirUrl "|" 123^~abc%) (Salt Value Decide by Merchant and uniPAY-Net))
6806 rajveer 209
		String pass = id + "|" + accountId + "|" +  subAccountId  + "|" + amount  + "|" +  "INR" + "|" + getDescription() + "|" +  billingDetails.getName() + "|" + billingDetails.getPhone() + "|" + billingDetails.getEmail() + "|" + returnUrl + "|" + salt;
6390 rajveer 210
		System.out.println(pass);
211
		MessageDigest md = MessageDigest.getInstance("MD5");
212
		md.update(pass.getBytes(), 0, pass.getBytes().length);
213
		byte[] mdbytes = md.digest();
214
		//	convert the byte to hex format method
215
		StringBuffer sb = new StringBuffer();
216
		for (int i = 0; i < mdbytes.length; i++) {
217
			sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
218
		}
219
		return sb.toString();
220
	}
221
 
222
	public ContactDetails getBillingDetails() {
223
		return billingDetails;
224
	}
225
 
6806 rajveer 226
 
6390 rajveer 227
	public static void main(String[] args) throws NoSuchAlgorithmException {
6432 rajveer 228
		//String pass = "410|1234567800|uniPAY|5985|INR|||||RedirectUrl|123^~abc%)";
229
		//String pass = "113724|544433321212272|3001|3586|INR|Micromax Smarty A25 Black|Rajveer|1111111111|rajveer.singh@shop2020.in|http://www.shop2020.in/innoviti-pay-response|123^~uni%";
230
		String pass = "113734|544433321212272|3001|3586|INR|Micromax Smarty A25 Black|Rajveer|1111111111|rajveer.singh@shop2020.in|http://www.shop2020.in/innoviti-pay-response|123^~uni%";
231
		//5fc923e520bce47ff14b3aa1a5245287
232
          //5fc923e520bce47ff14b3aa1a5245287
233
		//15d4dbc305d0b56ce1973726bdee526d
234
		//db4d6309123fd1543882eba3123f52b4
235
		//93d97a5f16fb132c8728ee799e81915e
236
		//bf29c85dcd387c1160fe64a2af8b0463
237
 
238
 
6390 rajveer 239
		System.out.println(pass);
240
		MessageDigest md = MessageDigest.getInstance("MD5");
241
		md.update(pass.getBytes(), 0, pass.getBytes().length);
242
		byte[] mdbytes = md.digest();
243
		//	convert the byte to hex format method
244
		StringBuffer sb = new StringBuffer();
245
		for (int i = 0; i < mdbytes.length; i++) {
246
			sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
247
		}
248
		System.out.println(sb.toString());
249
	}
250
 
251
	public static class ContactDetails{
252
		private String name;
253
		private String email;
254
		private String address1;
255
		private String address2;
256
		private String city;
257
		private String state;
258
		private String postalCode;
259
		private String country;
260
		private String phone;
261
 
262
		public ContactDetails(String name, String email, String address1, String address2,
263
				String city, String state, String postalCode, String country,
264
				String phone) {
265
			this.name = name;
266
			this.email = email;
267
			this.address1 = address1;
268
			this.address2 = address2;
269
			this.city = city;
270
			this.state = state;
271
			this.postalCode = postalCode;
272
			this.country = country;
273
			this.phone = phone;
274
		}
275
 
276
		@Override
277
		public String toString() {
278
			return "ContactDetails [name=" + name + ", email=" + email
279
					+ ", address1=" + address1 + ",  address2=" + address2 + ",city=" + city + ", state="
280
					+ state + ", postalCode=" + postalCode + ", country="
281
					+ country + ", phone=" + phone + "]";
282
		}
283
 
284
		public String getName() {
285
			return name;
286
		}
287
 
288
		public String getEmail() {
289
			return email;
290
		}
291
 
292
		public String getAddress1() {
293
			return address1;
294
		}
295
 
296
		public String getAddress2() {
297
			return address2;
298
		}
299
 
300
		public String getCity() {
301
			return city;
302
		}
303
 
304
		public String getState() {
305
			return state;
306
		}
307
 
308
		public String getPostalCode() {
309
			return postalCode;
310
		}
311
 
312
		public String getCountry() {
313
			return country;
314
		}
315
 
316
		public String getPhone() {
317
			return phone;
318
		}
319
	}
320
}