要集成Google应用内支付 先要了解文档
集成文档: https://developer.android.google.cn/google/play/billing/integrate
1.创建Google应用
Google play 应用平台: https://play.google.com/console/u/0/developers/6529195851168790505/app-list
创建完应用后 进入应用详情 这个里面创建订阅或者消耗类型商品 获取商品ID用于代码中查询商品进行吊起Google支付
2.付费使用测试卡
这个里面添加上你测试使用的Google账号然后回车添加后保存即可
3.集成Google应用内支付或者订阅
先导入依赖库
implementation 'com.android.billingclient:billing:4.0.0'
然后初始化 BillingClient
private PurchasesUpdatedListener purchasesUpdatedListener = new PurchasesUpdatedListener() {@Overridepublic void onPurchasesUpdated(BillingResult billingResult, List<Purchase> purchases) {// 支付后回调}
};private BillingClient billingClient = BillingClient.newBuilder(context).setListener(purchasesUpdatedListener).enablePendingPurchases().build();
然后与Google建立连接并且查询商品然后吊起Google支付或者订阅
在这里要先搞清楚一点 消耗品是购买商品一次性的 非消耗品是订阅
billingClientAfter.startConnection(new BillingClientStateListener() {@SuppressLint("LongLogTag")@Overridepublic void onBillingServiceDisconnected() {// 连接断开Log.d(TAG, "onBillingServiceDisconnected: 连接断开");}@Overridepublic void onBillingSetupFinished(@NonNull BillingResult billingResult) {// 连接成功if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder();List<String> skuList = new ArrayList<>();skuList.add(str);//str是商品IDskuList.add("gas");// 这个参数不能为空,值随便传if (//这里判断是商品订阅还是商品购买) {params.setSkusList(skuList).setType(BillingClient.SkuType.INAPP);//针对一次性商品} else {params.setSkusList(skuList).setType(BillingClient.SkuType.SUBS);//针对订阅}//商品查询billingClientAfter.querySkuDetailsAsync(params.build(), new SkuDetailsResponseListener() {@SuppressLint("LongLogTag")@Overridepublic void onSkuDetailsResponse(@NonNull BillingResult billingResult, @Nullable List<SkuDetails> list) {if (list != null && list.size() > 0) {for (SkuDetails skuDetails : list) {String sku = skuDetails.getSku();// 唤起GooglePay支付if (str.equals(sku)) {BillingFlowParams flowParams = BillingFlowParams.newBuilder().setSkuDetails(skuDetails).build();billingClientAfter.launchBillingFlow(ExhibitionPaymentActivity.this, flowParams);}}} else {Log.d(TAG, "onSkuDetailsResponse: 未找到订阅商品");}}});} else {// TODO 连接失败}}});
回调中还有一些内容我都列出来 方便理解
private PurchasesUpdatedListener purchasesUpdatedListenerAfter = new PurchasesUpdatedListener() {@SuppressLint("LongLogTag")@Overridepublic void onPurchasesUpdated(BillingResult billingResult, List<Purchase> purchases) {int responseCode = billingResult.getResponseCode();if (responseCode == BillingClient.BillingResponseCode.OK && purchases != null) {for (Purchase purchase : purchases) {if (//这里判断是商品订阅还是商品购买)) {//这是消耗商品handlePurchaseBUY(purchase); //这个方法是说表明某件一次性商品已被消耗} else {//这是订阅商品handlePurchase(purchase);//这个方法是订阅判断用户是否以及订阅 如果没订阅就是用来确认交易商品}// 这里是获取支付后订单号String orderId = purchase.getOrderId();int purchaseState = purchase.getPurchaseState();//判断用户是否付款了 2代办 1付款 0未指定if (purchaseState == Purchase.PurchaseState.PURCHASED) {//用户如果付款了在这里面做其他操作//这是非常重要的一步就是结束商品回调 要是不加这一步 应用内如果有多处商品购买的代码 这个回调就会瞎跑 加上就行了billingClientAfter.endConnection();} else {UIUtils.showToast(getString(R.string.支付失败));}}Log.d(TAG, "onPurchasesUpdated: 支付完成");} else if (responseCode == BillingClient.BillingResponseCode.USER_CANCELED) {// Handle an error caused by a user cancelling the purchase flow.Log.d(TAG, "onPurchasesUpdated: 用户取消了支付");} else if (responseCode == BillingClient.BillingResponseCode.ITEM_ALREADY_OWNED) {// Handle an error caused by a user cancelling the purchase flow.Log.d(TAG, "onPurchasesUpdated: 商品已经购买过(重复购买了此商品,如果需要支持重复购买,需要将商品购买成功后消费掉)");} else {// Handle any other error codes.}}};
大部分内容都加注解了 对照着Google集成都文档一看就明白了
//这是确认消耗类型商品被消耗都方法
void handlePurchaseBUY(Purchase purchase) {// Purchase retrieved from BillingClient#queryPurchasesAsync or your PurchasesUpdatedListener.// Verify the purchase.// Ensure entitlement was not already granted for this purchaseToken.// Grant entitlement to the user.ConsumeParams consumeParams =ConsumeParams.newBuilder().setPurchaseToken(purchase.getPurchaseToken()).build();ConsumeResponseListener listener = new ConsumeResponseListener() {@Overridepublic void onConsumeResponse(BillingResult billingResult, String purchaseToken) {if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {// Handle the success of the consume operation.}}};billingClientAfter.consumeAsync(consumeParams, listener);}
//这是对于订阅商品的确认方法
AcknowledgePurchaseResponseListener acknowledgePurchaseResponseListenerAfter;void handlePurchase(Purchase purchase) {if (purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED) {if (!purchase.isAcknowledged()) {AcknowledgePurchaseParams acknowledgePurchaseParams =AcknowledgePurchaseParams.newBuilder().setPurchaseToken(purchase.getPurchaseToken()).build();billingClientAfter.acknowledgePurchase(acknowledgePurchaseParams, acknowledgePurchaseResponseListenerAfter);}}}
关于Google应用内支付或者订阅大致内容就是这些了