美文网首页
android google 内购支付6.0 结算库

android google 内购支付6.0 结算库

作者: xq9527 | 来源:发表于2023-06-15 17:11 被阅读0次

前言

各位同学 大家好有段时间没有个大家更新文章 ,最近在更新google的最新结算库 。所以就在这里分享给废话不多说。

官方文档地址

Google结算库

需要的依赖

    def billing_version = "6.0.0"
    implementation "com.android.billingclient:billing:$billing_version"

请添加到build.gralde


image.png

效果图 :

Screenshot_20230616-033215.png
Screenshot_20230616-033223.png

具体接入

  • 支付初始化

private BillingClient billingClient;
     billingClient = BillingClient.newBuilder(context)
                .setListener(purchasesUpdatedListener)
                .enablePendingPurchases()
                .build();
     if(billingClient!=null){
         billingClient.startConnection(new BillingClientStateListener() {
             @Override
             public void onBillingSetupFinished(BillingResult billingResult) {
                 if (billingResult.getResponseCode() ==  BillingClient.BillingResponseCode.OK) {
                     

                 }
             }
             @Override
             public void onBillingServiceDisconnected() {

             }
         });
     }
  • 连接Google查询商品ID 信息

    String productId ="xxxxx"; //请替换成自己的商品id
        List<QueryProductDetailsParams.Product> productList= new ArrayList<>();
        productList.add(QueryProductDetailsParams.Product.newBuilder()
                .setProductId(productId)
                .setProductType(BillingClient.ProductType.INAPP)
                .build());
        QueryProductDetailsParams queryProductDetailsParams = QueryProductDetailsParams.newBuilder().
                setProductList(productList)
                .build();
        if(billingClient!=null){
            billingClient.queryProductDetailsAsync(
                    queryProductDetailsParams,
                    new ProductDetailsResponseListener() {
                        public void onProductDetailsResponse(BillingResult billingResult,
                                                             List<ProductDetails> productDetailsList) {
                          
                        }
                    }
            );
        }

  • 调起支付界面

ProductDetails productDetails=productDetailsList.get(0);
                                List<BillingFlowParams.ProductDetailsParams> productDetailsParamsList= new ArrayList<>();
                            productDetailsParamsList.add(BillingFlowParams.ProductDetailsParams.newBuilder()
                                        .setProductDetails(productDetails)
                                        .build());
                                BillingFlowParams billingFlowParams = BillingFlowParams.newBuilder()
                                        .setProductDetailsParamsList(productDetailsParamsList)
                                        .build();
                                billingClient.launchBillingFlow((Activity) context, billingFlowParams);
  • 支付回调

private PurchasesUpdatedListener purchasesUpdatedListener = new PurchasesUpdatedListener(){
        @Override
        public void onPurchasesUpdated(BillingResult billingResult, List<Purchase> purchases) {
            if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK
                    && purchases != null) {
                for (Purchase purchase : purchases) {
                    handlePurchase(purchase);

                    Log.e(TAG, "getPurchaseToken: "+ purchase.getPurchaseToken() );
                    Log.e(TAG, "getSignature: "+ purchase.getSignature() );
                    Log.e(TAG, "getSignature: "+ purchase.getSignature() );
                }
            } else if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.USER_CANCELED) {

            } else {

            }

        }
    };

我们拿到回调里面的 getPurchaseToken getSignature getSignature 信息之后可以去自己服务端进行验签 如果验签成功了才算是真正的成功支付成功 然后再进行消耗

  • 商品消耗

   private void handlePurchase(final Purchase purchase) {
        ConsumeParams consumeParams =
                ConsumeParams.newBuilder()
                        .setPurchaseToken(purchase.getPurchaseToken())
                        .build();
        ConsumeResponseListener listener = new ConsumeResponseListener() {
            @Override
            public void onConsumeResponse(BillingResult billingResult, String purchaseToken) {
                if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {

                    Log.e(TAG, "getPurchaseToken: "+ purchase.getPurchaseToken() );
                    Log.e(TAG, "getSignature: "+ purchase.getSignature() );
                    Log.e(TAG, "getSignature: "+ purchase.getSignature() );

                }
            }
        };
        if(billingClient!=null){
            billingClient.consumeAsync(consumeParams, listener);
        }

    }
  • 支付完成后下次支付前查询消耗

    private  void  queryPurchasesAsync(){
       if(billingClient!=null){
           if(billingClient.isReady()){
             Toast.makeText(context,"BillingClient is not ready",Toast.LENGTH_SHORT).show();
           }
           billingClient.queryPurchasesAsync(
                   QueryPurchasesParams.newBuilder().setProductType(BillingClient.ProductType.SUBS).build(),
                   new PurchasesResponseListener() {
                       public void onQueryPurchasesResponse(
                               BillingResult billingResult,
                               List<Purchase> purchases) {
                           if(billingResult.getResponseCode()==BillingClient.BillingResponseCode.OK){
                               if(purchases!=null&&purchases.size()>0){
                                   for (Purchase purchase:purchases){
                                       handlePurchase(purchase);
                                   }
                               }
                           }
                       }
                   }
           );
       }

    }

请再 onResume 声明周期方法里面调用


    @Override
    protected void onResume() {
        super.onResume();
        queryPurchasesAsync();

    }

完整接入示例

package com.example.myapplication;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.android.billingclient.api.BillingClient;
import com.android.billingclient.api.BillingClientStateListener;
import com.android.billingclient.api.BillingFlowParams;
import com.android.billingclient.api.BillingResult;
import com.android.billingclient.api.ConsumeParams;
import com.android.billingclient.api.ConsumeResponseListener;
import com.android.billingclient.api.ProductDetails;
import com.android.billingclient.api.ProductDetailsResponseListener;
import com.android.billingclient.api.Purchase;
import com.android.billingclient.api.PurchasesResponseListener;
import com.android.billingclient.api.PurchasesUpdatedListener;
import com.android.billingclient.api.QueryProductDetailsParams;
import com.android.billingclient.api.QueryPurchasesParams;
import com.testgame.demo.R;
import java.util.ArrayList;
import java.util.List;


/**
 *  创建人:xuqing
 *  创建时间:2023年5月18日14:53:03
 *  类说明:google支付测试
 *
 *
 */

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "-----MainActivity-----";
    private Context context=MainActivity.this;
    private BillingClient billingClient;

    private PurchasesUpdatedListener purchasesUpdatedListener = new PurchasesUpdatedListener() {
        @Override
        public void onPurchasesUpdated(BillingResult billingResult, List<Purchase> purchases) {
            if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK
                    && purchases != null) {
                for (Purchase purchase : purchases) {
                    handlePurchase(purchase);

                    Log.e(TAG, "getPurchaseToken: "+ purchase.getPurchaseToken() );
                    Log.e(TAG, "getSignature: "+ purchase.getSignature() );
                    Log.e(TAG, "getSignature: "+ purchase.getSignature() );
                }
            } else if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.USER_CANCELED) {
            } else {
            }
        }
    };
   private void handlePurchase(final Purchase purchase) {
        ConsumeParams consumeParams =
                ConsumeParams.newBuilder()
                        .setPurchaseToken(purchase.getPurchaseToken())
                        .build();
        ConsumeResponseListener listener = new ConsumeResponseListener() {
            @Override
            public void onConsumeResponse(BillingResult billingResult, String purchaseToken) {
                if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {

                    Log.e(TAG, "getPurchaseToken: "+ purchase.getPurchaseToken() );
                    Log.e(TAG, "getSignature: "+ purchase.getSignature() );
                    Log.e(TAG, "getSignature: "+ purchase.getSignature() );
                }
            }
        };
        if(billingClient!=null){
            billingClient.consumeAsync(consumeParams, listener);
        }
    }

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initgooglePlay();
        findViewById(R.id.togooglepaybtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toGooglePay();
            }
        });
    }

    private void initgooglePlay() {
     billingClient = BillingClient.newBuilder(context)
                .setListener(purchasesUpdatedListener)
                .enablePendingPurchases()
                .build();
     if(billingClient!=null){
         billingClient.startConnection(new BillingClientStateListener() {
             @Override
             public void onBillingSetupFinished(BillingResult billingResult) {
                 if (billingResult.getResponseCode() ==  BillingClient.BillingResponseCode.OK) {


                 }
             }
             @Override
             public void onBillingServiceDisconnected() {

             }
         });
     }
    }

    private  void  toGooglePay(){
        String productId ="xxxx";
        List<QueryProductDetailsParams.Product> productList= new ArrayList<>();
        productList.add(QueryProductDetailsParams.Product.newBuilder()
                .setProductId(productId)
                .setProductType(BillingClient.ProductType.INAPP)
                .build());
        QueryProductDetailsParams queryProductDetailsParams = QueryProductDetailsParams.newBuilder().
                setProductList(productList)
                .build();
        if(billingClient!=null){
            billingClient.queryProductDetailsAsync(
                    queryProductDetailsParams,
                    new ProductDetailsResponseListener() {
                        public void onProductDetailsResponse(BillingResult billingResult,
                                                             List<ProductDetails> productDetailsList) {
                            if(productDetailsList!=null&&productDetailsList.size()>0){


                                ProductDetails productDetails=productDetailsList.get(0);
                                List<BillingFlowParams.ProductDetailsParams> productDetailsParamsList= new ArrayList<>();
                                productDetailsParamsList.add(BillingFlowParams.ProductDetailsParams.newBuilder()
                                        .setProductDetails(productDetails)
                                        .build());
                                BillingFlowParams billingFlowParams = BillingFlowParams.newBuilder()
                                        .setProductDetailsParamsList(productDetailsParamsList)
                                        .build();
                                billingClient.launchBillingFlow((Activity) context, billingFlowParams);
                            }else {
                                Toast.makeText(context,"商品ID无效",Toast.LENGTH_SHORT).show();
                            }
                        }
                    }
            );
        }
    }
    private  void  queryPurchasesAsync(){
       if(billingClient!=null){
           if(billingClient.isReady()){
             Toast.makeText(context,"BillingClient is not ready",Toast.LENGTH_SHORT).show();
           }
           billingClient.queryPurchasesAsync(
                   QueryPurchasesParams.newBuilder().setProductType(BillingClient.ProductType.SUBS).build(),
                   new PurchasesResponseListener() {
                       public void onQueryPurchasesResponse(
                               BillingResult billingResult,
                               List<Purchase> purchases) {
                           if(billingResult.getResponseCode()==BillingClient.BillingResponseCode.OK){
                               if(purchases!=null&&purchases.size()>0){
                                   for (Purchase purchase:purchases){
                                       handlePurchase(purchase);
                                   }
                               }
                           }
                       }
                   }
           );
       }
    }
    @Override
    protected void onResume() {
        super.onResume();
        queryPurchasesAsync();
    }
    @Override
    public void onStop() {
        super.onStop();
    }
    @Override
    protected void onPause() {
        super.onPause();
    }
}

