Subversion Repositories SmartDukaan

Rev

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