Subversion Repositories SmartDukaan

Rev

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

Rev 2747 Rev 3010
Line 1... Line 1...
1
package in.shop2020.payment.service.handler;
1
package in.shop2020.payment.service.handler;
2
 
2
 
-
 
3
import java.text.SimpleDateFormat;
3
import java.util.ArrayList;
4
import java.util.ArrayList;
4
import java.util.Date;
5
import java.util.Date;
5
import java.util.HashMap;
6
import java.util.HashMap;
6
import java.util.List;
7
import java.util.List;
7
import java.util.Map;
8
import java.util.Map;
8
 
9
 
9
import org.apache.thrift.TException;
10
import org.apache.thrift.TException;
-
 
11
import org.slf4j.Logger;
-
 
12
import org.slf4j.LoggerFactory;
10
import org.springframework.context.ApplicationContext;
13
import org.springframework.context.ApplicationContext;
11
import org.springframework.context.support.ClassPathXmlApplicationContext;
14
import org.springframework.context.support.ClassPathXmlApplicationContext;
12
 
15
 
13
import in.shop2020.payment.domain.Refund;
16
import in.shop2020.payment.domain.Refund;
14
import in.shop2020.payment.handler.PaymentGatewayHandler;
17
import in.shop2020.payment.handler.PaymentGatewayHandler;
Line 20... Line 23...
20
import in.shop2020.payments.PaymentGateway;
23
import in.shop2020.payments.PaymentGateway;
21
import in.shop2020.payments.PaymentService.Iface;
24
import in.shop2020.payments.PaymentService.Iface;
22
import in.shop2020.payments.PaymentStatus;
25
import in.shop2020.payments.PaymentStatus;
23
 
26
 
24
public class PaymentServiceHandler implements Iface {
27
public class PaymentServiceHandler implements Iface {
-
 
28
    
-
 
29
    private static Logger logger = LoggerFactory.getLogger(PaymentServiceHandler.class);
-
 
30
    
-
 
31
    /**
-
 
32
     * Enum of all statuses that can be returned by the HDFC gateway
-
 
33
     * 
-
 
34
     * @author Chandranshu
-
 
35
     * 
-
 
36
     */
-
 
37
    private enum HdfcPaymentReturnStatus{
-
 
38
        APPROVED("APPROVED"),
-
 
39
        NOT_APPROVED("NOT APPROVED"),
-
 
40
        CAPTURED("CAPTURED"),
-
 
41
        NOT_CAPTURED ("NOT CAPTURED"),
-
 
42
        CANCELLED ("CANCELLED"),
-
 
43
        DENIED_BY_RISK("DENIED BY RISK"),
-
 
44
        HOST_TIMEOUT("HOST TIMEOUT");
-
 
45
        private String value;
-
 
46
        HdfcPaymentReturnStatus(String value) {
-
 
47
            this.value = value;
-
 
48
        }
-
 
49
        public String value(){
-
 
50
            return this.value;
-
 
51
        }
-
 
52
    }
-
 
53
    
25
	public static final long PAYMENT_NOT_CREATED = -1;
54
	public static final long PAYMENT_NOT_CREATED = -1;
26
	private static final String FLAG_KEY = "IsFlagged";
-
 
27
	private static final String TXN_KEY = "TransactionID";
-
 
28
	private static final String AUTH_TXN_ID = "AuthTxnId";
-
 
29
	private static final String CAPTURE_TXN_ID = "CaptureTxnId";
55
	private static final String CAPTURE_TXN_ID = "CaptureTxnId";
30
	private static final String CAPTURE_TIME = "CaptureTime";
56
	private static final String CAPTURE_TIME = "CaptureTime";
31
	
57
	
-
 
58
	private static final long HDFC_GATEWAY_ID = 1;
-
 
59
	private static final long EBS_GATEWAY_ID = 2;
-
 
60
	
32
	ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
61
	ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
33
	PaymentHandler paymentHandler = (PaymentHandler) context.getBean("paymentHandler");
62
	PaymentHandler paymentHandler = (PaymentHandler) context.getBean("paymentHandler");
34
	PaymentGatewayHandler paymentGatewayHandler = (PaymentGatewayHandler) context.getBean("paymentGatewayHandler");
63
	PaymentGatewayHandler paymentGatewayHandler = (PaymentGatewayHandler) context.getBean("paymentGatewayHandler");
35
	RefundHandler refundHandler = (RefundHandler) context.getBean("refundHandler");
64
	RefundHandler refundHandler = (RefundHandler) context.getBean("refundHandler");
36
	
65
	
Line 39... Line 68...
39
		// TODO Auto-generated method stub		
68
		// TODO Auto-generated method stub		
40
	}
69
	}
