Subversion Repositories SmartDukaan

Rev

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