|
| 1 | +package com.unity3d.services.store; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.os.Bundle; |
| 5 | +import android.os.IBinder; |
| 6 | + |
| 7 | +import com.unity3d.services.core.log.DeviceLog; |
| 8 | +import com.unity3d.services.core.properties.ClientProperties; |
| 9 | +import com.unity3d.services.store.core.StoreException; |
| 10 | + |
| 11 | +import org.json.JSONArray; |
| 12 | +import org.json.JSONException; |
| 13 | +import org.json.JSONObject; |
| 14 | + |
| 15 | +import java.lang.reflect.InvocationTargetException; |
| 16 | +import java.lang.reflect.Method; |
| 17 | +import java.util.ArrayList; |
| 18 | + |
| 19 | +public class StoreBilling { |
| 20 | + public static Object asInterface(Context context, IBinder service) { |
| 21 | + Object[] args = new Object[] { service }; |
| 22 | + |
| 23 | + Class billingServiceStub; |
| 24 | + |
| 25 | + try { |
| 26 | + billingServiceStub = Class.forName("com.android.vending.billing.IInAppBillingService$Stub"); |
| 27 | + } catch (ClassNotFoundException e) { |
| 28 | + DeviceLog.exception("Billing service stub not found", e); |
| 29 | + return null; |
| 30 | + } |
| 31 | + |
| 32 | + Method asInterface; |
| 33 | + |
| 34 | + try { |
| 35 | + asInterface = billingServiceStub.getMethod("asInterface", IBinder.class); |
| 36 | + } catch (NoSuchMethodException e) { |
| 37 | + DeviceLog.exception("asInterface method not found", e); |
| 38 | + return null; |
| 39 | + } |
| 40 | + |
| 41 | + try { |
| 42 | + return asInterface.invoke(null, service); |
| 43 | + } catch (IllegalAccessException e) { |
| 44 | + DeviceLog.exception("Illegal access exception while invoking asInterface", e); |
| 45 | + } catch (InvocationTargetException e) { |
| 46 | + DeviceLog.exception("Invocation target exception while invoking asInterface", e); |
| 47 | + } |
| 48 | + |
| 49 | + return null; |
| 50 | + } |
| 51 | + |
| 52 | + public static int isBillingSupported(Context context, Object billingServiceObject, String purchaseType) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, StoreException { |
| 53 | + Class billingService = Class.forName("com.android.vending.billing.IInAppBillingService"); |
| 54 | + Method isBillingSupported = billingService.getMethod("isBillingSupported", Integer.TYPE, String.class, String.class); |
| 55 | + |
| 56 | + Object result = isBillingSupported.invoke(billingServiceObject, 3, ClientProperties.getAppName(), purchaseType); |
| 57 | + |
| 58 | + if(result != null) { |
| 59 | + return (int) result; |
| 60 | + } |
| 61 | + |
| 62 | + throw new StoreException(); |
| 63 | + } |
| 64 | + |
| 65 | + public static JSONObject getPurchases(Context context, Object billingServiceObject, String purchaseType) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, JSONException, StoreException { |
| 66 | + Class billingService = Class.forName("com.android.vending.billing.IInAppBillingService"); |
| 67 | + Method getPurchases = billingService.getMethod("getPurchases", Integer.TYPE, String.class, String.class, String.class); |
| 68 | + |
| 69 | + JSONObject resultObject = new JSONObject(); |
| 70 | + JSONArray purchaseDataArray = new JSONArray(); |
| 71 | + JSONArray signatureArray = new JSONArray(); |
| 72 | + JSONArray productArray = new JSONArray(); |
| 73 | + String continuationToken = null; |
| 74 | + |
| 75 | + do { |
| 76 | + Object result = getPurchases.invoke(billingServiceObject, 3, ClientProperties.getAppName(), purchaseType, continuationToken); |
| 77 | + continuationToken = null; |
| 78 | + |
| 79 | + if(result instanceof Bundle) { |
| 80 | + Bundle resultBundle = (Bundle) result; |
| 81 | + |
| 82 | + int responseCode = resultBundle.getInt("RESPONSE_CODE"); |
| 83 | + DeviceLog.debug("getPurchases responds with code " + responseCode); |
| 84 | + |
| 85 | + if(responseCode == 0) { |
| 86 | + ArrayList<String> purchaseDataList = resultBundle.getStringArrayList("INAPP_PURCHASE_DATA_LIST"); |
| 87 | + for(String purchase : purchaseDataList) { |
| 88 | + purchaseDataArray.put(new JSONObject(purchase)); |
| 89 | + } |
| 90 | + |
| 91 | + ArrayList<String> signatureList = resultBundle.getStringArrayList("INAPP_DATA_SIGNATURE_LIST"); |
| 92 | + for(String signature : signatureList) { |
| 93 | + signatureArray.put(signature); |
| 94 | + } |
| 95 | + |
| 96 | + ArrayList<String> productList = resultBundle.getStringArrayList("INAPP_PURCHASE_ITEM_LIST"); |
| 97 | + for(String product : productList) { |
| 98 | + productArray.put(product); |
| 99 | + } |
| 100 | + |
| 101 | + continuationToken = resultBundle.getString("INAPP_CONTINUATION_TOKEN"); |
| 102 | + } else { |
| 103 | + throw new StoreException(responseCode); |
| 104 | + } |
| 105 | + } else { |
| 106 | + throw new StoreException(); |
| 107 | + } |
| 108 | + } while(continuationToken != null); |
| 109 | + |
| 110 | + resultObject.put("purchaseDataList", purchaseDataArray); |
| 111 | + resultObject.put("signatureList", signatureArray); |
| 112 | + resultObject.put("purchaseItemList", productArray); |
| 113 | + |
| 114 | + return resultObject; |
| 115 | + } |
| 116 | + |
| 117 | + public static JSONObject getPurchaseHistory(Context context, Object billingServiceObject, String purchaseType, int maxPurchases) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, JSONException, StoreException { |
| 118 | + Class billingService = Class.forName("com.android.vending.billing.IInAppBillingService"); |
| 119 | + Method getPurchaseHistory = billingService.getMethod("getPurchaseHistory", Integer.TYPE, String.class, String.class, String.class, Bundle.class); |
| 120 | + |
| 121 | + JSONObject resultObject = new JSONObject(); |
| 122 | + JSONArray purchaseDataArray = new JSONArray(); |
| 123 | + JSONArray signatureArray = new JSONArray(); |
| 124 | + JSONArray productArray = new JSONArray(); |
| 125 | + |
| 126 | + String continuationToken = null; |
| 127 | + int purchaseCount = 0; |
| 128 | + |
| 129 | + do { |
| 130 | + Object result = getPurchaseHistory.invoke(billingServiceObject, 6, ClientProperties.getAppName(), purchaseType, continuationToken, new Bundle()); |
| 131 | + continuationToken = null; |
| 132 | + |
| 133 | + if(result instanceof Bundle) { |
| 134 | + Bundle resultBundle = (Bundle) result; |
| 135 | + |
| 136 | + int responseCode = resultBundle.getInt("RESPONSE_CODE"); |
| 137 | + |
| 138 | + if(responseCode == 0) { |
| 139 | + ArrayList<String> purchaseDataList = resultBundle.getStringArrayList("INAPP_PURCHASE_DATA_LIST"); |
| 140 | + for(String purchase : purchaseDataList) { |
| 141 | + purchaseDataArray.put(new JSONObject(purchase)); |
| 142 | + purchaseCount++; |
| 143 | + } |
| 144 | + |
| 145 | + ArrayList<String> signatureList = resultBundle.getStringArrayList("INAPP_DATA_SIGNATURE_LIST"); |
| 146 | + for(String signature : signatureList) { |
| 147 | + signatureArray.put(signature); |
| 148 | + } |
| 149 | + |
| 150 | + ArrayList<String> productList = resultBundle.getStringArrayList("INAPP_PURCHASE_ITEM_LIST"); |
| 151 | + for(String product : productList) { |
| 152 | + productArray.put(product); |
| 153 | + } |
| 154 | + |
| 155 | + continuationToken = resultBundle.getString("INAPP_CONTINUATION_TOKEN"); |
| 156 | + } else { |
| 157 | + throw new StoreException(responseCode); |
| 158 | + } |
| 159 | + } else { |
| 160 | + throw new StoreException(); |
| 161 | + } |
| 162 | + } while(continuationToken != null && (maxPurchases == 0 || purchaseCount < maxPurchases)); |
| 163 | + |
| 164 | + resultObject.put("purchaseDataList", purchaseDataArray); |
| 165 | + resultObject.put("signatureList", signatureArray); |
| 166 | + resultObject.put("purchaseItemList", productArray); |
| 167 | + |
| 168 | + return resultObject; |
| 169 | + } |
| 170 | + |
| 171 | + public static JSONArray getSkuDetails(Context context, Object billingServiceObject, String purchaseType, ArrayList<String> skuList) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, StoreException, JSONException { |
| 172 | + Class billingService = Class.forName("com.android.vending.billing.IInAppBillingService"); |
| 173 | + Method getSkuDetails = billingService.getMethod("getSkuDetails", Integer.TYPE, String.class, String.class, Bundle.class); |
| 174 | + |
| 175 | + Bundle args = new Bundle(); |
| 176 | + args.putStringArrayList("ITEM_ID_LIST", skuList); |
| 177 | + |
| 178 | + Object result = getSkuDetails.invoke(billingServiceObject, 3, ClientProperties.getAppName(), purchaseType, args); |
| 179 | + |
| 180 | + JSONArray resultArray = new JSONArray(); |
| 181 | + |
| 182 | + if(result instanceof Bundle) { |
| 183 | + Bundle resultBundle = (Bundle) result; |
| 184 | + |
| 185 | + int responseCode = resultBundle.getInt("RESPONSE_CODE"); |
| 186 | + |
| 187 | + if(responseCode == 0) { |
| 188 | + ArrayList<String> detailsList = resultBundle.getStringArrayList("DETAILS_LIST"); |
| 189 | + |
| 190 | + for(String detail : detailsList) { |
| 191 | + resultArray.put(new JSONObject(detail)); |
| 192 | + } |
| 193 | + } else { |
| 194 | + throw new StoreException(responseCode); |
| 195 | + } |
| 196 | + } else { |
| 197 | + throw new StoreException(); |
| 198 | + } |
| 199 | + |
| 200 | + return resultArray; |
| 201 | + } |
| 202 | +} |
0 commit comments