需要注意的点

我们的google 支付初始化不要多次初始化 不然会出现多次回调的 会影响到我们支付回调里面进行数据上报的逻辑 。支付消耗查询也可以放在每次支付之前先查询后再去进行下一笔支付 ,还有示例中部分底和官方写法不一样是我改过的 ImmutableList.of 这个其实Java里面的 声明一个非空集合 因为我这边一直导入不了 所以就改掉了 其余代码跟官方一毛一样

支付调不起来的几个原因

1 保证拿到商品ID 是正确的可以在google play后台查看到
2 保证手机里面 Google账号是可以支付的


image.png

3、安装的app包的versionName、versionCode 和 Google Play Console上传的不一样
Google Play Console - 所有应用 - 查看应用 (上传安装的app或者修改版本号)
4、安装的app包的签名和上传到Google Play Console的包签名不一致
Google Play Console - 所有应用 - 查看应用 -设置 - 应用完整性 (查看签名是否一致)
5 保证vpn 翻墙工具稳定

最后总结:

如果你是4.0.0版本的结算库 请服务查看 从 Google Play 结算库版本 4 或 5 迁移到版本 6 说明里面有些api 还是有改动 最好用最新方法 不要用过时的方法 如果你是第一次接入就按照次文档或者官方最新文档接入即可 最后呢 希望我都文章能帮助到各位同学工作和学习 如果你觉得文章还不错麻烦给我三连 关注点赞和转发 谢谢

相关文章

  • google 内购支付流程

    前言 各位同学大家好 ,有段时间没有给各位更新文章了,具体多久我也不记得了, 最近接了google 的内购支付,遇...

  • Google支付和服务端验证

    因为公司业务需求,需要使用google的登录和支付。google支付分为订阅和应用内购买两种,笔者使用的是应用内购...

  • Volley设计与实现

    Volley是google提供网络请求库,使用说明看这里。注:由于Android6.0把HttpClient去掉了...

  • Android Google应用内支付

    前言 之前写过了Facebook登录Google登录,那么Google支付能错过吗?当然是不能的了。今天来说说Go...

  • 安卓内实现应用内购买结算(Google Play 结算库)

    Google Play 上的应用内购买结算提供了一个直接、简单的界面,让您可以使用 Google Play 发送应...

  • iOS支付

    支付宝支付 支付宝支付支付宝补充 微信支付 微信支付 内购 iOS开发 内购流程

  • iOS支付

    iOS支付分为两类,第三方支付和应用内支付(内购)。 应用内支付(In-App Purchase):在应用程序内购...

  • Android 6.0 系统新特性整理

    Android 6.0有哪些主要的更新 6.0系统介绍 全称为Marshamallow(棉花糖)Google 20...

  • 苹果内购到账周期

    果内购的结算? 我们作为收费方,苹果内购的结算在什么地方能看记录,钱是到什么地方了,同时苹果结算的周期是什么时间?...

  • Google内购

    Google内购是Google Play为发布在其上的APP提供的一种实现用户购买的服务,对一些简单的APP应用,...

网友评论

      本文标题:android google 内购支付6.0 结算库

      本文链接:https://www.haomeiwen.com/subject/qnslydtx.html