41
 
70
 
42
	@Override
71
	@Override
43
	public long createPayment(long userId, double amount, long gatewayId, long txnId) throws PaymentException, TException {
72
	public long createPayment(long userId, double amount, long gatewayId, long txnId) throws PaymentException, TException {
-
 
73
	    logger.info("Creating payment corresponding to our txn id:" + txnId);
44
		in.shop2020.payment.domain.Payment payment = new in.shop2020.payment.domain.Payment();
74
		in.shop2020.payment.domain.Payment payment = new in.shop2020.payment.domain.Payment();
45
		payment.setUserId(userId);
75
		payment.setUserId(userId);
46
		payment.setAmount(amount);
76
		payment.setAmount(amount);
47
		payment.setGatewayId(gatewayId);
77
		payment.setGatewayId(gatewayId);
48
		payment.setMerchantTxnId(txnId);
78
		payment.setMerchantTxnId(txnId);
Line 51... Line 81...
51
		return paymentHandler.insertPayment(payment);
81
		return paymentHandler.insertPayment(payment);
52
	}
82
	}
53
 
83
 
54
	@Override
84
	@Override
55
	public List<Payment> getPaymentsForUser(long userId, long fromTime, long toTime, PaymentStatus status, long gatewayId) throws PaymentException, TException {
85
	public List<Payment> getPaymentsForUser(long userId, long fromTime, long toTime, PaymentStatus status, long gatewayId) throws PaymentException, TException {
-
 
86
	    logger.info("Getting payments from " + fromTime + " to " + toTime + " for user: " + userId);
56
		int statusValue = -1;
87
		int statusValue = -1;
57
		if(status != null)
88
		if(status != null)
58
			statusValue = status.getValue();
89
			statusValue = status.getValue();
59
		else
90
		else
60
			statusValue = -1;
91
			statusValue = -1;
61
		return getThriftPayments(paymentHandler.getPaymentsForUser(userId, fromTime, toTime, statusValue, gatewayId));
92
		return getThriftPayments(paymentHandler.getPaymentsForUser(userId, fromTime, toTime, statusValue, gatewayId));
62
	}
93
	}
63
 
94
 
64
	@Override
95
	@Override
65
	public List<Payment> getPayments(long fromTime, long toTime, PaymentStatus status, long gatewayId) throws PaymentException,	TException {
96
	public List<Payment> getPayments(long fromTime, long toTime, PaymentStatus status, long gatewayId) throws PaymentException,	TException {
-
 
97
	    logger.info("Getting payments from " + fromTime + " to " + toTime);
66
		int statusValue = -1;
98
		int statusValue = -1;
67
		if(status != null)
99
		if(status != null)
68
			statusValue = status.getValue();
100
			statusValue = status.getValue();
69
		else
101
		else
70
			statusValue = -1;
102
			statusValue = -1;
71
		return getThriftPayments(paymentHandler.getPayments(fromTime, toTime, statusValue, gatewayId));
103
		return getThriftPayments(paymentHandler.getPayments(fromTime, toTime, statusValue, gatewayId));
72
	}
104
	}
73
 
105
 
74
	@Override
106
	@Override
75
	public PaymentGateway getPaymentGateway(long id) throws PaymentException, TException {
107
	public PaymentGateway getPaymentGateway(long id) throws PaymentException, TException {
-
 
108
	    logger.info("Getting payment gateway with id:" + id);
76
		return paymentGatewayHandler.getPaymentGateway(id).getThriftPaymentGateway();
109
		return paymentGatewayHandler.getPaymentGateway(id).getThriftPaymentGateway();
77
	}
110
	}
78
 
111
 
79
	@Override
112
	@Override
