谷歌充值接入流程

1.build.gradle

   implementation("com.android.billingclient:billing:8.0.0")

   implementation 'com.google.guava:guava:31.1-android'

   implementation 'com.google.android.gms:play-services-base:18.2.0'

2.写入代码

/// ///////////////////////////////谷歌充值/////////////////////////////////////////////////////////////


    //----------------------1.谷歌连接服务器-------------------------------
    private BillingClient billingClient;
    private static final long RECONNECT_TIMER_START_MILLISECONDS = 1L * 1000L;
    private static final long RECONNECT_TIMER_MAX_TIME_MILLISECONDS = 1000L * 60L * 15L; // 15 mins
    private long reconnectMilliseconds = RECONNECT_TIMER_START_MILLISECONDS;
    private boolean billingSetupComplete = false;
    private static final Handler handler = new Handler(Looper.getMainLooper());
    protected static Handler uiHandler = new Handler(Looper.getMainLooper());
    private static Map<String, SkuDetails> skuDetailsLiveDataMap = new HashMap<>();
    private String[] cacheRequestList;
    private String buyProductId;

    private static Map<String, ProductDetails> productDetailsMap = new HashMap<>();
    private boolean isConsumable;

    //----------------------1.谷歌连接服务器-------------------------------
    protected void BillingClientInit() {
        System.out.print("-------0.连接服务器-----" + "\n");
        PendingPurchasesParams params = PendingPurchasesParams.newBuilder()
                .enableOneTimeProducts()
                .build();

        billingClient = BillingClient.newBuilder(this)
                .setListener(this)
                .enablePendingPurchases(params)
                .enableAutoServiceReconnection() // Add this line to enable reconnection
                .build();

        billingClient.startConnection(this);

    }


    @Override
    public void onBillingServiceDisconnected() {
        billingSetupComplete = false;
        retryBillingServiceConnectionWithExponentialBackoff();
    }

    @Override
    public void onBillingSetupFinished(@NonNull BillingResult billingResult) {
        int responseCode = billingResult.getResponseCode();
        String debugMessage = billingResult.getDebugMessage();

        System.out.print("-------1.连接服务器状态\n");

        switch (responseCode) {
            case BillingClient.BillingResponseCode.OK:
                reconnectMilliseconds = RECONNECT_TIMER_START_MILLISECONDS;
                billingSetupComplete = true;
                System.out.print("-------2.连接服务器成功 onBillingSetupFinished " + debugMessage + "(" + GetResponseText(responseCode) + ")" + "\n");

//                new Handler(Looper.getMainLooper()).postDelayed(() -> {
//                    GoogleOnBuyProduct("3401101", false);
//                }, 2000);

                break;
            case BillingClient.BillingResponseCode.SERVICE_UNAVAILABLE:
            case BillingClient.BillingResponseCode.BILLING_UNAVAILABLE:
                System.out.print("-------3.连接服务器失败onBillingSetupFinished " + debugMessage + "(" + GetResponseText(responseCode) + ")" + "\n");
                break;
            default:
                retryBillingServiceConnectionWithExponentialBackoff();
                break;
        }
    }


    //----------------------------------------------------2.调用商品id----------------------------------------------------------------------------
    public static void GoogleOnBuyProduct(String productId, boolean isConsumable1) {
        System.out.print("-------2.开始购买1  OnBuyProduct------  productId:" + productId + ",isConsumable:" + isConsumable1 + "\n");
        uiHandler.post(new Runnable() {
            @Override
            public void run() {
                try {
                    activity.OnBuyProduct1(productId, true);
                } catch (Exception e) {
                    System.out.print("-------2.开始购买999  BuyProduct数据传输错误:" + e.getMessage() + "\n");
                }
            }
        });
    }

    private void OnBuyProduct1(String productId, boolean isConsumable) {
        if (!billingSetupComplete) {
            Log.e("Billing", "Billing client not ready");
            return;
        }
        System.out.print("-------2.开始购买2  OnBuyProduct1------  productId:" + productId + ",isConsumable:" + isConsumable + "\n");

        ProductDetails skuDetails = productDetailsMap.get(productId);

        StartQueryProducts(new String[]{productId}, productId, new Action() {
            @Override
            public void Invoke() {
                ProductDetails skuDetails = productDetailsMap.get(productId);

                System.out.print("-------2.查詢商品id -成功-返回商品信息  开始购
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值