|
@@ -5,6 +5,10 @@ import com.swago.baseswago.model.pay.PayModel
|
|
|
import com.swago.baseswago.util.ActivityManagerUtil
|
|
|
import com.swago.baseswago.util.AppContext
|
|
|
import com.swago.baseswago.util.LogUtil
|
|
|
+import kotlinx.coroutines.Dispatchers
|
|
|
+import kotlinx.coroutines.GlobalScope
|
|
|
+import kotlinx.coroutines.launch
|
|
|
+import kotlinx.coroutines.withContext
|
|
|
import java.lang.Exception
|
|
|
|
|
|
/**
|
|
@@ -14,13 +18,14 @@ import java.lang.Exception
|
|
|
object PayManager {
|
|
|
|
|
|
val listener = ArrayList<IPayCallback>()
|
|
|
- private const val publicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA184P/HXDtDi+3NgrxRkwIRU6tI2tiodETDzIyA/uwqgKwl/9JgXw2fZEFlsbEv1rUCVvvFGLGo7Z1GrUoR7WUAPktIuMWF330BurThfiwkpkSamzHEe+Pk/98/1YjZ8ltF2xaunUHZhRuE2WonrTy/rFvv/9PZtYd0xNFN5yOQwzFmT5nJvtpfCDWe2OhHXxuswG1bBn+faCcE0Rg1ZkSi36+0gDVvGjX/zQDgsXhwGJqFdVaJu7E7iR4zEGZLHquA931M0gYgGuF/tO9h8ppcaP1N7yR/ibek6KLcs969P47ENlRaL1QnrB3MSbomhhrtAzDLlV8YEIlukzsLrR7wIDAQAB"
|
|
|
+ private const val publicKey =
|
|
|
+ "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA184P/HXDtDi+3NgrxRkwIRU6tI2tiodETDzIyA/uwqgKwl/9JgXw2fZEFlsbEv1rUCVvvFGLGo7Z1GrUoR7WUAPktIuMWF330BurThfiwkpkSamzHEe+Pk/98/1YjZ8ltF2xaunUHZhRuE2WonrTy/rFvv/9PZtYd0xNFN5yOQwzFmT5nJvtpfCDWe2OhHXxuswG1bBn+faCcE0Rg1ZkSi36+0gDVvGjX/zQDgsXhwGJqFdVaJu7E7iR4zEGZLHquA931M0gYgGuF/tO9h8ppcaP1N7yR/ibek6KLcs969P47ENlRaL1QnrB3MSbomhhrtAzDLlV8YEIlukzsLrR7wIDAQAB"
|
|
|
|
|
|
private var isConnected = false
|
|
|
private var billingClient: BillingClient? = null
|
|
|
|
|
|
fun init() {
|
|
|
- if (billingClient!=null) return
|
|
|
+ if (billingClient != null) return
|
|
|
billingClient = BillingClient.newBuilder(AppContext.getContext()).enablePendingPurchases()
|
|
|
.setListener { billingResult, data ->
|
|
|
when (billingResult.responseCode) {
|
|
@@ -36,20 +41,27 @@ object PayManager {
|
|
|
list.add(it)
|
|
|
}
|
|
|
}
|
|
|
- listener.forEach {
|
|
|
- it.onPaySuccess(list)
|
|
|
+
|
|
|
+ GlobalScope.launch(Dispatchers.Main) {
|
|
|
+ listener.forEach {
|
|
|
+ it.onPaySuccess(list)
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
BillingClient.BillingResponseCode.USER_CANCELED -> {
|
|
|
- listener.forEach {
|
|
|
- it.onPayCancel()
|
|
|
+ GlobalScope.launch(Dispatchers.Main) {
|
|
|
+ listener.forEach {
|
|
|
+ it.onPayCancel()
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
else -> {
|
|
|
- listener.forEach {
|
|
|
- it.onPayError(billingResult.responseCode)
|
|
|
+ GlobalScope.launch(Dispatchers.Main) {
|
|
|
+ listener.forEach {
|
|
|
+ it.onPayError(billingResult.responseCode)
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -59,14 +71,18 @@ object PayManager {
|
|
|
override fun onBillingSetupFinished(p0: BillingResult) {
|
|
|
if (p0.responseCode == BillingClient.BillingResponseCode.OK) {
|
|
|
isConnected = true
|
|
|
- listener.forEach {
|
|
|
- it.onConnectedServer()
|
|
|
- LogUtil.d("链接到谷歌")
|
|
|
+ GlobalScope.launch(Dispatchers.Main) {
|
|
|
+ listener.forEach {
|
|
|
+ it.onConnectedServer()
|
|
|
+ LogUtil.d("链接到谷歌")
|
|
|
+ }
|
|
|
}
|
|
|
queryUnConsumeOrder("")
|
|
|
} else {
|
|
|
- listener.forEach {
|
|
|
- it.onPayError(p0.responseCode)
|
|
|
+ GlobalScope.launch(Dispatchers.Main) {
|
|
|
+ listener.forEach {
|
|
|
+ it.onPayError(p0.responseCode)
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -84,35 +100,51 @@ object PayManager {
|
|
|
dataNet.forEach {
|
|
|
list.add(it.google_product_id)
|
|
|
}
|
|
|
- val params = SkuDetailsParams.newBuilder()
|
|
|
- params.setSkusList(list).setType(BillingClient.SkuType.INAPP)
|
|
|
- billingClient?.querySkuDetailsAsync(params.build()) { billingResult, data ->
|
|
|
+ val listData = ArrayList<QueryProductDetailsParams.Product>()
|
|
|
+ list.forEach {
|
|
|
+ val product = QueryProductDetailsParams.Product.newBuilder()
|
|
|
+ .setProductId(it)
|
|
|
+ .setProductType(BillingClient.ProductType.INAPP)
|
|
|
+ .build()
|
|
|
+ listData.add(product)
|
|
|
+ }
|
|
|
+ val queryProductDetailsParams =
|
|
|
+ QueryProductDetailsParams.newBuilder().setProductList(listData).build()
|
|
|
+ billingClient?.queryProductDetailsAsync(queryProductDetailsParams) { billingResult, data ->
|
|
|
when (billingResult.responseCode) {
|
|
|
BillingClient.BillingResponseCode.OK -> {
|
|
|
- data?.let {
|
|
|
+ data.let {
|
|
|
if (it.isNotEmpty()) {
|
|
|
- it.forEach { skuDetails ->
|
|
|
- dataNet.forEach { dataSkuDetails ->
|
|
|
- if(skuDetails.sku == dataSkuDetails.google_product_id){
|
|
|
- dataSkuDetails.product_price = skuDetails.price
|
|
|
+ it.forEach { productDetails ->
|
|
|
+ dataNet.forEach { dataSkuDetails ->
|
|
|
+ if (productDetails.productId == dataSkuDetails.google_product_id) {
|
|
|
+ dataSkuDetails.product_price =
|
|
|
+ productDetails.oneTimePurchaseOfferDetails?.formattedPrice
|
|
|
+ ?: "0"
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- listener.forEach {
|
|
|
- it.onQueryProductPrice(dataNet)
|
|
|
+ GlobalScope.launch(Dispatchers.Main) {
|
|
|
+ listener.forEach {
|
|
|
+ it.onQueryProductPrice(dataNet)
|
|
|
+ }
|
|
|
}
|
|
|
- }else{
|
|
|
- listener.forEach {
|
|
|
- //未查询到商品
|
|
|
- it.onPayError(-5)
|
|
|
+ } else {
|
|
|
+ GlobalScope.launch(Dispatchers.Main) {
|
|
|
+ listener.forEach {
|
|
|
+ //未查询到商品
|
|
|
+ it.onPayError(-5)
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
else -> {
|
|
|
- listener.forEach {
|
|
|
- it.onPayError(billingResult.responseCode)
|
|
|
+ GlobalScope.launch(Dispatchers.Main) {
|
|
|
+ listener.forEach {
|
|
|
+ it.onPayError(billingResult.responseCode)
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -127,14 +159,18 @@ object PayManager {
|
|
|
billingClient?.consumeAsync(params) { billingResult, data ->
|
|
|
when (billingResult.responseCode) {
|
|
|
BillingClient.BillingResponseCode.OK -> {
|
|
|
- listener.forEach {
|
|
|
- it.consumeOrderSuccess()
|
|
|
+ GlobalScope.launch(Dispatchers.Main) {
|
|
|
+ listener.forEach {
|
|
|
+ it.consumeOrderSuccess()
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
else -> {
|
|
|
- listener.forEach {
|
|
|
- it.consumeOrderFail(billingResult.responseCode)
|
|
|
+ GlobalScope.launch(Dispatchers.Main) {
|
|
|
+ listener.forEach {
|
|
|
+ it.consumeOrderFail(billingResult.responseCode)
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -146,61 +182,83 @@ object PayManager {
|
|
|
*/
|
|
|
fun queryUnConsumeOrder(productId: String) {
|
|
|
billingClient?.let {
|
|
|
- val result = it.queryPurchases(BillingClient.SkuType.INAPP)
|
|
|
- when (result.responseCode) {
|
|
|
- BillingClient.BillingResponseCode.OK -> {
|
|
|
- if (result.purchasesList != null && result.purchasesList!!.size != 0) {
|
|
|
- val unConsumePurchase: MutableList<Purchase> = ArrayList()
|
|
|
- result.purchasesList?.onEach { purchase ->
|
|
|
- if (Security.verify(Security.generatePublicKey(publicKey), purchase.originalJson, purchase.signature) && purchase.purchaseState==1) {
|
|
|
- unConsumePurchase.add(purchase)
|
|
|
+ val params = QueryPurchasesParams.newBuilder()
|
|
|
+ .setProductType(BillingClient.ProductType.INAPP)
|
|
|
+ it.queryPurchasesAsync(params.build()) { billingResult, list ->
|
|
|
+ when (billingResult.responseCode) {
|
|
|
+ BillingClient.BillingResponseCode.OK -> {
|
|
|
+ if (list.size != 0) {
|
|
|
+ val unConsumePurchase: MutableList<Purchase> = ArrayList()
|
|
|
+ list.onEach { purchase ->
|
|
|
+ if (Security.verify(
|
|
|
+ Security.generatePublicKey(publicKey),
|
|
|
+ purchase.originalJson,
|
|
|
+ purchase.signature
|
|
|
+ ) && purchase.purchaseState == 1
|
|
|
+ ) {
|
|
|
+ unConsumePurchase.add(purchase)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ GlobalScope.launch(Dispatchers.Main) {
|
|
|
+ listener.forEach { callback ->
|
|
|
+ callback.onUnConsumeOrder(unConsumePurchase)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ /**
|
|
|
+ * 不存在未消费
|
|
|
+ * 当productId空字符串的时候
|
|
|
+ * 代表着只是查询未消费订单
|
|
|
+ * 当productId不为空字符串,
|
|
|
+ * 代表着此时没有未消费的订单
|
|
|
+ * 然后去创建新的订单购买商品
|
|
|
+ */
|
|
|
+ if (productId.isNotEmpty()) {
|
|
|
+ GlobalScope.launch(Dispatchers.Main) {
|
|
|
+ listener.forEach { callback ->
|
|
|
+ callback.toCreateNewOrder(productId)
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
- listener.forEach { callback ->
|
|
|
- callback.onUnConsumeOrder(unConsumePurchase)
|
|
|
- }
|
|
|
- } else {
|
|
|
- /**
|
|
|
- * 不存在未消费
|
|
|
- * 当productId空字符串的时候
|
|
|
- * 代表着只是查询未消费订单
|
|
|
- * 当productId不为空字符串,
|
|
|
- * 代表着此时没有未消费的订单
|
|
|
- * 然后去创建新的订单购买商品
|
|
|
- */
|
|
|
- if (productId.isNotEmpty()){
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ else -> {
|
|
|
+ GlobalScope.launch(Dispatchers.Main) {
|
|
|
listener.forEach { callback ->
|
|
|
- callback.toCreateNewOrder(productId)
|
|
|
+ callback.onPayError(billingResult.responseCode)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
}
|
|
|
-
|
|
|
- else -> {
|
|
|
- listener.forEach { callback ->
|
|
|
- callback.onPayError(result.responseCode)
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private fun queryProductIsExit(data: ArrayList<String>, userId: String, orderId: String) {
|
|
|
- val params = SkuDetailsParams.newBuilder()
|
|
|
- params.setSkusList(data).setType(BillingClient.SkuType.INAPP)
|
|
|
- billingClient?.let {
|
|
|
- it.querySkuDetailsAsync(params.build()) { billingResult, data ->
|
|
|
- when (billingResult.responseCode) {
|
|
|
- BillingClient.BillingResponseCode.OK -> {
|
|
|
- //查询到商品然后支付
|
|
|
- data?.onEach { skuDetails ->
|
|
|
- startPay(skuDetails, userId, orderId)
|
|
|
- }
|
|
|
+ val listData = ArrayList<QueryProductDetailsParams.Product>()
|
|
|
+ data.forEach {
|
|
|
+ val product = QueryProductDetailsParams.Product.newBuilder()
|
|
|
+ .setProductId(it)
|
|
|
+ .setProductType(BillingClient.ProductType.INAPP)
|
|
|
+ .build()
|
|
|
+ listData.add(product)
|
|
|
+ }
|
|
|
+ val queryProductDetailsParams =
|
|
|
+ QueryProductDetailsParams.newBuilder().setProductList(listData).build()
|
|
|
+ billingClient?.queryProductDetailsAsync(queryProductDetailsParams) { billingResult, dataX ->
|
|
|
+ when (billingResult.responseCode) {
|
|
|
+ BillingClient.BillingResponseCode.OK -> {
|
|
|
+ //查询到商品然后支付
|
|
|
+ dataX.onEach { productDetails ->
|
|
|
+ startPay(productDetails, userId, orderId)
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- else -> {
|
|
|
+ else -> {
|
|
|
+ GlobalScope.launch(Dispatchers.Main) {
|
|
|
listener.forEach { callback ->
|
|
|
callback.onPayError(billingResult.responseCode)
|
|
|
}
|
|
@@ -211,16 +269,26 @@ object PayManager {
|
|
|
}
|
|
|
|
|
|
|
|
|
- private fun startPay(skuDetails: SkuDetails, userId: String, orderId: String) {
|
|
|
+ private fun startPay(productDetails: ProductDetails, userId: String, orderId: String) {
|
|
|
try {
|
|
|
- val skuDetails = SkuDetails(skuDetails.originalJson)
|
|
|
+ val productDetailsParamsList = listOf(
|
|
|
+ BillingFlowParams.ProductDetailsParams.newBuilder()
|
|
|
+ .setProductDetails(productDetails)
|
|
|
+ .setOfferToken(
|
|
|
+ productDetails.subscriptionOfferDetails?.get(0)?.offerToken ?: ""
|
|
|
+ ).build()
|
|
|
+ )
|
|
|
+ // Retrieve a value for "skuDetails" by calling querySkuDetailsAsync().
|
|
|
val flowParams = BillingFlowParams.newBuilder()
|
|
|
.setObfuscatedAccountId(userId)
|
|
|
.setObfuscatedProfileId(orderId)
|
|
|
- .setSkuDetails(skuDetails)
|
|
|
+ .setProductDetailsParamsList(productDetailsParamsList)
|
|
|
.build()
|
|
|
- billingClient?.launchBillingFlow(ActivityManagerUtil.get().currentActivity(),flowParams)
|
|
|
- }catch (e:Exception){
|
|
|
+ billingClient?.launchBillingFlow(
|
|
|
+ ActivityManagerUtil.get().currentActivity(),
|
|
|
+ flowParams
|
|
|
+ )
|
|
|
+ } catch (e: Exception) {
|
|
|
e.printStackTrace()
|
|
|
}
|
|
|
}
|