Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
8842 anupam.sin 1
package in.shop2020.serving.services;
2
 
3
import java.util.ArrayList;
4
import java.util.List;
5
 
6
import org.apache.log4j.Logger;
7
 
8
import in.shop2020.model.v1.order.RechargeOrder;
9
import in.shop2020.payments.Attribute;
10
import in.shop2020.payments.Payment;
11
import in.shop2020.payments.PaymentStatus;
12
import in.shop2020.thrift.clients.PaymentClient;
13
 
14
public class HdfcPaymentService implements IPaymentService {
15
	private static final long serialVersionUID = 1L;
16
	private static Logger log = Logger.getLogger(Class.class);
17
 
18
	private String redirectURL;
19
	private static int gatewayId=1;
20
 
21
	public HdfcPaymentService() {
22
 
23
	}
24
 
25
	public long createPayment(long userId, long txnId, String paymentOption, int gatewayId){
26
		log.info("Creating payment for the txn#: " + txnId + " for the user: " + userId + " for processing through HDFC");
27
		CommonPaymentService cps = new CommonPaymentService();
28
		if(!cps.createPayment(userId, txnId, gatewayId)){
29
			log.error("Error while creating the basic payment");
30
			return PAYMENT_NOT_CREATED;
31
		}else{
32
			return initializePayment(cps.getPaymentId(), paymentOption);
33
		}
34
	}
35
 
36
	public long createPayment(RechargeOrder rechargeOrder, String paymentOption, String phone) {
37
	    log.info("Creating payment for the txn#: " + rechargeOrder.getId() + " for the user: " + rechargeOrder.getUserId() + " for processing through HDFC");
38
        CommonPaymentService cps = new CommonPaymentService();
39
        if(!cps.createPayment(rechargeOrder, gatewayId)){
40
            log.error("Error while creating the basic payment");
41
            return PAYMENT_NOT_CREATED;
42
        }else{
43
            return initializePayment(rechargeOrder, cps.getPaymentId(), paymentOption, phone);
44
        }
45
    }
46
 
47
	private long initializePayment(RechargeOrder rechargeOrder, long merchantPaymentId,
48
            String paymentOption, String phone) {
49
	    List<Attribute> attributes = new ArrayList<Attribute>();
50
        attributes.add(new Attribute(IPaymentService.PAYMENT_METHOD, paymentOption));
51
        PaymentClient paymentServiceClient = null;
52
        try {
53
            paymentServiceClient = new PaymentClient();
54
        } catch (Exception e) {
55
            log.error("Error while getting payment client", e);
56
            return PAYMENT_NOT_CREATED;
57
        }
58
 
59
        try {
60
            paymentServiceClient.getClient().updatePaymentDetails(merchantPaymentId, null, null, null, null, null, null, null, null, PaymentStatus.INIT, null, attributes);
61
            this.redirectURL = paymentServiceClient.getClient().doHdfcPaymentForDigitalOrder(merchantPaymentId, rechargeOrder.getId(), phone);
62
            return merchantPaymentId;
63
        }catch (Exception e) {
64
            log.error("Error while initializing payment.", e);
65
            return PAYMENT_NOT_CREATED;
66
        }
67
    }
68
 
69
    private long initializePayment(long merchantPaymentId, String paymentOption){
70
		List<Attribute> attributes = new ArrayList<Attribute>();
71
		attributes.add(new Attribute(IPaymentService.PAYMENT_METHOD, paymentOption));
72
		PaymentClient paymentServiceClient = null;
73
		try {
74
			paymentServiceClient = new PaymentClient();
75
		} catch (Exception e) {
76
			log.error("Error while getting payment client", e);
77
			return PAYMENT_NOT_CREATED;
78
		}
79
 
80
		try {
81
			paymentServiceClient.getClient().updatePaymentDetails(merchantPaymentId, null, null, null, null, null, null, null, null, PaymentStatus.INIT, null, attributes);
82
			this.redirectURL = paymentServiceClient.getClient().initializeHdfcPayment(merchantPaymentId);
83
			return merchantPaymentId;
84
		}catch (Exception e) {
85
			log.error("Error while initializing payment.", e);
86
			return PAYMENT_NOT_CREATED;
87
		}
88
	}
89
 
90
	public String getRedirectUrl(){
91
		return this.redirectURL;
92
	}
93
 
94
	public static void main(String args[]){
95
		Payment payment = new Payment();
96
		payment.setPaymentId(216);
97
		payment.setAmount(40000);
98
		payment.setGatewayPaymentId("TESTSTSTS");
99
 
100
        // The underlying method calls are no longer valid since all the
101
        // information required to capture a payment is read from the database
102
        // itself and can't be specified through the call.
103
 
104
		//This test checks what happens when the txn id is left blank
105
		//capturePayment(payment, "");					//Result: !ERROR!-GW00205-Invalid Subsequent Transaction.
106
 
107
		//This test checks what happends with an invalid txn id 
108
		//capturePayment(payment, "6022630101411740"); 	//Result: !ERROR!-GW00201-Transaction not found.
109
 
110
		//The next three tests require a valid AUTH transaction id.
111
		//This test checks what happens when we attempt to capture an amount greater than what was authorized.
112
		//capturePayment(payment, "9644960021411730");	//Result: !ERROR!-GW00177-Failed Capture Greater Than Auth check.
113
 
114
		//This test checks what happens when we attempt to capture a valid transaction with the right amount. This transaction should be CAPTURED.
115
		//payment.setAmount(21698);
116
		//capturePayment(payment, "9644960021411730");	//Result: CAPTURED
117
 
118
		//This test tries to capture an already captured payment.
119
		//capturePayment(payment, "9644960021411730");	//Result: !ERROR!-GW00177-Failed Capture Greater Than Auth check.
120
	}
121
}