Subversion Repositories SmartDukaan

Rev

Rev 2199 | Rev 2586 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2199 Rev 2334
Line 1... Line 1...
1
package in.shop2020.serving.services;
1
package in.shop2020.serving.services;
2
 
2
 
3
import java.util.ArrayList;
3
import java.util.ArrayList;
-
 
4
import java.util.HashMap;
4
import java.util.List;
5
import java.util.List;
-
 
6
import java.util.Map;
5
import java.util.Random;
7
import java.util.Random;
6
 
8
 
7
import org.apache.log4j.Logger;
9
import org.apache.log4j.Logger;
8
import org.apache.thrift.TException;
10
import org.apache.thrift.TException;
9
 
11
 
Line 12... Line 14...
12
import in.shop2020.model.v1.order.Order;
14
import in.shop2020.model.v1.order.Order;
13
import in.shop2020.model.v1.order.Transaction;
15
import in.shop2020.model.v1.order.Transaction;
14
import in.shop2020.model.v1.order.TransactionServiceException;
16
import in.shop2020.model.v1.order.TransactionServiceException;
15
import in.shop2020.model.v1.user.ShoppingCartException;
17
import in.shop2020.model.v1.user.ShoppingCartException;
16
import in.shop2020.payments.Attribute;
18
import in.shop2020.payments.Attribute;
-
 
19
import in.shop2020.payments.Payment;
17
import in.shop2020.payments.PaymentStatus;
20
import in.shop2020.payments.PaymentStatus;
18
import in.shop2020.thrift.clients.PaymentServiceClient;
21
import in.shop2020.thrift.clients.PaymentServiceClient;
19
import in.shop2020.thrift.clients.TransactionServiceClient;
22
import in.shop2020.thrift.clients.TransactionServiceClient;
20
import in.shop2020.thrift.clients.config.ConfigClient;
23
import in.shop2020.thrift.clients.config.ConfigClient;
21
 
24
 
22
 
-
 
23
 
-
 
24
import com.aciworldwide.commerce.gateway.plugins.NotEnoughDataException;
25
import com.aciworldwide.commerce.gateway.plugins.NotEnoughDataException;
25
import com.aciworldwide.commerce.gateway.plugins.e24PaymentPipe;
26
import com.aciworldwide.commerce.gateway.plugins.e24PaymentPipe;
-
 
27
import com.aciworldwide.commerce.gateway.plugins.e24TranPipe;
26
 
28
 
