Subversion Repositories SmartDukaan

Rev

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