记录下GooglePlay V5支付

这篇具有很好参考价值的文章主要介绍了记录下GooglePlay V5支付。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

经历了google支付从v1到v5版本,想吐槽想Google后台,变化非常平凡一段时间不上去快不认识了。这次想记录下客户端接入v5支付的流程。

支付流程如下图:

记录下GooglePlay V5支付

GooglePlayV3以后添加到maven,直接引用就能用

maven {
     url 'https://dl.google.com/dl/android/maven2/'
     name 'Google'
}
dependencies {
    def billing_version = "5.0.0" 
    implementation "com.android.billingclient:billing:$billing_version"
    implementation 'com.google.android.gms:play-services-base:17.1.0'
    implementation 'com.google.android.gms:play-services-auth:17.0.0'
    implementation 'com.google.android.gms:play-services-identity:17.0.0'
    implementation 'com.google.android.gms:play-services-wallet:18.0.0'
    implementation 'com.google.android.gms:play-services-games:18.0.1'
  
    implementation "com.google.guava:guava:20.0"
}

具体实现流程如下:

1.先实现google登录

GoogleSignInOptions options =
	new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
		.requestEmail()
		.build();
GoogleSignInClient mSignInClient = GoogleSignIn.getClient(context, options);
Intent intent = mSignInClient.getSignInIntent();
UnityPlayer.currentActivity.startActivityForResult(intent, SIGN_CODE);

2.连接google play

//准备连接
BillingClient m_BillingClient = BillingClient.newBuilder(mainActivity)
                .setListener(purchasesUpdatedListener)
                .enablePendingPurchases()
                .build();
m_BillingClient.startConnection(billingClientStateListener);

//startConnection callback,连接回调
private BillingClientStateListener billingClientStateListener = new BillingClientStateListener() {
        @Override
        public void onBillingSetupFinished(@NonNull BillingResult billingResult) {

            int responseCode = billingResult.getResponseCode();
            String debugMessage = billingResult.getDebugMessage();
            Log.d(TAG, "onBillingSetupFinished: " + responseCode + " " + debugMessage);
            if (responseCode == BillingClient.BillingResponseCode.OK) {
                m_IsInit = true;
                if (m_CallBack != null)
                    m_CallBack.onCallBack();
            }
        }

        @Override
        public void onBillingServiceDisconnected() {
            Log.d(TAG, "onBillingServiceDisconnected");
        }
    };

3.先检查历史支付情况,没有消耗的消耗掉,google只允许一个订单

