Subversion Repositories SmartDukaan

Rev

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