Google 内购 - Android

1. 添加依赖

implementation "com.android.billingclient:billing:5.0.0"

2.支付相关的代码

    /**
    * 连接
    **/
    public synchronized boolean connect() {
        if (billingClient == null) {
            if (app != null) {
                billingClient = BillingClient.newBuilder(app)
                        .setListener(purchasesUpdatedListener)
                        .enablePendingPurchases()
                        .build();
            }
        }
        if (billingClient != null) {
            if (!billingClient.isReady()) {
                billingClient.startConnection(billingClientStateListener);
            }
            return true;
        }
        return false;
    }

    /**
     * 释放连接,建议别调用
     */
    public void destroy() {
        Log.d(TAG, "ON_DESTROY");
        if (billingClient.isReady()) {
            Log.d(TAG, "BillingClient can only be used once -- closing connection");
            billingClient.endConnection();
        }
    }

    /**
     * 根据id获取Product
     *
     * @param productId
     * @return
     */
    public QueryProductDetailsParams.Product getProduct(String productId) {
        return QueryProductDetailsParams.Product.newBuilder()
                .setProductId(productId).setProductType(BillingClient.ProductType.INAPP)
                .build();
    }

    /**
     * 根据Product列表查询商品列表详情
     *
     * @param productList
     * @param productDetailsResponseListener 注意回调是在子线程,更新UI要注意
     */
    public void querySkuDetails(@NonNull List<QueryProductDetailsParams.Product> productList,
                                ProductDetailsResponseListener productDetailsResponseListener) {
        if (!connect()) {
            Log.e(TAG, "querySkuDetails: BillingClient is not ready");
        }
        Log.d(TAG, "querySkuDetails");
        QueryProductDetailsParams queryProductDetailsParams = QueryProductDetailsParams.newBuilder()
                .setProductList(productList).build();
        Log.i(TAG, "querySkuDetailsAsync");
        billingClient.queryProductDetailsAsync(queryProductDetailsParams, productDetailsResponseListener);
    }

    public void querySkuDetail(@NonNull String productId, ProductDetailsResponseListener productDetailsResponseListener) {
        if (!connect()) {
            Log.e(TAG, "querySkuDetails: BillingClient is not ready");
        }
        Log.d(TAG, "querySkuDetails");
        QueryProductDetailsParams queryProductDetailsParams = QueryProductDetailsParams.newBuilder()
                .setProductList(ImmutableList.of(getProduct(productId))).build();
        Log.i(TAG, "querySkuDetailsAsync");
        billingClient.queryProductDetailsAsync(queryProductDetailsParams, productDetailsResponseListener);
    }

    /**
     * 启动支付页面
     *
     * @param activity
     * @param productDetails
     * @return
     */
    public int launchBillingFlow(Activity activity, ProductDetails productDetails) {
        if (!connect()) {
            Log.e(TAG, "launchBillingFlow: BillingClient is not ready");
            return -1;
        }
        ImmutableList productDetailsParamsList;
        if (BillingClient.ProductType.SUBS.equals(productDetails.getProductType()) && productDetails.getSubscriptionOfferDetails() != null) {
            productDetailsParamsList = ImmutableList.of(BillingFlowParams.ProductDetailsParams.newBuilder()
                    .setProductDetails(productDetails)
                    .setOfferToken(productDetails.getSubscriptionOfferDetails().get(0).getOfferToken())
                    .build());
        } else {
            productDetailsParamsList = ImmutableList.of(BillingFlowParams.ProductDetailsParams.newBuilder()
                    .setProductDetails(productDetails)
                    .build());
        }
        BillingFlowParams billingFlowParams = BillingFlowParams.newBuilder()
                .setProductDetailsParamsList(productDetailsParamsList)
                .build();
        BillingResult billingResult = billingClient.launchBillingFlow(activity, billingFlowParams);
        int responseCode = billingResult.getResponseCode();
        String debugMessage = billingResult.getDebugMessage();
        Log.d(TAG, "launchBillingFlow: BillingResponse " + responseCode + " " + debugMessage);
        return responseCode;
    }


    /**
     * 确认、消费一次性商品交易,一般在购买成功后
     * <p>
     * 警告! 所有购买都需要确认。 未能确认购买将导致购买退款。 对于一次性产品,请确保使用此方法作为隐式确认,
     * 或者您可以通过{@link BillingClient#acknowledgePurchase(AcknowledgePurchaseParams, AcknowledgePurchaseResponseListener)} 明确确认购买。
     * 对于订阅,请使用{@link BillingClient#acknowledgePurchase(AcknowledgePurchaseParams, AcknowledgePurchaseResponseListener))。
     * 有关详细信息,请参阅https://developer.android.com/google/play/billing/billing_library_overview#acknowledge。
     */
    public void consumeAsync(@NonNull Purchase purchase, ConsumeResponseListener listener) {
        if (!connect()) {
            Log.e(TAG, "consumeAsync: BillingClient is not ready");
        }

        if (purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED) {
            ConsumeParams consumeParams =
                    ConsumeParams.newBuilder()
                            .setPurchaseToken(purchase.getPurchaseToken())
                            .build();
            billingClient.consumeAsync(consumeParams, listener);
        }
    }

    /**
     * 获取购买交易列表
     *
     * @param purchasesResponseListener
     */
    public void queryPurchasesAsync(PurchasesResponseListener purchasesResponseListener) {
        if (!connect()) {
            Log.e(TAG, "queryPurchasesAsync: BillingClient is not ready");
        }
        billingClient.queryPurchasesAsync(QueryPurchasesParams.newBuilder()
                .setProductType(BillingClient.ProductType.INAPP).build(), purchasesResponseListener);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

code_wang_hui

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值