```
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
maven {url 'https://developer.huawei.com/repo/'}
}
dependencies {
classpath 'com.android.tools.build:gradle:7.3.1'
classpath 'com.huawei.agconnect:agcp:1.6.0.300'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
```
/**
* Consume all the unconsumed purchases with priceType 0.
* @param iapClient IapClient instance to call the consumeOwnedPurchase API.
* @param purchaseToken which is generated by the Huawei payment server during product payment and returned to the app through InAppPurchaseData.
*/
public static void consumeOwnedPurchase(IapClient iapClient, String purchaseToken) {
Log.i(TAG, "call consumeOwnedPurchase");
Task<ConsumeOwnedPurchaseResult> task = iapClient.consumeOwnedPurchase(createConsumeOwnedPurchaseReq(purchaseToken));
task.addOnSuccessListener(new OnSuccessListener<ConsumeOwnedPurchaseResult>() {
@Override
public void onSuccess(ConsumeOwnedPurchaseResult result) {
Log.i(TAG, "consumeOwnedPurchase success");
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
if (e instanceof IapApiException) {
IapApiException apiException = (IapApiException)e;
int returnCode = apiException.getStatusCode();
Log.e(TAG, "consumeOwnedPurchase fail, IapApiException returnCode: " + returnCode);
} else {
// Other external errors
Log.e(TAG, e.getMessage());
}
}
});
}
```
## 查询未消耗逻辑(补单逻辑)
```
/**
* Call the obtainOwnedPurchases API to obtain the data about consumable
* products that the user has purchased but has not been delivered.
* 查询未消耗的逻辑
*
*
*
*/
public void queryPurchases(String continuationToken) {
final String tag = "obtainOwnedPurchases";
IapRequestHelper.obtainOwnedPurchases(mClient, IapClient.PriceType.IN_APP_CONSUMABLE,
continuationToken, new IapApiCallback<OwnedPurchasesResult>() {
@Override
public void onSuccess(OwnedPurchasesResult result) {
if (result == null) {
Log.e(TAG, tag + " result is null");
return;
}
Log.i(TAG, "obtainOwnedPurchases, success");
if (result.getInAppPurchaseDataList() != null) {
List<String> inAppPurchaseDataList = result.getInAppPurchaseDataList();
List<String> inAppSignature= result.getInAppSignature();
for (int i = 0; i < inAppPurchaseDataList.size(); i++) {
final String inAppPurchaseData = inAppPurchaseDataList.get(i);
final String inAppPurchaseDataSignature = inAppSignature.get(i);
deliverProduct(inAppPurchaseData);
}
}
if (!TextUtils.isEmpty(result.getContinuationToken())) {
queryPurchases(result.getContinuationToken());
}
}
@Override
public void onFail(Exception e) {
Log.e(TAG, "obtainOwnedPurchases, type=" + IapClient.PriceType.IN_APP_CONSUMABLE + ", " + e.getMessage());
ExceptionHandle.handle(context, e);
}
});
}
```
/**
* Example ace activity class, which will load ArkUI-X ability instance.
* StageActivity is provided by ArkUI-X
* @see <a href=
* "https://gitee.com/arkui-crossplatform/doc/blob/master/contribute/tutorial/how-to-build-Android-app.md">
* to build android library</a>
*/
public class EntryEntryAbilityActivity extends StageActivity {
/**
* Call the obtainOwnedPurchases API to obtain the data about consumable
* products that the user has purchased but has not been delivered.
* 查询未消耗的逻辑
*
*
*
*/
public void queryPurchases(String continuationToken) {
final String tag = "obtainOwnedPurchases";
IapRequestHelper.obtainOwnedPurchases(mClient, IapClient.PriceType.IN_APP_CONSUMABLE,
continuationToken, new IapApiCallback<OwnedPurchasesResult>() {
@Override
public void onSuccess(OwnedPurchasesResult result) {
if (result == null) {
Log.e(TAG, tag + " result is null");
return;
}
Log.i(TAG, "obtainOwnedPurchases, success");
if (result.getInAppPurchaseDataList() != null) {
List<String> inAppPurchaseDataList = result.getInAppPurchaseDataList();
List<String> inAppSignature= result.getInAppSignature();
for (int i = 0; i < inAppPurchaseDataList.size(); i++) {
final String inAppPurchaseData = inAppPurchaseDataList.get(i);
final String inAppPurchaseDataSignature = inAppSignature.get(i);
deliverProduct(inAppPurchaseData);
}
}
if (!TextUtils.isEmpty(result.getContinuationToken())) {
queryPurchases(result.getContinuationToken());
}
}
@Override
public void onFail(Exception e) {
Log.e(TAG, "obtainOwnedPurchases, type=" + IapClient.PriceType.IN_APP_CONSUMABLE + ", " + e.getMessage());
ExceptionHandle.handle(context, e);
}
});
}