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);}

Google 内购 - Android相关推荐

  1. google内购-订阅模式

    1.订单有变化接收google推送的接口,据此可以实现续订订单 /*** 接收google推送接口* @param body* @param request* @param response* @re ...

  2. Google 内购总结

    Google 内购坑之总结 最近项目中增加了 Google 内购的内容,接入并不难,在这里总结下接入过程中的细节和坑的地方. 内购接入过程 如何接入官方的教程写的很详细(传送门),并且官方也提供了一个 ...

  3. Python Google内购服务端验证

    Google内购完成后,服务端需要校验订单的状态是否正确(是否已经成功付款). 一.申请认证 参考https://developers.google.cn/android-publisher/gett ...

  4. 【Unity】Google内购

    目录 一.创建空安卓库工程 二.Unity配置 三.注意事项 版本更新注意事项 服务器相关(相关文章如下) 支付相关错误码 https://developer.android.com/google/p ...

  5. google内购In-App Billing

    本帖地址:http://blog.csdn.net/jinjian2009/article/details/9140891 这周做了google的内购,没搞过google的内购还是觉得比较繁琐的 go ...

  6. Google内购--封装版

    最近老大提出了一个需求,在应用里面加一个内购.由于之前没做过这块,所以百度一番.网上都是讲的使用一大堆的utils.还要加一个aidl文件.感觉挺麻烦的.最终让我找到了:com.android.bil ...

  7. java集成Google Pay内购

    挺简单的直接上代码: api入参 @Data @ApiModel("google支付表单信息") public class GooglePayForm {/*** 包名*/@Api ...

  8. Android内购+IM

    业余时间研究了一下Android内购与IM推送.用到了下面一些技术 1.内购服务端凭证验证 2.内购服务端与服务端实时通知Pub/Sub 3.IM用FCM来推送 很久以前做了一遍,最近又要弄了一天才弄 ...

  9. Google登录和内购简要说明

    Firebase-Google身份验证(登录) 我的博客:https://blog.csdn.net/qq_39574690/article/details/121246655 1.Google账号登 ...

最新文章

  1. ​两大顶级AI算法一起开源!Nature、Science齐发Alphafold2相关重磅,双厨狂喜~
  2. 大数据开发实战:Hive表DDL和DML
  3. 基于ssm框架的大学生社团管理系统
  4. 远程桌面mstsc /console(/admin) 的运用
  5. Gradle在IDEA中创建web项目
  6. 一种基于谷歌浏览器加载activex控件的解决方法与流程技术_Office控件使用总踩雷?畅写Office带你云端飞行...
  7. 详解Python线程对象daemon属性对线程退出的影响
  8. DBParameter比拼接字符串慢的解决办法
  9. win10更新后开不了机_坚决不更新!被微软雪藏的win10系统版本,只要3GB,老爷机的克星!...
  10. 动手设计 CPU(二)—— 微程序控制的运算器
  11. 【剑指Offer(专项突击版)】001~059题目题解汇总
  12. Linux驱动笔记--主机驱动与外设驱动概念以及分离思想
  13. Golang 1.16 新特性-embed 包及其使用
  14. 【博士论文】深度学习的对抗攻击与鲁棒性测评
  15. 直播商城源码,商城开发实现商城底部导航栏
  16. div显示在上层_如何让div总是显示在最上层,而不致于被其他div遮挡
  17. 蓝桥 算法训练 藏匿的刺客(C语言)
  18. 攻防世界各类题目相关
  19. 堆——以洛谷p3378,p1334,p1628,p1878为例
  20. 西北乱跑娃 --- python正则匹配中文以及数字和标点

热门文章

  1. ubuntu 20.04 安装百度网盘
  2. Linux刻录固态硬盘教程,linux迁移至固态硬盘全过程
  3. pitfall fields
  4. iOS定位经纬度问题
  5. python职场应用英语作文_职场英语作文万能句子
  6. 3D产品展示程序-古董
  7. 给图片添加文字(换行)水印
  8. ubuntu ogv
  9. cx oracle 输出中文,输出cx语言
  10. 安卓兼容7.0图库选择图片生成二维码