Subversion Repositories SmartDukaan

Rev

Rev 2758 | Rev 3126 | 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.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import in.shop2020.payments.Attribute;
import in.shop2020.payments.Payment;
import in.shop2020.payments.PaymentStatus;
import in.shop2020.payments.PaymentService.Client;
import in.shop2020.thrift.clients.PaymentServiceClient;

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){
                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)){
                        log.error("Error while creating the basic payment");
                        return PAYMENT_NOT_CREATED;
                }else{
                        return initializePayment(cps.getPaymentId(), paymentOption);
                }
        }
        
        private long initializePayment(long merchantPaymentId, String paymentOption){
                List<Attribute> attributes = new ArrayList<Attribute>();
                attributes.add(new Attribute(IPaymentService.PAYMENT_METHOD, paymentOption));
                PaymentServiceClient paymentServiceClient = null;
                try {
                        paymentServiceClient = new PaymentServiceClient();
                } 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 static Map<String, String> capturePayment(Payment payment, String gatewayTxnId){
                String amount = "" + payment.getAmount();
                String gatewayPaymentId = payment.getGatewayPaymentId();
                log.info("Capturing amount: Rs " + amount + " for payment Id: " + gatewayPaymentId);
                
                //Prepare resultMap to elicit failure behaviour in case anything goes wrong.
                Map<String, String> resultMap = new HashMap<String, String>();
            resultMap.put(STATUS, "-2");
            
                try {
                        PaymentServiceClient paymentServiceClient = new PaymentServiceClient();
                        Client paymentClient = paymentServiceClient.getClient();
                        resultMap = paymentClient.captureHdfcPayment(payment.getPaymentId());
                } catch (Exception e) {
                        log.error("Unable to capture payment", e);
                        resultMap.put(ERR_CODE, Errors.CONN_FAILURE.code);
                        resultMap.put(ERROR, "Unable to capture transaction.");
                }
                
                return resultMap;
        }
        
        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");
                
                //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.
        }
}