直播APP源码,Android APP接入Gooale Pay
为了满足国际客户的需求,直播APP源码在支付方式上的接入服务可谓是下尽功夫,面对国际客户,APP内支付的方式与国内大不相同,并且对于大多数要上架谷歌商店的APP来说,接入Geegle Pay是必不可少的一项功能,今天,就介绍下直播APP源码Android端的接入流程。
1.首先,引入依赖:
implementation 'com.android.billingclient:billing:2.0.1'
2.添加权限
<uses-permission android:name="com.android.vending.BILLING" />
这两步操作必不可少。然后,是支付流程的关键代码部分,基本都是一样的流程:
3.建立连接
mBillingClient = BillingClient.newBuilder(this.mActivity).enablePendingPurchases().setListener(this).build(); mBillingClient.startConnection(new BillingClientStateListener() { @Override public void onBillingSetupFinished(BillingResult billingResult) { Log.e(TAG, "onBillingSetupFinished code = " + billingResult.getResponseCode() + " , msg = " + billingResult.getDebugMessage()); if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) { } } @Override public void onBillingServiceDisconnected() { } });
4.查询商品信息
private void queryPurchases(@NonNull final String purchaseId) { if (!isClientInit()) { if (mListener != null) { mListener.onError(); } return; } List<String> skuList = new ArrayList<>(); skuList.add(purchaseId); skuList.add("xxx"); // 这个参数不能为空,值随便传 SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder(); params.setSkusList(skuList).setType(BillingClient.SkuType.INAPP); mBillingClient.querySkuDetailsAsync(params.build(), new SkuDetailsResponseListener() { @Override public void onSkuDetailsResponse(BillingResult billingResult, List<SkuDetails> skuDetailsList) { Log.e(TAG, "onSkuDetailsResponse code = " + billingResult.getResponseCode() + " , msg = " + billingResult.getDebugMessage() + " , skuDetailsList = " + skuDetailsList); // Process the result. if (skuDetailsList == null || skuDetailsList.isEmpty()) { if (mListener != null) { mListener.onError(); } return; } SkuDetails skuDetails = null; for (SkuDetails details : skuDetailsList) { Log.e(TAG, "onSkuDetailsResponse skuDetails = " + details.toString()); if (purchaseId.equals(details.getSku())) { skuDetails = details; } } if (skuDetails != null) { pay(skuDetails); } else { if (mListener != null) { mListener.onError(); } } } }); }
复制代码
5.执行真正的pay操作
BillingFlowParams flowParams = BillingFlowParams.newBuilder() .setSkuDetails(details) .build(); int code = mBillingClient.launchBillingFlow(mActivity, flowParams).getResponseCode();
支付回调
还记得前面mBillingClient =
BillingClient.newBuilder(this.mActivity).enablePendingPurchases().setListener(this)这个操作吗,这个this也是一个监听器,代表的是支付的回调: @Override public void onPurchasesUpdated(BillingResult billingResult, @Nullable List<Purchase> purchases) { Log.e(TAG, "onPurchasesUpdated code = " + billingResult.getResponseCode() + " , msg = " + billingResult.getDebugMessage()); if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK && purchases != null) { if (mListener != null) { mListener.onSuccess(); } for (Purchase purchase : purchases) { handlePurchase(purchase, true); } } else if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.USER_CANCELED) { // Handle an error caused by a user cancelling the purchase flow. if (mListener != null) { mListener.onCancel(); } } else { // Handle any other error codes. if (mListener != null) { mListener.onError(); } } }
支付成功之后需要处理订购信息
// 只有消费成功之后,才能真正到账,否则3天之后,会执行退款处理 测试阶段只有5分钟
mBillingClient.consumeAsync(ConsumeParams.newBuilder() .setDeveloperPayload(purchase.getDeveloperPayload()) .setPurchaseToken(purchase.getPurchaseToken()).build(), new ConsumeResponseListener() { @Override public void onConsumeResponse(BillingResult billingResult, String purchaseToken) { Log.e(TAG, "onConsumeResponse code = " + billingResult.getResponseCode() + " , msg = " + billingResult.getDebugMessage() + " , purchaseToken = " + purchaseToken);
// 消费成功 处理自己的流程,我选择先存入数据库
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) { PurchaseHistoryBean purchaseHistory = new PurchaseHistoryBean(); purchaseHistory.uid = NumberParserUtil.parseLong(UserInfoManager.create().getUserId(), -1); purchaseHistory.orderId = purchase.getOrderId(); purchaseHistory.purchaseToken = purchase.getPurchaseToken(); purchaseHistory.developerPayload = purchase.getDeveloperPayload(); purchaseHistory.productId = purchase.getSku(); purchaseHistory.purchaseTime = purchase.getPurchaseTime(); savePurchaseHistory(purchaseHistory, needToast); } else { // 消费失败,后面查询消费记录后再次消费,否则,就只能等待退款 } } });
最后一步,通知服务器,我已经支付成功了,把orderId,productId,purchaseToken等信息上报给服务器。前面mBillingClient. d的操作都建立在直播APP源码连接成功的情况下。但是直播源码可能会在这几步存在异常的情况,比如说支付成功了,但是没有消费成功(consumeAsync),消费成功了,但是上报服务器失败了,所以需要一些直播APP源码保证机制,保证最后一定能上报给服务器。这里,客户可根据自己业务的逻辑自行设计。
以上,就是直播APP源码接入Gooale Pay的关键代码,处理步骤基本大同小异,可根据客户自己的业务逻辑稍作改变。
声明:以上内容为云豹科技作者本人原创,未经作者本人同意,禁止转载,否则将追究相关法律责任www.yunbaokj.com