Subversion Repositories SmartDukaan

Rev

Rev 3126 | Rev 6091 | Go to most recent revision | 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 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.Transaction;
import in.shop2020.payments.Attribute;
import in.shop2020.payments.Payment;
import in.shop2020.serving.services.IPaymentService;
import in.shop2020.thrift.clients.PaymentClient;
import in.shop2020.thrift.clients.TransactionClient;
import in.shop2020.thrift.clients.config.ConfigClient;

@SuppressWarnings("serial")
@InterceptorRefs({
    @InterceptorRef("myDefault"),
    @InterceptorRef("login")
})
@Results({
        @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;
        
        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";
        }

        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;
                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 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;
                }
        }
}