Subversion Repositories SmartDukaan

Rev

Rev 2159 | Rev 2534 | 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){
111
		this.description = new StringBuilder(255);
112
		for(LineItem line: order.getLineitems()){
113
			if(line.getBrand() != null){
114
				description.append(line.getBrand() + " ");
115
			}
116
			if(line.getModel_name() != null){
117
				description.append(line.getModel_name() + " "); 
118
			}
119
			if(line.getModel_number() != null){
120
				description.append(line.getModel_number() + " ");
121
			}
122
			if(line.getColor() != null){
123
				description.append(line.getColor() + " ");
124
			}
125
		}
126
	}
127
 
1905 chandransh 128
	public void setId(String id) {
129
		this.id = id;
130
	}
131
 
132
	public String getId() {
133
		return id;
134
	}
135
 
136
	public String getAccountId() {
137
		return accountId;
138
	}
139
 
140
	public static String getReturnUrl() {
141
		return returnUrl;
142
	}
143
 
144
	public static String getMode() {
145
		return mode;
146
	}
147
 
148
	public double getAmount() {
149
		return amount;
150
	}
2159 chandransh 151
 
152
	public void setPaymentOption(Payment payment) {
2199 chandransh 153
		this.paymentOption = null;
2159 chandransh 154
		for(Attribute attr : payment.getAttributes()){
155
			if(attr.getName().equals(IPaymentService.PAYMENT_METHOD))
156
				this.paymentOption = attr.getValue();
157
		}
158
	}
1905 chandransh 159
 
2159 chandransh 160
	public String getPaymentOption(){
161
		return paymentOption;
162
	}
163
 
1905 chandransh 164
	public ContactDetails getBillingDetails() {
165
		return billingDetails;
166
	}
167
 
168
	public static class ContactDetails{
169
		private String name;
170
		private String email;
171
		private String address;
172
		private String city;
173
		private String state;
174
		private String postalCode;
175
		private String country;
176
		private String phone;
177
 
178
		public ContactDetails(String name, String email, String address,
179
				String city, String state, String postalCode, String country,
180
				String phone) {
181
			this.name = name;
182
			this.email = email;
183
			this.address = address;
184
			this.city = city;
185
			this.state = state;
186
			this.postalCode = postalCode;
187
			this.country = country;
188
			this.phone = phone;
189
		}
190
 
191
		@Override
192
		public String toString() {
193
			return "ContactDetails [name=" + name + ", email=" + email
194
					+ ", address=" + address + ", city=" + city + ", state="
195
					+ state + ", postalCode=" + postalCode + ", country="
196
					+ country + ", phone=" + phone + "]";
197
		}
198
 
199
		public String getName() {
200
			return name;
201
		}
202
 
203
		public String getEmail() {
204
			return email;
205
		}
206
 
207
		public String getAddress() {
208
			return address;
209
		}
210
 
211
		public String getCity() {
212
			return city;
213
		}
214
 
215
		public String getState() {
216
			return state;
217
		}
218
 
219
		public String getPostalCode() {
220
			return postalCode;
221
		}
222
 
223
		public String getCountry() {
224
			return country;
225
		}
226
 
227
		public String getPhone() {
228
			return phone;
229
		}
230
	}
231
}