Subversion Repositories SmartDukaan

Rev

Rev 6091 | Rev 6228 | 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
 
6215 anupam.sin 49
	private String phone;//This is used for recharge orders.
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();
149
        UserClient userClient;
150
        in.shop2020.model.v1.user.Address address = null;
151
        try {
152
            userClient = new UserClient();
153
            long addressId = userClient.getClient().getDefaultAddressId(rechargeOrder.getUserId());
154
            address = userClient.getClient().getAddressById(addressId);
155
        } catch (Exception e) {
156
            log.error("Unable to get address to put in billing details");
157
            e.printStackTrace();
158
        }
6215 anupam.sin 159
        this.billingDetails = new ContactDetails("",
160
                rechargeOrder.getUserEmailId(), "",
161
                "", "",
162
                "", "IND",
163
                getPhone());
6050 anupam.sin 164
 
6215 anupam.sin 165
        log.info("Billing details of recharge order " + rechargeOrder.getDisplayId() + " : " + billingDetails);
6050 anupam.sin 166
 
167
        return "show";
168
    }
1905 chandransh 169
 
2118 chandransh 170
	public String getDescription(){
171
		if(this.description.length() >= 255)
172
			return this.description.substring(0, 255);
173
		else
174
			return this.description.toString();
175
	}
176
 
177
	private void setDescription(Order order){
2534 chandransh 178
		StringBuilder descriptionBuilder = new StringBuilder(255);
2118 chandransh 179
		for(LineItem line: order.getLineitems()){
180
			if(line.getBrand() != null){
2534 chandransh 181
				descriptionBuilder.append(line.getBrand() + " ");
2118 chandransh 182
			}
183
			if(line.getModel_name() != null){
2534 chandransh 184
				descriptionBuilder.append(line.getModel_name() + " "); 
2118 chandransh 185
			}
186
			if(line.getModel_number() != null){
2534 chandransh 187
				descriptionBuilder.append(line.getModel_number() + " ");
2118 chandransh 188
			}
189
			if(line.getColor() != null){
2534 chandransh 190
				descriptionBuilder.append(line.getColor() + " ");
2118 chandransh 191
			}
192
		}
2534 chandransh 193
		String desc = descriptionBuilder.toString();
194
		desc = desc.replaceAll("[^a-zA-Z0-9\\s\\-\\@\\/\\.]", "");
195
		this.description = new StringBuilder(desc);
2118 chandransh 196
	}
197
 
1905 chandransh 198
	public void setId(String id) {
199
		this.id = id;
200
	}
201
 
202
	public String getId() {
203
		return id;
204
	}
205
 
206
	public String getAccountId() {
207
		return accountId;
208
	}
209
 
210
	public static String getReturnUrl() {
211
		return returnUrl;
212
	}
213
 
214
	public static String getMode() {
215
		return mode;
216
	}
217
 
218
	public double getAmount() {
219
		return amount;
220
	}
2159 chandransh 221
 
222
	public void setPaymentOption(Payment payment) {
2199 chandransh 223
		this.paymentOption = null;
2577 chandransh 224
		List<Attribute> attributes = payment.getAttributes();
225
		if(attributes == null)
226
			return;
227
		for(Attribute attr : attributes){
2159 chandransh 228
			if(attr.getName().equals(IPaymentService.PAYMENT_METHOD))
229
				this.paymentOption = attr.getValue();
230
		}
231
	}
1905 chandransh 232
 
2159 chandransh 233
	public String getPaymentOption(){
234
		return paymentOption;
235
	}
4566 rajveer 236
 
237
	public String getSecureHash() throws NoSuchAlgorithmException{
238
		String pass = ebsSecretKey + "|" + accountId + "|" + amount + "|" + id + "|" + returnUrl + "|" + mode;
239
		MessageDigest m = MessageDigest.getInstance("MD5");
240
		byte[] data = pass.getBytes();
241
		m.update(data,0,data.length);
242
		BigInteger i = new BigInteger(1,m.digest());
243
		String secureHash = String.format("%1$032X", i);
244
		return secureHash;
245
	}
2159 chandransh 246
 
1905 chandransh 247
	public ContactDetails getBillingDetails() {
248
		return billingDetails;
249
	}
250
 
6215 anupam.sin 251
	public void setPhone(String phone) {
252
        this.phone = phone;
253
    }
254
 
255
 
256
    public String getPhone() {
257
        return phone;
258
    }
259
 
260
    public static class ContactDetails{
1905 chandransh 261
		private String name;
262
		private String email;
263
		private String address;
264
		private String city;
265
		private String state;
266
		private String postalCode;
267
		private String country;
268
		private String phone;
269
 
270
		public ContactDetails(String name, String email, String address,
271
				String city, String state, String postalCode, String country,
272
				String phone) {
273
			this.name = name;
274
			this.email = email;
275
			this.address = address;
276
			this.city = city;
277
			this.state = state;
278
			this.postalCode = postalCode;
279
			this.country = country;
280
			this.phone = phone;
281
		}
282
 
283
		@Override
284
		public String toString() {
285
			return "ContactDetails [name=" + name + ", email=" + email
286
					+ ", address=" + address + ", city=" + city + ", state="
287
					+ state + ", postalCode=" + postalCode + ", country="
288
					+ country + ", phone=" + phone + "]";
289
		}
290
 
291
		public String getName() {
292
			return name;
293
		}
294
 
295
		public String getEmail() {
296
			return email;
297
		}
298
 
299
		public String getAddress() {
300
			return address;
301
		}
302
 
303
		public String getCity() {
304
			return city;
305
		}
306
 
307
		public String getState() {
308
			return state;
309
		}
310
 
311
		public String getPostalCode() {
312
			return postalCode;
313
		}
314
 
315
		public String getCountry() {
316
			return country;
317
		}
318
 
319
		public String getPhone() {
320
			return phone;
321
		}
322
	}
323
}