Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
5469 rajveer 1
package in.shop2020.support.services;
2
 
3
import in.shop2020.model.v1.user.PromotionService.Client;
4
import in.shop2020.model.v1.user.Voucher;
5
import in.shop2020.model.v1.user.VoucherType;
6
import in.shop2020.thrift.clients.PromotionClient;
7
 
8
import java.io.BufferedReader;
9
import java.io.InputStreamReader;
10
import java.net.URL;
11
import java.net.URLConnection;
12
import java.util.Date;
13
 
14
import org.slf4j.Logger;
15
import org.slf4j.LoggerFactory;
16
 
17
 
18
public class SpiceDeckRechargeService {
19
	private static Logger logger = LoggerFactory.getLogger(SpiceDeckRechargeService.class);
20
	private static final String voucherGenerationUrl = "http://www.spicedeck.com/mcommerce/couponregister.sdesk";
21
	private static final String voucherActivationUrl = "http://www.spicedeck.com/mcommerce/couponassign.sdesk";
22
 
5492 rajveer 23
	public static void storeVoucher(String username, String password, long amount, long numberOfVouchers) throws Exception{
24
		for(int i=0; i<numberOfVouchers; i++){
25
			String coupon = generateVoucher(username, password, amount);
26
			Voucher voucher = new Voucher();
27
			voucher.setVoucherType(VoucherType.SPICEDECK_MOBILE);
28
			voucher.setAmount(amount);
29
			voucher.setVoucherCode(coupon);
30
			voucher.setIssuedOn(new Date().getTime());
31
			Client pc = new PromotionClient().getClient();
32
			pc.addVoucher(voucher);
33
		}
5469 rajveer 34
	}
35
 
36
	public static void issueVoucher(String username, String password, long userId, String userEmail, long amount) throws Exception{
37
		Client pc = new PromotionClient().getClient();
5492 rajveer 38
		Voucher voucher = pc.assignVoucher(userId, userEmail, VoucherType.SPICEDECK_MOBILE, amount);
5469 rajveer 39
		boolean isActivated = activateVoucher(username, password, voucher.getVoucherCode(), voucher.getUserEmail());
40
		if(!isActivated){
41
			throw new Exception("Voucher could not get activated.");
42
		}
43
	}
44
 
45
	public static String generateVoucher(String username, String password, long amount) throws Exception{
46
		String params = "userid=" + username + "&password=" + password + "&amount=" + amount;
47
		String returnValue = sendGetRequest(voucherGenerationUrl, params);
48
		if("-1".equals(returnValue)){
49
			throw new Exception("Credit balance low.");
50
		}else if("0".equals(returnValue)){
51
			throw new Exception("Network or some other failure.");
52
		}
53
		return returnValue;
54
	}
55
 
56
	public static boolean activateVoucher(String username, String password, String voucherCode, String userEmail) throws Exception{
57
		String params = "userid=" + username + "&password=" + password + "&cid=" + voucherCode + "&email=" + userEmail; 
58
		String returnValue = sendGetRequest(voucherActivationUrl, params);
59
		if("1".equals(returnValue)){
60
			return true;
61
		}else if("-1".equals(returnValue)){
62
			throw new Exception("Invalid voucher.");
63
		}else if("0".equals(returnValue)){
64
			throw new Exception("Network or some other failure.");
65
		}
66
		return false;
67
	}
68
 
69
	private static String sendGetRequest(String endpoint, String requestParameters)
70
	{
71
		String result = null;
72
		try
73
		{
74
			String urlStr = endpoint;
75
			if (requestParameters != null && requestParameters.length () > 0){
76
				urlStr += "?" + requestParameters;
77
			}
78
			URL url = new URL(urlStr);
79
			URLConnection conn = url.openConnection ();
80
 
81
			// Get the response
82
			BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
83
			StringBuffer sb = new StringBuffer();
84
			String line;
85
			while ((line = rd.readLine()) != null)
86
			{
87
				sb.append(line);
88
			}
89
			rd.close();
90
			result = sb.toString();
91
		} catch (Exception e){
92
			e.printStackTrace();
93
	    	logger.error("Not able to get data from server");
94
		}
95
 
96
		return result;
97
	}
98
 
99
	public static void main(String args[]) throws Exception{
100
		//System.out.println(SpiceDeckRechargeService.generateVoucher("sachingyal", "abc1234", 1));
101
		//System.out.println(SpiceDeckRechargeService.activateVoucher("sachingyal", "abc1234", "SHL1340274524944", "rajveer.singh@saholic.com"));
5492 rajveer 102
		storeVoucher("sachingyal", "abc1234", 5, 1);
103
		issueVoucher("sachingyal", "abc1234", 191919, "mandeep.dhir@shop2020.in", 5);
5469 rajveer 104
	}
105
}