|
34 | 34 |
|
35 | 35 | import com.android.vending.billing.IInAppBillingService;
|
36 | 36 |
|
| 37 | +import org.json.JSONException; |
37 | 38 | import org.json.JSONObject;
|
38 | 39 |
|
39 | 40 | import java.util.ArrayList;
|
@@ -489,6 +490,42 @@ public boolean isOneTimePurchaseWithExtraParamsSupported(Bundle extraParams)
|
489 | 490 | return isOneTimePurchaseExtraParamsSupported;
|
490 | 491 | }
|
491 | 492 |
|
| 493 | + /** |
| 494 | + * Checks if API supports version 6 which required to request purchase history |
| 495 | + * @param type product type, accepts either {@value Constants#PRODUCT_TYPE_MANAGED} |
| 496 | + * or {@value Constants#PRODUCT_TYPE_SUBSCRIPTION} |
| 497 | + * @return {@code true} if feature supported {@code false} otherwise |
| 498 | + */ |
| 499 | + public boolean isRequestBillingHistorySupported(String type) throws BillingCommunicationException |
| 500 | + { |
| 501 | + if (!type.equals(Constants.PRODUCT_TYPE_MANAGED) && !type.equals(Constants.PRODUCT_TYPE_SUBSCRIPTION)) |
| 502 | + { |
| 503 | + throw new RuntimeException("Unsupported type " + type); |
| 504 | + } |
| 505 | + |
| 506 | + IInAppBillingService billing = billingService; |
| 507 | + |
| 508 | + if (billing != null) |
| 509 | + { |
| 510 | + |
| 511 | + try |
| 512 | + { |
| 513 | + int response = billing.isBillingSupported(Constants.GOOGLE_API_REQUEST_PURCHASE_HISTORY_VERSION, |
| 514 | + contextPackageName, type); |
| 515 | + return response == Constants.BILLING_RESPONSE_RESULT_OK; |
| 516 | + } |
| 517 | + catch (RemoteException e) |
| 518 | + { |
| 519 | + throw new BillingCommunicationException(e); |
| 520 | + } |
| 521 | + |
| 522 | + } |
| 523 | + else |
| 524 | + { |
| 525 | + throw new BillingCommunicationException("Billing service isn't connected"); |
| 526 | + } |
| 527 | + } |
| 528 | + |
492 | 529 | /**
|
493 | 530 | * Change subscription i.e. upgrade or downgrade
|
494 | 531 | *
|
@@ -1021,4 +1058,81 @@ private void reportBillingError(int errorCode, Throwable error)
|
1021 | 1058 | eventHandler.onBillingError(errorCode, error);
|
1022 | 1059 | }
|
1023 | 1060 | }
|
| 1061 | + |
| 1062 | + /** |
| 1063 | + * Returns the most recent purchase made by the user for each SKU, even if that purchase is expired, canceled, or consumed. |
| 1064 | + * |
| 1065 | + * @param type product type, accepts either {@value Constants#PRODUCT_TYPE_MANAGED} or |
| 1066 | + * {@value Constants#PRODUCT_TYPE_SUBSCRIPTION} |
| 1067 | + * @param extraParams a Bundle with extra params that would be appended into http request |
| 1068 | + * query string. Not used at this moment. Reserved for future functionality. |
| 1069 | + * |
| 1070 | + * @return @NotNull list of billing history records |
| 1071 | + * @throws BillingCommunicationException if billing isn't connected or there was an error during request execution |
| 1072 | + */ |
| 1073 | + public List<BillingHistoryRecord> getPurchaseHistory(String type, Bundle extraParams) throws BillingCommunicationException |
| 1074 | + { |
| 1075 | + |
| 1076 | + if (!type.equals(Constants.PRODUCT_TYPE_MANAGED) && !type.equals(Constants.PRODUCT_TYPE_SUBSCRIPTION)) |
| 1077 | + { |
| 1078 | + throw new RuntimeException("Unsupported type " + type); |
| 1079 | + } |
| 1080 | + |
| 1081 | + IInAppBillingService billing = billingService; |
| 1082 | + |
| 1083 | + if (billing != null) |
| 1084 | + { |
| 1085 | + |
| 1086 | + try |
| 1087 | + { |
| 1088 | + |
| 1089 | + List<BillingHistoryRecord> result = new ArrayList<>(); |
| 1090 | + int resultCode; |
| 1091 | + String continuationToken = null; |
| 1092 | + |
| 1093 | + do |
| 1094 | + { |
| 1095 | + |
| 1096 | + Bundle resultBundle = billing.getPurchaseHistory(Constants.GOOGLE_API_REQUEST_PURCHASE_HISTORY_VERSION, |
| 1097 | + contextPackageName, type, continuationToken, extraParams); |
| 1098 | + resultCode = resultBundle.getInt(Constants.RESPONSE_CODE); |
| 1099 | + |
| 1100 | + if (resultCode == Constants.BILLING_RESPONSE_RESULT_OK) |
| 1101 | + { |
| 1102 | + |
| 1103 | + List<String> purchaseData = resultBundle.getStringArrayList(Constants.INAPP_PURCHASE_DATA_LIST); |
| 1104 | + |
| 1105 | + List<String> signatures = resultBundle.getStringArrayList(Constants.INAPP_DATA_SIGNATURE_LIST); |
| 1106 | + |
| 1107 | + if (purchaseData != null && signatures != null) |
| 1108 | + { |
| 1109 | + |
| 1110 | + for (int i = 0, max = purchaseData.size(); i < max; i++) |
| 1111 | + { |
| 1112 | + String data = purchaseData.get(i); |
| 1113 | + String signature = signatures.get(i); |
| 1114 | + |
| 1115 | + BillingHistoryRecord record = new BillingHistoryRecord(data, signature); |
| 1116 | + result.add(record); |
| 1117 | + } |
| 1118 | + |
| 1119 | + continuationToken = resultBundle.getString(Constants.INAPP_CONTINUATION_TOKEN); |
| 1120 | + } |
| 1121 | + } |
| 1122 | + |
| 1123 | + } while (continuationToken != null && resultCode == Constants.BILLING_RESPONSE_RESULT_OK); |
| 1124 | + |
| 1125 | + return result; |
| 1126 | + |
| 1127 | + } catch (RemoteException | JSONException e) |
| 1128 | + { |
| 1129 | + throw new BillingCommunicationException(e); |
| 1130 | + } |
| 1131 | + |
| 1132 | + } |
| 1133 | + else |
| 1134 | + { |
| 1135 | + throw new BillingCommunicationException("Billing service isn't connected"); |
| 1136 | + } |
| 1137 | + } |
1024 | 1138 | }
|
0 commit comments