Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
2391 chandransh 1
package in.shop2020.payment.service.handler;
2
 
3
import in.shop2020.config.ConfigException;
4
import in.shop2020.model.v1.order.LineItem;
5
import in.shop2020.model.v1.order.Order;
6
import in.shop2020.model.v1.order.Transaction;
7
import in.shop2020.model.v1.order.TransactionServiceException;
8
import in.shop2020.model.v1.user.ShoppingCartException;
9
import in.shop2020.payment.domain.Payment;
10
import in.shop2020.payments.Attribute;
3046 chandransh 11
import in.shop2020.payments.PaymentException;
2391 chandransh 12
import in.shop2020.payments.PaymentStatus;
3128 rajveer 13
import in.shop2020.thrift.clients.TransactionClient;
2391 chandransh 14
import in.shop2020.thrift.clients.config.ConfigClient;
15
 
16
import java.util.ArrayList;
17
import java.util.HashMap;
18
import java.util.List;
19
import java.util.Map;
20
import java.util.Random;
21
 
22
import org.apache.log4j.Logger;
23
import org.apache.thrift.TException;
24
 
25
import com.aciworldwide.commerce.gateway.plugins.NotEnoughDataException;
26
import com.aciworldwide.commerce.gateway.plugins.e24PaymentPipe;
27
import com.aciworldwide.commerce.gateway.plugins.e24TranPipe;
28
 
