Subversion Repositories SmartDukaan

Rev

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

package in.shop2020.recharge.controllers;

import in.shop2020.model.v1.order.RechargeOrderStatus;
import in.shop2020.model.v1.order.RechargeTransaction;
import in.shop2020.model.v1.order.TransactionService;
import in.shop2020.thrift.clients.TransactionClient;

import java.nio.ByteBuffer;

import javax.servlet.ServletOutputStream;


public class RechargeResultController extends BaseController {

    private static final long serialVersionUID = 1L;
    private long rechargeId = 0l;
    private long paymentAmount;
        private RechargeTransaction recharge = null;
    
    
    public String index() {
        TransactionClient tcl;
        try {
            tcl = new TransactionClient();
            recharge =  tcl.getClient().getRechargeTransaction(rechargeId);
        } catch (Exception e) {
            log.error("Unable to get recharge txn for id : " + rechargeId, e);
            return ":";
        }
        return "index";
    }
    
    public String getResultMessage() {
        if(recharge.getStatus() == RechargeOrderStatus.RECHARGE_SUCCESSFUL) {
            return "Recharge successful";
        }
        else if (recharge.getStatus() == RechargeOrderStatus.RECHARGE_FAILED
                 || recharge.getStatus() == RechargeOrderStatus.INIT) {
            return "Recharge Failed";
        }
        else if (recharge.getStatus() == RechargeOrderStatus.RECHARGE_UNKNOWN) {
            return "Recharge Under Process";
        }
        else {
            return "There is some problem. Please try again later.";
        }
    }

    public boolean showPrintButton() {
        if(recharge.getStatus() == RechargeOrderStatus.RECHARGE_SUCCESSFUL) {
            return true;
        } else {
            return false;
        }
    }
    
    public String downloadInvoice() {
        ByteBuffer buffer = null;
        try {
            if (rechargeId == 0) {
                log.error("Recharge Id 0");
                return "index";
            }
            TransactionClient transactionServiceClient = new TransactionClient();
            TransactionService.Client orderClient = transactionServiceClient.getClient();
            buffer = orderClient.retrieveHotspotRechargeInvoice(rechargeId);
            if(!buffer.hasArray()) {
                log.error("The invoice was not found for rechargeId : " + rechargeId);
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        response.setContentType("application/pdf");
        response.setHeader("Content-disposition", "inline; filename=recharge-invoice-" + rechargeId + ".pdf");

        ServletOutputStream sos;
        try {
            sos = response.getOutputStream();
            sos.write(buffer.array());
            sos.flush();
        } catch (Exception e) {
            System.out.println("Unable to stream the invoice file");
        }
        return "index";
    }
    
    
    public long getRechargeId() {
        return rechargeId;
    }


    public void setRechargeId(long rechargeId) {
        this.rechargeId = rechargeId;
    }

    public RechargeTransaction getRecharge() {
        return recharge;
    }

    public void setRecharge(RechargeTransaction recharge) {
        this.recharge = recharge;
    }
    
    public long getPaymentAmount() {
                return paymentAmount;
        }

        public void setPaymentAmount(long paymentAmount) {
                this.paymentAmount = paymentAmount;
        }
}