Subversion Repositories SmartDukaan

Rev

Rev 2758 | Rev 3010 | 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
 
1905 chandransh 16
public class HdfcPaymentService implements IPaymentService {
1318 rajveer 17
	private static final long serialVersionUID = 1L;
18
	private static Logger log = Logger.getLogger(Class.class);
19
 
20
	private String redirectURL;
2758 chandransh 21
	private static int gatewayId=1;
1318 rajveer 22
 
23
	public HdfcPaymentService() {
24
 
25
	}
26
 
2159 chandransh 27
	public long createPayment(long currentCartId, long userId, long txnId, String paymentOption){
2199 chandransh 28
		log.info("Creating payment for the txn#: " + txnId + " for the user: " + userId + " for processing through HDFC");
1905 chandransh 29
		CommonPaymentService cps = new CommonPaymentService();
30
		if(!cps.createPayment(currentCartId, userId, txnId, gatewayId)){
31
			log.error("Error while creating the basic payment");
32
			return PAYMENT_NOT_CREATED;
2758 chandransh 33
		}else{
34
			return initializePayment(cps.getPaymentId(), paymentOption);
1318 rajveer 35
		}
2758 chandransh 36
	}
37
 
38
	private long initializePayment(long merchantPaymentId, String paymentOption){
39
		List<Attribute> attributes = new ArrayList<Attribute>();
40
		attributes.add(new Attribute(IPaymentService.PAYMENT_METHOD, paymentOption));
1905 chandransh 41
		PaymentServiceClient paymentServiceClient = null;
42
		try {
43
			paymentServiceClient = new PaymentServiceClient();
44
		} catch (Exception e) {
2758 chandransh 45
			log.error("Error while getting payment client", e);
1905 chandransh 46
			return PAYMENT_NOT_CREATED;
47
		}
1318 rajveer 48
 
49
		try {
2758 chandransh 50
			paymentServiceClient.getClient().updatePaymentDetails(merchantPaymentId, null, null, null, null, null, null, null, null, PaymentStatus.INIT, null, attributes);
51
			this.redirectURL = paymentServiceClient.getClient().initializeHdfcPayment(merchantPaymentId);
52
			return merchantPaymentId;
1318 rajveer 53
		}catch (Exception e) {
2334 chandransh 54
			log.error("Error while initializing payment.", e);
2758 chandransh 55
			return PAYMENT_NOT_CREATED;
1318 rajveer 56
		}
57
	}
58
 
2334 chandransh 59
	public static Map<String, String> capturePayment(Payment payment, String gatewayTxnId){
60
		String amount = "" + payment.getAmount();
61
		String gatewayPaymentId = payment.getGatewayPaymentId();
62
		log.info("Capturing amount: Rs " + amount + " for payment Id: " + gatewayPaymentId);
63
 
64
		//Prepare resultMap to elicit failure behaviour in case anything goes wrong.
65
		Map<String, String> resultMap = new HashMap<String, String>();
66
	    resultMap.put(STATUS, "-2");
67
 
68
		try {
2707 chandransh 69
			PaymentServiceClient paymentServiceClient = new PaymentServiceClient();
70
			Client paymentClient = paymentServiceClient.getClient();
71
			resultMap = paymentClient.captureHdfcPayment(payment.getPaymentId());
72
		} catch (Exception e) {
2334 chandransh 73
			log.error("Unable to capture payment", e);
74
			resultMap.put(ERR_CODE, Errors.CONN_FAILURE.code);
75
			resultMap.put(ERROR, "Unable to capture transaction.");
76
		}
2707 chandransh 77
 
2334 chandransh 78
		return resultMap;
2758 chandransh 79
	}
80
 
1318 rajveer 81
	public String getRedirectUrl(){
82
		return this.redirectURL;
83
	}
2334 chandransh 84
 
85
	public static void main(String args[]){
86
		Payment payment = new Payment();
87
		payment.setPaymentId(216);
88
		payment.setAmount(40000);
89
		payment.setGatewayPaymentId("TESTSTSTS");
90
 
91
		//This test checks what happens when the txn id is left blank
92
		capturePayment(payment, "");					//Result: !ERROR!-GW00205-Invalid Subsequent Transaction.
93
 
94
		//This test checks what happends with an invalid txn id 
95
		capturePayment(payment, "6022630101411740"); 	//Result: !ERROR!-GW00201-Transaction not found.
96
 
97
		//The next three tests require a valid AUTH transaction id.
98
		//This test checks what happens when we attempt to capture an amount greater than what was authorized.
99
		capturePayment(payment, "9644960021411730");	//Result: !ERROR!-GW00177-Failed Capture Greater Than Auth check.
100
 
101
		//This test checks what happens when we attempt to capture a valid transaction with the right amount. This transaction should be CAPTURED.
102
		payment.setAmount(21698);
103
		capturePayment(payment, "9644960021411730");	//Result: CAPTURED
104
 
105
		//This test tries to capture an already captured payment.
106
		capturePayment(payment, "9644960021411730");	//Result: !ERROR!-GW00177-Failed Capture Greater Than Auth check.
107
	}
1318 rajveer 108
}
109