Subversion Repositories SmartDukaan

Rev

Rev 1318 | Rev 1959 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1318 rajveer 1
package in.shop2020.serving.services;
2
 
3
import java.util.ArrayList;
4
import java.util.List;
5
import java.util.Random;
6
 
7
import org.apache.log4j.Logger;
8
import org.apache.struts2.convention.annotation.Result;
9
import org.apache.struts2.convention.annotation.Results;
10
import org.apache.thrift.TException;
11
 
12
import in.shop2020.config.ConfigException;
13
import in.shop2020.model.v1.order.LineItem;
14
import in.shop2020.model.v1.order.Order;
15
import in.shop2020.model.v1.order.Transaction;
16
import in.shop2020.model.v1.order.TransactionServiceException;
17
import in.shop2020.model.v1.user.ShoppingCartException;
18
import in.shop2020.payments.Attribute;
19
import in.shop2020.payments.PaymentStatus;
20
import in.shop2020.thrift.clients.PaymentServiceClient;
21
import in.shop2020.thrift.clients.TransactionServiceClient;
22
import in.shop2020.thrift.clients.config.ConfigClient;
23
 
24
 
25
 
26
import com.aciworldwide.commerce.gateway.plugins.NotEnoughDataException;
27
import com.aciworldwide.commerce.gateway.plugins.e24PaymentPipe;
28
 
