国内关于google login和pay

背景

公司海外部门,需要做一个海外的有内购的App,需要发布到GooglePlay,所以项目就涉及到了aab、google login 以及google pay。

AAB包

官方的介绍

An Android App Bundle is a publishing format that includes all your app’s compiled code and resources, and defers APK generation and signing to Google Play.

Android App Bundle 是一种发布格式,其中包含您应用的所有经过编译的代码和资源,它会将 APK 生成及签名交由 Google Play 来完成。

使用AndroidStuido,Build-__>Generate Signed Bundle / APK —>选择Android App Bundle,

WX20230613-100706@2x.png

然后点Next,然后输入相应的KeyStore信息,输入完成后,点击Next

WX20230613-100732@2x.png

最终到达这个界面,这里注意 需要选择release打包,否则在gpc上传时,会上传失败(如有同学知道具体原因,可以留言告知,大家一起进步)。


WX20230613-100752@2x.png

点击Create后,在工程app下,会生成一个release包,里面就有aab包

WX20230613-101554@2x.png

后面简单的介绍下上传、测试流程

胖子以内部测试为例:

  • 创建内部测试版本,上传aab,并填写相关资料。
  • 在”测试用户数量“tab下,增加测试用户,并让测试用户通过链接进行登录授权
  • 最后就可以测试了。(不用每次都从链接下载,只要保持version Code一直,测试apk也能正常调试、测试
    WX20230613-102352@2x.png

google login

前提:已安装GMS(google mobile service)服务,否则,就放弃吧。

第一步:

const val googlePlay = "com.google.android.gms:play-services-auth:${Version.googlePlayVersion}"

api(Deps.googlePlay)

第二步:

检查是否有google play 服务GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(activity)

第三部:

private lateinit var googleSignInOptions: GoogleSignInOptions
private lateinit var googleSignInClient: GoogleSignInClient

//回调
private lateinit var senderResultLauncher: ActivityResultLauncher<Intent>

senderResultLauncher =
        
fragment.registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
                when (it.resultCode) {
                    RESULT_OK -> {
                        try {
                            it.data?.let { result ->
                                val result = Auth.GoogleSignInApi.getSignInResultFromIntent(result)
                                if (result?.isSuccess == true) {
                                    val signInAccount = result.signInAccount
                                    val idToken = signInAccount?.idToken
                                    val username = signInAccount?.displayName
                                    val password = signInAccount?.id
                                    when {
                                        idToken != null -> {
                                        }
                                        password != null -> {

                                        }
                                        else -> {
                                        }
                                    }
                                }
                            }

                        } catch (e: ApiException) {

                        }
                    }
                    else->{

                    }
                }
            }

//准备工作
googleSignInOptions = GoogleSignInOptions
            .Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .requestId()
//这里特别注意。requestIdToken填入的是服务端的clientId,不是客户端的id,切记切记。
            .requestIdToken填入的是服务端的clientId,不是客户端的id,切记切记。(context.getString(R.string.google_client_id))
            .build()
googleSignInClient = GoogleSignIn.getClient(context, googleSignInOptions)

//开始拉起登录
val signInIntent = googleSignInClient.signInIntent
senderResultLauncher.launch(signInIntent)

google pay

(公司业务流程:下单后台获取支付数据—>下单支付中心获取google pay的数据—>拉起google pay)

前提:已安装GMS(google mobile service)服务,否则,就放弃吧。

第一步:

const val billingclient = "com.android.billingclient:billing:${Version.billingVersion}"

api(Deps.billingclient)

第二步:

