Subversion Repositories SmartDukaan

Rev

Rev 1959 | Rev 2199 | 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){
1318 rajveer 75
 
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 {
1905 chandransh 121
			if(pipe.performPaymentInitialization() != e24PaymentPipe.SUCCESS) {
122
				log.error("Error sending Payment Initialization Request: ");
123
				paymentServiceClient.getClient().updatePaymentDetails(paymentId, null, "", "", pipe.getErrorMsg(), "", "", "", "", PaymentStatus.INIT, "", attributes);
124
				redirectURL = errorURL + "?paymentId="+ paymentId + "&ErrorText="+pipe.getErrorMsg();
125
			}
126
			else {
127
				log.info("Payment Initialization Request processed successfully for payment Id: " + paymentId);
128
				String paymentID = pipe.getPaymentId();
129
				paymentServiceClient.getClient().updatePaymentDetails(paymentId, paymentID, "", "", "", "", "", "", "", PaymentStatus.INIT, "", attributes);
130
 
131
				String payURL = pipe.getPaymentPage();
132
				redirectURL = payURL + "?PaymentID=" + paymentID;
133
			}
134
			return paymentId;
1318 rajveer 135
		} catch (NotEnoughDataException e) {
136
			log.error("Error while initializing payment." + e.getMessage());
137
			e.printStackTrace();
138
		}catch (Exception e) {
139
			log.error("Error while initializing payment." + e.getMessage());
140
			e.printStackTrace();
141
		}
142
 
143
		redirectURL = errorURL + "?paymentId="+paymentId + "?ErrorText=Error while initializing payment.";
1905 chandransh 144
		return PAYMENT_NOT_CREATED;
1318 rajveer 145
	}
146
 
147
	private List<Attribute> getAttributesAndSetUdfs(long txnId) throws TransactionServiceException, TException{
148
		StringBuilder orderDetails = new StringBuilder();
149
		StringBuilder billingAddress = new StringBuilder();
150
		String email = "";
151
		String contactNumber = "";
152
 
153
		//get udfs
154
		Transaction transaction;
155
		TransactionServiceClient transactionServiceClient = null;
156
		try {
157
			transactionServiceClient = new TransactionServiceClient();
158
		} catch (Exception e) {
159
			// TODO Auto-generated catch block
160
			e.printStackTrace();
161
		}
162
 
163
		in.shop2020.model.v1.order.TransactionService.Client txnClient = transactionServiceClient.getClient();
164
		transaction = txnClient.getTransaction(txnId);
165
		orderDetails.append(transaction.getOrdersSize() + " ");
166
		for (Order order : transaction.getOrders()) {
167
			contactNumber= order.getCustomer_mobilenumber();
168
			email = order.getCustomer_email();
169
			billingAddress.append(" ");
170
			if(order.getCustomer_pincode()!=null){
171
				billingAddress.append(order.getCustomer_pincode());
172
			}
173
			if(order.getCustomer_city()!=null){
174
				billingAddress.append(" " + order.getCustomer_city());
175
			}
176
			if(order.getCustomer_address1()!=null){
177
				billingAddress.append(" " + order.getCustomer_address1());
178
			}
179
			if(order.getCustomer_address2()!=null){
180
				billingAddress.append(" " + order.getCustomer_address2());
181
			}
182
			if(order.getCustomer_state()!=null){
183
				billingAddress.append(" " + order.getCustomer_state());
184
			}
185
 
186
 
187
			for(LineItem line: order.getLineitems()){
188
				if(line.getBrand() != null){
189
					orderDetails.append(line.getBrand());
190
				}
191
				if(line.getModel_name() != null){
192
					orderDetails.append(line.getModel_name()); 
193
				}
194
				if(line.getModel_number() != null){
195
					orderDetails.append(line.getModel_number());
196
				}
197
				if(line.getColor() != null){
198
					orderDetails.append(line.getColor());
199
				}
200
				orderDetails.append(" ");
201
			}
202
 
203
		}
204
 
205
		Random random = new Random();
206
		String merchantInfo = ""+random.nextLong(); 
207
 
208
	    String udf1 = formatUdf(orderDetails.toString()); 
209
	    String udf2 = formatUdf(email);
210
	    String udf3 = formatUdf(contactNumber);
211
	    String udf4 = formatUdf(billingAddress.toString());
212
	    String udf5 = merchantInfo;
213
 
214
	    log.info("udf1:"  + udf1);
215
	    log.info("udf2:"  + udf2);
216
	    log.info("udf3:"  + udf3);
217
	    log.info("udf4:"  + udf4);
218
	    log.info("udf5:"  + udf5);
219
 
220
 
221
	    pipe.setUdf1(udf1);       //	UDF 1 - Order details
222
		pipe.setUdf2(udf2);        	  				//	UDF 2 - Email ID
223
		pipe.setUdf3(udf3);      			//	UDF 3 - Contact Number. 
224
		pipe.setUdf4(udf4);     //	UDF 4 - Billing Address
225
		pipe.setUdf5(udf5);		  						//	UDF 5 - Merchant specific
226
 
227
		List<Attribute> attributes = new ArrayList<Attribute>();
228
		Attribute attribute1 = new Attribute("udf1",udf1);
229
		Attribute attribute2 = new Attribute("udf2",udf2);
230
		Attribute attribute3 = new Attribute("udf3",udf3);
231
		Attribute attribute4 = new Attribute("udf4",udf4);
232
		Attribute attribute5 = new Attribute("udf5",udf5);
233
 
234
		attributes.add(attribute1);
235
		attributes.add(attribute2);
236
		attributes.add(attribute3);
237
		attributes.add(attribute4);
238
		attributes.add(attribute5);
239
 
240
		return attributes;
241
	}
