Subversion Repositories SmartDukaan

Rev

Rev 10493 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.serving.controllers;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.thrift.transport.TTransportException;

import com.opensymphony.xwork2.ValidationAwareSupport;

import in.shop2020.config.ConfigException;
import in.shop2020.model.v1.order.LineItem;
import in.shop2020.model.v1.order.Order;
import in.shop2020.model.v1.order.RechargeOrder;
import in.shop2020.model.v1.order.Transaction;
import in.shop2020.payments.Attribute;
import in.shop2020.payments.Payment;
import in.shop2020.serving.services.IPaymentService;
import in.shop2020.test.Address;
import in.shop2020.thrift.clients.PaymentClient;
import in.shop2020.thrift.clients.TransactionClient;
import in.shop2020.thrift.clients.UserClient;
import in.shop2020.thrift.clients.config.ConfigClient;

@SuppressWarnings("serial")

@Results({
        @Result(name="recharge-redirect", type="redirectAction", 
                params = {"actionName" , "recharge"}),
        @Result(name="shipping-redirect", type="redirectAction", 
                        params = {"actionName" , "shipping"})
})
public class EbsPayController extends ValidationAwareSupport{

        private static Logger log = Logger.getLogger(Class.class);
        
        private static String accountId;
        
        private static String returnUrl;
        
        private static String mode;
        
        private static String ebsSecretKey;
        
        private String phone;//This is used for recharge orders of value less than 1000.
        
        static{
                try {
                        accountId = ConfigClient.getClient().get("ebs_account_id");
                        returnUrl = ConfigClient.getClient().get("ebs_return_url");
                        mode = ConfigClient.getClient().get("ebs_pay_mode");
                        ebsSecretKey = ConfigClient.getClient().get("ebs_secret_key");
                } catch (ConfigException e) {
                        mode = "LIVE";
                        log.error("Unable to get EBS payment configuration.");
                }
        }
        
        private String id;
        
        private String paymentOption = null;
        
        private StringBuilder description;
        
        private double amount;
        
        private ContactDetails billingDetails;
        
        public String show(){
                PaymentClient paymentServiceClient = null;
                Payment payment = null;
                try {
                        long paymentId = Long.parseLong(this.id);
                        paymentServiceClient = new PaymentClient();
                        payment = paymentServiceClient.getClient().getPayment(paymentId);
                } catch (Exception e) {
                        log.error("Error while getting payment client", e);
                        addActionError("We are experiencing some problems. Please try later.");
                        return "shipping-redirect";
                }
                
                Order order = null;
                try {
                        long txnId = payment.getMerchantTxnId();
                        TransactionClient transactionServiceClient = new TransactionClient();
                        in.shop2020.model.v1.order.TransactionService.Client txnClient = transactionServiceClient.getClient();
                        Transaction transaction = txnClient.getTransaction(txnId);
                        order = transaction.getOrders().get(0);
                } catch (Exception e) {
                        log.error("Error while getting transaction information", e);
                        addActionError("We are experiencing some problems. Please try later.");
                        return "shipping-redirect";
                }
                
                setDescription(order);
                setPaymentOption(payment);
                
                this.amount = payment.getAmount();
                this.billingDetails = new ContactDetails(order.getCustomer_name(),
                                order.getCustomer_email(), order.getCustomer_address1(),
                                order.getCustomer_city(), order.getCustomer_state(),
                                order.getCustomer_pincode(), "IND",
                                order.getCustomer_mobilenumber());
                
                log.info(billingDetails);
                
                return "show";
        }
        
        
        /**
         * This method is used for Recharge payments. It is being called by RechargePaymentController.
         * @return
         */
        public String edit(){
        PaymentClient paymentServiceClient = null;
        Payment payment = null;
        try {
            long paymentId = Long.parseLong(this.id);
            paymentServiceClient = new PaymentClient();
            payment = paymentServiceClient.getClient().getPayment(paymentId);
        } catch (Exception e) {
            log.error("Error while getting payment client", e);
            addActionError("We are experiencing some problems. Please try later.");
            return "recharge-redirect";
        }
        
        RechargeOrder rechargeOrder = null;
        try {
            long txnId = payment.getMerchantTxnId();
            TransactionClient transactionServiceClient = new TransactionClient();
            in.shop2020.model.v1.order.TransactionService.Client txnClient = transactionServiceClient.getClient();
            rechargeOrder = txnClient.getRechargeOrdersForTransaction(txnId);
        } catch (Exception e) {
            log.error("Error while getting transaction information", e);
            addActionError("We are experiencing some problems. Please try later.");
            return "recharge-redirect";
        }
        
        String desc = "Recharge for Rs. " + rechargeOrder.getTotalAmount() + ", operator : " + rechargeOrder.getOperatorId();
        this.description = new StringBuilder(desc);
        setPaymentOption(payment);
        
        this.amount = payment.getAmount();
        if(getPhone().isEmpty()) {
            UserClient userClient;
            in.shop2020.model.v1.user.Address address = null;
            try {
                userClient = new UserClient();
                long addressId = userClient.getClient().getDefaultAddressId(rechargeOrder.getUserId());
                address = userClient.getClient().getAddressById(addressId);
                this.billingDetails = new ContactDetails(address.getName(),
                        rechargeOrder.getUserEmailId(), address.getLine1(),
                        address.getCity(), address.getState(),
                        address.getPin(), "IND",
                        address.getPhone());
            } catch (Exception e) {
                log.error("Unable to get address to put in billing details");
                e.printStackTrace();
            }
        } else {
        this.billingDetails = new ContactDetails(rechargeOrder.getUserEmailId(),
                rechargeOrder.getUserEmailId(), id,
                id, id,
                id, "IND",
                getPhone());
        }
        log.info("Billing details of recharge order " + rechargeOrder.getDisplayId() + " : " + billingDetails);
        return "show";
    }

