Subversion Repositories SmartDukaan

Rev

Rev 10968 | Rev 13425 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.serving.controllers;

import java.util.List;

import in.shop2020.model.v1.order.RechargeOrder;
import in.shop2020.model.v1.user.Address;
import in.shop2020.payments.Payment;
import in.shop2020.payments.PaymentException;
import in.shop2020.serving.services.EbsPaymentService;
import in.shop2020.serving.services.HdfcPaymentService;
import in.shop2020.serving.services.IPaymentService;
import in.shop2020.thrift.clients.PaymentClient;
import in.shop2020.thrift.clients.TransactionClient;
import in.shop2020.thrift.clients.UserClient;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;


@Results({
    @Result(name="recharge-redirect", type="redirectAction", params = {"actionName" , "recharge", "error", "${errorMessage}"}),
    @Result(name="recharge-pay-options-redirect", type="redirectAction", params = {"actionName" , "recharge-pay-options", "rechargeOrderId", "${rechargeOrderId}"}),
    @Result(name="ebs-pay-redirect", type="redirectAction", params = {"actionName", "/ebs-pay/${paymentId}/edit", "phone", "${phone}"}),
    @Result(name="cod-redirect", type="redirect", location="/pay-success?paymentId=${paymentId}")
})


public class RechargePaymentController extends BaseController {
    
    /**
     * 
     */
    private String rechargeOrderId = "";
    private String name = "";
    private String line1 = "";
    private String line2 = "";
    private String city = "";
    private String state = "";
    private String pincode = "";
    private String phone = "";
    
    
    private static final long serialVersionUID = 2079308723099307749L;

    private static Logger log = Logger.getLogger(Class.class);
    
    public String index() {
        return "index";
    }
    
    /******************************************************
     * 
     * 
     * 
     *****************************************************/
    
    
    public long getPaymentId() {
        return paymentId;
    }
    
    private String id;
    private long txnId = 0;
    
    private long paymentId;
    private String errorMessage;
    
    private String redirectURL;
    private RechargeOrder rechargeOrder;
    
    // GET /order/ orderid
    public String show() {
        log.info("id=" + id);
        htmlSnippets.put("MYACCOUNT_HEADER", pageLoader.getMyaccountHeaderHtml());
        htmlSnippets.put("ORDER_DETAILS", pageLoader.getOrderDetailsHtml(Long.parseLong(id), userinfo));
        return "show";
    }

    // POST /order/
    public String create(){

        if(rechargeOrderId == null){
            setErrorMessage("There was some problem. Please try again.");
            return "recharge-redirect";
        }
        try {
            TransactionClient tc = new TransactionClient();
            rechargeOrder = tc.getClient().getRechargeOrder(Long.parseLong(rechargeOrderId));
        } catch (Exception ee) {
            log.error("problem fetching rechargeOrder : " + rechargeOrderId);
            setErrorMessage("There was some problem. Please try again.");
            return "recharge-redirect";
        }
        
        try {
            PaymentClient paymentServiceClient = new PaymentClient();
            List<Payment> payments = paymentServiceClient.getClient().getPaymentForRechargeTxnId(rechargeOrder.getTransactionId());
            if(payments.size() > 0) {
                setErrorMessage("There was some problem. Please try again.");
                return "recharge-redirect";
            }
        } catch (Exception ee) {
            log.error("problem fetching payment for rechargeId : " + rechargeOrderId);
            setErrorMessage("There was some problem. Please try again.");
            return "recharge-redirect";
        }
        
        try {
            UserClient uc = new UserClient();
            if(!name.isEmpty() && !phone.isEmpty()) {
                Address address = new Address();
                address.setName(name);
                address.setLine1(line1);
                address.setLine2(line2);
                address.setCity(city);
                address.setState(state);
                address.setCountry("India");
                address.setPin(pincode);
                address.setPhone(phone);

                uc.getClient().addAddressForUser(rechargeOrder.getUserId(), address, true);
            }

        } catch (Exception e) {
            log.error("Problem saving the address", e);
            return "recharge-pay-options-redirect";
        }

        String paymentOption = request.getParameter("payment_option");
        log.info("Payment Option Selected: " + paymentOption);
        if(paymentOption == null || paymentOption.equals("")){
            addActionError("Please select a payment method to continue.");
            return "recharge-pay-options-redirect";
        }
        
        if (name.isEmpty() && !phone.isEmpty()) {
            //This means we only got the phone from UI.
            //Lets keep it.
        } else {
            //For all other cases make it empty.
            phone = "";
        }
        
        if(paymentOption.equals(IPaymentService.HDFC_MASTERCARD_CREDIT) || paymentOption.equals(IPaymentService.HDFC_MASTERCARD_DEBIT) || 
                paymentOption.equals(IPaymentService.HDFC_VISA) || paymentOption.equals(IPaymentService.HDFC_VISA_ELECTRON)) {
            // User has selected Visa or MasterCard
            HdfcPaymentService hdfcPaymentService = new HdfcPaymentService();
            paymentId = hdfcPaymentService.createPayment(rechargeOrder, paymentOption, phone);
            if (paymentId == IPaymentService.PAYMENT_NOT_CREATED) {
                log.error("Unable to process payment through HDFC. Falling through to EBS.");
            } else {
                this.redirectURL = hdfcPaymentService.getRedirectUrl();
                log.info(this.redirectURL);
                return "success";
            }
        }

        if(paymentOption.equals(IPaymentService.HDFC_VISA))
            paymentOption = IPaymentService.EBS_VISA;
        else if(paymentOption.equals(IPaymentService.HDFC_MASTERCARD_DEBIT) || paymentOption.equals(IPaymentService.HDFC_MASTERCARD_CREDIT))
            paymentOption = IPaymentService.EBS_MASTERCARD;
        else if(paymentOption.equals(IPaymentService.HDFC_VISA_ELECTRON))
            paymentOption = null;           //Since we don't know the bank's name in this case, we'll let the user select the bank on the EBS page.

        EbsPaymentService ebsPaymentService = new EbsPaymentService();
        paymentId = ebsPaymentService.createPayment(rechargeOrder, paymentOption);
        if(paymentId == IPaymentService.PAYMENT_NOT_CREATED){
            addActionError("We are experiencing some problems. Please try later.");
            log.error("Unable to process payment through EBS.");
            setErrorMessage("There was some problem. Please try again.");
            return "recharge-redirect";
        } else {
            log.info("Successfully created payment for EBS to process. Redirecting to /ebs-pay/" + paymentId);
            return "ebs-pay-redirect";
        }           
    }
            
    public String getMyaccountHeaderSnippet(){
        return htmlSnippets.get("MYACCOUNT_HEADER");
    }
    
    public String getOrderDetailsSnippet(){
        return htmlSnippets.get("ORDER_DETAILS");
    }
    
    public long getTxn(){
        return this.txnId;
    }

    public String getRedirectURL(){
        return this.redirectURL;
    }

    public void setRechargeOrderId(String rechargeOrderId) {
        this.rechargeOrderId = rechargeOrderId;
    }

    public String getRechargeOrderId() {
        return rechargeOrderId;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        log.info("phone---- " + phone);
        this.phone = phone;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        log.info("name---- " + name);
        this.name = name;
    }

    public String getLine1() {
        return line1;
    }

    public void setLine1(String line1) {
        this.line1 = line1;
    }

    public String getLine2() {
        return line2;
    }

    public void setLine2(String line2) {
        this.line2 = line2;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getPincode() {
        return pincode;
    }

    public void setPincode(String pincode) {
        this.pincode = pincode;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public String getErrorMessage() {
        return errorMessage;
    }
}