3010 chandransh 29
public class HdfcPaymentHandler implements IPaymentHandler {
2391 chandransh 30
	private static Logger log = Logger.getLogger(Class.class);
31
 
3010 chandransh 32
	private static final long gatewayId=1;
2391 chandransh 33
 
34
	private static String resourceFilePath;
35
	private static String aliasName;
36
	private static String responseURL;
37
	private static String errorURL;
38
	private static final String regex = "[^a-zA-Z0-9\\s\\-\\@\\/\\.]";
39
	private static final String replacement = " ";
40
	private static final int MAX_UDF_LENGTH = 30;
41
	private static final String currencyCode = "356";
42
 
43
	private enum ActionType{
44
		PURCHASE("1"),
45
		AUTH ("4"),
46
		CAPTURE("5");
47
		private String value;
48
		ActionType(String value) {
49
			this.value = value;
50
		}
51
		public String value(){
52
			return this.value;
53
		}
54
	}
55
 
56
	public HdfcPaymentHandler() {
57
		// TODO Auto-generated constructor stub
58
	}
59
 
60
	static{
61
		try {
62
			resourceFilePath = ConfigClient.getClient().get("payment_resource_file_path");
63
			aliasName  = ConfigClient.getClient().get("payment_alias_name");
64
			responseURL = ConfigClient.getClient().get("payment_response_url");
65
			errorURL = ConfigClient.getClient().get("payment_error_url");
66
		} catch (ConfigException e) {
67
			log.error("Unable to get data from config server.");
68
		}
69
	}
70
 
71
	public static Map<String, String> capturePayment(Payment payment){
72
		String amount = "" + payment.getAmount();
73
		String gatewayPaymentId = payment.getGatewayPaymentId();
74
		log.info("Capturing amount: Rs " + amount + " for payment Id: " + gatewayPaymentId);
75
 
76
		//Prepare resultMap to elicit failure behaviour in case anything goes wrong.
77
		Map<String, String> resultMap = new HashMap<String, String>();
78
	    resultMap.put(STATUS, "-2");
79
 
80
		e24TranPipe pipe = new e24TranPipe();
81
		pipe.setResourcePath(resourceFilePath);
82
		pipe.setAlias(aliasName);
83
		pipe.setAction(ActionType.CAPTURE.value());
84
		pipe.setAmt(amount);
85
		pipe.setTrackId("" + payment.getId());
86
		pipe.setMember("SAHOLIC");
87
		pipe.setCurrencyCode(currencyCode);
88
		pipe.setTransId(payment.getGatewayTxnId());
89
 
90
		//Check if the values have been set properly
91
		log.info("Pipe Amount: " + pipe.getAmt());
92
		log.info("Pipe Action Code: " + pipe.getAction());
93
		log.info("Pipe Currency Code: " + pipe.getCurrencyCode());
94
		log.info("Pipe Track Id: " + pipe.getTrackId());
95
		log.info("Pipe Trans Id:" + pipe.getTransId());
96
 
97
		int captureStatus = e24TranPipe.FAILURE;
98
		try {
99
			captureStatus = pipe.performTransaction();
100
 
101
			log.info("Capture Status: " + captureStatus);
102
			log.info("Gateway Txn Status: " + pipe.getResult());
103
			log.info("Debug Msg: " + pipe.getDebugMsg());
104
			log.info("Auth: " + pipe.getAuth());
105
			log.info("Ref: " + pipe.getRef());
106
			log.info("TransId:" + pipe.getTransId());
107
			log.info("Amount: " + pipe.getAmt());
108
 
109
			resultMap.put(STATUS, "" + captureStatus);
110
			String result = pipe.getResult();
111
			resultMap.put(GATEWAY_STATUS, result.substring(0, Math.min(result.length(), 20)).trim());		//This will return the result of the transaction. (Successful or Failed)
112
			if(captureStatus != e24TranPipe.SUCCESS || !"CAPTURED".equals(result)){
113
				resultMap.put(ERROR, pipe.getErrorMsg());				// In case of any error, we only need to get the error message
114
			}else{
115
				resultMap.put(CAPTURE_AUTH_ID, pipe.getAuth());			// Unique ID generated by Authorizer of the transaction
116
				resultMap.put(CAPTURE_REF_ID, pipe.getRef());			// Unique reference number generated during the transaction
117
				resultMap.put(CAPTURE_TXN_ID, pipe.getTransId());		// Unique Transaction ID generated after every successful transaction
118
				resultMap.put(CAPTURE_AMNT, pipe.getAmt());			// Original Amount of the transaction
119
			}
120
		} catch (NotEnoughDataException e) {
121
			log.error("Unable to capture payment", e);
122
			resultMap.put(ERR_CODE, Errors.CONN_FAILURE.code);
123
			resultMap.put(ERROR, "Unable to capture transaction.");
124
		}
125
 
126
		return resultMap;
127
	}
128
 
129
	public static String initializeHdfcPayment(Payment payment, PaymentServiceHandler handler) throws Exception{
130
		long merchantPaymentId = payment.getId();
131
		double amount = payment.getAmount();
132
		e24PaymentPipe pipe = new e24PaymentPipe();
133
		String redirectURL;
134
 
135
		try {
136
			initializePayment(pipe, merchantPaymentId, amount);
137
		} catch (ShoppingCartException e1) {
138
			log.error("Error while creating hdfc payment.", e1);
139
			throw e1;    //Payment couldn't be initialized. Will be redirected to the shipping page.
140
		} catch (TException e1) {
141
			log.error("Error while creating hdfc payment.", e1);
142
			throw e1;   //Payment couldn't be initialized. Will be redirected to the shipping page.
143
		}
144
 
145
		List<Attribute> attributes = null;
146
		try {
147
			attributes = getAttributesAndSetUdfs(pipe, payment.getMerchantTxnId());
148
		} catch (TransactionServiceException e1) {
149
			log.error("Error while setting udfs to payment.", e1);
150
			throw e1;   //Payment couldn't be initialized. Will be redirected to the shipping page.
151
		} catch (TException e1) {
152
			log.error("Error while setting udfs to payment.", e1);
153
			throw e1;   //Payment couldn't be initialized. Will be redirected to the shipping page.
154
		}
155
 
156
		try {
157
			if(pipe.performPaymentInitialization() != e24PaymentPipe.SUCCESS) {
158
				log.error("Error sending Payment Initialization Request: ");
159
				handler.updatePaymentDetails(merchantPaymentId, null, "", "", pipe.getErrorMsg(), "", "", "", "", PaymentStatus.INIT, "", attributes);
160
			} else {
161
				log.info("Payment Initialization Request processed successfully for payment Id: " + merchantPaymentId);
162
				String paymentID = pipe.getPaymentId();
163
				handler.updatePaymentDetails(merchantPaymentId, paymentID, "", "", "", "", "", "", "", PaymentStatus.INIT, "", attributes);
164
 
165
				String payURL = pipe.getPaymentPage();
3046 chandransh 166
				return payURL + "?PaymentID=" + paymentID;
2391 chandransh 167
			}
168
		} catch (NotEnoughDataException e) {
169
			log.error("Error while initializing payment.", e);
170
		}catch (Exception e) {
171
			log.error("Error while initializing payment.", e);
172
		}
3046 chandransh 173
		//If the code reaches here, the payment initialization was not successful and we've not returned the redirect URL.
174
		//Throw the exception so that any failovers can happen.
175
		throw new PaymentException(115, "Unable to initialize HDFC payment");
2391 chandransh 176
	}
177
 
178
	private static List<Attribute> getAttributesAndSetUdfs(e24PaymentPipe pipe, long txnId) throws TransactionServiceException, TException{
179
		StringBuilder orderDetails = new StringBuilder();
180
		StringBuilder billingAddress = new StringBuilder();
181
		String email = "";
182
		String contactNumber = "";
183
 
184
		//get udfs
185
		Transaction transaction;
3128 rajveer 186
		TransactionClient transactionServiceClient = null;
2391 chandransh 187
		try {
3128 rajveer 188
			transactionServiceClient = new TransactionClient();
2391 chandransh 189
		} catch (Exception e) {
190
			log.error("Unable to get transaction details", e);
191
		}
192
 
193
		in.shop2020.model.v1.order.TransactionService.Client txnClient = transactionServiceClient.getClient();
194
		transaction = txnClient.getTransaction(txnId);
195
		orderDetails.append(transaction.getOrdersSize() + " ");
196
		for (Order order : transaction.getOrders()) {
197
			contactNumber= order.getCustomer_mobilenumber();
198
			email = order.getCustomer_email();
199
			billingAddress.append(" ");
200
			if(order.getCustomer_pincode()!=null){
201
				billingAddress.append(order.getCustomer_pincode());
202
			}
203
			if(order.getCustomer_city()!=null){
204
				billingAddress.append(" " + order.getCustomer_city());
205
			}
206
			if(order.getCustomer_address1()!=null){
207
				billingAddress.append(" " + order.getCustomer_address1());
208
			}
209
			if(order.getCustomer_address2()!=null){
210
				billingAddress.append(" " + order.getCustomer_address2());
211
			}
212
			if(order.getCustomer_state()!=null){
213
				billingAddress.append(" " + order.getCustomer_state());
214
			}
215
 
216
 
217
			for(LineItem line: order.getLineitems()){
218
				if(line.getBrand() != null){
219
					orderDetails.append(line.getBrand());
220
				}
221
				if(line.getModel_name() != null){
222
					orderDetails.append(line.getModel_name()); 
223
				}
224
				if(line.getModel_number() != null){
225
					orderDetails.append(line.getModel_number());
226
				}
227
				if(line.getColor() != null){
228
					orderDetails.append(line.getColor());
229
				}
230
				orderDetails.append(" ");
231
			}
232
 
233
		}
234
 
235
		Random random = new Random();
236
		String merchantInfo = ""+random.nextLong(); 
237
 
238
	    String udf1 = formatUdf(orderDetails.toString()); 
239
	    String udf2 = formatUdf(email);
240
	    String udf3 = formatUdf(contactNumber);
241
	    String udf4 = formatUdf(billingAddress.toString());
242
	    String udf5 = merchantInfo;
243
 
244
	    log.info("udf1:"  + udf1);
245
	    log.info("udf2:"  + udf2);
246
	    log.info("udf3:"  + udf3);
247
	    log.info("udf4:"  + udf4);
248
	    log.info("udf5:"  + udf5);
249
 
250
 
251
	    pipe.setUdf1(udf1);      //	UDF 1 - Order details
252
		pipe.setUdf2(udf2);      //	UDF 2 - Email ID
253
		pipe.setUdf3(udf3);      //	UDF 3 - Contact Number. 
254
		pipe.setUdf4(udf4);      //	UDF 4 - Billing Address
255
		pipe.setUdf5(udf5);		 //	UDF 5 - Merchant specific
256
 
257
		List<Attribute> attributes = new ArrayList<Attribute>();
258
		Attribute attribute1 = new Attribute("udf1",udf1);
259
		Attribute attribute2 = new Attribute("udf2",udf2);
260
		Attribute attribute3 = new Attribute("udf3",udf3);
261
		Attribute attribute4 = new Attribute("udf4",udf4);
262
		Attribute attribute5 = new Attribute("udf5",udf5);
263
 
264
		attributes.add(attribute1);
265
		attributes.add(attribute2);
266
		attributes.add(attribute3);
267
		attributes.add(attribute4);
268
		attributes.add(attribute5);
269
 
270
		return attributes;
271
	}
272
 
273
	private static void initializePayment(e24PaymentPipe pipe, long merchantPaymentId, double amounta) throws ShoppingCartException, TException{
274
		String amount = (new Double(amounta)).toString();
275
 
276
		//Following is the code which initilize e24PaymentPipe with proper value
277
		pipe.setResourcePath(resourceFilePath);	//mandatory 
278
		String as = pipe.getResourcePath();
279
		log.info("Resource= " +as);
280
 
281
		pipe.setAlias(aliasName);			//mandatory 
282
		String ab=pipe.getAlias();
283
		log.info("Alias= " +ab);
284
 
2757 chandransh 285
		pipe.setAction(ActionType.AUTH.value());			//mandatory 
2391 chandransh 286
		String ac=pipe.getAction();
287
		log.info("Action= " +ac);
288
 
289
		pipe.setResponseURL( responseURL );	//mandatory
290
		String at=pipe.getResponseURL();
291
		log.info("ResponseURL= "+at);
292
 
293
		//pipe.setErrorURL( errorURL + "?paymentId=" + merchantPaymentId );		//mandatory
294
		pipe.setErrorURL( errorURL);
295
	    String ak=pipe.getErrorURL();
296
	    log.info("ErrorURL= " + ak);
297
 
298
 
299
		pipe.setAmt(amount);		
300
		String ap=pipe.getAmt();
301
		log.info("Amt= " + ap);
302
 
2761 chandransh 303
		pipe.setCurrency(currencyCode);
2391 chandransh 304
		String a=pipe.getCurrency();
305
		log.info("Currency= " + a);
306
 
307
		pipe.setLanguage("USA");
308
		String p=pipe.getLanguage();
309
		log.info("Language= "+ p);
310
 
311
		pipe.setTrackId((new Long(merchantPaymentId)).toString());
312
	}
313
 
314
	private static String formatUdf(String udfString){
315
		udfString = udfString.replaceAll(regex, replacement);
316
		if(udfString.length() > MAX_UDF_LENGTH){
317
			udfString = udfString.substring(0, MAX_UDF_LENGTH);
318
		}
319
		return udfString;
320
	}
321
}