Subversion Repositories SmartDukaan

Rev

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