这里注意2点

  • productId,因与gpc配置的商品id一致


    WX20230613-112720@2x.png
  • 官方有这么一句话
    Rarely, some devices are unable to support ProductDetails and queryProductDetailsAsync(), usually due to outdated versions of Google Play Services. To ensure proper support for this scenario, learn how to use backwards compatibility features in the Play Billing Library 5 migration guide.
    在极少数情况下,某些设备无法支持 ProductDetails 和 queryProductDetailsAsync(),这通常是因为 Google Play 服务版本已过时。为确保对此场景提供适当的支持,请参阅 Play 结算库版本 5 迁移指南,了解如何使用向后兼容的功能。
    结论:
    用isFeatureSupported判断设备是否支持你购买的商品
    如果支持:使用queryProductDetailsAsync
    否则:使用querySkuDetailsAsync

     private var billingClient: BillingClient = BillingClient.newBuilder(activity)
          .setListener { billingResult, purchases ->
              println("billingResult = $billingResult,purchases = $purchases")
              if (billingResult.responseCode == BillingClient.BillingResponseCode.OK && purchases != null) {
                  for (purchase in purchases) {
                      handlePurchase(orderId,purchase)
                  }
              } else if (billingResult.responseCode == BillingClient.BillingResponseCode.USER_CANCELED) {
                  println("USER_CANCELED")
              } else {
                  println("other error")
              }
          }
          .enablePendingPurchases()
          .build() billingClient?.apply {
          startConnection(object : BillingClientStateListener {
              override fun onBillingSetupFinished(billingResult: BillingResult) {
                  if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
                      val featureTypeResponseCode =
                          isFeatureSupported(BillingClient.FeatureType.PRODUCT_DETAILS)
                      when (featureTypeResponseCode.responseCode) {
                          BillingClient.BillingResponseCode.OK -> {
                              val queryProductDetailsParams =
                                  QueryProductDetailsParams.newBuilder()
                                      .setProductList(
                                          listOf(
                                              QueryProductDetailsParams.Product.newBuilder()
                                                  .setProductId(productId)
                                                  .setProductType(BillingClient.ProductType.INAPP)
                                                  .build()
                                          )
                                      )
                                      .build()
                              billingClient?.apply {
                                  queryProductDetailsAsync(queryProductDetailsParams) { billingResult,
                                                                                        productDetailsList ->
                                      if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
                                          productDetailsList.find {
                                              it.productId == productId
                                          }?.let {
                                              launchPay(activity, orderId, it)
                                          }
                                          //查询未消费交易
                              }
                          }
                          BillingClient.BillingResponseCode.FEATURE_NOT_SUPPORTED -> {
                              val skuDetailsParams = SkuDetailsParams.newBuilder().setType(BillingClient.SkuType.INAPP).setSkusList(
                                  listOf(productId)
                              ) .build()
                              billingClient?.apply {
                                  querySkuDetailsAsync(skuDetailsParams) { billingResult,
                                                                           productDetailsList ->
                                      if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
                                          productDetailsList?.find {
                                              it.sku == productId
                                          }?.let {
                                              launchPaySku(activity, orderId, it)
                                          }
                                          //查询未消费交易
                                  }
                              }
                          }
                          else -> {
                              println("when else !!")
                          }
                      }
                  } else {
                      println("end!!")
                  }
              }
    
              override fun onBillingServiceDisconnected() {
                  retryBillingServiceConnection()
              }
          })
      }
    

第三步:拉起支付

fun launchPay(activity: Activity, orderId: String,productDetails: ProductDetails) {
        val productDetailsParamsList = listOf(
            BillingFlowParams.ProductDetailsParams.newBuilder()
                .setProductDetails(productDetails)
                .build()
        )

        val billingFlowParams = BillingFlowParams.newBuilder()
            .setObfuscatedAccountId(AccountManager.accountInfoFlow().value.p_user_id)
            .setObfuscatedProfileId(orderId)
            .setProductDetailsParamsList(productDetailsParamsList)
            .build()
        // Launch the billing flow
        val billingResult = billingClient?.launchBillingFlow(activity, billingFlowParams)
    }

fun launchPaySku(activity: Activity, orderId: String,sku: SkuDetails) {
        val billingFlowParams = BillingFlowParams.newBuilder()
            .setObfuscatedAccountId(AccountManager.accountInfoFlow().value.p_user_id)
            .setObfuscatedProfileId(orderId)
            .setSkuDetails(sku)
            .build()

        val billingResult = billingClient?.launchBillingFlow(activity, billingFlowParams)
    }
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容