Subversion Repositories SmartDukaan

Rev

Rev 3583 | Rev 6390 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.serving.services;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import in.shop2020.model.v1.order.RechargeOrder;
import in.shop2020.payments.Attribute;
import in.shop2020.payments.Payment;
import in.shop2020.payments.PaymentStatus;
import in.shop2020.thrift.clients.PaymentClient;

public class HdfcPaymentService implements IPaymentService {
        private static final long serialVersionUID = 1L;
        private static Logger log = Logger.getLogger(Class.class);
        
        private String redirectURL;
        private static int gatewayId=1;
        
        public HdfcPaymentService() {
        
        }
        
        public long createPayment(long currentCartId, long userId, long txnId, String paymentOption, long sourceId){
                log.info("Creating payment for the txn#: " + txnId + " for the user: " + userId + " for processing through HDFC");
                CommonPaymentService cps = new CommonPaymentService();
                if(!cps.createPayment(currentCartId, userId, txnId, gatewayId, sourceId)){
                        log.error("Error while creating the basic payment");
                        return PAYMENT_NOT_CREATED;
                }else{
                        return initializePayment(cps.getPaymentId(), paymentOption);
                }
        }
        
        public long createPayment(RechargeOrder rechargeOrder, String paymentOption) {
            log.info("Creating payment for the txn#: " + rechargeOrder.getId() + " for the user: " + rechargeOrder.getUserId() + " for processing through HDFC");
        CommonPaymentService cps = new CommonPaymentService();
        if(!cps.createPayment(rechargeOrder, gatewayId)){
            log.error("Error while creating the basic payment");
            return PAYMENT_NOT_CREATED;
        }else{
            return initializePayment(rechargeOrder, cps.getPaymentId(), paymentOption);
        }
    }
        
        private long initializePayment(RechargeOrder rechargeOrder, long merchantPaymentId,
            String paymentOption) {
            List<Attribute> attributes = new ArrayList<Attribute>();
        attributes.add(new Attribute(IPaymentService.PAYMENT_METHOD, paymentOption));
        PaymentClient paymentServiceClient = null;
        try {
            paymentServiceClient = new PaymentClient();
        } catch (Exception e) {
            log.error("Error while getting payment client", e);
            return PAYMENT_NOT_CREATED;
        }
        
        try {
            paymentServiceClient.getClient().updatePaymentDetails(merchantPaymentId, null, null, null, null, null, null, null, null, PaymentStatus.INIT, null, attributes);
            this.redirectURL = paymentServiceClient.getClient().doHdfcPaymentForDigitalOrder(merchantPaymentId, rechargeOrder.getId());
            return merchantPaymentId;
        }catch (Exception e) {
            log.error("Error while initializing payment.", e);
            return PAYMENT_NOT_CREATED;
        }
    }

    private long initializePayment(long merchantPaymentId, String paymentOption){
                List<Attribute> attributes = new ArrayList<Attribute>();
                attributes.add(new Attribute(IPaymentService.PAYMENT_METHOD, paymentOption));
                PaymentClient paymentServiceClient = null;
                try {
                        paymentServiceClient = new PaymentClient();
                } catch (Exception e) {
                        log.error("Error while getting payment client", e);
                        return PAYMENT_NOT_CREATED;
                }
                
                try {
                        paymentServiceClient.getClient().updatePaymentDetails(merchantPaymentId, null, null, null, null, null, null, null, null, PaymentStatus.INIT, null, attributes);
                        this.redirectURL = paymentServiceClient.getClient().initializeHdfcPayment(merchantPaymentId);
                        return merchantPaymentId;
                }catch (Exception e) {
                        log.error("Error while initializing payment.", e);
                        return PAYMENT_NOT_CREATED;
                }
        }
        
        public String getRedirectUrl(){
                return this.redirectURL;
        }
        
        public static void main(String args[]){
                Payment payment = new Payment();
                payment.setPaymentId(216);
                payment.setAmount(40000);
                payment.setGatewayPaymentId("TESTSTSTS");
                
        // The underlying method calls are no longer valid since all the
        // information required to capture a payment is read from the database
        // itself and can't be specified through the call.
                
                //This test checks what happens when the txn id is left blank
                //capturePayment(payment, "");                                  //Result: !ERROR!-GW00205-Invalid Subsequent Transaction.
                
                //This test checks what happends with an invalid txn id 
                //capturePayment(payment, "6022630101411740");  //Result: !ERROR!-GW00201-Transaction not found.
                
                //The next three tests require a valid AUTH transaction id.
                //This test checks what happens when we attempt to capture an amount greater than what was authorized.
                //capturePayment(payment, "9644960021411730");  //Result: !ERROR!-GW00177-Failed Capture Greater Than Auth check.
                
                //This test checks what happens when we attempt to capture a valid transaction with the right amount. This transaction should be CAPTURED.
                //payment.setAmount(21698);
                //capturePayment(payment, "9644960021411730");  //Result: CAPTURED
                
                //This test tries to capture an already captured payment.
                //capturePayment(payment, "9644960021411730");  //Result: !ERROR!-GW00177-Failed Capture Greater Than Auth check.
        }
}