Subversion Repositories SmartDukaan

Rev

Rev 3126 | Rev 3583 | 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;
3126 rajveer 14
import in.shop2020.thrift.clients.PaymentClient;
1318 rajveer 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
 
3561 rajveer 27
	public long createPayment(long currentCartId, long userId, long txnId, String paymentOption, long sourceId){
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();
3561 rajveer 30
		if(!cps.createPayment(currentCartId, userId, txnId, gatewayId, sourceId)){
1905 chandransh 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));
3126 rajveer 41
		PaymentClient paymentServiceClient = null;
1905 chandransh 42
		try {
3126 rajveer 43
			paymentServiceClient = new PaymentClient();
1905 chandransh 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
 
3010 chandransh 59
	public static Map<String, String> capturePayment(Payment payment){
2334 chandransh 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 {
3126 rajveer 69
			PaymentClient paymentServiceClient = new PaymentClient();
2707 chandransh 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
 
3010 chandransh 91
        // The underlying method calls are no longer valid since all the
92
        // information required to capture a payment is read from the database
93
        // itself and can't be specified through the call.
94
 
2334 chandransh 95
		//This test checks what happens when the txn id is left blank
3010 chandransh 96
		//capturePayment(payment, "");					//Result: !ERROR!-GW00205-Invalid Subsequent Transaction.
2334 chandransh 97
 
98
		//This test checks what happends with an invalid txn id 
3010 chandransh 99
		//capturePayment(payment, "6022630101411740"); 	//Result: !ERROR!-GW00201-Transaction not found.
2334 chandransh 100
 
101
		//The next three tests require a valid AUTH transaction id.
102
		//This test checks what happens when we attempt to capture an amount greater than what was authorized.
3010 chandransh 103
		//capturePayment(payment, "9644960021411730");	//Result: !ERROR!-GW00177-Failed Capture Greater Than Auth check.
2334 chandransh 104
 
105
		//This test checks what happens when we attempt to capture a valid transaction with the right amount. This transaction should be CAPTURED.
3010 chandransh 106
		//payment.setAmount(21698);
107
		//capturePayment(payment, "9644960021411730");	//Result: CAPTURED
2334 chandransh 108
 
109
		//This test tries to capture an already captured payment.
3010 chandransh 110
		//capturePayment(payment, "9644960021411730");	//Result: !ERROR!-GW00177-Failed Capture Greater Than Auth check.
2334 chandransh 111
	}
1318 rajveer 112
}
113