需要先提交google商店,才能在google创建商品,查询支付。
1导入googlepay
//GooglePay
implementation("com.android.billingclient:billing:4.1.0")
//支付工具类
class GooglePayHelper {
private val TAG = "GooglePayHelper"
private var mActivity: Activity;
constructor(mActivity: Activity) {
this.mActivity = mActivity
}
/**
* 购买监听
*/
private val purchasesUpdatedListener =
PurchasesUpdatedListener { billingResult, purchases ->
// To be implemented in a later section.
if (billingResult.responseCode == BillingResponseCode.OK) {
//支付成功
if (!purchases.isNullOrEmpty()) {
for (i in 0 until purchases.size) {
var purchase = purchases[i];
//购买成功 消耗掉 同步后台
consume(purchase)
}
}
} else if (billingResult.responseCode == BillingResponseCode.ITEM_ALREADY_OWNED) {
//未消耗掉 查询补单
queryPurchases()
} else if (billingResult.responseCode == BillingResponseCode.USER_CANCELED) {
//取消支付
} else {
// when (billingResult.responseCode) {
// BillingResponseCode.SERVICE_TIMEOUT -> {
// //服务连接超时
// }
// BillingResponseCode.FEATURE_NOT_SUPPORTED -> {
// }
// BillingResponseCode.SERVICE_DISCONNECTED -> {
// //服务未连接
// }
// BillingResponseCode.USER_CANCELED -> {
// //取消
// }
// BillingResponseCode.SERVICE_UNAVAILABLE -> {
// //服务不可用
// }
// BillingResponseCode.BILLING_UNAVAILABLE -> {
// //购买不可用
// }
// BillingResponseCode.ITEM_UNAVAILABLE -> {
// //商品不存在
//
// }
// BillingResponseCode.DEVELOPER_ERROR -> {
// //提供给 API 的无效参数
// }
// BillingResponseCode.ERROR -> {
// //错误
// }
// BillingResponseCode.ITEM_NOT_OWNED -> {
// //不可购买
// }
// }
}
}
private var billingClient = BillingClient.newBuilder(Application)
.setListener(purchasesUpdatedListener)
.enablePendingPurchases()
.build()
//检查之前的商品是否被消费
fun init() {
//已经连接查询订单
if (billingClient.isReady) {
queryPurchases()
} else {
//开始连接
billingClient.startConnection(object : BillingClientStateListener {
override fun onBillingSetupFinished(billingResult: BillingResult) {
if (billingResult.responseCode == BillingResponseCode.OK) {
//BillingClient准备好了。你可以在这里查询购买情况。
queryPurchases()
} else {
Log.d(TAG, "init 连接失败 code:" + billingResult.responseCode)
}
}
override fun onBillingServiceDisconnected() {
// Try to restart the connection on the next request to
// Google Play by calling the startConnection() method.
Log.d(TAG, "init onBillingServiceDisconnected")
}
})
}
}
/**
* 购买商品
*/
private fun purchase(mSku: String, orderId: String) {
if (billingClient.isReady) {
val skuList: MutableList<String> = ArrayList()
skuList.add(mSku)
val params = SkuDetailsParams.newBuilder()
params.setSkusList(skuList).setType(SkuType.INAPP)
billingClient.querySkuDetailsAsync(
params.build()
) { billingResult, skuDetailsList ->
// Process the result.
if (billingResult.responseCode == BillingResponseCode.OK) {
if (!skuDetailsList.isNullOrEmpty()) {
for (i in 0 until skuDetailsList.size) {
val skuDetails = skuDetailsList[i]
Log.d(TAG, "purchase skuDetails $skuDetails")
if (mSku == skuDetails.sku) {
val flowParams = BillingFlowParams.newBuilder()
.setSkuDetails(skuDetails)
.setObfuscatedAccountId(orderId)
.build()
//吊起支付
val responseCode =
billingClient.launchBillingFlow(
mActivity,
flowParams
).responseCode
Log.d(TAG, "purchase 去支付 code $responseCode")
}
}
} else {
Log.d(TAG, "purchase 未查询到商品")
}
} else {
Log.d(TAG, "purchase 查询连接失败 cede" + billingResult.responseCode)
}
}
} else {
Log.d(TAG, "purchase 购买 未连接")
}
}
/**
* 消耗商品
*
* @param purchaseToken 商品token
*/
private fun consume(purchase: Purchase) {
if (billingClient.isReady) {
val consumeParams =
ConsumeParams.newBuilder()
.setPurchaseToken(purchase.purchaseToken)
.build()
billingClient.consumeAsync(
consumeParams
) { billingResult, s ->
if (billingResult.responseCode == BillingResponseCode.OK) {
// Handle the success of the consume operation.
Log.d(TAG, "consume 消耗成功${googlePayResult.purchaseToken}")
//同步给服务器
//do something
} else {
Log.d(TAG, "consume 消耗失败 code:" + billingResult.responseCode)
}
}
} else {
billingClient.startConnection(object : BillingClientStateListener {
override fun onBillingSetupFinished(billingResult: BillingResult) {
if (billingResult.responseCode == BillingResponseCode.OK) {
//重连后 消耗掉
val consumeParams =
ConsumeParams.newBuilder()
.setPurchaseToken(purchase.purchaseToken)
.build()
billingClient.consumeAsync(
consumeParams
) { billingResult, s ->
if (billingResult.responseCode == BillingResponseCode.OK) {
// Handle the success of the consume operation.
Log.d(TAG, "consume 消耗成功${googlePayResult.purchaseToken}")
//同步给服务器
//do something
} else {
Log.d(TAG, "consume 消耗失败 code:" + billingResult.responseCode)
}
}
} else {
// Log.d(TAG, "consume init 连接失败 code:" + billingResult.responseCode)
}
}
override fun onBillingServiceDisconnected() {
// Try to restart the connection on the next request to
// Google Play by calling the startConnection() method.
}
})
}
}
/**
* 补单消耗商品
*
* @param purchaseToken 商品token
*/
private fun consume2(purchaseToken: String) {
if (billingClient.isReady) {
val consumeParams =
ConsumeParams.newBuilder()
.setPurchaseToken(purchaseToken)
.build()
billingClient.consumeAsync(
consumeParams
) { billingResult, s ->
if (billingResult.responseCode == BillingResponseCode.OK) {
// Handle the success of the consume operation.
Log.d(TAG, "consume 消耗成功$purchaseToken")
} else {
Log.d(TAG, "consume 消耗失败 code:" + billingResult.responseCode)
}
}
} else {
billingClient.startConnection(object : BillingClientStateListener {
override fun onBillingSetupFinished(billingResult: BillingResult) {
if (billingResult.responseCode == BillingResponseCode.OK) {
//重连后 消耗掉
val consumeParams =
ConsumeParams.newBuilder()
.setPurchaseToken(purchaseToken)
.build()
billingClient.consumeAsync(
consumeParams
) { billingResult, s ->
if (billingResult.responseCode == BillingResponseCode.OK) {
// Handle the success of the consume operation.
Log.d(TAG, "consume 消耗成功$purchaseToken")
} else {
Log.d(TAG, "consume 消耗失败 code:" + billingResult.responseCode)
}
}
} else {
Log.d(TAG, "consume init 连接失败 code:" + billingResult.responseCode)
}
}
override fun onBillingServiceDisconnected() {
// Try to restart the connection on the next request to
// Google Play by calling the startConnection() method.
Log.d(TAG, "consume init onBillingServiceDisconnected")
}
})
}
}
/**
* 补单操作 查询购买交易,以确保所有购买交易都得到成功处理,如购买未发货,或者未消耗
*/
private fun queryPurchases() {
if (billingClient.isReady) {
billingClient.queryPurchasesAsync(
SkuType.INAPP
) { billingResult, mutableList ->
if (billingResult.responseCode == BillingResponseCode.OK) {
if (mutableList.isNullOrEmpty()) {
Log.d(TAG, "queryPurchases null")
} else {
for (i in 0 until mutableList.size) {
Log.d(TAG, "queryPurchases init " + mutableList[i].purchaseState)
if (mutableList[i].purchaseState == Purchase.PurchaseState.PURCHASED) {
//已购买,消耗即可
consume2(mutableList[i].purchaseToken)
}
}
}
} else {
Log.d(TAG, "queryPurchases error code" + billingResult.responseCode)
}
}
} else {
billingClient.startConnection(object : BillingClientStateListener {
override fun onBillingSetupFinished(billingResult: BillingResult) {
if (billingResult.responseCode == BillingResponseCode.OK) {
//BillingClient准备好了。你可以在这里查询购买情况。
Log.d(TAG, "queryPurchases init 连接成功")
} else {
Log.d(TAG, "queryPurchases init 连接失败 code:" + billingResult.responseCode)
}
}
override fun onBillingServiceDisconnected() {
// Try to restart the connection on the next request to
// Google Play by calling the startConnection() method.
Log.d(TAG, "queryPurchases init onBillingServiceDisconnected")
}
})
}
}
/**
* google支付
*/
fun googlePay() {
//向服务端获取订单ID 开始支付
purchase("sku","orderid")
}
fun release() {
if (billingClient.isReady) {
billingClient.endConnection();
}
mPayCallBack = null
Log.d(TAG, "billingClient release")
}
}