Skip to content

Instantly share code, notes, and snippets.

@davidjgraph
Created November 4, 2014 21:27
Show Gist options
  • Save davidjgraph/2270096a3e030849e96b to your computer and use it in GitHub Desktop.
Save davidjgraph/2270096a3e030849e96b to your computer and use it in GitHub Desktop.
Legacy Google licensing code
package com.jgraph.licensing;
import java.io.IOException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.api.client.sample.appsmarket.AppsMarketService;
import com.google.api.client.sample.appsmarket.model.Cart;
import com.google.api.client.sample.appsmarket.model.ClockSkew;
import com.google.api.client.sample.appsmarket.model.CustomerLicense;
import com.google.api.client.sample.appsmarket.model.Edition;
import com.google.api.client.sample.appsmarket.model.InitialCart;
import com.google.api.client.sample.appsmarket.model.LicenseNotification;
import com.google.api.client.sample.appsmarket.model.LicenseNotificationList;
import com.google.api.client.sample.appsmarket.model.LineItem;
import com.google.api.client.sample.appsmarket.model.RecurringCart;
import com.google.api.client.sample.appsmarket.model.Subscription;
import com.google.api.client.sample.appsmarket.model.UserLicense;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.appengine.labs.repackaged.org.json.JSONException;
import com.google.appengine.labs.repackaged.org.json.JSONObject;
@SuppressWarnings("serial")
public class LicensingServlet extends HttpServlet
{
private final static Logger LOGGER = Logger.getLogger(LicensingServlet.class.getName());
private final static DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException
{
/*String domainString = req.getParameter("domain");
Query q = new Query("email");
q.setKeysOnly();
q.setAncestor(KeyFactory.createKey("domain", domainString));
Integer size = datastore.prepare(q).countEntities(FetchOptions.Builder.withDefaults());
LOGGER.info("Users for " + domainString + " domain : " + size);
UserService us = UserServiceFactory.getUserService();
if(!UserServiceFactory.getUserService().isUserLoggedIn())
{
String loginUrl = us.createLoginURL("http://localhost:8888");
LOGGER.info("Login URL : " + loginUrl);
}*/
AppsMarketService service = new AppsMarketService();
service.appId = "1007707086061";
service.appName = "draw.io";
service.endpoint = "https://www.googleapis.com/appsmarket/v2/";
service.consumerKey = service.appId + ".apps.googleusercontent.com";
service.consumerSecret = "SKLuxoAfQlBDE4-Iwt6XBaam";
service.authorize();
String userId = "[email protected]";
//UserLicense license = service.getUserLicense(userId);
CustomerLicense cl = service.getCustomerLicense("lucidchart.com", true);
//String pt = cl.subscription.purchaseToken;
//ClockSkew cs = service.getClockSkew("lucidchart.com");
int maxResults = 1000;
LicenseNotificationList list = service.getLicenseNotifications(maxResults, 0);
String token = "";
HashSet<String> customers = new HashSet<String>();
Subscription sub = new Subscription();
sub.exemptPayment = true;
sub.subscriptionId = "subID";
sub.applicationId = service.appId;
sub.name = "Full";
sub.description = "Full service";
sub.currencyCode = "USD";
sub.customerId = cl.customerId;
sub.initialCart = new InitialCart();
sub.initialCart.cart = new Cart();
sub.initialCart.cart.receiptName = "Receipt name";
sub.initialCart.cart.receiptDescription = "Receipt description";
LineItem item = new LineItem();
item.name = "My Subscription";
item.description = "Subscription billing for SaaSy App";
item.editionId = "default_edition";
//item.developerSku = "default_edition";
item.seatCount = 100; // Set this to -1 for a site license.
item.price = 100L;
sub.initialCart.cart.items = new ArrayList<LineItem>();;
sub.initialCart.cart.items.add(item);
Subscription response = service.insertSubscription(sub);
System.out.println(response);
/*while (list.notifications != null)
{
for (LicenseNotification notification : list.notifications)
{
customers.add(notification.customerId);
}
if (!token.equals(list.nextPageToken))
{
token = list.nextPageToken;
list = service.getLicenseNotifications(maxResults, list.nextPageToken);
}
else
{
break;
}
}
for(String c : customers)
{
/*Key domainKey = KeyFactory.createKey("domain", c);
Entity domain = new Entity(domainKey);
datastore.put(domain);
System.out.println(c);
CustomerLicense cl = service.getCustomerLicense(c, true);
if(cl.editions != null)
for(Edition e : cl.editions)
{
System.out.println("\t" + e.editionId);
}
else {
System.out.println("\t null");
}
}*/
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
String dataString = req.getParameter("userData");
JSONObject ud;
try
{
ud = new JSONObject(dataString);
}
catch (JSONException e)
{
resp.setStatus(500);
LOGGER.severe("Unabled to parse request JSON string : " + dataString);
return;
}
try
{
String domainString = ud.getString("domain");
Key domainKey = KeyFactory.createKey("domain", domainString);
Entity domain = getEntity(domainKey);
if (domain == null)
{
domain = new Entity(domainKey);
datastore.put(domain);
LOGGER.info("Added new domain : " + domainString);
}
String emailHash = ud.getString("emailHash");
Key emailKey = domainKey.getChild("emailHash", emailHash);
Entity email = getEntity(emailKey);
if (email == null)
{
email = new Entity(emailKey);
datastore.put(email);
LOGGER.info("Added new email hash : " + emailHash);
}
}
catch (JSONException e)
{
LOGGER.severe(e.getMessage());
}
resp.setStatus(200);
}
private Entity getEntity(Key key)
{
Entity ent = null;
try
{
ent = datastore.get(key);
}
catch (EntityNotFoundException e)
{
}
return ent;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment