Subversion Repositories SmartDukaan

Rev

Rev 20112 | 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;
20112 aman.kumar 20
	private long paymentId;
1318 rajveer 21
	public HdfcPaymentService() {
22
 
23
	}
24
 
20278 aman.kumar 25
	public long createPayment(long userId, long txnId, String paymentOption, int gatewayId, String paymentType){
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;
1318 rajveer 31
		}
20112 aman.kumar 32
		paymentId = cps.getPaymentId();
33
		if(paymentOption != null){
34
			List<Attribute> attributes = new ArrayList<Attribute>();
35
			attributes.add(new Attribute(IPaymentService.PAYMENT_METHOD, paymentOption));
20278 aman.kumar 36
			attributes.add(new Attribute(IPaymentService.PAYMENT_TYPE, paymentType));
20112 aman.kumar 37
 
38
			try {
39
				PaymentClient paymentServiceClient = new PaymentClient();
40
				paymentServiceClient.getClient().updatePaymentDetails(paymentId, null, null, null, null, null, null, null, null, PaymentStatus.INIT, null, attributes);
41
			} catch (Exception e) {
42
				log.error("Error while saving payment option attribute", e);
43
				// TODO: We've already created the payment. We could allow the
44
				// payment to go through. The customer will be a little
45
				// annoyed to have to select from a host of options again but
46
				// will be better than completely disallowing him.
47
				return PAYMENT_NOT_CREATED;
48
			}
49
		}
50
 
51
		return paymentId;
2758 chandransh 52
	}
53
 
20278 aman.kumar 54
	public long createPayment(RechargeOrder rechargeOrder, String paymentOption, String phone, String paymentType) {
6050 anupam.sin 55
	    log.info("Creating payment for the txn#: " + rechargeOrder.getId() + " for the user: " + rechargeOrder.getUserId() + " for processing through HDFC");
10968 amit.gupta 56
	    log.info("createPayment phone---- " + phone);
6050 anupam.sin 57
        CommonPaymentService cps = new CommonPaymentService();
58
        if(!cps.createPayment(rechargeOrder, gatewayId)){
59
            log.error("Error while creating the basic payment");
60
            return PAYMENT_NOT_CREATED;
61
        }else{
20278 aman.kumar 62
            return initializePayment(rechargeOrder, cps.getPaymentId(), paymentOption, phone, paymentType);
6050 anupam.sin 63
        }
64
    }
65
 
66
	private long initializePayment(RechargeOrder rechargeOrder, long merchantPaymentId,
20278 aman.kumar 67
            String paymentOption, String phone, String paymentType) {
6050 anupam.sin 68
	    List<Attribute> attributes = new ArrayList<Attribute>();
69
        attributes.add(new Attribute(IPaymentService.PAYMENT_METHOD, paymentOption));
20278 aman.kumar 70
        attributes.add(new Attribute(IPaymentService.PAYMENT_TYPE, paymentType));
10968 amit.gupta 71
        log.info("initializePayment phone---- " + phone);
6050 anupam.sin 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);
10269 amit.gupta 82
            this.redirectURL = paymentServiceClient.getClient().doHdfcPaymentForDigitalOrder(merchantPaymentId, rechargeOrder.getId(), phone, false);
6050 anupam.sin 83
            return merchantPaymentId;
84
        }catch (Exception e) {
85
            log.error("Error while initializing payment.", e);
86
            return PAYMENT_NOT_CREATED;
87
        }
88
    }
89
 
20112 aman.kumar 90
 
1318 rajveer 91
	public String getRedirectUrl(){
92
		return this.redirectURL;
93
	}
2334 chandransh 94
 
95
	public static void main(String args[]){
96
		Payment payment = new Payment();
97
		payment.setPaymentId(216);
98
		payment.setAmount(40000);
99
		payment.setGatewayPaymentId("TESTSTSTS");
100
 
3010 chandransh 101
        // The underlying method calls are no longer valid since all the
102
        // information required to capture a payment is read from the database
103
        // itself and can't be specified through the call.
104
 
2334 chandransh 105
		//This test checks what happens when the txn id is left blank
3010 chandransh 106
		//capturePayment(payment, "");					//Result: !ERROR!-GW00205-Invalid Subsequent Transaction.
2334 chandransh 107
 
108
		//This test checks what happends with an invalid txn id 
3010 chandransh 109
		//capturePayment(payment, "6022630101411740"); 	//Result: !ERROR!-GW00201-Transaction not found.
2334 chandransh 110
 
111
		//The next three tests require a valid AUTH transaction id.
112
		//This test checks what happens when we attempt to capture an amount greater than what was authorized.
3010 chandransh 113
		//capturePayment(payment, "9644960021411730");	//Result: !ERROR!-GW00177-Failed Capture Greater Than Auth check.
2334 chandransh 114
 
115
		//This test checks what happens when we attempt to capture a valid transaction with the right amount. This transaction should be CAPTURED.
3010 chandransh 116
		//payment.setAmount(21698);
117
		//capturePayment(payment, "9644960021411730");	//Result: CAPTURED
2334 chandransh 118
 
119
		//This test tries to capture an already captured payment.
3010 chandransh 120
		//capturePayment(payment, "9644960021411730");	//Result: !ERROR!-GW00177-Failed Capture Greater Than Auth check.
2334 chandransh 121
	}
1318 rajveer 122
}