Subversion Repositories SmartDukaan

Rev

Rev 10493 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1905 chandransh 1
package in.shop2020.serving.controllers;
2
 
4566 rajveer 3
import java.math.BigInteger;
4
import java.security.MessageDigest;
5
import java.security.NoSuchAlgorithmException;
2577 chandransh 6
import java.util.List;
7
 
1905 chandransh 8
import org.apache.log4j.Logger;
2118 chandransh 9
import org.apache.struts2.convention.annotation.InterceptorRef;
10
import org.apache.struts2.convention.annotation.InterceptorRefs;
11
import org.apache.struts2.convention.annotation.Result;
12
import org.apache.struts2.convention.annotation.Results;
6050 anupam.sin 13
import org.apache.thrift.transport.TTransportException;
1905 chandransh 14
 
2118 chandransh 15
import com.opensymphony.xwork2.ValidationAwareSupport;
16
 
1905 chandransh 17
import in.shop2020.config.ConfigException;
2118 chandransh 18
import in.shop2020.model.v1.order.LineItem;
1905 chandransh 19
import in.shop2020.model.v1.order.Order;
6050 anupam.sin 20
import in.shop2020.model.v1.order.RechargeOrder;
1905 chandransh 21
import in.shop2020.model.v1.order.Transaction;
2159 chandransh 22
import in.shop2020.payments.Attribute;
1905 chandransh 23
import in.shop2020.payments.Payment;
2159 chandransh 24
import in.shop2020.serving.services.IPaymentService;
6050 anupam.sin 25
import in.shop2020.test.Address;
3126 rajveer 26
import in.shop2020.thrift.clients.PaymentClient;
27
import in.shop2020.thrift.clients.TransactionClient;
6050 anupam.sin 28
import in.shop2020.thrift.clients.UserClient;
1905 chandransh 29
import in.shop2020.thrift.clients.config.ConfigClient;
30
 
2118 chandransh 31
@SuppressWarnings("serial")
6091 anupam.sin 32
 
