android同胞我相信很多人跟我一样谷歌支付运行自己的app的时候调用支付发现都是出现一个问题签名不同我们今天就来解决这个问题
先正常导入接入流程后面会提到问题的解决
1、导入依赖
//谷歌支付 def billing_version = "5.0.0" implementation "com.android.billingclient:billing-ktx:$billing_version"
清淡文件添加权限
代码接入kotlin的代码是在activity里面写
companion object { private val billProxy = GoogleBillHelper() private var billingListenerImpl: GoogleBillingListenerImpl? = null }//初始化billingListenerImpl = GoogleBillingListenerImpl(this) //建立连接 GoogleBillingManager.getInstance().createClient(this)//调用方法 onClickGooglePlay() private fun onClickGooglePlay() { billProxy .onQuerySkuDetailsAsync( billingListenerImpl, BillingClient.ProductType.INAPP, "fzvip_weak") } //事件监听 private class GoogleBillingListenerImpl(private val activity: Activity) : GoogleBillingListener { override fun onProductDetailsSus(list: List) { if (null == list || list.size <= 0) { Log.e("TAG", "没有查询到相关产品~~~~") return } //查询方法中只传了一个商品,所以直接取第一个了 //根据实际情况处理~ Log.e(TAG, "onProductDetailsSus: " ) list.forEach { Log.e(TAG, "onProductDetailsSus: "+it ) billProxy.onOpenGooglePlay(this, activity, it) } } override fun onPurchasesUpdated(result: BillingResult?, purchases: MutableList?) { Log.e(TAG, "onPurchasesUpdated: "+result?.responseCode ) Log.e(TAG, "onPurchasesUpdated: "+result?.debugMessage ) if (null == purchases || purchases.size == 0) { return } //循环调用消耗 for (purchase in purchases) { billProxy.onConsumeAsync(this, purchase) } } override fun onConsumeSus(purchaseToken: String) { Log.e("TAG", "消费结束,处理自己的业务逻辑~~~") //去与后台验证。处理APP的页面逻辑, 比如充值后发放金币啥的~~~ } } override fun onDestroy() { super.onDestroy() //结束连接 GoogleBillingManager.getInstance().endConn() }
支付的类和接口直接用就可以
1、第一个类
public class GoogleBillHelper { public static final String TAG = GoogleBillHelper.class.getSimpleName(); public void onQuerySkuDetailsAsync(GoogleBillingListener billingListener, String productType, String... productIds) { if (null == productIds || productIds.length == 0 || !GoogleBillingManager.getInstance().isReady() ) { return; } List skuList = new ArrayList<>(); for (String productId : productIds) { QueryProductDetailsParams.Product product = QueryProductDetailsParams .Product.newBuilder() .setProductId(productId) .setProductType(productType) .build(); //添加对应的 产品id 去查询详情 skuList.add(product); } QueryProductDetailsParams params = QueryProductDetailsParams .newBuilder() .setProductList(skuList) .build(); GoogleBillingManager.getInstance().getBillingClient().queryProductDetailsAsync(params, (billingResult, list) -> { if (BillingClient.BillingResponseCode.OK == billingResult.getResponseCode()) { if (null != billingListener) { billingListener.onProductDetailsSus(list); } } else { Log.e("TAG", "code : " + billingResult.getResponseCode() + " message : " + billingResult.getDebugMessage()); } }); } public void onOpenGooglePlay(GoogleBillingListener billingListener, Activity activity, ProductDetails details) { Log.e(TAG, "onOpenGooglePlay: "); if (null == details) { return; } List params = new ArrayList<>(); //根据自己的判断是否是连续包月还是一次性消费连续包月需要添加setOfferToken不加不能调谷歌支付面板 //就是一次性商品是一个商品连续包月的话里面有优惠卷 if (details.getProductId().equals("fzvip_android_succession_quarter")) { //添加购买数据 BillingFlowParams.ProductDetailsParams productDetailsParams = BillingFlowParams.ProductDetailsParams .newBuilder() .setProductDetails(details) .setOfferToken(details.getSubscriptionOfferDetails().get(0).getOfferToken()) .build(); params.add(productDetailsParams); } else { BillingFlowParams.ProductDetailsParams productDetailsParams = BillingFlowParams.ProductDetailsParams .newBuilder() .setProductDetails(details) .build(); params.add(productDetailsParams); } SharedPreferences mProductId = activity.getSharedPreferences("ProductId", Context.MODE_PRIVATE); String mProductId1 = mProductId.getString("mProductId", "");// BillingFlowParams billingFlowParams = BillingFlowParams.newBuilder() .setProductDetailsParamsList(params) .setObfuscatedAccountId(mProductId1)//Uid .build(); //响应code 码 int responseCode = GoogleBillingManager.getInstance().getBillingClient().launchBillingFlow(activity, billingFlowParams).getResponseCode(); Log.e(TAG, "onOpenGooglePlay: " + responseCode); //成功换起 if (BillingClient.BillingResponseCode.OK == responseCode) { //添加购买监听 GoogleBillingManager.getInstance().setBillingListener(billingListener); } } public void onConsumeAsync(GoogleBillingListener billingListener, Purchase purchase, Activity activity) { QMUITipDialog tipDialog = new QMUITipDialog.Builder(activity) .setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING) .setTipWord("正在加载") .create(); tipDialog.show(); if (!GoogleBillingManager.getInstance().isReady()) { return; } ConsumeParams consumeParams = ConsumeParams.newBuilder() .setPurchaseToken(purchase.getPurchaseToken()) .build(); ConsumeResponseListener listener = (billingResult, purchaseToken) -> { if (billingResult.getResponseCode() == 5 && BillingClient.BillingResponseCode.OK == 0) { //连续订阅 if (null != billingListener) { tipDialog.dismiss(); billingListener.onConsumeSus(purchaseToken); } else { tipDialog.dismiss(); Log.e(TAG, "消费失败 code : " + billingResult.getResponseCode() + " message : " + billingResult.getDebugMessage()); } } //一次性消费 if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) { if (null != billingListener) { tipDialog.dismiss(); billingListener.onConsumeSus(purchaseToken); } else { tipDialog.dismiss(); Log.e(TAG, "消费失败 code : " + billingResult.getResponseCode() + " message : " + billingResult.getDebugMessage()); } } }; GoogleBillingManager.getInstance().getBillingClient().consumeAsync(consumeParams, listener); }}
第二个类
public class GoogleBillingManager { private static GoogleBillingManager instance; private BillingClient billingClient; private GoogleBillingListener billingListener; private GoogleBillingManager() { } public static GoogleBillingManager getInstance() { if (instance == null) { synchronized (GoogleBillingManager.class) { if (instance == null) { instance = new GoogleBillingManager(); } } } return instance; } public void createClient(Context context) { if (null == context) { return; } billingClient = BillingClient.newBuilder(context.getApplicationContext()) .enablePendingPurchases() .setListener(new PurchasesUpdatedListener() { @Override public void onPurchasesUpdated(@NonNull BillingResult billingResult, @Nullable List purchases) { if (null != billingListener) {billingListener.onPurchasesUpdated(billingResult, purchases); } } }) .build(); //启动支付连接 startConn(); } public BillingClient getBillingClient() { return billingClient; } public void setBillingListener(GoogleBillingListener billingListener) { this.billingListener = billingListener; } public boolean isReady() { return !(null == billingClient || !billingClient.isReady()); } private void startConn() { if (isReady()) { return; } billingClient.startConnection(new BillingClientStateListener() { @Override public void onBillingSetupFinished(BillingResult billingResult) { if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) { Log.e("TAG", "连接成功,可以开始操作了~~~"); } } @Override public void onBillingServiceDisconnected() { //连接失败。 可以尝试调用 startConnection 重新建立连接 Log.e("TAG", "连接失败"); } }); } public void endConn() { if (null != billingClient) { billingClient.endConnection(); } }}
接口
public interface GoogleBillingListener { void onPurchasesUpdated(BillingResult result, List purchases); void onProductDetailsSus(List list); void onConsumeSus(String purchaseToken);}
重点总结:这些东西我写完发现调用完了之后开始支付如果运行我们android studio上面的app根本行不通无法支付只能出来弹框他不能成功唤起支付页面、解决的话很简单就是我们不安装自己的去安装Google play平台上面我们上传上去的app光说可能听不太懂我们直接上图片
1、我们直接点击自己的上传的app
查看自己需要的版本
下载apk就可以了
弹框下载apk就可以测试了
到这里基本就可以了如果还有别的我们可以一起讨论
来源地址:https://blog.csdn.net/jiayuanwai/article/details/130268036