Subversion Repositories SmartDukaan

Rev

Rev 6228 | Rev 10269 | 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;
4
import java.util.List;
5
 
6
import org.apache.log4j.Logger;
7
 
6050 anupam.sin 8
import in.shop2020.model.v1.order.RechargeOrder;
1318 rajveer 9
import in.shop2020.payments.Attribute;
2334 chandransh 10
import in.shop2020.payments.Payment;
1318 rajveer 11
import in.shop2020.payments.PaymentStatus;
3126 rajveer 12
import in.shop2020.thrift.clients.PaymentClient;
1318 rajveer 13
 
1905 chandransh 14
public class HdfcPaymentService implements IPaymentService {
1318 rajveer 15
	private static final long serialVersionUID = 1L;
16
	private static Logger log = Logger.getLogger(Class.class);
17
 
18
	private String redirectURL;
2758 chandransh 19
	private static int gatewayId=1;
1318 rajveer 20
 
21
	public HdfcPaymentService() {
22
 
23
	}
24
 
6390 rajveer 25
	public long createPayment(long userId, long txnId, String paymentOption, int gatewayId){
2199 chandransh 26
		log.info("Creating payment for the txn#: " + txnId + " for the user: " + userId + " for processing through HDFC");
1905 chandransh 27
		CommonPaymentService cps = new CommonPaymentService();
6390 rajveer 28
		if(!cps.createPayment(userId, txnId, gatewayId)){
1905 chandransh 29
			log.error("Error while creating the basic payment");
30
			return PAYMENT_NOT_CREATED;
2758 chandransh 31
		}else{
32
			return initializePayment(cps.getPaymentId(), paymentOption);
1318 rajveer 33
		}
2758 chandransh 34
	}
35
 
6228 anupam.sin 36
	public long createPayment(RechargeOrder rechargeOrder, String paymentOption, String phone) {
6050 anupam.sin 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{
6228 anupam.sin 43
            return initializePayment(rechargeOrder, cps.getPaymentId(), paymentOption, phone);
6050 anupam.sin 44
        }
45
    }
46
 
47
	private long initializePayment(RechargeOrder rechargeOrder, long merchantPaymentId,
6228 anupam.sin 48
            String paymentOption, String phone) {
6050 anupam.sin 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);
6228 anupam.sin 61
            this.redirectURL = paymentServiceClient.getClient().doHdfcPaymentForDigitalOrder(merchantPaymentId, rechargeOrder.getId(), phone);
6050 anupam.sin 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){
2758 chandransh 70
		List<Attribute> attributes = new ArrayList<Attribute>();
71
		attributes.add(new Attribute(IPaymentService.PAYMENT_METHOD, paymentOption));
3126 rajveer 72
		PaymentClient paymentServiceClient = null;
1905 chandransh 73
		try {
3126 rajveer 74
			paymentServiceClient = new PaymentClient();
1905 chandransh 75
		} catch (Exception e) {
2758 chandransh 76
			log.error("Error while getting payment client", e);
1905 chandransh 77
			return PAYMENT_NOT_CREATED;
78
		}
1318 rajveer 79
 
80
		try {
2758 chandransh 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;
1318 rajveer 84
		}catch (Exception e) {
2334 chandransh 85
			log.error("Error while initializing payment.", e);
2758 chandransh 86
			return PAYMENT_NOT_CREATED;
1318 rajveer 87
		}
88
	}
89
 
90
	public String getRedirectUrl(){
91
		return this.redirectURL;
92
	}
2334 chandransh 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
 
3010 chandransh 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
 
2334 chandransh 104
		//This test checks what happens when the txn id is left blank
3010 chandransh 105
		//capturePayment(payment, "");					//Result: !ERROR!-GW00205-Invalid Subsequent Transaction.
2334 chandransh 106
 
107
		//This test checks what happends with an invalid txn id 
3010 chandransh 108
		//capturePayment(payment, "6022630101411740"); 	//Result: !ERROR!-GW00201-Transaction not found.
2334 chandransh 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.
3010 chandransh 112
		//capturePayment(payment, "9644960021411730");	//Result: !ERROR!-GW00177-Failed Capture Greater Than Auth check.
2334 chandransh 113
 
114
		//This test checks what happens when we attempt to capture a valid transaction with the right amount. This transaction should be CAPTURED.
3010 chandransh 115
		//payment.setAmount(21698);
116
		//capturePayment(payment, "9644960021411730");	//Result: CAPTURED
2334 chandransh 117
 
118
		//This test tries to capture an already captured payment.
3010 chandransh 119
		//capturePayment(payment, "9644960021411730");	//Result: !ERROR!-GW00177-Failed Capture Greater Than Auth check.
2334 chandransh 120
	}
1318 rajveer 121
}