Google Pay 官方集成链接: 点这里
//谷歌支付
implementation 'com.google.android.gms:play-services-wallet:19.0.0'
implementation 'com.android.billingclient:billing:5.0.0'
//建立连接
GoogleBillingManager.getInstance().createClient(this);
在onDestory()中释放连接
//结束连接
GoogleBillingManager.getInstance().endConn();
在页面点击某个按钮发起支付
billProxy.onQuerySkuDetailsAsync(billingListenerImpl,BillingClient.ProductType.INAPP,infoBean.getOut_trade_goodsid());
第一个参数:billingListenerImpl: 支付监听回调
第二个参数: BillingClient.ProductType.INAPP 你后台如果是一次性支付传此参数, 如果是订阅产品就传BillingClient.ProductType.SUBS
第三个参数: 在谷歌后台创建产品对应的ID
onQuerySkuDetailsAsync方法实现如下:
public void onQuerySkuDetailsAsync(GoogleBillingListener billingListener, String productType, String... productIds) {
if (null == productIds || productIds.length == 0
|| !GoogleBillingManager.getInstance().isReady()
) {
Log.d(TAG,"onQuerySkuDetailsAsync.return.isReady : "+GoogleBillingManager.getInstance().isReady());
return;
}
List<QueryProductDetailsParams.Product> skuList = new ArrayList<>();
for (String productId : productIds) {
QueryProductDetailsParams.Product product = QueryProductDetailsParams
.Product.newBuilder()
.setProductId(productId)
.setProductType(productType)
.build();
//添加对应的 产品id 去查询详情
skuList.add(product);
Log.d(TAG, "onQuerySkuDetailsAsync.productId : "+productId+" , productType : "+productType);
}
QueryProductDetailsParams params = QueryProductDetailsParams
.newBuilder()
.setProductList(skuList)
.build();
GoogleBillingManager.getInstance().getBillingClient().queryProductDetailsAsync(params, (billingResult, list) -> {
Log.d(TAG, "queryProductDetailsAsync.getDebugMessage : "+billingResult.getDebugMessage()) ;
if (BillingClient.BillingResponseCode.OK == billingResult.getResponseCode()) {
if (null != billingListener) {
billingListener.onProductDetailsSus(list);
}
} else {
Log.e("TAG", "code : " + billingResult.getResponseCode() + " message : " + billingResult.getDebugMessage());
}
});
}
以上函数根据ID查询谷歌产品是否存在, 存在的话就调用Google Pay拉起支付
//响应code 码
BillingResult billingResult = GoogleBillingManager.getInstance().getBillingClient().launchBillingFlow(activity, billingFlowParams);
执行到这里, 手机APP会拉起Google Pay的支付页面, 当用户完成支付后回调GoogleBillingListener的onProductDetailsSus方法.
public void onPurchasesUpdated(BillingResult billingResult, List<Purchase> purchases) {
在这里我们调用谷歌支付核心订单
//核销订单
billProxy.onConsumeAsync(this, purchase);
同时调用后台接口, 通知后台用户购买此商品支付成功.