80
	public Payment getPayment(long id) throws PaymentException, TException {
113
	public Payment getPayment(long id) throws PaymentException, TException {
-
 
114
	    logger.info("Getting payment with id: " + id);
81
		return paymentHandler.getPayment(id).getThriftPayment();
115
		return paymentHandler.getPayment(id).getThriftPayment();
82
	}
116
	}
83
 
117
 
84
	@Override
118
	@Override
85
	public List<Payment> getPaymentForTxnId(long txnId) throws PaymentException, TException {
119
	public List<Payment> getPaymentForTxnId(long txnId) throws PaymentException, TException {
-
 
120
	    logger.info("Getting payment for the txn id: " + txnId);
86
		return getThriftPayments(paymentHandler.getPaymentForTxn(txnId));
121
		return getThriftPayments(paymentHandler.getPaymentForTxn(txnId));
87
	}
122
	}
88
 
123
 
89
	@Override
124
	@Override
90
	public boolean updatePaymentDetails(long id, String gatewayPaymentId,
125
	public boolean updatePaymentDetails(long id, String gatewayPaymentId,
91
			String sessionId, String gatewayTxnStatus, String description,
126
			String sessionId, String gatewayTxnStatus, String description,
92
			String gatewayTxnId, String authCode, String referenceCode,
127
			String gatewayTxnId, String authCode, String referenceCode,
93
			String errorCode, PaymentStatus status, String gatewayTxnDate,
128
			String errorCode, PaymentStatus status, String gatewayTxnDate,
94
			List<Attribute> attributes) throws PaymentException, TException {
129
			List<Attribute> attributes) throws PaymentException, TException {
-
 
130
	    logger.info("Updating details of payment id: " + id);
95
		in.shop2020.payment.domain.Payment payment = paymentHandler.getPayment(id);
131
		in.shop2020.payment.domain.Payment payment = paymentHandler.getPayment(id);
96
		payment.setGatewayPaymentId(gatewayPaymentId);
132
		payment.setGatewayPaymentId(gatewayPaymentId);
97
		payment.setSessionId(sessionId);
133
		payment.setSessionId(sessionId);
98
		payment.setGatewayTxnStatus(gatewayTxnStatus);
134
		payment.setGatewayTxnStatus(gatewayTxnStatus);
99
		payment.setDescription(description);
135
		payment.setDescription(description);
Line 122... Line 158...
122
		return true;
158
		return true;
123
	}
159
	}
124
 
160
 
125
	@Override
161
	@Override
126
	public List<Double> getSuccessfulPaymentsAmountRange() throws TException {
162
	public List<Double> getSuccessfulPaymentsAmountRange() throws TException {
-
 
163
	    logger.info("Getting the range of successful payments.");
127
		List<Double> minMaxAmounts = new ArrayList<Double>();
164
		List<Double> minMaxAmounts = new ArrayList<Double>();
128
		Map<String, Float> minMax = paymentHandler.getMinMaxPaymentAmount();
165
		Map<String, Float> minMax = paymentHandler.getMinMaxPaymentAmount();
129
		minMaxAmounts.add(Double.parseDouble(Float.toString(minMax.get("MIN"))));
166
		minMaxAmounts.add(Double.parseDouble(Float.toString(minMax.get("MIN"))));
130
		minMaxAmounts.add(Double.parseDouble(Float.toString(minMax.get("MAX"))));
167
		minMaxAmounts.add(Double.parseDouble(Float.toString(minMax.get("MAX"))));
131
		return minMaxAmounts;
168
		return minMaxAmounts;
132
	}
169
	}
133
 
170
 
134
	private List<Payment> getThriftPayments(List<in.shop2020.payment.domain.Payment> daoPayments){
-
 
135
		List<Payment> payments = new ArrayList<Payment>();
-
 
136
		for(in.shop2020.payment.domain.Payment payment : daoPayments){
-
 
137
			payments.add(payment.getThriftPayment());
-
 
138
		}
-
 
139
		return payments;
-
 
140
	}
-
 
141
 
-
 
142
	@Override
-
 
143
	public Payment updateAndCaptureEbsPayment(Map<String, String> paymentParams) throws PaymentException, TException {
-
 
144
		long merchantPaymentId = Long.parseLong(paymentParams.get("MerchantRefNo"));
-
 
145
		String gatewayPaymentId = paymentParams.get("PaymentID");
-
 
146
		double amount = Double.parseDouble(paymentParams.get("Amount"));
-
 
147
		String isFlagged = paymentParams.get(FLAG_KEY);
-
 
148
		String gatewayTxnStatus = paymentParams.get("ResponseCode");
-
 
149
		String description = paymentParams.get("ResponseMessage");
-
 
150
		String authTxnId = paymentParams.get(TXN_KEY);
-
 
151
		
-
 
152
		
-
 
153
		List<Attribute> attributes = new ArrayList<Attribute>();
-
 
154
		attributes.add(new Attribute(FLAG_KEY, isFlagged));
-
 
155
		attributes.add(new Attribute(AUTH_TXN_ID, authTxnId));
-
 
156
		
-
 
157
		Payment payment = null;
-
 
158
		try {
-
 
159
			payment = getPayment(merchantPaymentId);
-
 
160
		} catch (PaymentException e1) {
-
 
161
			throw new PaymentException(e1);
-
 
162
		}
-
 
163
		
-
 
164
		if(!validatePaymentParams(amount, payment)){
-
 
165
			throw new PaymentException(102, "Checks and balance failed on returned data");
-
 
166
		}
-
 
167
		
-
 
168
		if(gatewayTxnStatus.equals("0")){
-
 
169
			//Update payment status as authorized
-
 
170
			updatePaymentDetails(merchantPaymentId, gatewayPaymentId, "", gatewayTxnStatus, description, "", "", "", "", PaymentStatus.AUTHORIZED, "", attributes);
-
 
171
 
-
 
172
			Map<String, String> captureResult = EbsPaymentHandler.capturePayment(amount, "" + gatewayPaymentId);
-
 
173
			
-
 
174
			String captureStatus = captureResult.get(EbsPaymentHandler.STATUS);
-
 
175
			
-
 
176
			if("".equals(captureStatus)){
-
 
177
				//Failure
-
 
178
				description = captureResult.get(EbsPaymentHandler.ERROR);
-
 
179
				String errorCode = captureResult.get(EbsPaymentHandler.ERR_CODE);
-
 
180
				
-
 
181
				updatePaymentDetails(merchantPaymentId, gatewayPaymentId, "", gatewayTxnStatus, description, "", "", "", errorCode, PaymentStatus.FAILED, "", attributes);
-
 
182
			}else{
-
 
183
				//Success
-
 
184
				attributes.add(new Attribute(CAPTURE_TXN_ID, captureResult.get(EbsPaymentHandler.TXN_ID)));
-
 
185
				attributes.add(new Attribute(CAPTURE_TIME, captureResult.get(EbsPaymentHandler.DATE_TIME)));
-
 
186
				
-
 
187
				updatePaymentDetails(merchantPaymentId, gatewayPaymentId, "", captureStatus, description, "", "", "", "", PaymentStatus.SUCCESS, "", attributes);
-
 
188
			}
-
 
189
			
-
 
190
 
-
 
191
		}else{
-
 
192
			updatePaymentDetails(merchantPaymentId, gatewayPaymentId, "", gatewayTxnStatus, description, "", "", "", "", PaymentStatus.FAILED, "", attributes);		
-
 
193
		}
-
 
194
		
-
 
195
		payment = getPayment(merchantPaymentId);
-
 
196
		
-
 
197
		return payment;
-
 
198
	}
-
 
199
	
-
 
200
	@Override
171
	@Override
