Subversion Repositories SmartDukaan

Rev

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