        public String getDescription(){
                if(this.description.length() >= 255)
                        return this.description.substring(0, 255);
                else
                        return this.description.toString();
        }
        
        private void setDescription(Order order){
                StringBuilder descriptionBuilder = new StringBuilder(255);
                for(LineItem line: order.getLineitems()){
                        if(line.getBrand() != null){
                                descriptionBuilder.append(line.getBrand() + " ");
                        }
                        if(line.getModel_name() != null){
                                descriptionBuilder.append(line.getModel_name() + " "); 
                        }
                        if(line.getModel_number() != null){
                                descriptionBuilder.append(line.getModel_number() + " ");
                        }
                        if(line.getColor() != null){
                                descriptionBuilder.append(line.getColor() + " ");
                        }
                }
                String desc = descriptionBuilder.toString();
                desc = desc.replaceAll("[^a-zA-Z0-9\\s\\-\\@\\/\\.]", "");
                this.description = new StringBuilder(desc);
        }
        
        public void setId(String id) {
                this.id = id;
        }

        public String getId() {
                return id;
        }

        public String getAccountId() {
                return accountId;
        }

        public static String getReturnUrl() {
                return returnUrl;
        }

        public static String getMode() {
                return mode;
        }
        
        public double getAmount() {
                return amount;
        }

        public void setPaymentOption(Payment payment) {
                this.paymentOption = null;
                List<Attribute> attributes = payment.getAttributes();
                if(attributes == null)
                        return;
                for(Attribute attr : attributes){
                        if(attr.getName().equals(IPaymentService.PAYMENT_METHOD))
                                this.paymentOption = attr.getValue();
                }
        }
        
        public String getPaymentOption(){
                return paymentOption;
        }

        public String getSecureHash() throws NoSuchAlgorithmException{
                String pass = ebsSecretKey + "|" + accountId + "|" + amount + "|" + id + "|" + returnUrl + "|" + mode;
                log.info("pass:" + pass);
                MessageDigest m = MessageDigest.getInstance("MD5");
                byte[] data = pass.getBytes();
                m.update(data,0,data.length);
                BigInteger i = new BigInteger(1,m.digest());
                String secureHash = String.format("%1$032X", i);
                return secureHash;
        }
        
        public ContactDetails getBillingDetails() {
                return billingDetails;
        }

        public void setPhone(String phone) {
        this.phone = phone;
    }


    public String getPhone() {
        return phone;
    }

    public static class ContactDetails{
                private String name;
                private String email;
                private String address;
                private String city;
                private String state;
                private String postalCode;
                private String country;
                private String phone;
                
                public ContactDetails(String name, String email, String address,
                                String city, String state, String postalCode, String country,
                                String phone) {
                        this.name = name;
                        this.email = email;
                        this.address = address;
                        this.city = city;
                        this.state = state;
                        this.postalCode = postalCode;
                        this.country = country;
                        this.phone = phone;
                }

                @Override
                public String toString() {
                        return "ContactDetails [name=" + name + ", email=" + email
                                        + ", address=" + address + ", city=" + city + ", state="
                                        + state + ", postalCode=" + postalCode + ", country="
                                        + country + ", phone=" + phone + "]";
                }
                
                public String getName() {
                        return name;
                }

                public String getEmail() {
                        return email;
                }

                public String getAddress() {
                        return address;
                }

                public String getCity() {
                        return city;
                }

                public String getState() {
                        return state;
                }

                public String getPostalCode() {
                        return postalCode;
                }

                public String getCountry() {
                        return country;
                }

                public String getPhone() {
                        return phone;
                }
        }
    
    public static void main(String[] args) {
        {
            String encrypted = "2be98afc86aa7f2e4cb79ac798cc2fd8a"; 
            BigInteger bi_r0 = new BigInteger("0933910847463829827159347601486730416058");
            /*String password = "shop2020";
            
            BigInteger bi_passwd = new BigInteger(password.getBytes());
            System.out.println(bi_passwd);

            
            BigInteger bi_r1 = bi_r0.xor(bi_passwd);
            System.out.println(bi_r1);
            System.out.println((bi_r1.toString(16)));
            */
            try
            {
              BigInteger bi_enc = new BigInteger(encrypted, 16);
              System.out.println(bi_enc);
              BigInteger bi_result = bi_enc.xor(bi_r0);
              System.out.println(bi_result);
              String str = new String(bi_result.toByteArray());
              System.out.println(str);
            } catch (Exception e) {
                System.out.println("oops");
            }
        }
    }
}