Subversion Repositories SmartDukaan

Rev

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

package in.shop2020.serving.controllers;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
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")

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

        private static Logger log = Logger.getLogger(Class.class);
        
        private static String accountId;
        
        private static String returnUrl;
        
        private static String salt;
        
        private static String gatewayUrl;
        
        private static String subAccountId;
        
        static{
                try {
                        accountId = ConfigClient.getClient().get("innoviti_account_id");
                        returnUrl = ConfigClient.getClient().get("innoviti_return_url");
                        salt = ConfigClient.getClient().get("innoviti_secret_key");
                        subAccountId = ConfigClient.getClient().get("innoviti_sub_account_id");
                        gatewayUrl = ConfigClient.getClient().get("innoviti_gateway_url");
                } catch (ConfigException e) {
                        log.error("Unable to get Innoviti payment configuration.");
                }
        }
        
        private String id;
        
        private String paymentOption = null;
        
        private StringBuilder description;
        
        private double amount;
        
        private double emiAmount = 0;
        
        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);
//                      Map<Long, Double> miscCharges = txnClient.getMiscCharges(txnId);
//                      System.out.println(miscCharges);
//                      if(miscCharges != null & !miscCharges.isEmpty()){
//                              emiAmount = miscCharges.get(1L);
//                      }
                } 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().trim(),
                                order.getCustomer_email().trim(), order.getCustomer_address1(), order.getCustomer_address2(),
                                order.getCustomer_city(), order.getCustomer_state(),
                                order.getCustomer_pincode(), "India",
                                order.getCustomer_mobilenumber().trim());
                
                log.info(billingDetails);
                
                return "show";
        }

        public String getDescription(){
                if(this.description.length() >= 255)
                        return this.description.substring(0, 255).trim();
                else
                        return this.description.toString().trim();
        }
        
        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 String getSubAccountId() {
                return subAccountId;
        }

        public static String getReturnUrl() {
                return returnUrl;
        }

        public String getGatewayUrl(){
                return gatewayUrl;
        }

        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 getProcessingCode(){
                return paymentOption;
        }
                
        public String isCtx(){
                return "YES";
        }
        
        public double getCtxProcessingFee(){
                return emiAmount;
        }
        
        public double getCtxInterest(){
                return ProceedToPayController.getInterestRate(Long.parseLong(paymentOption));
        }
        
        public String getSecureHash() throws NoSuchAlgorithmException{
//              MD5(orderId "|" merchantId "|" subMerchantId "|" amt "|" cur "|" proSku "|" Cname "|" mobile "|" emailId "|" redirUrl "|" 123^~abc%) (Salt Value Decide by Merchant and uniPAY-Net))
                String pass = id + "|" + accountId + "|" +  subAccountId  + "|" + amount  + "|" +  "INR" + "|" + getDescription() + "|" +  billingDetails.getName() + "|" + billingDetails.getPhone() + "|" + billingDetails.getEmail() + "|" + returnUrl + "|" + salt;
                System.out.println(pass);
                MessageDigest md = MessageDigest.getInstance("MD5");
                md.update(pass.getBytes(), 0, pass.getBytes().length);
                byte[] mdbytes = md.digest();
                //      convert the byte to hex format method
                StringBuffer sb = new StringBuffer();
                for (int i = 0; i < mdbytes.length; i++) {
                        sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
                }
                return sb.toString();
        }
        
        public ContactDetails getBillingDetails() {
                return billingDetails;
        }
        
                
        public static void main(String[] args) throws NoSuchAlgorithmException {
                //String pass = "410|1234567800|uniPAY|5985|INR|||||RedirectUrl|123^~abc%)";
                //String pass = "113724|544433321212272|3001|3586|INR|Micromax Smarty A25 Black|Rajveer|1111111111|rajveer.singh@shop2020.in|http://www.shop2020.in/innoviti-pay-response|123^~uni%";
                String pass = "113734|544433321212272|3001|3586|INR|Micromax Smarty A25 Black|Rajveer|1111111111|rajveer.singh@shop2020.in|http://www.shop2020.in/innoviti-pay-response|123^~uni%";
                //5fc923e520bce47ff14b3aa1a5245287
          //5fc923e520bce47ff14b3aa1a5245287
                //15d4dbc305d0b56ce1973726bdee526d
                //db4d6309123fd1543882eba3123f52b4
                //93d97a5f16fb132c8728ee799e81915e
                //bf29c85dcd387c1160fe64a2af8b0463

                
                System.out.println(pass);
                MessageDigest md = MessageDigest.getInstance("MD5");
                md.update(pass.getBytes(), 0, pass.getBytes().length);
                byte[] mdbytes = md.digest();
                //      convert the byte to hex format method
                StringBuffer sb = new StringBuffer();
                for (int i = 0; i < mdbytes.length; i++) {
                        sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
                }
                System.out.println(sb.toString());
        }

        public static class ContactDetails{
                private String name;
                private String email;
                private String address1;
                private String address2;
                private String city;
                private String state;
                private String postalCode;
                private String country;
                private String phone;
                
                public ContactDetails(String name, String email, String address1, String address2,
                                String city, String state, String postalCode, String country,
                                String phone) {
                        this.name = name;
                        this.email = email;
                        this.address1 = address1;
                        this.address2 = address2;
                        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
                                        + ", address1=" + address1 + ",  address2=" + address2 + ",city=" + city + ", state="
                                        + state + ", postalCode=" + postalCode + ", country="
                                        + country + ", phone=" + phone + "]";
                }
                
                public String getName() {
                        return name;
                }

                public String getEmail() {
                        return email;
                }

                public String getAddress1() {
                        return address1;
                }

                public String getAddress2() {
                        return address2;
                }

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