​
//查询订阅情况
private  void  queuePurchasSUBS()
{
    try
    {
        m_BillingClient.queryPurchasesAsync(
                QueryPurchasesParams.newBuilder()
                        .setProductType(BillingClient.ProductType.SUBS)
                        .build(),
                new PurchasesResponseListener() {
                    public void onQueryPurchasesResponse(BillingResult billingResult, List<Purchase> list) {
                        // check billingResult
                        // process returned purchase list, e.g. display the plans user owns
                        Log.d(TAG, "queryPurchases: SUBS  count :" + list.size());
                        if (list != null && list.size() > 0)
                        {
                            for (int i = 0 ;i<list.size() ;i++)
                            {
                                consumePurchase(BillingClient.ProductType.SUBS,list.get(i));
                            }
                        }
                        queuePurchasINAPP();
                    }
                }
        );
    }catch (Exception e)
    {
        e.printStackTrace();
        if (m_IsStartPay)
        {
            //ProgressbarUtil.hideLoadingView();
            m_IsStartPay = false;
        }
    }
}
//查询支付订单
private  void  queuePurchasINAPP()
{
    try{
        m_BillingClient.queryPurchasesAsync(
                QueryPurchasesParams.newBuilder()
                        .setProductType(BillingClient.ProductType.INAPP)
                        .build(),
                new PurchasesResponseListener() {
                    public void onQueryPurchasesResponse(BillingResult billingResult, List<Purchase> list) {
                        // check billingResult
                        // process returned purchase list, e.g. display the plans user owns

                        Log.d(TAG, "queryPurchases: INAPP  count :" + list.size());
                        if (list != null && list.size() > 0)
                        {
                            for (int i = 0 ;i<list.size() ;i++)
                            {
                                consumePurchase(BillingClient.ProductType.INAPP,list.get(i));
                            }
                        }
                        if (m_CallBack != null)
                        {
                            m_CallBack.onCallBack();
                            m_CallBack = null;
                        }
                    }
                }
        );
    }catch (Exception e)
    {
        e.printStackTrace();
        if (m_IsStartPay)
        {
            //ProgressbarUtil.hideLoadingView();
            m_IsStartPay = false;
        }
    }
}
//订阅和普通支付走不同消耗
private void consumePurchase(final String skuType , final Purchase purchase)
{
    Log.d(TAG,"comsumePurchase params : " +skuType );
    if (skuType == BillingClient.ProductType.INAPP)
    {
        Log.d(TAG,"comsumePurchase BillingClient.ProductType.INAPP " );
        ConsumeParams consumeParams =
                ConsumeParams.newBuilder()
                        .setPurchaseToken(purchase.getPurchaseToken())
                        .build();

        m_BillingClient.consumeAsync(consumeParams, new ConsumeResponseListener() {
            @Override
            public void onConsumeResponse(BillingResult billingResult, String purchaseToken) {
                if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
                    int responseCode = billingResult.getResponseCode();
                    String debugMessage = billingResult.getDebugMessage();
                    Log.d(TAG, "onConsumeResponse: " + responseCode + " " + debugMessage);
                     //消耗完成通知web服务器
                    toPlatformNotifyOrder(BillingClient.ProductType.INAPP,purchase);
                }
            }
        });
    }

    if (skuType == BillingClient.ProductType.SUBS)
    {
        Log.d(TAG,"comsumePurchase BillingClient.ProductType.SUBS " );
        if (!purchase.isAcknowledged()) {
            AcknowledgePurchaseParams acknowledgePurchaseParams =
                    AcknowledgePurchaseParams.newBuilder()
                            .setPurchaseToken(purchase.getPurchaseToken())
                            .build();
            m_BillingClient.acknowledgePurchase(acknowledgePurchaseParams, new AcknowledgePurchaseResponseListener() {
                @Override
                public void onAcknowledgePurchaseResponse(BillingResult billingResult) {
                    int responseCode = billingResult.getResponseCode();
                    String debugMessage = billingResult.getDebugMessage();
                    Log.d(TAG, "onAcknowledgePurchaseResponse: " + responseCode + " " + debugMessage);
                    //消耗完成通知web服务器
                    toPlatformNotifyOrder(BillingClient.ProductType.SUBS,purchase);
                }
            });
        }
    }
}

​

4.处理完历史订单后,正式支付

 public void querySkuDetails() {
    if (m_CurPayItem == null) return;
    Log.d(TAG, "querySkuDetails : " + m_CurPayItem.sku +"   " + m_CurPayItem.skuType );

    QueryProductDetailsParams queryProductDetailsParams =
            QueryProductDetailsParams.newBuilder()
                    .setProductList(
                            ImmutableList.of(QueryProductDetailsParams.Product.newBuilder()
                                    .setProductId(m_CurPayItem.sku)
                                    .setProductType(m_CurPayItem.skuType)
                                    .build()))
                    .build();
    //查询支付项
    m_BillingClient.queryProductDetailsAsync(
            queryProductDetailsParams,
            new ProductDetailsResponseListener() {
                public void onProductDetailsResponse(BillingResult billingResult,List<ProductDetails> productDetailsList) {
                    // check billingResult
                    // process returned productDetailsList
                    if (productDetailsList.size() > 0)
                    {
                        ProductDetails productDetails = productDetailsList.get(0);
                        Log.i(TAG, "onSkuDetailsResponse:  productDetails :" + productDetails.getProductId());
                        Log.i(TAG, "onSkuDetailsResponse:  m_CurPayItem.sku :" + m_CurPayItem.sku);
                        ImmutableList productDetailsParamsList;
                        //处理订阅和普通支付查询结果
                        if (m_CurPayItem.skuType == BillingClient.ProductType.SUBS && productDetails.getSubscriptionOfferDetails() != null)
                        {
                            productDetailsParamsList = ImmutableList.of(
                                    BillingFlowParams.ProductDetailsParams.newBuilder()
                                            // retrieve a value for "productDetails" by calling queryProductDetailsAsync()
                                            .setProductDetails(productDetails)
                                            // to get an offer token, call ProductDetails.getSubscriptionOfferDetails()
                                            // for a list of offers that are available to the user
                                            .setOfferToken(productDetails.getSubscriptionOfferDetails().get(0).getOfferToken())
                                            .build()
                            );
                        }else
                        {
                            productDetailsParamsList = ImmutableList.of(
                                    BillingFlowParams.ProductDetailsParams.newBuilder()
                                            // retrieve a value for "productDetails" by calling queryProductDetailsAsync()
                                            .setProductDetails(productDetails)
                                            .build()
                            );
                        }

                        //实行支付功能,支付后结果在PurchasesUpdatedListener中
                        BillingFlowParams billingFlowParams = BillingFlowParams.newBuilder()
                                .setProductDetailsParamsList(productDetailsParamsList)
                                .setObfuscatedAccountId(m_CurPayItem.orderId)
                                .build();

                        BillingResult launchBillingResult = m_BillingClient.launchBillingFlow(mainActivity, billingFlowParams);
                        Log.d(TAG,"queryProductDetailsAsync BillingResult : " +launchBillingResult.getResponseCode()+"         "+ launchBillingResult.getDebugMessage());
                    }else
                    {
                        //ProgressbarUtil.hideLoadingView();
                        Toast.makeText(UnityPlayer.currentActivity, "購買失敗", Toast.LENGTH_SHORT).show();
                    }
                }
            }
    );
}

