查询商店是否可用
返回不可用的情况:
- 服务建立连接阶段
- Play Store 未登录账号
- Google Play Services 不可用
- 网络故障
1 2 3 4
| private fun isAvailable(): Boolean { return Athana.getInstance().iapService.isAvailable() }
|
1 2 3 4
| private boolean isAvailable() { return Athana.getInstance().getIapService().isAvailable() }
|
查询商品
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| private fun queryProduct(keys: Set<String>) { Athana.getInstance().queryProducts( keys = keys, requestListener = object : AthanaRequestListener<List<IapProduct>> { override fun onSuccess(data: List<IapProduct>) { } override fun onError(error: AthanaError) { } }) }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| private void queryProduct(Set<String> keys) { Athana.getInstance().queryProducts( keys, new AthanaRequestListener<>() { @Override public void onSuccess(List<IapProduct> iapProducts) { } @Override public void onError(@NonNull AthanaError athanaError) { } }); }
|
购买
注意,如需限制一个账号只能购买一次,则需要设置 consumable = false
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| private fun purchase( product: IapProduct, customOrderId: Long?, consumable: Boolean = true ) { Athana.getInstance().purchase( product = product, clientOrderId = customOrderId, consumable = consumable, requestListener = object : AthanaRequestListener<Unit> { override fun onSuccess(data: Unit) { } override fun onError(error: AthanaError) { } }) }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| private void purchase( IapProduct product, @Nullable Long customOrderId, // 游戏订单号 @Nullable Boolean consumable // 是否可消耗 ) { Athana.getInstance().purchase( product, customOrderId, null, consumable, new AthanaRequestListener<>() { @Override public void onSuccess(Unit data) { } @Override public void onError(@NonNull AthanaError athanaError) { } }); }
|
查询记录
可作为 Restore Purchase 机制使用
符合以下条件的订单才能够被查询:
IapPurchaseDetail 参数说明
| 参数名 |
类型 |
说明 |
| productId |
String |
商品 Key,和 IapProduct 的 key |
| state |
PurchaseState |
订单状态: 处理中 - PENDING 已购买 - PURCHASED 错误 - ERROR 取消 - CANCEL |
| purchaseId |
String? |
商店订单流水号 |
| purchaseToken |
String? |
商店订单凭证 |
| isAcknowledged |
Boolean? |
是否已确认,如果未确认(false),则需要调用 verifyOrder 重新验证订单 |
| isAutoRenewing |
Boolean? |
是否自动续订,如果是订阅类商品/服务,返回 true |
1 2 3 4 5 6 7 8 9 10 11 12 13
| private fun queryPurchaseHistory() { Athana.getInstance().queryPurchaseHistory( object : AthanaRequestListener<List<IapPurchaseDetail>> { override fun onSuccess(result: List<IapPurchaseDetail>) { } override fun onError(error: AthanaError) { } }) }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| private void queryPurchaseHistory() { Athana.getInstance().queryPurchaseHistory(new AthanaRequestListener<>() { @Override public void onSuccess(List<IapPurchaseDetail> iapPurchaseDetails) { } @Override public void onError(AthanaError throwable) { } }); }
|
重新验单
IapPurchaseDetail 可从 queryPurchaseHistory 查询获得
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| private fun verifyOrder(purchase: IapPurchaseDetail, consumable: Boolean) { Athana.getInstance().verifyOrder( purchase = purchase, consumable = consumable, requestListener = object : AthanaRequestListener<List<IapPurchaseDetail>> { override fun onSuccess(result: List<IapPurchaseDetail>) { } override fun onError(error: AthanaError) { } }) }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| private void verifyOrder(IapPurchaseDetail purchase, Boolean consumable) { Athana.getInstance().verifyOrder( purchase, null, consumable, new AthanaRequestListener<>() {
@Override public void onSuccess(List<IapPurchaseDetail> iapPurchaseDetails) { } @Override public void onError(@Nullable Throwable throwable) { } }); }
|