27
public class HdfcPaymentService implements IPaymentService {
29
public class HdfcPaymentService implements IPaymentService {
28
	private static final long serialVersionUID = 1L;
30
	private static final long serialVersionUID = 1L;
29
	private static Logger log = Logger.getLogger(Class.class);
31
	private static Logger log = Logger.getLogger(Class.class);
30
	
32
	
Line 33... Line 35...
33
	private static String responseURL;
35
	private static String responseURL;
34
	private static String errorURL;
36
	private static String errorURL;
35
	private static final String regex = "[^a-zA-Z0-9\\s\\-\\@\\/\\.]";
37
	private static final String regex = "[^a-zA-Z0-9\\s\\-\\@\\/\\.]";
36
	private static final String replacement = " ";
38
	private static final String replacement = " ";
37
	private static final int MAX_UDF_LENGTH = 30;
39
	private static final int MAX_UDF_LENGTH = 30;
-
 
40
	private static final String currencyCode = "356";
38
	private String redirectURL;
41
	private String redirectURL;
39
	private e24PaymentPipe pipe = null;
42
	private e24PaymentPipe pipe = null;
40
	
43
	
41
	private double amount;
44
	private double amount;
42
	private int gatewayId=1;
45
	private int gatewayId=1;
43
	
46
	
44
	private long paymentId;
47
	private long paymentId;
45
	
48
	
46
	private enum ActionType{
49
	private enum ActionType{
47
		PURCHASE("1"),
50
		PURCHASE("1"),
48
		AUTH ("4");
51
		AUTH ("4"),
-
 
52
		CAPTURE("5");
49
		private String value;
53
		private String value;
50
		ActionType(String value) {
54
		ActionType(String value) {
51
			this.value = value;
55
			this.value = value;
52
		}
56
		}
53
		public String value(){
57
		public String value(){
Line 123... Line 127...
123
			if(status != e24PaymentPipe.SUCCESS) {
127
			if(status != e24PaymentPipe.SUCCESS) {
124
				log.error("Error sending Payment Initialization Request: ");
128
				log.error("Error sending Payment Initialization Request: ");
125
				paymentServiceClient.getClient().updatePaymentDetails(paymentId, null, "", "", pipe.getErrorMsg(), "", "", "", "", PaymentStatus.INIT, "", attributes);
129
				paymentServiceClient.getClient().updatePaymentDetails(paymentId, null, "", "", pipe.getErrorMsg(), "", "", "", "", PaymentStatus.INIT, "", attributes);
126
				redirectURL = errorURL + "?paymentId="+ paymentId + "&ErrorText="+pipe.getErrorMsg();
130
				redirectURL = errorURL + "?paymentId="+ paymentId + "&ErrorText="+pipe.getErrorMsg();
127
				return PAYMENT_NOT_CREATED;
131
				return PAYMENT_NOT_CREATED;
128
			}
-
 
129
			else {
132
			} else {
130
				log.info("Payment Initialization Request processed successfully for payment Id: " + paymentId);
133
				log.info("Payment Initialization Request processed successfully for payment Id: " + paymentId);
131
				String paymentID = pipe.getPaymentId();
134
				String paymentID = pipe.getPaymentId();
132
				paymentServiceClient.getClient().updatePaymentDetails(paymentId, paymentID, "", "", "", "", "", "", "", PaymentStatus.INIT, "", attributes);
135
				paymentServiceClient.getClient().updatePaymentDetails(paymentId, paymentID, "", "", "", "", "", "", "", PaymentStatus.INIT, "", attributes);
133
				
136
				
134
				String payURL = pipe.getPaymentPage();
137
				String payURL = pipe.getPaymentPage();
135
				redirectURL = payURL + "?PaymentID=" + paymentID;
138
				redirectURL = payURL + "?PaymentID=" + paymentID;
136
				return paymentId;
139
				return paymentId;
137
			}
140
			}
138
		} catch (NotEnoughDataException e) {
141
		} catch (NotEnoughDataException e) {
139
			log.error("Error while initializing payment." + e.getMessage());
142
			log.error("Error while initializing payment.", e);
140
			e.printStackTrace();
-
 
141
		}catch (Exception e) {
143
		}catch (Exception e) {
142
			log.error("Error while initializing payment." + e.getMessage());
144
			log.error("Error while initializing payment.", e);
143
			e.printStackTrace();
-
 
144
		}
145
		}
145
		
146
		
146
		redirectURL = errorURL + "?paymentId="+paymentId + "?ErrorText=Error while initializing payment.";
147
		redirectURL = errorURL + "?paymentId="+paymentId + "?ErrorText=Error while initializing payment.";
147
		return PAYMENT_NOT_CREATED;
148
		return PAYMENT_NOT_CREATED;
148
	}
149
	}
149
	
150
	
-
 
151
	public static Map<String, String> capturePayment(Payment payment, String gatewayTxnId){
-
 
152
		String amount = "" + payment.getAmount();
-
 
153
		String gatewayPaymentId = payment.getGatewayPaymentId();
-
 
154
		log.info("Capturing amount: Rs " + amount + " for payment Id: " + gatewayPaymentId);
-
 
155
		
-
 
156
		//Prepare resultMap to elicit failure behaviour in case anything goes wrong.
-
 
157
		Map<String, String> resultMap = new HashMap<String, String>();
-
 
158
	    resultMap.put(STATUS, "-2");
-
 
159
	    
-
 
160
		e24TranPipe pipe = new e24TranPipe();
-
 
161
		pipe.setResourcePath(resourceFilePath);
-
 
162
		pipe.setAlias(aliasName);
-
 
163
		pipe.setAction(ActionType.CAPTURE.value());
-
 
164
		pipe.setAmt(amount);
-
 
165
		pipe.setTrackId("" + payment.getPaymentId());
-
 
166
		pipe.setMember("SAHOLIC");
-
 
167
		pipe.setCurrencyCode(currencyCode);
-
 
168
		pipe.setTransId(gatewayTxnId);
-
 
169
		
-
 
170
		//Check if the values have been set properly
-
 
171
		log.info("Pipe Amount: " + pipe.getAmt());
-
 
172
		log.info("Pipe Action Code: " + pipe.getAction());
-
 
173
		log.info("Pipe Currency Code: " + pipe.getCurrencyCode());
-
 
174
		log.info("Pipe Track Id: " + pipe.getTrackId());
-
 
175
		log.info("Pipe Trans Id:" + pipe.getTransId());
-
 
176
		
-
 
177
		int captureStatus = e24TranPipe.FAILURE;
-
 
178
		try {
-
 
179
			captureStatus = pipe.performTransaction();
-
 
180
			
-
 
181
			log.info("Capture Status: " + captureStatus);
-
 
182
			log.info("Gateway Txn Status: " + pipe.getResult());
-
 
183
			log.info("Debug Msg: " + pipe.getDebugMsg());
-
 
184
			log.info("Auth: " + pipe.getAuth());
-
 
185
			log.info("Ref: " + pipe.getRef());
-
 
186
			log.info("TransId:" + pipe.getTransId());
-
 
187
			log.info("Amount: " + pipe.getAmt());
-
 
188
			
-
 
189
			resultMap.put(STATUS, "" + captureStatus);
-
 
190
			String result = pipe.getResult();
-
 
191
			resultMap.put(GATEWAY_STATUS, result.substring(0, Math.min(result.length(), 20)).trim());		//This will return the result of the transaction. (Successful or Failed)
-
 
192
			if(captureStatus != e24TranPipe.SUCCESS || !"CAPTURED".equals(result)){
-
 
193
				resultMap.put(ERROR, pipe.getErrorMsg());				// In case of any error, we only need to get the error message
-
 
194
			}else{
-
 
195
				resultMap.put(CAPTURE_AUTH_ID, pipe.getAuth());			// Unique ID generated by Authorizer of the transaction
-
 
196
				resultMap.put(CAPTURE_REF_ID, pipe.getRef());			// Unique reference number generated during the transaction
-
 
197
				resultMap.put(CAPTURE_TXN_ID, pipe.getTransId());		// Unique Transaction ID generated after every successful transaction
-
 
198
				resultMap.put(CAPTURE_AMNT, pipe.getAmt());			// Original Amount of the transaction
-
 
199
			}
-
 
200
		} catch (NotEnoughDataException e) {
-
 
201
			log.error("Unable to capture payment", e);
-
 
202
			resultMap.put(ERR_CODE, Errors.CONN_FAILURE.code);
-
 
203
			resultMap.put(ERROR, "Unable to capture transaction.");
-
 
204
		}
-
 
205
		        		
-
 
206
		return resultMap;
-
 
207
	}
-
 
208
	
150
	private List<Attribute> getAttributesAndSetUdfs(long txnId) throws TransactionServiceException, TException{
209
	private List<Attribute> getAttributesAndSetUdfs(long txnId) throws TransactionServiceException, TException{
151
		StringBuilder orderDetails = new StringBuilder();
210
		StringBuilder orderDetails = new StringBuilder();
152
		StringBuilder billingAddress = new StringBuilder();
211
		StringBuilder billingAddress = new StringBuilder();
153
		String email = "";
212
		String email = "";
154
		String contactNumber = "";
213
		String contactNumber = "";
Line 256... Line 315...
256
		
315
		
257
		pipe.setAlias(aliasName);			//mandatory 
316
		pipe.setAlias(aliasName);			//mandatory 
258
		String ab=pipe.getAlias();
317
		String ab=pipe.getAlias();
259
		log.info("Alias= " +ab);
318
		log.info("Alias= " +ab);
260
	
319
	
261
		pipe.setAction(ActionType.PURCHASE.value());			//mandatory 
320
		pipe.setAction(ActionType.AUTH.value());			//mandatory 
262
		String ac=pipe.getAction();
321
		String ac=pipe.getAction();
263
		log.info("Action= " +ac);
322
		log.info("Action= " +ac);
264
	
323
	
265
		pipe.setResponseURL( responseURL );	//mandatory
324
		pipe.setResponseURL( responseURL );	//mandatory
266
		String at=pipe.getResponseURL();
325
		String at=pipe.getResponseURL();
Line 274... Line 333...
274
	
333
	
275
		pipe.setAmt(amount);		
334
		pipe.setAmt(amount);		
276
		String ap=pipe.getAmt();
335
		String ap=pipe.getAmt();
277
		log.info("Amt= " + ap);
336
		log.info("Amt= " + ap);
278
	
337
	
279
		pipe.setCurrency("356");
338
		pipe.setCurrency(currencyCode);
280
		String a=pipe.getCurrency();
339
		String a=pipe.getCurrency();
281
		log.info("Currency= " + a);
340
		log.info("Currency= " + a);
282
	
341
	
283
		pipe.setLanguage("USA");
342
		pipe.setLanguage("USA");
284
		String p=pipe.getLanguage();
343
		String p=pipe.getLanguage();
Line 297... Line 356...
297
	}
356
	}
298
	
357
	
299
	public String getRedirectUrl(){
358
	public String getRedirectUrl(){
300
		return this.redirectURL;
359
		return this.redirectURL;
301
	}
360
	}
302
 
361
	
-
 
362
	public static void main(String args[]){
-
 
363
		Payment payment = new Payment();
-
 
364
		payment.setPaymentId(216);
-
 
365
		payment.setAmount(40000);
-
 
366
		payment.setGatewayPaymentId("TESTSTSTS");
-
 
367
		
-
 
368
		//This test checks what happens when the txn id is left blank
-
 
369
		capturePayment(payment, "");					//Result: !ERROR!-GW00205-Invalid Subsequent Transaction.
-
 
370
		
-
 
371
		//This test checks what happends with an invalid txn id 
-
 
372
		capturePayment(payment, "6022630101411740"); 	//Result: !ERROR!-GW00201-Transaction not found.
-
 
373
		
-
 
374
		//The next three tests require a valid AUTH transaction id.
-
 
375
		//This test checks what happens when we attempt to capture an amount greater than what was authorized.
-
 
376
		capturePayment(payment, "9644960021411730");	//Result: !ERROR!-GW00177-Failed Capture Greater Than Auth check.
-
 
377
		
-
 
378
		//This test checks what happens when we attempt to capture a valid transaction with the right amount. This transaction should be CAPTURED.
-
 
379
		payment.setAmount(21698);
-
 
380
		capturePayment(payment, "9644960021411730");	//Result: CAPTURED
-
 
381
		
-
 
382
		//This test tries to capture an already captured payment.
-
 
383
		capturePayment(payment, "9644960021411730");	//Result: !ERROR!-GW00177-Failed Capture Greater Than Auth check.
-
 
384
	}
303
}
385
}
304
 
386