Subversion Repositories SmartDukaan

Rev

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