您好我已经使用android-inapp-billing-v3实施了inapp billing V3,对一个项目进行为期一年的订阅。我想在我的应用中显示剩余的日子。我正在调用getSubscriptionTransactionDetails
来获取产品的交易详情,但它始终返回null
。这是我的代码。
private BillingProcessor startInappCheck(){
bp = new BillingProcessor(mContext, BASE64ENCODEDPUBLICKEY, new BillingProcessor.IBillingHandler() {
@Override
public void onProductPurchased(String productId, TransactionDetails details) {
LogUtils.e(TAG, "onProductPurchased :" +productId);
// showToast("onProductPurchased: " + productId);
}
@Override
public void onBillingError(int errorCode, Throwable error) {
LogUtils.e(TAG, "onBillingError :" +errorCode);
}
@Override
public void onBillingInitialized() {
// showToast("onBillingInitialized");
readyToPurchase = true;
try{
SkuDetails subs = bp.getSubscriptionListingDetails(SUBSCRIPTION_ID);
LogUtils.d(TAG, "Owned Subscription: " + subs.toString());
TransactionDetails tr = bp.getSubscriptionTransactionDetails(SUBSCRIPTION_ID);
LogUtils.d(TAG, "Owned Subscription: " + tr.toString());
}catch (Exception e) {
// TODO: handle exception
}
}
@Override
public void onPurchaseHistoryRestored() {
// showToast("onPurchaseHistoryRestored");
for(String sku : bp.listOwnedSubscriptions()){
LogUtils.d(TAG, "Owned Subscription: " + sku);
}
// showToast("onPurchaseHistoryRestored");
}
});
return bp;
}
我从onCreate
调用此方法。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (!bp.handleActivityResult(requestCode, resultCode, data))
super.onActivityResult(requestCode, resultCode, data);
}
我的订阅项按钮在fragment
中实施。我发现另一个问题是,在成功订阅后,onProductPurchased
未被调用,但我已在onResume
中实现逻辑,以便在bp.isSubscribed(SUBSCRIPTION_ID)
返回true
时更新UI。请告诉我如何获得订阅发起日期和到期日。
答案 0 :(得分:4)
我想把Ans给我自己的问题,所以如果有人找到同样的话就可以找到解决方案。
经过大量的谷歌我没有找到一个我正在寻找的确切解决方案所以我在BillingProcessor
项目的iabv3 library
类中创建了一个方法,该方法返回带有购买详细信息的Bundle
,其中我得到了购买日期。现在我能用这个找到到期日。该方法如下所示
public Bundle getPurchases(){
if (!isInitialized())
return null;
try{
return billingService.getPurchases(Constants.GOOGLE_API_VERSION, contextPackageName, Constants.PRODUCT_TYPE_SUBSCRIPTION, null);
}catch (Exception e) {
e.printStackTrace();
}
return null;
}
答案 1 :(得分:1)
我已使用此代码在结算处理器库中获取购买详情。
TransactionDetails transactionDetails = bp.getSubscriptionTransactionDetails(channelModel.getAndroidProductId());
Log.d(TAG, "initializePaymentSetup: " + transactionDetails.toString());
Log.d(TAG, "initializePaymentSetup: " + transactionDetails.purchaseInfo.toString());
transactionDetails.purchaseInfo.purchaseData// this will return the purchase date
答案 2 :(得分:0)
我正在使用这个
@Nullable
public Date getSubscriptionRenewingDate(String sku) {
// Get the Purchase object:
Purchase purchase = null;
Purchase.PurchasesResult purchasesResult = _billingClient.queryPurchases(BillingClient.SkuType.SUBS);
if (purchasesResult.getPurchasesList() != null) {
for (Purchase p : purchasesResult.getPurchasesList()) {
if (p.getSku().equals(sku) && p.getPurchaseState() == Purchase.PurchaseState.PURCHASED && p.isAutoRenewing()) {
purchase = p;
break;
}
}
}
// Get the SkuDetails object:
SkuDetails skuDetails = null;
for (SkuDetails s : _skuDetails) { // _skuDetails is an array of SkuDetails retrieved with querySkuDetailsAsync
if (s.getSku().equals(sku)) {
skuDetails = s;
break;
}
}
if (purchase != null && skuDetails != null) {
Date purchaseDate = new Date(purchase.getPurchaseTime());
Calendar calendar = Calendar.getInstance();
calendar.setTime(purchaseDate);
Date now = new Date();
while (calendar.getTime().before(now)) {
switch (skuDetails.getSubscriptionPeriod()) {
case "P1W": calendar.add(Calendar.HOUR, 7*24); break;
case "P1M": calendar.add(Calendar.MONTH, 1); break;
case "P3M": calendar.add(Calendar.MONTH, 3); break;
case "P6M": calendar.add(Calendar.MONTH, 6); break;
case "P1Y": calendar.add(Calendar.YEAR, 1); break;
}
}
return calendar.getTime();
}
return null;
}