Subversion Repositories SmartDukaan

Rev

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

package in.shop2020.support.services;

import in.shop2020.model.v1.user.PromotionService.Client;
import in.shop2020.model.v1.user.Voucher;
import in.shop2020.model.v1.user.VoucherType;
import in.shop2020.thrift.clients.PromotionClient;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class SpiceDeckRechargeService {
        private static Logger logger = LoggerFactory.getLogger(SpiceDeckRechargeService.class);
        private static final String voucherGenerationUrl = "http://www.spicedeck.com/mcommerce/couponregister.sdesk";
        private static final String voucherActivationUrl = "http://www.spicedeck.com/mcommerce/couponassign.sdesk";
        
        public static void storeVoucher(String username, String password, long amount, long numberOfVouchers) throws Exception{
                for(int i=0; i<numberOfVouchers; i++){
                        String coupon = generateVoucher(username, password, amount);
                        Voucher voucher = new Voucher();
                        voucher.setVoucherType(VoucherType.SPICEDECK_MOBILE);
                        voucher.setAmount(amount);
                        voucher.setVoucherCode(coupon);
                        voucher.setIssuedOn(new Date().getTime());
                        Client pc = new PromotionClient().getClient();
                        pc.addVoucher(voucher);
                }
        }

        public static void issueVoucher(String username, String password, long userId, String userEmail, long amount) throws Exception{
                Client pc = new PromotionClient().getClient();
                Voucher voucher = pc.assignVoucher(userId, userEmail, VoucherType.SPICEDECK_MOBILE, amount);
                boolean isActivated = activateVoucher(username, password, voucher.getVoucherCode(), voucher.getUserEmail());
                if(!isActivated){
                        throw new Exception("Voucher could not get activated.");
                }
        }
        
        public static String generateVoucher(String username, String password, long amount) throws Exception{
                String params = "userid=" + username + "&password=" + password + "&amount=" + amount;
                String returnValue = sendGetRequest(voucherGenerationUrl, params);
                if("-1".equals(returnValue)){
                        throw new Exception("Credit balance low.");
                }else if("0".equals(returnValue)){
                        throw new Exception("Network or some other failure.");
                }
                return returnValue;
        }
        
        public static boolean activateVoucher(String username, String password, String voucherCode, String userEmail) throws Exception{
                String params = "userid=" + username + "&password=" + password + "&cid=" + voucherCode + "&email=" + userEmail; 
                String returnValue = sendGetRequest(voucherActivationUrl, params);
                if("1".equals(returnValue)){
                        return true;
                }else if("-1".equals(returnValue)){
                        throw new Exception("Invalid voucher.");
                }else if("0".equals(returnValue)){
                        throw new Exception("Network or some other failure.");
                }
                return false;
        }
        
        private static String sendGetRequest(String endpoint, String requestParameters)
        {
                String result = null;
                try
                {
                        String urlStr = endpoint;
                        if (requestParameters != null && requestParameters.length () > 0){
                                urlStr += "?" + requestParameters;
                        }
                        URL url = new URL(urlStr);
                        URLConnection conn = url.openConnection ();
                        
                        // Get the response
                        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                        StringBuffer sb = new StringBuffer();
                        String line;
                        while ((line = rd.readLine()) != null)
                        {
                                sb.append(line);
                        }
                        rd.close();
                        result = sb.toString();
                } catch (Exception e){
                        e.printStackTrace();
                logger.error("Not able to get data from server");
                }
        
                return result;
        }

        public static void main(String args[]) throws Exception{
                //System.out.println(SpiceDeckRechargeService.generateVoucher("sachingyal", "abc1234", 1));
                //System.out.println(SpiceDeckRechargeService.activateVoucher("sachingyal", "abc1234", "SHL1340274524944", "rajveer.singh@saholic.com"));
                storeVoucher("sachingyal", "abc1234", 5, 1);
                issueVoucher("sachingyal", "abc1234", 191919, "mandeep.dhir@shop2020.in", 5);
        }
}