Subversion Repositories SmartDukaan

Rev

Rev 3126 | Rev 6050 | 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;
1905 chandransh 13
 
2118 chandransh 14
import com.opensymphony.xwork2.ValidationAwareSupport;
15
 
1905 chandransh 16
import in.shop2020.config.ConfigException;
2118 chandransh 17
import in.shop2020.model.v1.order.LineItem;
1905 chandransh 18
import in.shop2020.model.v1.order.Order;
19
import in.shop2020.model.v1.order.Transaction;
2159 chandransh 20
import in.shop2020.payments.Attribute;
1905 chandransh 21
import in.shop2020.payments.Payment;
2159 chandransh 22
import in.shop2020.serving.services.IPaymentService;
3126 rajveer 23
import in.shop2020.thrift.clients.PaymentClient;
24
import in.shop2020.thrift.clients.TransactionClient;
1905 chandransh 25
import in.shop2020.thrift.clients.config.ConfigClient;
26
 
2118 chandransh 27
@SuppressWarnings("serial")
28
@InterceptorRefs({
29
    @InterceptorRef("myDefault"),
30
    @InterceptorRef("login")
31
})
32
@Results({
33
	@Result(name="shipping-redirect", type="redirectAction", 
34
    		params = {"actionName" , "shipping"})
35
})
36
public class EbsPayController extends ValidationAwareSupport{
1905 chandransh 37
 
38
	private static Logger log = Logger.getLogger(Class.class);
39
 
40
	private static String accountId;
41
 
42
	private static String returnUrl;
43
 
44
	private static String mode;
45
 
4566 rajveer 46
	private static String ebsSecretKey;
47
 
1905 chandransh 48
	static{
49
		try {
50
			accountId = ConfigClient.getClient().get("ebs_account_id");
51
			returnUrl = ConfigClient.getClient().get("ebs_return_url");
52
			mode = ConfigClient.getClient().get("ebs_pay_mode");
4566 rajveer 53
			ebsSecretKey = ConfigClient.getClient().get("ebs_secret_key");
1905 chandransh 54
		} catch (ConfigException e) {
2046 chandransh 55
			mode = "LIVE";
1905 chandransh 56
			log.error("Unable to get EBS payment configuration.");
57
		}
58
	}
59
 
60
	private String id;
61
 
2577 chandransh 62
	private String paymentOption = null;
2159 chandransh 63
 
2118 chandransh 64
	private StringBuilder description;
65
 
1905 chandransh 66
	private double amount;
67
 
68
	private ContactDetails billingDetails;
69
 
70
	public String show(){
3126 rajveer 71
		PaymentClient paymentServiceClient = null;
1905 chandransh 72
		Payment payment = null;
73
		try {
74
			long paymentId = Long.parseLong(this.id);
3126 rajveer 75
			paymentServiceClient = new PaymentClient();
1905 chandransh 76
			payment = paymentServiceClient.getClient().getPayment(paymentId);
77
		} catch (Exception e) {
2118 chandransh 78
			log.error("Error while getting payment client", e);
79
			addActionError("We are experiencing some problems. Please try later.");
80
			return "shipping-redirect";
1905 chandransh 81
		}
82
 
83
		Order order = null;
84
		try {
85
			long txnId = payment.getMerchantTxnId();
3126 rajveer 86
			TransactionClient transactionServiceClient = new TransactionClient();
1905 chandransh 87
			in.shop2020.model.v1.order.TransactionService.Client txnClient = transactionServiceClient.getClient();
88
			Transaction transaction = txnClient.getTransaction(txnId);
89
			order = transaction.getOrders().get(0);
90
		} catch (Exception e) {
2118 chandransh 91
			log.error("Error while getting transaction information", e);
92
			addActionError("We are experiencing some problems. Please try later.");
93
			return "shipping-redirect";
1905 chandransh 94
		}
95
 
2118 chandransh 96
		setDescription(order);
2159 chandransh 97
		setPaymentOption(payment);
2118 chandransh 98
 
1905 chandransh 99
		this.amount = payment.getAmount();
100
		this.billingDetails = new ContactDetails(order.getCustomer_name(),
101
				order.getCustomer_email(), order.getCustomer_address1(),
102
				order.getCustomer_city(), order.getCustomer_state(),
103
				order.getCustomer_pincode(), "IND",
104
				order.getCustomer_mobilenumber());
105
 
106
		log.info(billingDetails);
107
 
108
		return "show";
109
	}
110
 
2118 chandransh 111
	public String getDescription(){
112
		if(this.description.length() >= 255)
113
			return this.description.substring(0, 255);
114
		else
115
			return this.description.toString();
116
	}
117
 
118
	private void setDescription(Order order){
2534 chandransh 119
		StringBuilder descriptionBuilder = new StringBuilder(255);
2118 chandransh 120
		for(LineItem line: order.getLineitems()){
121
			if(line.getBrand() != null){
2534 chandransh 122
				descriptionBuilder.append(line.getBrand() + " ");
2118 chandransh 123
			}
124
			if(line.getModel_name() != null){
2534 chandransh 125
				descriptionBuilder.append(line.getModel_name() + " "); 
2118 chandransh 126
			}
127
			if(line.getModel_number() != null){
2534 chandransh 128
				descriptionBuilder.append(line.getModel_number() + " ");
2118 chandransh 129
			}
130
			if(line.getColor() != null){
2534 chandransh 131
				descriptionBuilder.append(line.getColor() + " ");
2118 chandransh 132
			}
133
		}
2534 chandransh 134
		String desc = descriptionBuilder.toString();
135
		desc = desc.replaceAll("[^a-zA-Z0-9\\s\\-\\@\\/\\.]", "");
136
		this.description = new StringBuilder(desc);
2118 chandransh 137
	}
138
 
1905 chandransh 139
	public void setId(String id) {
140
		this.id = id;
141
	}
142
 
143
	public String getId() {
144
		return id;
145
	}
146
 
147
	public String getAccountId() {
148
		return accountId;
149
	}
150
 
151
	public static String getReturnUrl() {
152
		return returnUrl;
153
	}
154
 
155
	public static String getMode() {
156
		return mode;
157
	}
158
 
159
	public double getAmount() {
160
		return amount;
161
	}
2159 chandransh 162
 
163
	public void setPaymentOption(Payment payment) {
2199 chandransh 164
		this.paymentOption = null;
2577 chandransh 165
		List<Attribute> attributes = payment.getAttributes();
166
		if(attributes == null)
167
			return;
168
		for(Attribute attr : attributes){
2159 chandransh 169
			if(attr.getName().equals(IPaymentService.PAYMENT_METHOD))
170
				this.paymentOption = attr.getValue();
171
		}
172
	}
1905 chandransh 173
 
2159 chandransh 174
	public String getPaymentOption(){
175
		return paymentOption;
176
	}
4566 rajveer 177
 
178
	public String getSecureHash() throws NoSuchAlgorithmException{
179
		String pass = ebsSecretKey + "|" + accountId + "|" + amount + "|" + id + "|" + returnUrl + "|" + mode;
180
		MessageDigest m = MessageDigest.getInstance("MD5");
181
		byte[] data = pass.getBytes();
182
		m.update(data,0,data.length);
183
		BigInteger i = new BigInteger(1,m.digest());
184
		String secureHash = String.format("%1$032X", i);
185
		return secureHash;
186
	}
2159 chandransh 187
 
1905 chandransh 188
	public ContactDetails getBillingDetails() {
189
		return billingDetails;
190
	}
191
 
192
	public static class ContactDetails{
193
		private String name;
194
		private String email;
195
		private String address;
196
		private String city;
197
		private String state;
198
		private String postalCode;
199
		private String country;
200
		private String phone;
201
 
202
		public ContactDetails(String name, String email, String address,
203
				String city, String state, String postalCode, String country,
204
				String phone) {
205
			this.name = name;
206
			this.email = email;
207
			this.address = address;
208
			this.city = city;
209
			this.state = state;
210
			this.postalCode = postalCode;
211
			this.country = country;
212
			this.phone = phone;
213
		}
214
 
215
		@Override
216
		public String toString() {
217
			return "ContactDetails [name=" + name + ", email=" + email
218
					+ ", address=" + address + ", city=" + city + ", state="
219
					+ state + ", postalCode=" + postalCode + ", country="
220
					+ country + ", phone=" + phone + "]";
221
		}
222
 
223
		public String getName() {
224
			return name;
225
		}
226
 
227
		public String getEmail() {
228
			return email;
229
		}
230
 
231
		public String getAddress() {
232
			return address;
233
		}
234
 
235
		public String getCity() {
236
			return city;
237
		}
238
 
239
		public String getState() {
240
			return state;
241
		}
242
 
243
		public String getPostalCode() {
244
			return postalCode;
245
		}
246
 
247
		public String getCountry() {
248
			return country;
249
		}
250
 
251
		public String getPhone() {
252
			return phone;
253
		}
254
	}
255
}