Subversion Repositories SmartDukaan

Rev

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