201
	public String initializeHdfcPayment(long merchantPaymentId) throws PaymentException, TException {
172
	public String initializeHdfcPayment(long merchantPaymentId) throws PaymentException, TException {
-
 
173
	    logger.info("Initializing HDFC payment with id: " + merchantPaymentId);
202
		in.shop2020.payment.domain.Payment payment = paymentHandler.getPayment(merchantPaymentId);
174
		in.shop2020.payment.domain.Payment payment = paymentHandler.getPayment(merchantPaymentId);
203
		String redirectURL;
175
		String redirectURL;
204
		try {
176
		try {
205
			redirectURL = HdfcPaymentHandler.initializeHdfcPayment(payment, this);
177
			redirectURL = HdfcPaymentHandler.initializeHdfcPayment(payment, this);
206
		} catch (Exception e) {
178
		} catch (Exception e) {
Line 209... Line 181...
209
		return redirectURL;
181
		return redirectURL;
210
	}
182
	}
211
	
183
	
212
	@Override
184
	@Override
213
	public Map<String, String> captureHdfcPayment(long merchantPaymentId) throws PaymentException, TException {
185
	public Map<String, String> captureHdfcPayment(long merchantPaymentId) throws PaymentException, TException {
-
 
186
	    logger.info("Capturing HDFC payment with id: " + merchantPaymentId);
214
		in.shop2020.payment.domain.Payment payment = paymentHandler.getPayment(merchantPaymentId);
187
		in.shop2020.payment.domain.Payment payment = paymentHandler.getPayment(merchantPaymentId);
215
		return HdfcPaymentHandler.capturePayment(payment);
188
		return HdfcPaymentHandler.capturePayment(payment);
216
	}
189
	}
217
	
190
	
218
	@Override
191
	@Override
219
	public Map<String, String> captureEbsPayment(long merchantPaymentId) throws PaymentException, TException {
192
	public Map<String, String> captureEbsPayment(long merchantPaymentId) throws PaymentException, TException {
-
 
193
	    logger.info("Capturing EBS payment with id: " + merchantPaymentId);
220
		in.shop2020.payment.domain.Payment payment = paymentHandler.getPayment(merchantPaymentId);
194
		in.shop2020.payment.domain.Payment payment = paymentHandler.getPayment(merchantPaymentId);
221
		return EbsPaymentHandler.capturePayment(payment.getAmount(), payment.getGatewayPaymentId());
195
		return EbsPaymentHandler.capturePayment(payment);
222
	}
196
	}
223
	
197
	
224
	@Override
198
	@Override
225
    public long createRefund(long orderId, long merchantTxnId, double amount) throws PaymentException, TException{
199
    public long createRefund(long orderId, long merchantTxnId, double amount) throws PaymentException, TException{
-
 
200
	    logger.info("Attempting to create a refund for order: " + orderId);
226
		List<in.shop2020.payment.domain.Payment> payments = paymentHandler.getPaymentForTxn(merchantTxnId);
201
		List<in.shop2020.payment.domain.Payment> payments = paymentHandler.getPaymentForTxn(merchantTxnId);
227
		in.shop2020.payment.domain.Payment payment = null;
-
 
228
		if(payments ==null || payments.isEmpty())
202
		if(payments ==null || payments.isEmpty())
229
			throw new PaymentException(104, "No payments found corresponding to the merchant txn " + merchantTxnId);
203
			throw new PaymentException(104, "No payments found corresponding to the merchant txn " + merchantTxnId);
230
		
204
		
231
		payment = payments.get(0);
205
		in.shop2020.payment.domain.Payment payment = payments.get(0);
232
		if(payment.getStatus() != PaymentStatus.SUCCESS.getValue())
206
		if(payment.getStatus() != PaymentStatus.SUCCESS.getValue())
233
			throw new PaymentException(104, "No successful payments found corresponding to the merchant txn " + merchantTxnId);
207
			throw new PaymentException(104, "No successful payments found corresponding to the merchant txn " + merchantTxnId);
234
 
208
 
235
		Refund refund = new Refund();
209
		Refund refund = new Refund();
236
		refund.setOrderId(orderId);
210
		refund.setOrderId(orderId);
Line 239... Line 213...
239
		refund.setAmount(amount);
213
		refund.setAmount(amount);
240
		refund.setAttempts(0);
214
		refund.setAttempts(0);
241
		return refundHandler.createRefund(refund);
215
		return refundHandler.createRefund(refund);
242
    }
216
    }
243
	
217
	
-
 
218
    @Override
244
	private boolean validatePaymentParams(double amount, Payment payment){
219
    public boolean capturePayment(long merchantTxnId) throws PaymentException, TException {
-
 
220
        logger.info("Attempting to capture payment corresponding to our transaction " + merchantTxnId);
-
 
221
        List<in.shop2020.payment.domain.Payment> payments = paymentHandler.getPaymentForTxn(merchantTxnId);
245
		if(payment==null || payment.getAmount()!= amount){
222
        if(payments ==null || payments.isEmpty())
-
 
223
            throw new PaymentException(104, "No payments found corresponding to the merchant txn " + merchantTxnId);
-
 
224
        
-
 
225
        in.shop2020.payment.domain.Payment payment = payments.get(0);
-
 
226
        switch(PaymentStatus.findByValue(payment.getStatus())){
-
 
227
        case PENDING:
-
 
228
            logger.error("Attempt to capture a non-authorized payment");
-
 
229
            return false;
-
 
230
        case INIT:
-
 
231
            logger.warn("Attempt to capture a non-authorized payment");
-
 
232
            return false;
-
 
233
        case AUTHORIZED:
-
 
234
            //Actual work to be done in this case. Let the call proceed.
-
 
235
            break;
-
 
236
        case SUCCESS:
-
 
237
            logger.warn("Attempting to capture an already captured payment but we can let the client proceed.");
-
 
238
            return true;
-
 
239
        case FAILED:
246
			// We did not request this payment or the authorised amount is different.
240
            logger.error("Attempting to capture a failed payment");
-
 
241
            return false;
-
 
242
        }
-
 
243
        
-
 
244
        long gatewayId = payment.getGatewayId();
-
 
245
        
-
 
246
        if(gatewayId == HDFC_GATEWAY_ID){
-
 
247
            //Capture and update the HDFC payment
-
 
248
            return captureAndUpdateHdfcPayment(payment);
-
 
249
        } else if (gatewayId == EBS_GATEWAY_ID){
-
 
250
            //Capture and update the EBS payment
-
 
251
            return captureAndUpdateEbsPayment(payment);
-
 
252
        }
-
 
253
        
-
 
254
        logger.error("We have an authorized payment from unknown gateway: " + gatewayId);
247
			return false;
255
        return false;
248
		}
256
    }
-
 
257
    
-
 
258
    /**
-
 
259
     * Capture the HDFC payment represented by the given payment object. If the
-
 
260
     * capture attempt is not successful, we mark this payment as failed. We
-
 
261
     * don't retry or anything. We'll add the support of multiple attempts later
-
 
262
     * on.
-
 
263
     * 
-
 
264
     * @param payment The payment which has to be captured.
-
 
265
     * @return True if the payment attempt is successful, false if not.
-
 
266
     */
-
 
267
    private boolean captureAndUpdateHdfcPayment(in.shop2020.payment.domain.Payment payment){
-
 
268
        long merchantPaymentId = payment.getId();
-
 
269
        logger.info("Capturing HDFC payment with id: " + merchantPaymentId);
-
 
270
        Map<String, String> captureResult = HdfcPaymentHandler.capturePayment(payment);
-
 
271
        String captureStatus = captureResult.get(IPaymentHandler.STATUS);
-
 
272
        String gatewayStatus = captureResult.get(IPaymentHandler.GATEWAY_STATUS);
-
 
273
 
-
 
274
        Map<String, String> attrMap = new HashMap<String, String>();
-
 
275
        if (!captureStatus.trim().equals("0") 
-
 
276
                || !HdfcPaymentReturnStatus.CAPTURED.value().equals(gatewayStatus)) {
-
 
277
            // Failure
-
 
278
            logger.info("Capture attempt failed for HDFC payment with id: " + merchantPaymentId);
-
 
279
            String description = captureResult.get(IPaymentHandler.ERROR);
-
 
280
            String errorCode = captureResult.get(IPaymentHandler.ERR_CODE);
-
 
281
 
-
 
282
            payment.setDescription(description);
-
 
283
            payment.setErrorCode(errorCode);
-
 
284
            payment.setStatus(PaymentStatus.FAILED.getValue());
-
 
285
            payment.setErrorTimestamp(new Date());
-
 
286
            paymentHandler.updatePayment(payment, attrMap);
-
 
287
            return false;
-
 
288
        } else {
-
 
289
            // Success
-
 
290
            logger.error("Capture attempt successful for HDFC payment with id: " + merchantPaymentId);
-
 
291
            payment.setDescription("Payment Captured");
-
 
292
            payment.setGatewayTxnStatus(gatewayStatus);
-
 
293
            payment.setStatus(PaymentStatus.SUCCESS.getValue());
-
 
294
            payment.setSuccessTimestamp(new Date());           
-
 
295
            
-
 
296
            attrMap.put(IPaymentHandler.CAPTURE_TXN_ID, captureResult.get(IPaymentHandler.CAPTURE_TXN_ID));
-
 
297
            attrMap.put(IPaymentHandler.CAPTURE_REF_ID, captureResult.get(IPaymentHandler.CAPTURE_REF_ID));
-
 
298
            attrMap.put(IPaymentHandler.CAPTURE_AUTH_ID, captureResult.get(IPaymentHandler.CAPTURE_AUTH_ID));
-
 
299
 
-
 
300
            SimpleDateFormat captureTimeDateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
-
 
301
            attrMap.put(HdfcPaymentHandler.CAPTURE_TIME, captureTimeDateFormatter.format(new Date()));
-
 
302
 
-
 
303
            paymentHandler.updatePayment(payment, attrMap);
249
		return true;
304
            return true;
-
 
305
          }
-
 
306
    }
250
	}
307
 
-
 
308
    /**
-
 
309
     * Capture the EBS payment represented by the given payment object. If the
-
 
310
     * capture attempt is not successful, we mark this payment as failed. We
-
 
311
     * don't retry or anything. We'll add the support of multiple attempts later
-
 
312
     * on.
-
 
313
     * 
-
 
314
     * @param payment The payment which has to be captured.
-
 
315
     * @return True if the payment attempt is successful, false if not.
-
 
316
     */
-
 
317
    private boolean captureAndUpdateEbsPayment(in.shop2020.payment.domain.Payment payment){
-
 
318
        Map<String, String> captureResult = EbsPaymentHandler.capturePayment(payment);
-
 
319
        String captureStatus = captureResult.get(EbsPaymentHandler.STATUS);
-
 
320
        
-
 
321
        Map<String, String> attrMap = new HashMap<String, String>();
-
 
322
        if("".equals(captureStatus)){
-
 
323
            //Failure
-
 
324
            String description = captureResult.get(EbsPaymentHandler.ERROR);
-
 
325
            String errorCode = captureResult.get(EbsPaymentHandler.ERR_CODE);
-
 
326
            
-
 
327
            payment.setDescription(description);
-
 
328
            payment.setErrorCode(errorCode);
-
 
329
            payment.setStatus(PaymentStatus.FAILED.getValue());
-
 
330
            payment.setErrorTimestamp(new Date());
-
 
331
            paymentHandler.updatePayment(payment, attrMap);
-
 
332
            return false;
-
 
333
        }else{
-
 
334
            //Success
-
 
335
            payment.setGatewayTxnStatus(captureStatus);
-
 
336
            payment.setStatus(PaymentStatus.SUCCESS.getValue());
-
 
337
            payment.setSuccessTimestamp(new Date());
-
 
338
            
-
 
339
            attrMap.put(IPaymentHandler.CAPTURE_TXN_ID, captureResult.get(IPaymentHandler.CAPTURE_TXN_ID));
-
 
340
            attrMap.put(IPaymentHandler.CAPTURE_TIME, captureResult.get(IPaymentHandler.CAPTURE_TIME));
-
 
341
            paymentHandler.updatePayment(payment, attrMap);
-
 
342
            return true;
-
 
343
        }
-
 
344
    }
-
 
345
    
-
 
346
 
-
 
347
    /**
-
 
348
     * Creates a list of thrift payment objects corresponding to a list of
-
 
349
     * payment data objects.
-
 
350
     * 
-
 
351
     * @param daoPayments
-
 
352
     *            A list of payment DAO.
-
 
353
     * @return A list of Thrift payment objects.
-
 
354
     */
-
 
355
    private List<Payment> getThriftPayments(List<in.shop2020.payment.domain.Payment> daoPayments){
-
 
356
        
-
 
357
        List<Payment> payments = new ArrayList<Payment>();
-
 
358
        for(in.shop2020.payment.domain.Payment payment : daoPayments){
-
 
359
            payments.add(payment.getThriftPayment());
-
 
360
        }
-
 
361
        return payments;
-
 
362
    }
251
}
363
}