2118 chandransh 33
@Results({
6091 anupam.sin 34
	@Result(name="recharge-redirect", type="redirectAction", 
10118 amit.gupta 35
    		params = {"actionName" , "recharge"}),
36
	@Result(name="shipping-redirect", type="redirectAction", 
10124 amit.gupta 37
			params = {"actionName" , "shipping"})
2118 chandransh 38
})
39
public class EbsPayController extends ValidationAwareSupport{
1905 chandransh 40
 
41
	private static Logger log = Logger.getLogger(Class.class);
42
 
43
	private static String accountId;
44
 
45
	private static String returnUrl;
46
 
47
	private static String mode;
48
 
4566 rajveer 49
	private static String ebsSecretKey;
50
 
6228 anupam.sin 51
	private String phone;//This is used for recharge orders of value less than 1000.
6215 anupam.sin 52
 
1905 chandransh 53
	static{
54
		try {
55
			accountId = ConfigClient.getClient().get("ebs_account_id");
56
			returnUrl = ConfigClient.getClient().get("ebs_return_url");
57
			mode = ConfigClient.getClient().get("ebs_pay_mode");
4566 rajveer 58
			ebsSecretKey = ConfigClient.getClient().get("ebs_secret_key");
1905 chandransh 59
		} catch (ConfigException e) {
2046 chandransh 60
			mode = "LIVE";
1905 chandransh 61
			log.error("Unable to get EBS payment configuration.");
62
		}
63
	}
64
 
65
	private String id;
66
 
2577 chandransh 67
	private String paymentOption = null;
2159 chandransh 68
 
2118 chandransh 69
	private StringBuilder description;
70
 
1905 chandransh 71
	private double amount;
72
 
73
	private ContactDetails billingDetails;
74
 
75
	public String show(){
3126 rajveer 76
		PaymentClient paymentServiceClient = null;
1905 chandransh 77
		Payment payment = null;
78
		try {
79
			long paymentId = Long.parseLong(this.id);
3126 rajveer 80
			paymentServiceClient = new PaymentClient();
1905 chandransh 81
			payment = paymentServiceClient.getClient().getPayment(paymentId);
82
		} catch (Exception e) {
2118 chandransh 83
			log.error("Error while getting payment client", e);
84
			addActionError("We are experiencing some problems. Please try later.");
85
			return "shipping-redirect";
1905 chandransh 86
		}
87
 
88
		Order order = null;
89
		try {
90
			long txnId = payment.getMerchantTxnId();
3126 rajveer 91
			TransactionClient transactionServiceClient = new TransactionClient();
1905 chandransh 92
			in.shop2020.model.v1.order.TransactionService.Client txnClient = transactionServiceClient.getClient();
93
			Transaction transaction = txnClient.getTransaction(txnId);
94
			order = transaction.getOrders().get(0);
95
		} catch (Exception e) {
2118 chandransh 96
			log.error("Error while getting transaction information", e);
97
			addActionError("We are experiencing some problems. Please try later.");
98
			return "shipping-redirect";
1905 chandransh 99
		}
100
 
2118 chandransh 101
		setDescription(order);
2159 chandransh 102
		setPaymentOption(payment);
2118 chandransh 103
 
1905 chandransh 104
		this.amount = payment.getAmount();
105
		this.billingDetails = new ContactDetails(order.getCustomer_name(),
106
				order.getCustomer_email(), order.getCustomer_address1(),
107
				order.getCustomer_city(), order.getCustomer_state(),
108
				order.getCustomer_pincode(), "IND",
109
				order.getCustomer_mobilenumber());
110
 
111
		log.info(billingDetails);
112
 
113
		return "show";
114
	}
6050 anupam.sin 115
 
116
 
117
	/**
118
	 * This method is used for Recharge payments. It is being called by RechargePaymentController.
119
	 * @return
120
	 */
121
	public String edit(){
122
        PaymentClient paymentServiceClient = null;
123
        Payment payment = null;
124
        try {
125
            long paymentId = Long.parseLong(this.id);
126
            paymentServiceClient = new PaymentClient();
127
            payment = paymentServiceClient.getClient().getPayment(paymentId);
128
        } catch (Exception e) {
129
            log.error("Error while getting payment client", e);
130
            addActionError("We are experiencing some problems. Please try later.");
6091 anupam.sin 131
            return "recharge-redirect";
6050 anupam.sin 132
        }
133
 
134
        RechargeOrder rechargeOrder = null;
135
        try {
136
            long txnId = payment.getMerchantTxnId();
137
            TransactionClient transactionServiceClient = new TransactionClient();
138
            in.shop2020.model.v1.order.TransactionService.Client txnClient = transactionServiceClient.getClient();
139
            rechargeOrder = txnClient.getRechargeOrdersForTransaction(txnId);
140
        } catch (Exception e) {
141
            log.error("Error while getting transaction information", e);
142
            addActionError("We are experiencing some problems. Please try later.");
6091 anupam.sin 143
            return "recharge-redirect";
6050 anupam.sin 144
        }
145
 
146
        String desc = "Recharge for Rs. " + rechargeOrder.getTotalAmount() + ", operator : " + rechargeOrder.getOperatorId();
147
        this.description = new StringBuilder(desc);
148
        setPaymentOption(payment);
149
 
150
        this.amount = payment.getAmount();
6233 anupam.sin 151
        if(getPhone().isEmpty()) {
6228 anupam.sin 152
            UserClient userClient;
153
            in.shop2020.model.v1.user.Address address = null;
154
            try {
155
                userClient = new UserClient();
156
                long addressId = userClient.getClient().getDefaultAddressId(rechargeOrder.getUserId());
157
                address = userClient.getClient().getAddressById(addressId);
158
                this.billingDetails = new ContactDetails(address.getName(),
159
                        rechargeOrder.getUserEmailId(), address.getLine1(),
160
                        address.getCity(), address.getState(),
161
                        address.getPin(), "IND",
162
                        address.getPhone());
163
            } catch (Exception e) {
164
                log.error("Unable to get address to put in billing details");
165
                e.printStackTrace();
166
            }
167
        } else {
168
        this.billingDetails = new ContactDetails(rechargeOrder.getUserEmailId(),
169
                rechargeOrder.getUserEmailId(), id,
170
                id, id,
11416 amit.gupta 171
                id, "IND",
6228 anupam.sin 172
                getPhone());
6050 anupam.sin 173
        }
6215 anupam.sin 174
        log.info("Billing details of recharge order " + rechargeOrder.getDisplayId() + " : " + billingDetails);
6050 anupam.sin 175
        return "show";
176
    }
1905 chandransh 177
 
2118 chandransh 178
	public String getDescription(){
179
		if(this.description.length() >= 255)
180
			return this.description.substring(0, 255);
181
		else
182
			return this.description.toString();
183
	}
184
 
185
	private void setDescription(Order order){
2534 chandransh 186
		StringBuilder descriptionBuilder = new StringBuilder(255);
2118 chandransh 187
		for(LineItem line: order.getLineitems()){
188
			if(line.getBrand() != null){
2534 chandransh 189
				descriptionBuilder.append(line.getBrand() + " ");
2118 chandransh 190
			}
191
			if(line.getModel_name() != null){
2534 chandransh 192
				descriptionBuilder.append(line.getModel_name() + " "); 
2118 chandransh 193
			}
194
			if(line.getModel_number() != null){
2534 chandransh 195
				descriptionBuilder.append(line.getModel_number() + " ");
2118 chandransh 196
			}
197
			if(line.getColor() != null){
2534 chandransh 198
				descriptionBuilder.append(line.getColor() + " ");
2118 chandransh 199
			}
200
		}
2534 chandransh 201
		String desc = descriptionBuilder.toString();
202
		desc = desc.replaceAll("[^a-zA-Z0-9\\s\\-\\@\\/\\.]", "");
203
		this.description = new StringBuilder(desc);
2118 chandransh 204
	}
205
 
1905 chandransh 206
	public void setId(String id) {
207
		this.id = id;
208
	}
209
 
210
	public String getId() {
211
		return id;
212
	}
213
 
214
	public String getAccountId() {
215
		return accountId;
216
	}
217
 
218
	public static String getReturnUrl() {
219
		return returnUrl;
220
	}
221
 
222
	public static String getMode() {
223
		return mode;
224
	}
225
 
226
	public double getAmount() {
227
		return amount;
228
	}
2159 chandransh 229
 
230
	public void setPaymentOption(Payment payment) {
2199 chandransh 231
		this.paymentOption = null;
2577 chandransh 232
		List<Attribute> attributes = payment.getAttributes();
233
		if(attributes == null)
234
			return;
235
		for(Attribute attr : attributes){
2159 chandransh 236
			if(attr.getName().equals(IPaymentService.PAYMENT_METHOD))
237
				this.paymentOption = attr.getValue();
238
		}
239
	}
1905 chandransh 240
 
2159 chandransh 241
	public String getPaymentOption(){
242
		return paymentOption;
243
	}
4566 rajveer 244
 
245
	public String getSecureHash() throws NoSuchAlgorithmException{
246
		String pass = ebsSecretKey + "|" + accountId + "|" + amount + "|" + id + "|" + returnUrl + "|" + mode;
10493 amit.gupta 247
		log.info("pass:" + pass);
4566 rajveer 248
		MessageDigest m = MessageDigest.getInstance("MD5");
249
		byte[] data = pass.getBytes();
250
		m.update(data,0,data.length);
251
		BigInteger i = new BigInteger(1,m.digest());
252
		String secureHash = String.format("%1$032X", i);
253
		return secureHash;
254
	}
2159 chandransh 255
 
1905 chandransh 256
	public ContactDetails getBillingDetails() {
257
		return billingDetails;
258
	}
259
 
6215 anupam.sin 260
	public void setPhone(String phone) {
261
        this.phone = phone;
262
    }
263
 
264
 
265
    public String getPhone() {
266
        return phone;
267
    }
268
 
269
    public static class ContactDetails{
1905 chandransh 270
		private String name;
271
		private String email;
272
		private String address;
273
		private String city;
274
		private String state;
275
		private String postalCode;
276
		private String country;
277
		private String phone;
278
 
279
		public ContactDetails(String name, String email, String address,
280
				String city, String state, String postalCode, String country,
281
				String phone) {
282
			this.name = name;
283
			this.email = email;
284
			this.address = address;
285
			this.city = city;
286
			this.state = state;
287
			this.postalCode = postalCode;
288
			this.country = country;
289
			this.phone = phone;
290
		}
291
 
292
		@Override
293
		public String toString() {
294
			return "ContactDetails [name=" + name + ", email=" + email
295
					+ ", address=" + address + ", city=" + city + ", state="
296
					+ state + ", postalCode=" + postalCode + ", country="
297
					+ country + ", phone=" + phone + "]";
298
		}
299
 
300
		public String getName() {
301
			return name;
302
		}
303
 
304
		public String getEmail() {
305
			return email;
306
		}
307
 
308
		public String getAddress() {
309
			return address;
310
		}
311
 
312
		public String getCity() {
313
			return city;
314
		}
315
 
316
		public String getState() {
317
			return state;
318
		}
319
 
320
		public String getPostalCode() {
321
			return postalCode;
322
		}
323
 
324
		public String getCountry() {
325
			return country;
326
		}
327
 
328
		public String getPhone() {
329
			return phone;
330
		}
331
	}
6903 anupam.sin 332
 
333
    public static void main(String[] args) {
334
        {
335
            String encrypted = "2be98afc86aa7f2e4cb79ac798cc2fd8a"; 
336
            BigInteger bi_r0 = new BigInteger("0933910847463829827159347601486730416058");
337
            /*String password = "shop2020";
338
 
339
            BigInteger bi_passwd = new BigInteger(password.getBytes());
340
            System.out.println(bi_passwd);
341
 
342
 
343
            BigInteger bi_r1 = bi_r0.xor(bi_passwd);
344
            System.out.println(bi_r1);
345
            System.out.println((bi_r1.toString(16)));
346
            */
347
            try
348
            {
349
              BigInteger bi_enc = new BigInteger(encrypted, 16);
350
              System.out.println(bi_enc);
351
              BigInteger bi_result = bi_enc.xor(bi_r0);
352
              System.out.println(bi_result);
353
              String str = new String(bi_result.toByteArray());
354
              System.out.println(str);
355
            } catch (Exception e) {
356
                System.out.println("oops");
357
            }
358
        }
359
    }
1905 chandransh 360
}