我已经成功实现了应用内购买流程,现在我正在尝试切换到订阅模式,但是 makePurchase() 方法有问题,它没有打开用户付款的窗口。
private void createPurchaseFlow(String productID, PostTaskListener<String> callback)
{
Print.e("createPurchaseFlow productID: "+ productID);
PurchasesUpdatedListener purchasesUpdatedListener = new PurchasesUpdatedListener()
{
@Override
public void onPurchasesUpdated(@NonNull BillingResult billingResult,
List<Purchase> purchases)
{
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK
&& purchases != null)
{
for (Purchase purchase : purchases)
{
consumePurchase(purchase,productID,callback);
}
}
else if (billingResult.getResponseCode() ==
BillingClient.BillingResponseCode.USER_CANCELED)
{
callback.onPostTask("User Canceled");
Print.e("onPurchasesUpdated: Purchase Canceled");
}
else
{
callback.onPostTask("Purchase Failed");
Print.e("onPurchasesUpdated: Error");
}
}
};
billingClient = BillingClient.newBuilder(activity)
.setListener(purchasesUpdatedListener)
.enablePendingPurchases()
.build();
billingClient.startConnection(new BillingClientStateListener()
{
@Override
public void onBillingSetupFinished(@NonNull BillingResult billingResult)
{
if (billingResult.getResponseCode() ==
BillingClient.BillingResponseCode.OK)
{
Print.e("OnBillingSetupFinish connected");
queryProduct(productID,callback);
}
else
{
Print.e("OnBillingSetupFinish failed");
}
}
@Override
public void onBillingServiceDisconnected()
{
// Try to restart the connection on the next request to
// Google Play by calling the startConnection() method.
Print.e("onBillingServiceDisconnected");
callback.onPostTask("Billing Service Disconnected");
}
});
}
private void queryProduct(String subscriptionID, PostTaskListener<String> callback)
{
QueryProductDetailsParams queryProductDetailsParams =
QueryProductDetailsParams.newBuilder()
.setProductList(
ImmutableList.from(
QueryProductDetailsParams.Product.newBuilder()
.setProductId(subscriptionID)
.setProductType(
BillingClient.ProductType.SUBS)
.build()))
.build();
billingClient.queryProductDetailsAsync(
queryProductDetailsParams,
new ProductDetailsResponseListener()
{
@Override
public void onProductDetailsResponse(@NonNull BillingResult billingResult,
@NonNull List<ProductDetails> productDetailsList)
{
if (!productDetailsList.isEmpty())
{
Print.e("onProductDetailsResponse");
ProductDetails productDetails = productDetailsList.get(0);
makePurchase(productDetails);
}
else
{
callback.onPostTask(null);
Print.e("onProductDetailsResponse: No subscriptions");
}
}
}
);
}
private void makePurchase(ProductDetails productDetails)
{
Print.e("makePurchase: " + productDetails.getName());
List<BillingFlowParams.ProductDetailsParams> productDetailsParamsList = ImmutableList.from(
BillingFlowParams.ProductDetailsParams.newBuilder()
.setProductDetails(productDetails)
.build()
);
BillingFlowParams billingFlowParams =
BillingFlowParams.newBuilder()
.setProductDetailsParamsList(productDetailsParamsList)
.build();
BillingResult billingResult = billingClient.launchBillingFlow(activity, billingFlowParams);
Print.e("billingResult: " + billingResult.getResponseCode() +
" getDebugMessage: " + billingResult.getDebugMessage() +
" toString: " + billingResult.toString());
}
此行后不打印消息
BillingResult billingResult = billingClient.launchBillingFlow(activity, billingFlowParams);
所以我放置了断点,代码一直运行到这段代码:
List<BillingFlowParams.ProductDetailsParams> productDetailsParamsList = ImmutableList.from(
BillingFlowParams.ProductDetailsParams.newBuilder()
.setProductDetails(productDetails)
.build()
);
然后什么也没有发生,没有错误,零日志。这是怎么回事?
好的,所以我需要进行此更改:
List<BillingFlowParams.ProductDetailsParams> productDetailsParamsList = ImmutableList.from(
BillingFlowParams.ProductDetailsParams.newBuilder()
.setOfferToken(productDetails.getSubscriptionOfferDetails().get(0).getOfferToken())
.setProductDetails(productDetails)
.build()
);