//监听支付回调
private PurchasesUpdatedListener purchasesUpdatedListener = new PurchasesUpdatedListener() {
    @Override
    public void onPurchasesUpdated(@NonNull BillingResult billingResult, @Nullable List<Purchase> list) {
        if (billingResult == null) {
            Log.wtf(TAG, "onPurchasesUpdated: null BillingResult");
            return;
        }
        int responseCode = billingResult.getResponseCode();
        String debugMessage = billingResult.getDebugMessage();
        Log.i(TAG, "onPurchasesUpdated: $responseCode $debugMessage");
        m_IsStartPay = false;

        switch (responseCode) {
            case BillingClient.BillingResponseCode.OK:

                Log.i(TAG, "onPurchasesUpdated: BillingClient.BillingResponseCode.OK  ,clear m_CurPayItem size:" + list.size());
                Purchase purchase = list.get(0);

                String orderid = purchase.getAccountIdentifiers().getObfuscatedAccountId();
                Log.d(TAG,"buy success  purchase orderid : " + orderid + " " + m_CurPayItem.orderId);
                //支付完成消耗产品
                consumePurchase(m_CurPayItem.skuType,purchase);
                m_CurPayItem = null;
                break;
            case BillingClient.BillingResponseCode.USER_CANCELED:
                Log.i(TAG, "onPurchasesUpdated: User canceled the purchase");
                Toast.makeText(UnityPlayer.currentActivity, "用戶取消", Toast.LENGTH_SHORT).show();
                break;
            case BillingClient.BillingResponseCode.ITEM_ALREADY_OWNED:
                Log.i(TAG, "onPurchasesUpdated: The user already owns this item");
                break;
            case BillingClient.BillingResponseCode.DEVELOPER_ERROR:
                Log.e(TAG, "onPurchasesUpdated: Developer error means that Google Play " +
                        "does not recognize the configuration. If you are just getting started, " +
                        "make sure you have configured the application correctly in the " +
                        "Google Play Console. The SKU product ID must match and the APK you " +
                        "are using must be signed with release keys."
                );
                break;
        }
    }
};

以上代码仅为代码,参考文件如下

https://download.csdn.net/download/gqj108/87218063文章来源地址https://www.toymoban.com/news/detail-514847.html

