Android Open Source - GameThrive-Android-SDK Track Amazon Purchase






From Project

Back to project page GameThrive-Android-SDK.

License

The source code is released under:

MIT License

If you think the Android project GameThrive-Android-SDK listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/**
 * Modified MIT License//from w  w w  .  j  a  v a  2 s  .co m
 * 
 * Copyright 2014 GameThrive
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * 1. The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * 2. All copies of substantial portions of the Software may only be used in connection
 * with services provided by GameThrive.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.gamethrive;

import java.lang.reflect.Field;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;

import com.amazon.device.iap.PurchasingListener;
import com.amazon.device.iap.PurchasingService;
import com.amazon.device.iap.model.Product;
import com.amazon.device.iap.model.ProductDataResponse;
import com.amazon.device.iap.model.PurchaseResponse;
import com.amazon.device.iap.model.PurchaseUpdatesResponse;
import com.amazon.device.iap.model.RequestId;
import com.amazon.device.iap.model.UserDataResponse;

class TrackAmazonPurchase {
  
  private Context context;
  
  private boolean canTrack = false;
  
  private GTPurchasingListener gtPurchasingListener;
  
  private Class<?> listnerHandlerClass;
  private Object listnerHandlerObject;
  private Field listnerHandlerField;
  
  TrackAmazonPurchase(Context context, GameThrive gameThrive) {
    this.context = context;
      
      try {
        // 2.0.1
        listnerHandlerClass = Class.forName("com.amazon.device.iap.internal.d");
        listnerHandlerObject = listnerHandlerClass.getMethod("d").invoke(null);
        listnerHandlerField = listnerHandlerClass.getDeclaredField("f");
        listnerHandlerField.setAccessible(true);
        
        gtPurchasingListener = new GTPurchasingListener(gameThrive);
        gtPurchasingListener.orgPurchasingListener = (PurchasingListener)listnerHandlerField.get(listnerHandlerObject);
        
        canTrack = true;
        setListener();
      } catch(Throwable t) {}
  }
  
  private void setListener() {
    PurchasingService.registerListener(context, gtPurchasingListener);
  }
  
  public void checkListener() {
    if (canTrack) {
      try {
        PurchasingListener curPurchasingListener = (PurchasingListener)listnerHandlerField.get(listnerHandlerObject);
        if (curPurchasingListener != gtPurchasingListener) {
          gtPurchasingListener.orgPurchasingListener = curPurchasingListener;
          setListener();
        }
      } catch (Throwable t) {}
    }
  }
  
  private class GTPurchasingListener implements PurchasingListener {
    private static final String TAG = "GameThriveSDK-AmazonIAP";
    
    private GameThrive gameThrive;
    
    public PurchasingListener orgPurchasingListener;
    
    private RequestId lastRequestId;
    private String currentMarket;
    
    GTPurchasingListener(GameThrive gameThrive) {
      this.gameThrive = gameThrive;
    }
    
    private String marketToCurrencyCode(String market) {
      switch(market) {
        case "US":
          return "USD";
        case "GB":
          return "GBP";
        case "DE":
        case "FR":
        case "ES":
        case "IT":
          return "EUR";
        case "JP":
          return "JPY";
        case "CA":
          return "CDN";
        case "BR":
          return "BRL";
        case "AU":
          return "AUD";
      }
      
      return "";
    }

    @Override
    public void onProductDataResponse(final ProductDataResponse response) {
      if (lastRequestId != null && lastRequestId.toString().equals(response.getRequestId().toString())) {
        try {
          switch (response.getRequestStatus()) {
            case SUCCESSFUL:
              for (final String s : response.getUnavailableSkus()) {
                Log.v(TAG, "Unavailable SKU:" + s);
              }
      
              JSONArray purchasesToReport = new JSONArray();
              final Map<String, Product> products = response.getProductData();
              for(final String key : products.keySet()) {
                Product product = products.get(key);
                
                JSONObject jsonItem = new JSONObject();
                jsonItem.put("sku",  product.getSku());
                jsonItem.put("iso", marketToCurrencyCode(currentMarket));
                
                String price = product.getPrice();
                if (!price.matches("^[0-9]"))
                  price = price.substring(1);
                jsonItem.put("amount", price);
                
                purchasesToReport.put(jsonItem);
              }
              gameThrive.sendPurchases(purchasesToReport, false, null);
              break;
          }
        } catch(Throwable t) {
          t.printStackTrace();
        }
      }
      else if (orgPurchasingListener != null)
        orgPurchasingListener.onProductDataResponse(response);
    }

    @Override
    public void onPurchaseResponse(PurchaseResponse response) {
      try {
        final PurchaseResponse.RequestStatus status = response.getRequestStatus();
        
        if (status == PurchaseResponse.RequestStatus.SUCCESSFUL) {
          currentMarket = response.getUserData().getMarketplace();
          
          Set<String> productSkus = new HashSet<String>();
          productSkus.add(response.getReceipt().getSku());
          lastRequestId = PurchasingService.getProductData(productSkus);
        }
      } catch(Throwable t)  {
        t.printStackTrace();
      }

      if (orgPurchasingListener != null)
        orgPurchasingListener.onPurchaseResponse(response);
    }

    @Override
    public void onPurchaseUpdatesResponse(final PurchaseUpdatesResponse response) {
      if (orgPurchasingListener != null)
        orgPurchasingListener.onPurchaseUpdatesResponse(response);
    }

    @Override
    public void onUserDataResponse(final UserDataResponse response) {
      if (orgPurchasingListener != null)
        orgPurchasingListener.onUserDataResponse(response);
    }
  }
}




Java Source Code List

com.gamethrive.ADMMessageHandler.java
com.gamethrive.AdvertisingIdProviderFallback.java
com.gamethrive.AdvertisingIdProviderGPS.java
com.gamethrive.AdvertisingIdentifierProvider.java
com.gamethrive.GameThriveRestClient.java
com.gamethrive.GameThriveUnityProxy.java
com.gamethrive.GameThrive.java
com.gamethrive.GcmBroadcastReceiver.java
com.gamethrive.GcmIntentService.java
com.gamethrive.GenerateNotification.java
com.gamethrive.NotificationOpenedActivity.java
com.gamethrive.NotificationOpenedHandler.java
com.gamethrive.PushRegistratorADM.java
com.gamethrive.PushRegistratorGPS.java
com.gamethrive.PushRegistrator.java
com.gamethrive.TrackAmazonPurchase.java
com.gamethrive.TrackGooglePurchase.java
com.hiptic.gamethriveexample.MainActivity.java
com.loopj.android.http.AssertUtils.java
com.loopj.android.http.AsyncHttpClient.java
com.loopj.android.http.AsyncHttpRequest.java
com.loopj.android.http.AsyncHttpResponseHandler.java
com.loopj.android.http.Base64DataException.java
com.loopj.android.http.Base64OutputStream.java
com.loopj.android.http.Base64.java
com.loopj.android.http.BaseJsonHttpResponseHandler.java
com.loopj.android.http.BinaryHttpResponseHandler.java
com.loopj.android.http.DataAsyncHttpResponseHandler.java
com.loopj.android.http.FileAsyncHttpResponseHandler.java
com.loopj.android.http.JsonHttpResponseHandler.java
com.loopj.android.http.JsonStreamerEntity.java
com.loopj.android.http.JsonValueInterface.java
com.loopj.android.http.MyRedirectHandler.java
com.loopj.android.http.MySSLSocketFactory.java
com.loopj.android.http.PersistentCookieStore.java
com.loopj.android.http.PreemtiveAuthorizationHttpRequestInterceptor.java
com.loopj.android.http.RangeFileAsyncHttpResponseHandler.java
com.loopj.android.http.RequestHandle.java
com.loopj.android.http.RequestParams.java
com.loopj.android.http.ResponseHandlerInterface.java
com.loopj.android.http.RetryHandler.java
com.loopj.android.http.SaxAsyncHttpResponseHandler.java
com.loopj.android.http.SerializableCookie.java
com.loopj.android.http.SimpleMultipartEntity.java
com.loopj.android.http.SyncHttpClient.java
com.loopj.android.http.TextHttpResponseHandler.java
com.loopj.android.http.package-info.java
com.stericson.RootTools.internal.RootToolsInternalMethods.java