Subversion Repositories SmartDukaan

Rev

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