到了这里,关于记录下GooglePlay V5支付的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • HCIP--云计算题库 V5.0版本

    在国家政策的支持下,我国云计算应用市场发展明显加快,越来越多的企业开始介入云产业,出现了大量的应用解决方案,云应用的成功案例逐渐丰富,用户了解和认可程度不断提高,云计算产业发展迎来了“黄金机遇期”。云计算近年来已经接近我们的生活,成为一个较为

    2024年02月14日
    浏览(23)
  • 记录一下误删除libc.so.6的经历

    起因: 在配置环境时,出现’GLIBCXX_3.4.29 not found’的错误,在解决这个问题的过程中,需要删除 sudo rm /usr/lib/x86_64-linux-gnu/libstdc++.so.6 软连接,但是一不小心 sudo rm /lib/x86_64-linux-gpu/libc.so.6 ,可恶的tab键。 科普: libc.so.6是c运行时库glibc的软链接,而系统几乎所有程序都依赖

    2024年01月24日
    浏览(22)
  • 简单记录一次帮维修手机经历(Vivo x9)

    手边有一台朋友亲戚之前坏掉的Vivo X9手机, 一直说要我帮忙修理一下, 我一直是拒绝的, 因为搞程序的不等于维修的(会电脑不等于维修电器),不知道这种思路如何根深蒂固的,不过好吧, 今天无聊了, 拆一拆… 1. 充电 在充了差不多半个小时到一个小时的电之后开始

    2024年04月17日
    浏览(27)
  • 踩坑记录2——RK3588跑通YOLO v5+DeepSORT

    上篇说到RK3588编译OpenCV, 这篇记录一下跑通YOLO v5+DeepSORT的愉(chi)快(shi)历程. 如果本身缺少ffmpeg而编译了没有ffmpeg版本的OpenCV, 则视频无法读取. 解决方案参照CSDN, 首先安装ffmpeg: 之后安装一堆dev: libavcodec-dev 、libavformat-dev、libavutil-dev 、libavfilter-dev、 libavresample-dev、 libswresample

    2024年01月22日
    浏览(30)
  • Keil538版本中安装ARM complier V5

    目录 一、问题阐述: 二、ARM complierV5编译器安装步骤: 2.1 下载ARM complierV5的安装包 2.2 在Keil中导入ARM complierV5编译器  三、ARM ComplierV5编译器的使用:         在Keil5.37之后的版本中,默认安装了ARM complier V6, 没有安装ARM complierV5 编译器。但是目前最新版本的STM32CubeMX最

    2024年02月08日
    浏览(26)
  • 记录一下keil mdk社区版免费注册经历【已解决】

    Keil5社区版 MDK(MDK-Community edition),完全免费,没有代码大小限制 一、注册网址 照着网上的方法打开这个网址 https://www.keil.arm.com/mdk-community/ 二、注册过程 先填入邮箱,然后回收到一个验证码,最后进去填写好资料提交,此时说账户需要审核,然后就漫长的等待 大概过了一周

    2024年02月17日
    浏览(30)
  • Python Apex YOLO V5 6.2 目标检测 全过程记录

    博文目录 效果展示 Python YOLO V5 实时截屏与目标检测 GitHub Windows Python PyCharm 开发环境搭建 Windows Python PyTorch CUDA 11.7 TensorRT 环境配置 先根据上述两篇文章将开发环境和虚拟环境都创建好, 然后下载 YOLO V5 6.2 或 YOLO V5 7.0 (最新) 的源码, 用 PyCharm 打开, 选择刚刚创建的虚拟环境 W

    2024年02月03日
    浏览(40)
  • Vue中启动提示polyfill缺少-webpack v5版本导致

    因为我们的项目使用webpack v5,其中polyfill Node核心模块被删除。所以,我们安装它是为了在项目中访问这些模块

    2024年02月09日
    浏览(41)
  • 记录GL3523-OY30使用经历(usb3.0无法识别)

    中度电子爱好者,心血来潮想自己做一块USB集线器,网上资料: Genesys GL3523,允许下行端口ZUI多连接8个设备,4个USB3.1不可移动设备与4个USB2.0不可移动设备或暴露端口。它符合USB3.1规范。GL3523集成了Genesys Logic自主开发的USB 3.1 Gen 1超高速收发物理层(PHY)和USB 2.0高速PHY。它支

    2024年02月12日
    浏览(36)
  • 记录一次最近遇到的新网络诈骗经历,大家要提高警惕啊

    第一次接到诈骗电话,说是要求修改支付宝信息的,一开始说的确实是很迷惑人,一下子可能没法马上分辨出来,但是到后面说要加QQ操作什么什么的,那肯定就是有严重问题的,因为很多诈骗都是通过QQ来操作的,一听到这个就要警惕了。 他的诈骗流程是这样的: 先是说你

    2023年04月23日
    浏览(36)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包