1905 chandransh 29
public class HdfcPaymentService implements IPaymentService {
1318 rajveer 30
	private static final long serialVersionUID = 1L;
31
	private static Logger log = Logger.getLogger(Class.class);
32
 
33
	private static String resourceFilePath;
34
	private static String aliasName;
35
	private static String responseURL;
36
	private static String errorURL;
37
	private static final String regex = "[^a-zA-Z0-9\\s\\-\\@\\/\\.]";
38
	private static final String replacement = " ";
39
	private static final int MAX_UDF_LENGTH = 30;
40
	private String redirectURL;
41
	private e24PaymentPipe pipe = null;
42
 
43
	private double amount;
44
	private int gatewayId=1;
45
 
46
	private long paymentId;
47
 
48
	private enum ActionType{
49
		PURCHASE("1"),
50
		AUTH ("4");
51
		private String value;
52
		ActionType(String value) {
53
			this.value = value;
54
		}
55
		public String value(){
56
			return this.value;
57
		}
58
	}
59
 
60
	public HdfcPaymentService() {
61
 
62
	}
63
 
64
	static{
65
		try {
66
			resourceFilePath = ConfigClient.getClient().get("payment_resource_file_path");
67
			aliasName  = ConfigClient.getClient().get("payment_alias_name");
68
			responseURL = ConfigClient.getClient().get("payment_response_url");
69
			errorURL = ConfigClient.getClient().get("payment_error_url");
70
		} catch (ConfigException e) {
71
			log.error("Unable to get data from config server.");
72
		}
73
	}
74
 
75
 
1905 chandransh 76
	public long createPayment(long currentCartId, long userId, long txnId){
1318 rajveer 77
 
1905 chandransh 78
		CommonPaymentService cps = new CommonPaymentService();
79
		if(!cps.createPayment(currentCartId, userId, txnId, gatewayId)){
80
			log.error("Error while creating the basic payment");
81
			return PAYMENT_NOT_CREATED;
1318 rajveer 82
		}
83
 
1905 chandransh 84
		paymentId = cps.getPaymentId();
85
		amount = cps.getAmount();
1318 rajveer 86
 
87
		try {
1905 chandransh 88
			initializePayment(paymentId, amount);
1318 rajveer 89
		} catch (ShoppingCartException e1) {
90
			log.error("Error while creating hdfc payment.");
91
			e1.printStackTrace();
1905 chandransh 92
			return PAYMENT_NOT_CREATED;
1318 rajveer 93
		} catch (TException e1) {
94
			log.error("Error while creating hdfc payment.");
95
			e1.printStackTrace();
1905 chandransh 96
			return PAYMENT_NOT_CREATED;
1318 rajveer 97
		}
98
 
99
		List<Attribute> attributes = null;
100
		try {
101
			attributes = getAttributesAndSetUdfs(txnId);
102
		} catch (TransactionServiceException e1) {
103
			log.error("Error while setting udfs to payment.");
104
			e1.printStackTrace();
1905 chandransh 105
			return PAYMENT_NOT_CREATED;
1318 rajveer 106
		} catch (TException e1) {
107
			log.error("Error while setting udfs to payment.");
108
			e1.printStackTrace();
1905 chandransh 109
			return PAYMENT_NOT_CREATED;
1318 rajveer 110
		}
111
 
1905 chandransh 112
		PaymentServiceClient paymentServiceClient = null;
113
		try {
114
			paymentServiceClient = new PaymentServiceClient();
115
		} catch (Exception e) {
116
			log.error("Error while getting payment client");
117
			e.printStackTrace();
118
			return PAYMENT_NOT_CREATED;
119
		}
1318 rajveer 120
 
121
		try {
1905 chandransh 122
			if(pipe.performPaymentInitialization() != e24PaymentPipe.SUCCESS) {
123
				log.error("Error sending Payment Initialization Request: ");
124
				paymentServiceClient.getClient().updatePaymentDetails(paymentId, null, "", "", pipe.getErrorMsg(), "", "", "", "", PaymentStatus.INIT, "", attributes);
125
				redirectURL = errorURL + "?paymentId="+ paymentId + "&ErrorText="+pipe.getErrorMsg();
126
			}
127
			else {
128
				log.info("Payment Initialization Request processed successfully for payment Id: " + paymentId);
129
				String paymentID = pipe.getPaymentId();
130
				paymentServiceClient.getClient().updatePaymentDetails(paymentId, paymentID, "", "", "", "", "", "", "", PaymentStatus.INIT, "", attributes);
131
 
132
				String payURL = pipe.getPaymentPage();
133
				redirectURL = payURL + "?PaymentID=" + paymentID;
134
			}
135
			return paymentId;
1318 rajveer 136
		} catch (NotEnoughDataException e) {
137
			log.error("Error while initializing payment." + e.getMessage());
138
			e.printStackTrace();
139
		}catch (Exception e) {
140
			log.error("Error while initializing payment." + e.getMessage());
141
			e.printStackTrace();
142
		}
143
 
144
		redirectURL = errorURL + "?paymentId="+paymentId + "?ErrorText=Error while initializing payment.";
1905 chandransh 145
		return PAYMENT_NOT_CREATED;
1318 rajveer 146
	}
147
 
148
	private List<Attribute> getAttributesAndSetUdfs(long txnId) throws TransactionServiceException, TException{
149
		StringBuilder orderDetails = new StringBuilder();
150
		StringBuilder billingAddress = new StringBuilder();
151
		String email = "";
152
		String contactNumber = "";
153
 
154
		//get udfs
155
		Transaction transaction;
156
		TransactionServiceClient transactionServiceClient = null;
157
		try {
158
			transactionServiceClient = new TransactionServiceClient();
159
		} catch (Exception e) {
160
			// TODO Auto-generated catch block
161
			e.printStackTrace();
162
		}
163
 
164
		in.shop2020.model.v1.order.TransactionService.Client txnClient = transactionServiceClient.getClient();
165
		transaction = txnClient.getTransaction(txnId);
166
		orderDetails.append(transaction.getOrdersSize() + " ");
167
		for (Order order : transaction.getOrders()) {
168
			contactNumber= order.getCustomer_mobilenumber();
169
			email = order.getCustomer_email();
170
			billingAddress.append(" ");
171
			if(order.getCustomer_pincode()!=null){
172
				billingAddress.append(order.getCustomer_pincode());
173
			}
174
			if(order.getCustomer_city()!=null){
175
				billingAddress.append(" " + order.getCustomer_city());
176
			}
177
			if(order.getCustomer_address1()!=null){
178
				billingAddress.append(" " + order.getCustomer_address1());
179
			}
180
			if(order.getCustomer_address2()!=null){
181
				billingAddress.append(" " + order.getCustomer_address2());
182
			}
183
			if(order.getCustomer_state()!=null){
184
				billingAddress.append(" " + order.getCustomer_state());
185
			}
186
 
187
 
188
			for(LineItem line: order.getLineitems()){
189
				if(line.getBrand() != null){
190
					orderDetails.append(line.getBrand());
191
				}
192
				if(line.getModel_name() != null){
193
					orderDetails.append(line.getModel_name()); 
194
				}
195
				if(line.getModel_number() != null){
196
					orderDetails.append(line.getModel_number());
197
				}
198
				if(line.getColor() != null){
199
					orderDetails.append(line.getColor());
200
				}
201
				orderDetails.append(" ");
202
			}
203
 
204
		}
205
 
206
		Random random = new Random();
207
		String merchantInfo = ""+random.nextLong(); 
208
 
209
	    String udf1 = formatUdf(orderDetails.toString()); 
210
	    String udf2 = formatUdf(email);
211
	    String udf3 = formatUdf(contactNumber);
212
	    String udf4 = formatUdf(billingAddress.toString());
213
	    String udf5 = merchantInfo;
214
 
215
	    log.info("udf1:"  + udf1);
216
	    log.info("udf2:"  + udf2);
217
	    log.info("udf3:"  + udf3);
218
	    log.info("udf4:"  + udf4);
219
	    log.info("udf5:"  + udf5);
220
 
221
 
222
	    pipe.setUdf1(udf1);       //	UDF 1 - Order details
223
		pipe.setUdf2(udf2);        	  				//	UDF 2 - Email ID
224
		pipe.setUdf3(udf3);      			//	UDF 3 - Contact Number. 
225
		pipe.setUdf4(udf4);     //	UDF 4 - Billing Address
226
		pipe.setUdf5(udf5);		  						//	UDF 5 - Merchant specific
227
 
228
		List<Attribute> attributes = new ArrayList<Attribute>();
229
		Attribute attribute1 = new Attribute("udf1",udf1);
230
		Attribute attribute2 = new Attribute("udf2",udf2);
231
		Attribute attribute3 = new Attribute("udf3",udf3);
232
		Attribute attribute4 = new Attribute("udf4",udf4);
233
		Attribute attribute5 = new Attribute("udf5",udf5);
234
 
235
		attributes.add(attribute1);
236
		attributes.add(attribute2);
237
		attributes.add(attribute3);
238
		attributes.add(attribute4);
239
		attributes.add(attribute5);
240
 
241
		return attributes;
242
	}
243
 
244
	private void initializePayment(long merchantPaymentId, double amounta) throws ShoppingCartException, TException{
245
		String amount;
246
 
247
		amount = (new Double(amounta)).toString();
248
 
249
		//Following is the code which initilize e24PaymentPipe with proper value		
250
		pipe=new e24PaymentPipe();
251
		pipe.setResourcePath(resourceFilePath);	//mandatory 
252
		String as = pipe.getResourcePath();
253
		log.info("Resource= " +as);
254
 
255
		pipe.setAlias(aliasName);			//mandatory 
256
		String ab=pipe.getAlias();
257
		log.info("Alias= " +ab);
258
 
259
		pipe.setAction(ActionType.PURCHASE.value());			//mandatory 
260
		String ac=pipe.getAction();
261
		log.info("Action= " +ac);
262
 
263
		pipe.setResponseURL( responseURL );	//mandatory
264
		String at=pipe.getResponseURL();
265
		log.info("ResponseURL= "+at);
266
 
267
		//pipe.setErrorURL( errorURL + "?paymentId=" + merchantPaymentId );		//mandatory
268
		pipe.setErrorURL( errorURL);
269
	    String ak=pipe.getErrorURL();
270
	    log.info("ErrorURL= " + ak);
271
 
272
 
273
		pipe.setAmt(amount);		
274
		String ap=pipe.getAmt();
275
		log.info("Amt= " + ap);
276
 
277
		pipe.setCurrency("356");
278
		String a=pipe.getCurrency();
279
		log.info("Currency= " + a);
280
 
281
		pipe.setLanguage("USA");
282
		String p=pipe.getLanguage();
283
		log.info("Language= "+ p);
284
 
285
		pipe.setTrackId((new Long(merchantPaymentId)).toString());
286
 
287
	}
288
 
289
	String formatUdf(String udfString){
290
		udfString = udfString.replaceAll(regex, replacement);
291
		if(udfString.length() > MAX_UDF_LENGTH){
292
			udfString = udfString.substring(0, MAX_UDF_LENGTH);
293
		}
294
		return udfString;
295
	}
296
 
297
	public String getRedirectUrl(){
298
		return this.redirectURL;
299
	}
300
 
301
}
302