Subversion Repositories SmartDukaan

Rev

Rev 20112 | 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;
        private long paymentId;
        public HdfcPaymentService() {
        
        }
        
        public long createPayment(long userId, long txnId, String paymentOption, int gatewayId, String paymentType){
                log.info("Creating payment for the txn#: " + txnId + " for the user: " + userId + " for processing through HDFC");
                CommonPaymentService cps = new CommonPaymentService();
                if(!cps.createPayment(userId, txnId, gatewayId)){
                        log.error("Error while creating the basic payment");
                        return PAYMENT_NOT_CREATED;
                }
                paymentId = cps.getPaymentId();
                if(paymentOption != null){
                        List<Attribute> attributes = new ArrayList<Attribute>();
                        attributes.add(new Attribute(IPaymentService.PAYMENT_METHOD, paymentOption));
                        attributes.add(new Attribute(IPaymentService.PAYMENT_TYPE, paymentType));
                        
                        try {
                                PaymentClient paymentServiceClient = new PaymentClient();
                                paymentServiceClient.getClient().updatePaymentDetails(paymentId, null, null, null, null, null, null, null, null, PaymentStatus.INIT, null, attributes);
                        } catch (Exception e) {
                                log.error("Error while saving payment option attribute", e);
                                // TODO: We've already created the payment. We could allow the
                                // payment to go through. The customer will be a little
                                // annoyed to have to select from a host of options again but
                                // will be better than completely disallowing him.
                                return PAYMENT_NOT_CREATED;
                        }
                }

                return paymentId;
        }
        
        public long createPayment(RechargeOrder rechargeOrder, String paymentOption, String phone, String paymentType) {
            log.info("Creating payment for the txn#: " + rechargeOrder.getId() + " for the user: " + rechargeOrder.getUserId() + " for processing through HDFC");
            log.info("createPayment phone---- " + phone);
        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, phone, paymentType);
        }
    }
        
        private long initializePayment(RechargeOrder rechargeOrder, long merchantPaymentId,
            String paymentOption, String phone, String paymentType) {
            List<Attribute> attributes = new ArrayList<Attribute>();
        attributes.add(new Attribute(IPaymentService.PAYMENT_METHOD, paymentOption));
        attributes.add(new Attribute(IPaymentService.PAYMENT_TYPE, paymentType));
        log.info("initializePayment phone---- " + phone);
        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(), phone, false);
            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.
        }
}