242
 
243
	private void initializePayment(long merchantPaymentId, double amounta) throws ShoppingCartException, TException{
244
		String amount;
245
 
246
		amount = (new Double(amounta)).toString();
247
 
248
		//Following is the code which initilize e24PaymentPipe with proper value		
249
		pipe=new e24PaymentPipe();
250
		pipe.setResourcePath(resourceFilePath);	//mandatory 
251
		String as = pipe.getResourcePath();
252
		log.info("Resource= " +as);
253
 
254
		pipe.setAlias(aliasName);			//mandatory 
255
		String ab=pipe.getAlias();
256
		log.info("Alias= " +ab);
257
 
258
		pipe.setAction(ActionType.PURCHASE.value());			//mandatory 
259
		String ac=pipe.getAction();
260
		log.info("Action= " +ac);
261
 
262
		pipe.setResponseURL( responseURL );	//mandatory
263
		String at=pipe.getResponseURL();
264
		log.info("ResponseURL= "+at);
265
 
266
		//pipe.setErrorURL( errorURL + "?paymentId=" + merchantPaymentId );		//mandatory
267
		pipe.setErrorURL( errorURL);
268
	    String ak=pipe.getErrorURL();
269
	    log.info("ErrorURL= " + ak);
270
 
271
 
272
		pipe.setAmt(amount);		
273
		String ap=pipe.getAmt();
274
		log.info("Amt= " + ap);
275
 
276
		pipe.setCurrency("356");
277
		String a=pipe.getCurrency();
278
		log.info("Currency= " + a);
279
 
280
		pipe.setLanguage("USA");
281
		String p=pipe.getLanguage();
282
		log.info("Language= "+ p);
283
 
284
		pipe.setTrackId((new Long(merchantPaymentId)).toString());
285
 
286
	}
287
 
288
	String formatUdf(String udfString){
289
		udfString = udfString.replaceAll(regex, replacement);
290
		if(udfString.length() > MAX_UDF_LENGTH){
291
			udfString = udfString.substring(0, MAX_UDF_LENGTH);
292
		}
293
		return udfString;
294
	}
295
 
296
	public String getRedirectUrl(){
297
		return this.redirectURL;
298
	}
299
 
300
}
301