Subversion Repositories SmartDukaan

Rev

Go to most recent revision | Details | 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
 
23
	public static void storeVoucher(String username, String password, long amount) throws Exception{
24
		String coupon = generateVoucher(username, password, amount);
25
		Voucher voucher = new Voucher();
26
		voucher.setVoucherType(VoucherType.MOBILE);
27
		voucher.setAmount(amount);
28
		voucher.setVoucherCode(coupon);
29
		voucher.setIssuedOn(new Date().getTime());
30
		Client pc = new PromotionClient().getClient();
31
		pc.addVoucher(voucher);
32
	}
33
 
34
	public static void issueVoucher(String username, String password, long userId, String userEmail, long amount) throws Exception{
35
		Client pc = new PromotionClient().getClient();
36
		Voucher voucher = pc.assignVoucher(userId, userEmail, VoucherType.MOBILE, amount);
37
		boolean isActivated = activateVoucher(username, password, voucher.getVoucherCode(), voucher.getUserEmail());
38
		if(!isActivated){
39
			throw new Exception("Voucher could not get activated.");
40
		}
41
	}
42
 
43
	public static String generateVoucher(String username, String password, long amount) throws Exception{
44
		String params = "userid=" + username + "&password=" + password + "&amount=" + amount;
45
		String returnValue = sendGetRequest(voucherGenerationUrl, params);
46
		if("-1".equals(returnValue)){
47
			throw new Exception("Credit balance low.");
48
		}else if("0".equals(returnValue)){
49
			throw new Exception("Network or some other failure.");
50
		}
51
		return returnValue;
52
	}
53
 
54
	public static boolean activateVoucher(String username, String password, String voucherCode, String userEmail) throws Exception{
55
		String params = "userid=" + username + "&password=" + password + "&cid=" + voucherCode + "&email=" + userEmail; 
56
		String returnValue = sendGetRequest(voucherActivationUrl, params);
57
		if("1".equals(returnValue)){
58
			return true;
59
		}else if("-1".equals(returnValue)){
60
			throw new Exception("Invalid voucher.");
61
		}else if("0".equals(returnValue)){
62
			throw new Exception("Network or some other failure.");
63
		}
64
		return false;
65
	}
66
 
67
	private static String sendGetRequest(String endpoint, String requestParameters)
68
	{
69
		String result = null;
70
		try
71
		{
72
			String urlStr = endpoint;
73
			if (requestParameters != null && requestParameters.length () > 0){
74
				urlStr += "?" + requestParameters;
75
			}
76
			URL url = new URL(urlStr);
77
			URLConnection conn = url.openConnection ();
78
 
79
			// Get the response
80
			BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
81
			StringBuffer sb = new StringBuffer();
82
			String line;
83
			while ((line = rd.readLine()) != null)
84
			{
85
				sb.append(line);
86
			}
87
			rd.close();
88
			result = sb.toString();
89
		} catch (Exception e){
90
			e.printStackTrace();
91
	    	logger.error("Not able to get data from server");
92
		}
93
 
94
		return result;
95
	}
96
 
97
	public static void main(String args[]) throws Exception{
98
		//System.out.println(SpiceDeckRechargeService.generateVoucher("sachingyal", "abc1234", 1));
99
		//System.out.println(SpiceDeckRechargeService.activateVoucher("sachingyal", "abc1234", "SHL1340274524944", "rajveer.singh@saholic.com"));
100
		//storeVoucher("sachingyal", "abc1234", 5);
101
		issueVoucher("sachingyal", "abc1234", 191919, "anupam.singh@shop2020.in", 5);
102
	}
103
}