- 先集成google支付插件
//新旧版版本获取方式跟调起支付方式不同
//旧版本
implementation "com.android.billingclient:billing:4.0.0"
//新版本要接入最新的,比如:
implementation "com.android.billingclient:billing:6.0.0"
- 在Application进行初始化连接谷歌,判断是否接通谷歌,只有接通谷歌才能做后面操作
//初始化
BillingClient billingClient = BillingClient.newBuilder(this)
.setListener(purchasesUpdatedListener)
.enablePendingPurchases()
.build();
if (!billingClient.isReady()) {
billingClient.startConnection(new BillingClientStateListener() {
@Override
public void onBillingServiceDisconnected() {
}
@Override
public void onBillingSetupFinished(@NonNull BillingResult billingResult) {
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
//这里代表接通谷歌成功
}
}
});
}
3.获取谷歌商品信息,获取商品信息,主要分应用内商品,订阅,传入产品id获取数据
BillingClient.SkuType.SUBS 代表订阅商品,BillingClient.SkuType.INAPP,应用内商品,具体看在谷歌后台创建什么商品
//5.0.0以下版本
List<String> skuList = new ArrayList<>();
skuList.add("test1"); //test1 代表产品id,具体产品去后谷歌后台查看id
SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder();
params.setSkusList(skuList).setType(BillingClient.SkuType.SUBS);
//
billingClient.querySkuDetailsAsync(params.build(),new SkuDetailsResponseListener() {
@Override
public void onSkuDetailsResponse(BillingResult billingResult,
List<SkuDetails> skuDetailsList) {
if(billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK){
}
}
});
}
//5.0.0(包含)以上版本
List<String> skuList = new ArrayList<>();
skuList.add("test1"); //test1 代表产品id,具体产品去后谷歌后台查看id
List<QueryProductDetailsParams.Product> productList = new ArrayList<>();
for (String sku: skuList) {
productList.add(QueryProductDetailsParams.Product.newBuilder()
.setProductId(sku)
.setProductType(BillingClient.ProductType.SUBS)
.build());
}
QueryProductDetailsParams queryProductDetailsParams =
QueryProductDetailsParams.newBuilder()
.setProductList(productList)
.build();
DemoApplication.billingClient.queryProductDetailsAsync(
queryProductDetailsParams,
new ProductDetailsResponseListener() {
public void onProductDetailsResponse(BillingResult billingResult,
List<ProductDetails> productDetailsList) {
if(billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK){
}
}
}
);
- 调起支付 界面
//5.0.0以下版本
BillingFlowParams billingFlowParams = BillingFlowParams.newBuilder()
.setSkuDetails(skuDetails)
.build();
int responseCode = MyApplication.billingClient.launchBillingFlow(
GooglePayActivity.this, billingFlowParams).getResponseCode();
if (responseCode == 0){
// 调起成功
} else{
// 调起失败
}
//5.0.0(包含)以上版本
ImmutableList productDetailsParamsList =
ImmutableList.of(
BillingFlowParams.ProductDetailsParams.newBuilder()
// retrieve a value for "productDetails" by calling queryProductDetailsAsync()
.setProductDetails(skuDetail)
// to get an offer token, call ProductDetails.getSubscriptionOfferDetails()
// for a list of offers that are available to the user
.setOfferToken(skuDetail.getSubscriptionOfferDetails().get(0).getOfferToken())
.build()
);
BillingFlowParams billingFlowParams = BillingFlowParams.newBuilder()
.setProductDetailsParamsList(productDetailsParamsList)
.build();
// Launch the billing flow
BillingResult billingResult = DemoApplication.billingClient.launchBillingFlow( MemberActivity.this, billingFlowParams);
- 监听用户是否支付
private PurchasesUpdatedListener purchasesUpdatedListener = new PurchasesUpdatedListener() {
@Override
public void onPurchasesUpdated(BillingResult billingResult, List<Purchase> purchases) {
// To be implemented in a later section.);
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK
&& purchases != null) {
for (Purchase purchase : purchases) {
//完成支付
handlePurchase(purchase);
}
} else if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.USER_CANCELED) {
// 用户取消
} else {
// 其他错误
}
}
};
6.用户支付完成,要进行验证,否则测试环境5分钟会自动退单退款,真实环境3天自动退单退款
if (purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED) {
if(!purchase.isAcknowledged()){
//Toast.makeText(MyApplication.this,"支付成功:非消耗类" ,Toast.LENGTH_SHORT).show();
AcknowledgePurchaseParams acknowledgePurchaseParams =
AcknowledgePurchaseParams.newBuilder()
.setPurchaseToken(purchase.getPurchaseToken())
.build();
billingClient.acknowledgePurchase(acknowledgePurchaseParams,acknowledgePurchaseResponseListener);
} else{
//Toast.makeText(MyApplication.this,"支付成功:消耗类" ,Toast.LENGTH_SHORT).show();
ConsumeParams consumeParams =
ConsumeParams.newBuilder()
.setPurchaseToken(purchase.getPurchaseToken())
.build();
billingClient.consumeAsync(consumeParams, listener);
}
//这里可以通知自己app服务器进行相应处理
}
到这里就完成!!!