以 com.android.billingclient:billing:4.0.0
版本介绍:
一,准备工作
1,准备 Google play 开发者账号。
2,创建应用内商品(商品id,商品描述,定价)。
3,应用为发布状态之后,在 app 里面才能执行支付等操作。
就算是开发内测阶段,也必须上传一个 apk,审核通过到发布状态。
二,集成
1,连接 Google Play 服务
private val purchasesUpdatedListener = PurchasesUpdatedListener {
billingResult, purchases ->
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK && purchases != null) {
handPurchase(skuType, purchases)
} else if (billingResult.responseCode == BillingClient.BillingResponseCode.USER_CANCELED) {
PayManager.doCallBack("pay_cancel", false)
ToastUtil.showShort(
ContextUtils.appContext,
ContextUtils.appContext.getString(R.string.vip_pay_no)
)
} else {
handError(billingResult.responseCode, null)
}
}
private val connListener = object : BillingClientStateListener {
override fun onBillingSetupFinished(connResult: BillingResult) {
if (connResult.responseCode == BillingClient.BillingResponseCode.OK) {
reConnectionCount = 0
// 连接成功后,建议 检查是否有支付完成,待确认的订单。
checkPurchasesAsync(BillingClient.SkuType.INAPP)
checkPurchasesAsync(BillingClient.SkuType.SUBS)