1、添加依赖项
在项目的build.gradle文件中添加以下依赖项:
dependencies {
implementation 'com.android.billingclient:billing:5.0.0'
}
从 Google Play 结算库版本 4 迁移到版本 5 的迁移指南
2、初始化google请求
/**
* 初始化google play
*/
public static void initPlay(Activity mActivity) {
activity = mActivity;
googleBillingUtil = GoogleBillingUtil.getInstance()
// 注意,监听器设置是可选的,视个人需求设置。
.setOnPurchaseFinishedListener(mOnPurchaseFinishedListener)//购买回调接口
.setOnQueryFinishedListener(mOnQueryFinishedListener) //查询回调接口
.setOnStartSetupFinishedListener(mOnStartSetupFinishedListener) //启动结果回调接口
.build(activity);
}
public static GoogleBillingUtil getInstance() {
cleanListener();
return mGoogleBillingUtil;
}
/**
* google play初始化
*
* @param context
* @return
*/
public GoogleBillingUtil build(Context context) {
if (mBillingClient == null) {
synchronized (mGoogleBillingUtil) {
if (mBillingClient == null) {
//检测GooglePlay服务是否可用
if (isGooglePlayServicesAvailable(context)) {
builder = BillingClient.newBuilder(context);
mBillingClient = builder.enablePendingPurchases().setListener(mGoogleBillingUtil.new MyPurchasesUpdatedListener()).build();
} else {
if (IS_DEBUG) {
log("警告:GooglePlay服务处于不可用状态,请检查");
}
if (mOnStartSetupFinishedListener != null) {
mOnStartSetupFinishedListener.onSetupError();
}
}
} else {
//Google购买商品回调接口(订阅和内购都走这个接口)
builder.setListener(mGoogleBillingUtil.new MyPurchasesUpdatedListener());
}
}
} else {
//Google购买商品回调接口(订阅和内购都走这个接口)
builder.setListener(mGoogleBillingUtil.new MyPurchasesUpdatedListener());
}
//请求连接到GooglePay
synchronized (mGoogleBillingUtil) {
if (mGoogleBillingUtil.startConnection()) {
}
}
return mGoogleBillingUtil;
}
/**
* 请求连接到GooglePay
*
* @return
*/
public boolean startConnection() {
if (mBillingClient == null) {
MyToash.Log("pay", "初始化失败:mBillingClient==null");
return false;
}
MyToash.Log("pay", "--mBillingClient.isReady()-=" + mBillingClient.isReady());
if (!mBillingClient.isReady()) {
mBillingClient.startConnection(new BillingClientStateListener() {
@Override
public void onBillingSetupFinished(BillingResult billingResult) {
// MyToash.Log("van--", "--mBillingClient.startConnection-=" + billingResult.getResponseCode() + "===" + billingResult.getDebugMessage());
//连接到GooglePay成功
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
if (mOnStartSetupFinishedListener != null) {
mOnStartSetupFinishedListener.onSetupSuccess();
}
} else {
MyToash.Log("pay", "初始化失败:onSetupFail:code=" + billingResult.getResponseCode());
if (mOnStartSetupFinishedListener != null) {
mOnStartSetupFinishedListener.onSetupFail(billingResult.getResponseCode());
}
}
}
@Override
public void onBillingServiceDisconnected() {
if (mOnStartSetupFinishedListener != null) {
mOnStartSetupFinishedListener.onSetupError();
}
//log("初始化失败:onBillingServiceDisconnected");
MyToash.Log("pay", "初始化失败:onBillingServiceDisconnected");
}
});
return false;
} else {
return true;
}
}
3、调用google方法,查询到要购买的商品,去弹出支付弹窗
//购买APP内部商品
purchase(activity, skuId, BillingClient.SkuType.INAPP);
//订阅APP商品
purchase(activity, skuId, BillingClient.SkuType.SUBS);
/**
* 发起google play支付
*
* @param activity
* @param skuId
* @param skuType
*/
private void purchase(Activity activity, final String skuId, final String skuType) {
if (mBillingClient == null) {
if (mOnPurchaseFinishedListener != null) {
mOnPurchaseFinishedListener.onPurchaseError();
MyToash.Log("pay", "-purchase-onPurchaseError");
}
return;
}
if (startConnection()) {
MyToash.Log("pay", "-开始调用google查询商品接口");
//新版本
ImmutableList<QueryProductDetailsParams.Product> productList = ImmutableList.of(QueryProductDetailsParams.Product.newBuilder()
.setProductId(skuId)
.setProductType(skuType)
.build());
//组装参数
QueryProductDetailsParams params = QueryProductDetailsParams.newBuilder()
.setProductList(productList)
.build();
//查询参数
mBillingClient.queryProductDetailsAsync(params, new ProductDetailsResponseListener() {
@Override
public void onProductDetailsResponse(@NonNull BillingResult billingResult, @NonNull List<ProductDetails> list) {
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK && list != null) {
MyToash.Log("pay", "skuDetailsList---=" + list.toString());
for (int i = 0; i < list.size(); i++) {
ProductDetails productDetailsBean = list.get(i);
String sku = productDetailsBean.getProductId();//商品id
MyToash.Log("pay", "skuId---=" + skuId + " sku: " + sku);
if (skuId.equals(sku)) {
MyToash.Log("pay", "-开始调用google支付弹窗");
ImmutableList<BillingFlowParams.ProductDetailsParams> productDetailsParamsList = null;
if (skuType.equals(BillingClient.SkuType.SUBS)) {
//订阅
String offerToken = productDetailsBean
.getSubscriptionOfferDetails()
.get(0)//这是一个数组,要选择合适的优惠方式,我默认选择了第一个
.getOfferToken();
// MyToash.Log("pay", "offerToken---=" + offerToken);
productDetailsParamsList =
ImmutableList.of(
BillingFlowParams.ProductDetailsParams.newBuilder()
.setProductDetails(productDetailsBean)
.setOfferToken(offerToken)
.build()
);
} else {
//非订阅
productDetailsParamsList =
ImmutableList.of(
BillingFlowParams.ProductDetailsParams.newBuilder()
.setProductDetails(productDetailsBean)
.build()
);
}
BillingFlowParams billingFlowParams = BillingFlowParams.newBuilder()
.setProductDetailsParamsList(productDetailsParamsList)
.build();
//调用支付弹窗
mBillingClient.launchBillingFlow(activity, billingFlowParams);
}
}
} else {
MyToash.Log("pay", "goods search fail");
}
}
});
} else {
if (mOnPurchaseFinishedListener != null) {
MyToash.Log("pay", "---google连接失败");
mOnPurchaseFinishedListener.onPurchaseError();
}
}
}
4、消耗普通商品
/**
* 公共消费商品接口
*/
public void getConsumeGoods(Activity activity, Purchase purchase, String price) {
if (purchase != null) {
MyToash.Log("pay", "-----开始消耗商品:" + purchase.toString());
//本地发送书券成功之后,调用消耗接口
ConsumeParams.Builder consumeParams = ConsumeParams.newBuilder();
consumeParams.setPurchaseToken(purchase.getPurchaseToken());
//调用消耗商品方法
consumeAsync(activity, consumeParams.build(), purchase, price);
}
}
public void consumeAsync(Activity activity, ConsumeParams consumeParams, Purchase purchase, String price) {
if (mBillingClient == null) {
return;
}
//本地记录消费的商品
saveRecordLoaclConsumeGoods(activity, purchase, 1);
mBillingClient.consumeAsync(consumeParams, new ConsumeResponseListener() {
@Override
public void onConsumeResponse(@NonNull BillingResult billingResult, @NonNull String token) {
// MyToash.Log("van---", "--consumeAsync--=" + billingResult.toString() + "---" + token);
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
// ----------- 沙盒代码 - 正式环境要上报-------- -
try {
String pId = purchase.getSkus().get(0);
String currentPrice = "0";
if (TextUtils.isEmpty(price)) {
currentPrice = CommonOkHttpUtils.INSTANCE.getPayPrice(activity, pId);
MyToash.Log("pay", "本地价格-----消耗商品成功 " + "价格:" + currentPrice);
} else {
currentPrice = price;
MyToash.Log("pay", "传递过来的价格-----消耗商品成功 " + "价格:" + currentPrice);
}
AdjustEvent event = new AdjustEvent(Constant.MD_ADJUST_czcg);
double upPrice = Double.parseDouble(currentPrice);
event.setRevenue(upPrice, "USD");
Adjust.trackEvent(event);
} catch (Exception e) {
e.printStackTrace();
MyToash.Log("pay", "异常处理-----e:" + e.toString());
}
//请求本地接口
getHttpPay(activity, purchase);
//移除
saveRecordLoaclConsumeGoods(activity, purchase, 2);
} else {
MyToash.Log("pay", "-----消耗商品失败" + billingResult.toString() + "---" + token + "--code:" + billingResult.getResponseCode());
}
}
});
}
5、消耗订阅商品
/**
* 公共订阅方法消费
*/
public void getConsumeSubscribeGoods(Activity activity, Purchase purchase, String price){
if (purchase != null) {
if (purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED) {
if (!purchase.isAcknowledged()) {
AcknowledgePurchaseParams acknowledgePurchaseParams =
AcknowledgePurchaseParams.newBuilder()
.setPurchaseToken(purchase.getPurchaseToken())
.build();
mBillingClient.acknowledgePurchase(acknowledgePurchaseParams, new AcknowledgePurchaseResponseListener(){
@Override
public void onAcknowledgePurchaseResponse(@NonNull BillingResult billingResult) {
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
MyToash.Log("pay","-------消费订阅商品成功");
//请求本地接口
getHttpPay(activity, purchase);
}
}
});
}
}
}
}
6、查询未完成的商品状态
老的方法:
/**
* 查询失败的订单
* skuType :,BillingClient.SkuType.INAPP; BillingClient.SkuType.SUBS
* 0:PurchaseState.UNSPECIFIED_STATE:未知状态
* 1:PurchaseState.PURCHASED:付款完成
* 2:PurchaseState.PENDING:购买正在等待付款完成。
*/
public void queryFailOrder(Activity activity, String skuType) {
MyToash.Log("pay", "-----查询需要补货的商品:");
if (mBillingClient != null) {
Purchase.PurchasesResult result = mBillingClient.queryPurchases(skuType);
if (BillingClient.BillingResponseCode.OK == result.getResponseCode()) {
for (Purchase purchase : result.getPurchasesList()) {
//PURCHASED --已购买
if (Purchase.PurchaseState.PURCHASED == purchase.getPurchaseState()) {
//MyToash.Log("pay", "----------需要补货的商品:");
//调用google去消费
getConsumeGoods(activity, purchase, "");
}
}
}
}
}
5.0新的方法:
/**
* 查询失败的订单
* skuType :,BillingClient.SkuType.INAPP; BillingClient.SkuType.SUBS
* 0:PurchaseState.UNSPECIFIED_STATE:未知状态
* 1:PurchaseState.PURCHASED:付款完成
* 2:PurchaseState.PENDING:购买正在等待付款完成。
*/
public void queryFailOrder(Activity activity, String skuType) {
MyToash.Log("pay", "-----查询需要补货的商品:");
if (mBillingClient != null) {
QueryPurchasesParams queryPurchasesParams = QueryPurchasesParams.newBuilder()
.setProductType(skuType)
.build();
mBillingClient.queryPurchasesAsync(queryPurchasesParams, new PurchasesResponseListener() {
@Override
public void onQueryPurchasesResponse(@NonNull BillingResult billingResult, @NonNull List<Purchase> list) {
if (list != null) {
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
for (Purchase purchase : list) {
//PURCHASED --已购买
if (Purchase.PurchaseState.PURCHASED == purchase.getPurchaseState()) {
//MyToash.Log("pay", "----------需要补货的商品:");
//调用google去消费
getConsumeGoods(activity, purchase, "");
}
}
}
}
}
});
}
}
7、完整代码
package com.youjiakeji.yjkjreader.pay;
import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.adjust.sdk.Adjust;
import com.adjust.sdk.AdjustEvent;
import com.android.billingclient.api.AcknowledgePurchaseParams;
import com.android.billingclient.api.AcknowledgePurchaseResponseListener;
import com.android.billingclient.api.BillingClient;
import com.android.billingclient.api.BillingClientStateListener;
import com.android.billingclient.api.BillingFlowParams;
import com.android.billingclient.api.BillingResult;
import com.android.billingclient.api.ConsumeParams;
import com.android.billingclient.api.ConsumeResponseListener;
import com.android.billingclient.api.ProductDetails;
import com.android.billingclient.api.ProductDetailsResponseListener;
import com.android.billingclient.api.Purchase;
import com.android.billingclient.api.PurchasesResponseListener;
import com.android.billingclient.api.PurchasesUpdatedListener;
import com.android.billingclient.api.QueryProductDetailsParams;
import com.android.billingclient.api.QueryPurchasesParams;
import com.android.billingclient.api.SkuDetails;
import com.android.billingclient.api.SkuDetailsParams;
import com.android.billingclient.api.SkuDetailsResponseListener;
import com.google.common.collect.ImmutableList;
import com.youjiakeji.yjkjreader.base.BWNApplication;
import com.youjiakeji.yjkjreader.base.BaseConfig;
import com.youjiakeji.yjkjreader.base.BaseKey;
import com.youjiakeji.yjkjreader.constant.Api;
import com.youjiakeji.yjkjreader.constant.CommonConstantUtils;
import com.youjiakeji.yjkjreader.constant.Constant;
import com.youjiakeji.yjkjreader.eventbus.RefreshMine;
import com.youjiakeji.yjkjreader.eventbus.RefreshRead;
import com.youjiakeji.yjkjreader.kotlin.utils.CommonPayUtilsKt;
import com.youjiakeji.yjkjreader.model.OrderFailBean;
import com.youjiakeji.yjkjreader.net.CommonOkHttpUtils;
import com.youjiakeji.yjkjreader.net.HttpUtils;
import com.youjiakeji.yjkjreader.net.ReaderParams;
import com.youjiakeji.yjkjreader.ui.dialog.PublicPurchaseDialog;
import com.youjiakeji.yjkjreader.ui.dialog.WaitDialogUtils;
import com.youjiakeji.yjkjreader.ui.utils.MyToash;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.youjiakeji.yjkjreader.ui.utils.MmkvUtils;
import com.youjiakeji.yjkjreader.utils.GsonUtil;
import com.youjiakeji.yjkjreader.utils.ShareUitls;
import com.youjiakeji.yjkjreader.utils.UserUtils;
import org.greenrobot.eventbus.EventBus;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
/**
* Created by TJbaobao on 2017/11/2.
* CSDN:http://blog.csdn.net/u013640004/article/details/78257536
* <p>
* 当前版本:V1.1.7
* 更新日志:
* <p>
* v1.1.7 2019/01/05
*
* <p>
* v1.1.6 2018/09/05
* 去掉我项目了的BaseApplication.getContext()的方法,初始现在需要传入一个Context,可以使用Application的Context
* 对isGooglePlayServicesAvailable方法进行了说明,因为这个方法是要导入一个包才能使用的。
* --> api "com.google.android.gms:play-services-location:11.8.0"
* <p>
* v1.1.5 2018/07/13
* 优化-尽可能处理了一些可能造成的内存泄漏的问题。
* 修改-查询成功接口增加一个String skuType,参数,各位在查询的时候需要判断skuType
* 增加-增加两处接口为Null的Log提示,tag为GoogleBillingUtil。
* <p>
* V1.1.4 2018/01/03
* 修改-实现单例模式,避免多实例导致的谷歌接口回调错乱问题。
* <p>
* V1.1.3 2017/12/19
* 修复-服务启动失败时导致的空指针错误。
* <p>
* V1.1.2 2017/12/18
* 修复-修复内购未被消耗的BUG。
* 增加-每次启动都获取一次历史内购订单,并且全部消耗。
* 增加-可以通过设置isAutoConsumeAsync来确定内购是否每次自动消耗。
* 增加-将consumeAsync改为public,你可以手动调用消耗。
* <p>
* V1.1.1 2017/11/2
* 特性-不需要key了,不需要IInAppBillingService.aidl了,不需要那一大堆Utils了,创建新实例的时候必须要传入购买回调接口。
* <p>
* V1.0.3 2017/10/27
* 增加-支持内购
* <p>
* V1.0.2 2017/09/11
* 修复-修复BUG
* <p>
* v1.0.1 2017/07/29
* 初始版本
*/
@SuppressWarnings("ALL")
public class GoogleBillingUtil {
private static final String TAG = "GoogleBillingUtil";
private static final boolean IS_DEBUG = true;
public static final String BILLING_TYPE_INAPP = BillingClient.SkuType.INAPP;//内购
public static final String BILLING_TYPE_SUBS = BillingClient.SkuType.SUBS;//订阅
private static BillingClient mBillingClient;
private static BillingClient.Builder builder;
private static OnPurchaseFinishedListener mOnPurchaseFinishedListener;//购买回调接口
private static OnStartSetupFinishedListener mOnStartSetupFinishedListener;//启动结果回调接口
private static OnQueryFinishedListener mOnQueryFinishedListener; //查询回调接口
private static OnConsumeResponseListener mOnConsumeResponseListener; //消耗商品接口
private boolean isAutoConsumeAsync = true;//是否在购买成功后自动消耗商品
private static final GoogleBillingUtil mGoogleBillingUtil = new GoogleBillingUtil();
private GoogleBillingUtil() {
}
/**
* 设置skus
*
* @param inAppSKUS 内购id
* @param subsSKUS 订阅id
*/
public static void setSkus(@Nullable String[] inAppSKUS, @Nullable String[] subsSKUS) {
/* if (inAppSKUS != null) {
GoogleBillingUtil.inAppSKUS = inAppSKUS;
}
if (subsSKUS != null) {
GoogleBillingUtil.subsSKUS = subsSKUS;
}*/
}
public static GoogleBillingUtil getInstance() {
cleanListener();
return mGoogleBillingUtil;
}
/**
* google play初始化
*
* @param context
* @return
*/
public GoogleBillingUtil build(Context context) {
if (mBillingClient == null) {
synchronized (mGoogleBillingUtil) {
if (mBillingClient == null) {
//检测GooglePlay服务是否可用
if (isGooglePlayServicesAvailable(context)) {
builder = BillingClient.newBuilder(context);
mBillingClient = builder.enablePendingPurchases().setListener(mGoogleBillingUtil.new MyPurchasesUpdatedListener()).build();
} else {
if (IS_DEBUG) {
log("警告:GooglePlay服务处于不可用状态,请检查");
}
if (mOnStartSetupFinishedListener != null) {
mOnStartSetupFinishedListener.onSetupError();
}
}
} else {
//Google购买商品回调接口(订阅和内购都走这个接口)
builder.setListener(mGoogleBillingUtil.new MyPurchasesUpdatedListener());
}
}
} else {
//Google购买商品回调接口(订阅和内购都走这个接口)
builder.setListener(mGoogleBillingUtil.new MyPurchasesUpdatedListener());
}
//请求连接到GooglePay
synchronized (mGoogleBillingUtil) {
if (mGoogleBillingUtil.startConnection()) {
}
}
return mGoogleBillingUtil;
}
/**
* 请求连接到GooglePay
*
* @return
*/
public boolean startConnection() {
if (mBillingClient == null) {
MyToash.Log("pay", "初始化失败:mBillingClient==null");
return false;
}
MyToash.Log("pay", "--mBillingClient.isReady()-=" + mBillingClient.isReady());
if (!mBillingClient.isReady()) {
mBillingClient.startConnection(new BillingClientStateListener() {
@Override
public void onBillingSetupFinished(BillingResult billingResult) {
// MyToash.Log("van--", "--mBillingClient.startConnection-=" + billingResult.getResponseCode() + "===" + billingResult.getDebugMessage());
//连接到GooglePay成功
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
if (mOnStartSetupFinishedListener != null) {
mOnStartSetupFinishedListener.onSetupSuccess();
}
} else {
MyToash.Log("pay", "初始化失败:onSetupFail:code=" + billingResult.getResponseCode());
if (mOnStartSetupFinishedListener != null) {
mOnStartSetupFinishedListener.onSetupFail(billingResult.getResponseCode());
}
}
}
@Override
public void onBillingServiceDisconnected() {
if (mOnStartSetupFinishedListener != null) {
mOnStartSetupFinishedListener.onSetupError();
}
//log("初始化失败:onBillingServiceDisconnected");
MyToash.Log("pay", "初始化失败:onBillingServiceDisconnected");
}
});
return false;
} else {
return true;
}
}
/**
* getResponseCode值:
* <p>
* Google购买商品回调接口(订阅和内购都走这个接口)
* int SERVICE_TIMEOUT = -3;//服务超时
* int FEATURE_NOT_SUPPORTED = -2;//不支持功能
* int SERVICE_DISCONNECTED = -1;//服务单元已断开
* int OK = 0;//成功
* int USER_CANCELED = 1;//用户按上一步或取消对话框
* int SERVICE_UNAVAILABLE = 2;//网络连接断开
* int BILLING_UNAVAILABLE = 3;//所请求的类型不支持 Google Play 结算服务 AIDL 版本
* int ITEM_UNAVAILABLE = 4;//请求的商品已不再出售。
* int DEVELOPER_ERROR = 5;//提供给 API 的参数无效。此错误也可能说明应用未针对结算服务正确签名或设置,或者在其清单中缺少必要的权限。
* int ERROR = 6;//API 操作期间出现严重错误
* int ITEM_ALREADY_OWNED = 7;//未能购买,因为已经拥有此商品
* int ITEM_NOT_OWNED = 8;//未能消费,因为尚未拥有此商品
*/
private class MyPurchasesUpdatedListener implements PurchasesUpdatedListener {
@Override
public void onPurchasesUpdated(BillingResult billingResult, @Nullable List<Purchase> list) {
MyToash.Log("pay", "--MyPurchasesUpdatedListener--调用本地接口");
//支付成功
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK && list != null && !list.isEmpty()) {
MyToash.Log("pay", "--onPurchasesUpdated--=" + list.size());
//回调支付成功
if (mOnPurchaseFinishedListener != null) {
//消耗商品
Purchase purchase = list.get(0);
mOnPurchaseFinishedListener.onPurchaseSuccess(purchase);
}
} else {
MyToash.Log("pay", "--onPurchasesUpdatedFail--=" + billingResult.getResponseCode());
mOnPurchaseFinishedListener.onPurchaseFail(billingResult.getResponseCode());
}
}
}
/**
* 查询内购商品信息
*/
public void queryInventoryInApp() {
queryInventory(BillingClient.SkuType.INAPP);
}
/**
* 查询订阅商品信息
*/
public void queryInventorySubs() {
queryInventory(BillingClient.SkuType.SUBS);
}
private void queryInventory(final String skuType) {
/* Runnable runnable = new Runnable() {
@Override
public void run() {
if (mBillingClient == null) {
if (mOnQueryFinishedListener != null) {
mOnQueryFinishedListener.onQueryError();
}
return;
}
ArrayList<String> skuList = new ArrayList<>();
if (skuType.equals(BillingClient.SkuType.INAPP)) {
Collections.addAll(skuList, inAppSKUS);
} else if (skuType.equals(BillingClient.SkuType.SUBS)) {
Collections.addAll(skuList, subsSKUS);
}
SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder();
params.setSkusList(skuList).setType(skuType);
mBillingClient.querySkuDetailsAsync(params.build(), new MySkuDetailsResponseListener(skuType));
}
};
executeServiceRequest(runnable);*/
}
/**
* Google查询商品信息回调接口
*/
private class MySkuDetailsResponseListener implements SkuDetailsResponseListener {
private String skuType;
public MySkuDetailsResponseListener(String skuType) {
this.skuType = skuType;
}
@Override
public void onSkuDetailsResponse(BillingResult billingResult, List<SkuDetails> list) {
if (mOnQueryFinishedListener == null) {
if (IS_DEBUG) {
log("警告:接收到查询商品回调,但查询商品接口为Null,请设置购买接口。eg:setOnQueryFinishedListener()");
}
return;
}
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK && list != null) {
mOnQueryFinishedListener.onQuerySuccess(skuType, list);
} else {
mOnQueryFinishedListener.onQueryFail(billingResult.getResponseCode());
}
}
}
/**
* 发起内购
*
* @param activity
* @param skuId:googleID
* @param goods_id:商品ID
*/
public void purchaseInApp(Activity activity, String skuId, String goods_id) {
//创建本地订单
WaitDialogUtils.showDialog(activity);
getCreateOrder(activity, goods_id, "1", new CallCheckOrder() {
@Override
public void call(String result) {
if (!TextUtils.isEmpty(result)) {
//初次必须赋值
GooglePayCommonUtils.CURRENTPAYMODE = BillingClient.SkuType.INAPP;
purchase(activity, skuId, BillingClient.SkuType.INAPP);
}
}
});
}
/**
* 发起订阅
*
* @param skuId
* @return
*/
public void purchaseSubs(Activity activity, String skuId, String goods_id) {
//创建本地订单
WaitDialogUtils.showDialog(activity);
//初次必须赋值
GooglePayCommonUtils.CURRENTPAYMODE = BillingClient.SkuType.SUBS;
purchase(activity, skuId, BillingClient.SkuType.SUBS);
}
/**
* 发起google play支付
*
* @param activity
* @param skuId
* @param skuType
*/
private void purchase(Activity activity, final String skuId, final String skuType) {
if (mBillingClient == null) {
if (mOnPurchaseFinishedListener != null) {
mOnPurchaseFinishedListener.onPurchaseError();
MyToash.Log("pay", "-purchase-onPurchaseError");
}
return;
}
if (startConnection()) {
MyToash.Log("pay", "-开始调用google查询商品接口");
//新版本
ImmutableList<QueryProductDetailsParams.Product> productList = ImmutableList.of(QueryProductDetailsParams.Product.newBuilder()
.setProductId(skuId)
.setProductType(skuType)
.build());
//组装参数
QueryProductDetailsParams params = QueryProductDetailsParams.newBuilder()
.setProductList(productList)
.build();
//查询参数
mBillingClient.queryProductDetailsAsync(params, new ProductDetailsResponseListener() {
@Override
public void onProductDetailsResponse(@NonNull BillingResult billingResult, @NonNull List<ProductDetails> list) {
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK && list != null) {
MyToash.Log("pay", "skuDetailsList---=" + list.toString());
for (int i = 0; i < list.size(); i++) {
ProductDetails productDetailsBean = list.get(i);
String sku = productDetailsBean.getProductId();//商品id
MyToash.Log("pay", "skuId---=" + skuId + " sku: " + sku);
if (skuId.equals(sku)) {
MyToash.Log("pay", "-开始调用google支付弹窗");
ImmutableList<BillingFlowParams.ProductDetailsParams> productDetailsParamsList = null;
if (skuType.equals(BillingClient.SkuType.SUBS)) {
//订阅
String offerToken = productDetailsBean
.getSubscriptionOfferDetails()
.get(0)//这是一个数组,要选择合适的优惠方式,我默认选择了第一个
.getOfferToken();
// MyToash.Log("pay", "offerToken---=" + offerToken);
productDetailsParamsList =
ImmutableList.of(
BillingFlowParams.ProductDetailsParams.newBuilder()
.setProductDetails(productDetailsBean)
.setOfferToken(offerToken)
.build()
);
} else {
//非订阅
productDetailsParamsList =
ImmutableList.of(
BillingFlowParams.ProductDetailsParams.newBuilder()
.setProductDetails(productDetailsBean)
.build()
);
}
BillingFlowParams billingFlowParams = BillingFlowParams.newBuilder()
.setProductDetailsParamsList(productDetailsParamsList)
.build();
//调用支付弹窗
mBillingClient.launchBillingFlow(activity, billingFlowParams);
}
}
} else {
MyToash.Log("pay", "goods search fail");
WaitDialogUtils.dismissDialog();
}
}
});
} else {
if (mOnPurchaseFinishedListener != null) {
MyToash.Log("pay", "---google连接失败");
mOnPurchaseFinishedListener.onPurchaseError();
WaitDialogUtils.dismissDialog();
}
}
}
/**
* 消耗商品
*
* @param purchaseToken int SERVICE_TIMEOUT = -3; //服务超时
* int FEATURE_NOT_SUPPORTED = -2; //不支持功能
* int SERVICE_DISCONNECTED = -1; //服务单元已断开
* int OK = 0; //成功
* int USER_CANCELED = 1; //用户按上一步或取消对话框
* int SERVICE_UNAVAILABLE = 2; //网络连接断开
* int BILLING_UNAVAILABLE = 3; //所请求的类型不支持 Google Play 结算服务 AIDL 版本
* int ITEM_UNAVAILABLE = 4; //请求的商品已不再出售。
* int DEVELOPER_ERROR = 5; //提供给 API 的参数无效。此错误也可能说明应用未针对结算服务正确签名或设置,或者在其清单中缺少必要的权限。
* int ERROR = 6; //API 操作期间出现严重错误
* int ITEM_ALREADY_OWNED = 7; //未能购买,因为已经拥有此商品
* int ITEM_NOT_OWNED = 8; //未能消费,因为尚未拥有此商品
*/
public void consumeAsync(Activity activity, ConsumeParams consumeParams, Purchase purchase, String price) {
if (mBillingClient == null) {
return;
}
//本地记录消费的商品
saveRecordLoaclConsumeGoods(activity, purchase, 1, BillingClient.ProductType.INAPP);
mBillingClient.consumeAsync(consumeParams, new ConsumeResponseListener() {
@Override
public void onConsumeResponse(@NonNull BillingResult billingResult, @NonNull String token) {
// MyToash.Log("van---", "--consumeAsync--=" + billingResult.toString() + "---" + token);
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
// ----------- 沙盒代码 - 正式环境要上报-------- -
try {
String pId = purchase.getSkus().get(0);
String currentPrice = "0";
if (TextUtils.isEmpty(price)) {
currentPrice = CommonOkHttpUtils.INSTANCE.getPayPrice(activity, pId);
MyToash.Log("pay", "本地价格-----消耗商品成功 " + "价格:" + currentPrice);
} else {
currentPrice = price;
MyToash.Log("pay", "传递过来的价格-----消耗商品成功 " + "价格:" + currentPrice);
}
AdjustEvent event = new AdjustEvent(Constant.MD_ADJUST_czcg);
double upPrice = Double.parseDouble(currentPrice);
event.setRevenue(upPrice, "USD");
Adjust.trackEvent(event);
} catch (Exception e) {
e.printStackTrace();
MyToash.Log("pay", "异常处理-----e:" + e.toString());
}
//请求本地接口
getHttpPay(activity, purchase, BillingClient.ProductType.INAPP);
//移除
saveRecordLoaclConsumeGoods(activity, purchase, 2, BillingClient.ProductType.INAPP);
} else {
WaitDialogUtils.dismissDialog();
MyToash.Log("pay", "-----消耗商品失败" + billingResult.toString() + "---" + token + "--code:" + billingResult.getResponseCode());
}
}
});
}
/**
* 记录本地消费的商品
*
* @param activity
* @param purchase
* @param type: 1:是保存;2:移除
*/
private void saveRecordLoaclConsumeGoods(Activity activity, Purchase purchase, int type, String skuType) {
try {
if (purchase != null) {
String uid_key = UserUtils.getUID() + CommonConstantUtils.CONSUME;
List<String> list = new ArrayList<>();
//获取存储的值
String lastJson = MmkvUtils.decodeString(uid_key);
String pId = purchase.getSkus().get(0);
String purchaseToken = purchase.getPurchaseToken();
String packageName = purchase.getPackageName();
//键值对唯一的key
String onlyKey = purchaseToken + CommonConstantUtils.CONSUME;
if (type == 1) { //保存
//不是空
if (!lastJson.isEmpty()) {
List<String> list2 = GsonUtil.GsonToList(lastJson, String.class);
for (int i = 0; i < list2.size(); i++) {
String data = list2.get(i);
OrderFailBean bean = GsonUtil.GsonToBean(data, OrderFailBean.class);
String tokenKey = bean.getPurchaseToken() + CommonConstantUtils.CONSUME;
//是否要保存一条数据
if (!tokenKey.equals(onlyKey)) {
list.add(data);
}
}
}
//没有相同单据,保存
HashMap map = new HashMap();
map.put("pId", pId);
map.put("purchaseToken", purchaseToken);
map.put("packageName", packageName);
map.put("skuType", skuType);
map.put("time", CommonPayUtilsKt.isSaveCurrentTime());
list.add(GsonUtil.BeanToJson(map));
String json = GsonUtil.BeanToJson(list);
//保存上传失败的订单
MmkvUtils.encode(uid_key, json);
MyToash.Log("pay", "--保存消耗的商品json:" + purchaseToken);
// MyToash.Log("pay", "--保存消耗的商品json:" + json);
} else if (type == 2) { //移除
//不是空
if (!lastJson.isEmpty()) {
int currentIndex = -1;
List<String> list3 = GsonUtil.GsonToList(lastJson, String.class);
for (int i = 0; i < list3.size(); i++) {
String data = list3.get(i);
list.add(data);
OrderFailBean bean = GsonUtil.GsonToBean(data, OrderFailBean.class);
String tokenKey = bean.getPurchaseToken() + CommonConstantUtils.CONSUME;
if (tokenKey.equals(onlyKey)) {
currentIndex = i;
}
}
if (currentIndex != -1) {
list.remove(currentIndex);
String json = GsonUtil.BeanToJson(list);
//保存上传失败的订单
MmkvUtils.encode(uid_key, json);
MyToash.Log("pay", "--移除消耗的商品json:" + json);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 查询本地消费商品未返回数据
*
* @param type
* @param activity
* @param purchase
*/
public void checkRecordLoaclConsumeGoods(Activity activity) {
try {
String uid = UserUtils.getUID();
String uid_key = UserUtils.getUID() + CommonConstantUtils.CONSUME;
List<String> list = new ArrayList<>();
//获取存储的值
String lastJson = MmkvUtils.decodeString(uid_key);
MyToash.Log("pay", "--查询本地消费后商品未返回数据lastJson:" + lastJson);
if (!lastJson.isEmpty()) {
//不是空
list = GsonUtil.GsonToList(lastJson, String.class);
if (list != null && list.size() > 0) {
//上传
for (int i = 0; i < list.size(); i++) {
String data = list.get(i);
OrderFailBean bean = GsonUtil.GsonToBean(data, OrderFailBean.class);
String pId = bean.getpId();
long time = bean.getTime();
String purchaseToken = bean.getPurchaseToken();
String packageName = bean.getPackageName();
String skuType = bean.getSkuType();
MyToash.Log("pay", "--查询本地消费后商品未返回数据上传本地单据:" + purchaseToken);
if (CommonPayUtilsKt.isUploadLocalFailOrder(time)) {
//上传本地单据
UploadLocalFailOrder(activity, uid, pId, packageName, purchaseToken, skuType);
Thread.sleep(1000);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取已经内购的商品
*
* @return
*/
public List<Purchase> queryPurchasesInApp() {
return queryPurchases(BillingClient.SkuType.INAPP);
}
/**
* 获取已经订阅的商品
*
* @return
*/
public List<Purchase> queryPurchasesSubs() {
return queryPurchases(BillingClient.SkuType.SUBS);
}
/**
* 查询最近的购买交易
*
* @param skuType
* @return
*/
private List<Purchase> queryPurchases(String skuType) {
List<Purchase> purchaseList = new ArrayList<>();
if (mBillingClient == null) {
return null;
}
if (!mBillingClient.isReady()) {
//如果断开google连接,重新开始连接
startConnection();
} else {
QueryPurchasesParams queryPurchasesParams = QueryPurchasesParams.newBuilder()
.setProductType(skuType)
.build();
mBillingClient.queryPurchasesAsync(queryPurchasesParams, new PurchasesResponseListener() {
@Override
public void onQueryPurchasesResponse(@NonNull BillingResult billingResult, @NonNull List<Purchase> list) {
if (list != null) {
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
purchaseList.clear();
purchaseList.addAll(list);
}
}
}
});
return purchaseList;
// Purchase.PurchasesResult purchasesResult = mBillingClient.queryPurchases(skuType);
//
// if (purchasesResult != null) {
// if (purchasesResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
// List<Purchase> purchaseList = purchasesResult.getPurchasesList();
// /* if (isAutoConsumeAsync) {
// if (purchaseList != null) {
// for (Purchase purchase : purchaseList) {
// if (skuType.equals(purchase.getSku())) {
// ConsumeParams.Builder consumeParams = ConsumeParams.newBuilder();
// consumeParams.setPurchaseToken(purchase.getPurchaseToken());
// *//* consumeParams
// consumeParams.setDeveloperPayload(purchase.getDeveloperPayload());*//*
// consumeAsync(consumeParams.build(), purchase);
// }
// }
// }
// }*/
// return purchaseList;
// }
// }
}
return null;
}
/**
* 获取有效订阅的数量
*
* @return -1查询失败,0没有有效订阅,>0具有有效的订阅
*/
public int getPurchasesSizeSubs() {
List<Purchase> list = queryPurchasesSubs();
if (list != null) {
return list.size();
}
return -1;
}
/**
* 通过sku获取订阅商品序号
*
* @param sku
* @return
*/
public int getSubsPositionBySku(String sku) {
return getPositionBySku(sku, BillingClient.SkuType.SUBS);
}
/**
* 通过sku获取内购商品序号
*
* @param sku
* @return 成功返回需要 失败返回-1
*/
public int getInAppPositionBySku(String sku) {
return getPositionBySku(sku, BillingClient.SkuType.INAPP);
}
private int getPositionBySku(String sku, String skuType) {
/* if (skuType.equals(BillingClient.SkuType.INAPP)) {
int i = 0;
for (String s : inAppSKUS) {
if (s.equals(sku)) {
return i;
}
i++;
}
} else if (skuType.equals(BillingClient.SkuType.SUBS)) {
int i = 0;
for (String s : subsSKUS) {
if (s.equals(sku)) {
return i;
}
i++;
}
}*/
return -1;
}
private void executeServiceRequest(final Runnable runnable) {
if (startConnection()) {
runnable.run();
}
}
/**
* 通过序号获取订阅sku
*
* @param position
* @return
*/
public String getSubsSkuByPosition(int position) {
/* if (position >= 0 && position < subsSKUS.length) {
return subsSKUS[position];
} else {
return null;
}*/
return null;
}
/**
* 通过序号获取内购sku
*
* @param position
* @return
*/
public String getInAppSkuByPosition(int position) {
/* if (position >= 0 && position < inAppSKUS.length) {
return inAppSKUS[position];
} else {
return null;
}*/
return null;
}
/**
* 通过sku获取商品类型(订阅获取内购)
*
* @param sku
* @return inapp内购,subs订阅
*/
public String getSkuType(String sku) {
/* if (Arrays.asList(inAppSKUS).contains(sku)) {
return BillingClient.SkuType.INAPP;
} else if (Arrays.asList(subsSKUS).contains(sku)) {
return BillingClient.SkuType.SUBS;
}*/
return null;
}
/**
* 检测GooglePlay服务是否可用(需要导入包api "com.google.android.gms:play-services-location:11.8.0",也可以不检查,跳过这个代码)
*
* @param context
* @return
*/
public static boolean isGooglePlayServicesAvailable(Context context) {
GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance();
if (googleApiAvailability != null) {
int resultCode = googleApiAvailability.isGooglePlayServicesAvailable(context);
return resultCode == ConnectionResult.SUCCESS;
}
return false;
//return_img true;//不检查直接跳过
}
public GoogleBillingUtil setOnQueryFinishedListener(OnQueryFinishedListener onQueryFinishedListener) {
mOnQueryFinishedListener = onQueryFinishedListener;
return mGoogleBillingUtil;
}
public GoogleBillingUtil setOnPurchaseFinishedListener(OnPurchaseFinishedListener onPurchaseFinishedListener) {
mOnPurchaseFinishedListener = onPurchaseFinishedListener;
return mGoogleBillingUtil;
}
public OnStartSetupFinishedListener getOnStartSetupFinishedListener() {
return mOnStartSetupFinishedListener;
}
public GoogleBillingUtil setOnStartSetupFinishedListener(OnStartSetupFinishedListener onStartSetupFinishedListener) {
mOnStartSetupFinishedListener = onStartSetupFinishedListener;
return mGoogleBillingUtil;
}
public static OnConsumeResponseListener getmOnConsumeResponseListener() {
return mOnConsumeResponseListener;
}
public GoogleBillingUtil setOnConsumeResponseListener(OnConsumeResponseListener onConsumeResponseListener) {
mOnConsumeResponseListener = onConsumeResponseListener;
return mGoogleBillingUtil;
}
/**
* 本工具查询回调接口
*/
public interface OnQueryFinishedListener {
//Inapp和sub都走这个接口查询的时候一定要判断skuType
public void onQuerySuccess(String skuType, List<SkuDetails> list);
public void onQueryFail(int responseCode);
public void onQueryError();
}
/**
* 本工具购买回调接口(内购与订阅都走这接口)
*/
public interface OnPurchaseFinishedListener {
public void onPurchaseSuccess(Purchase purchase);
public void onPurchaseFail(int responseCode);
public void onPurchaseError();
}
/**
* google服务启动接口
*/
public interface OnStartSetupFinishedListener {
public void onSetupSuccess();
public void onSetupFail(int responseCode);
public void onSetupError();
}
/**
* 消耗回调监听器
*/
public interface OnConsumeResponseListener {
public void onConsumeSuccess(String purchaseToken);
public void onConsumeFail(int responseCode);
}
public boolean isReady() {
return mBillingClient != null && mBillingClient.isReady();
}
public boolean isAutoConsumeAsync() {
return isAutoConsumeAsync;
}
public void setIsAutoConsumeAsync(boolean isAutoConsumeAsync) {
this.isAutoConsumeAsync = isAutoConsumeAsync;
}
/**
* 清除所有监听器,防止内存泄漏
* 如果有多个页面使用了支付,需要确保上个页面的cleanListener在下一个页面的GoogleBillingUtil.getInstance()前使用。
* 所以不建议放在onDestory里调用
*/
public static void cleanListener() {
mOnPurchaseFinishedListener = null;
mOnQueryFinishedListener = null;
mOnStartSetupFinishedListener = null;
mOnConsumeResponseListener = null;
if (builder != null) {
builder.setListener(null);
}
}
/**
* 断开连接google服务
* 注意!!!一般情况不建议调用该方法,让google保留连接是最好的选择。
*/
public static void endConnection() {
//注意!!!一般情况不建议调用该方法,让google保留连接是最好的选择。
if (mBillingClient != null) {
if (mBillingClient.isReady()) {
mBillingClient.endConnection();
mBillingClient = null;
}
}
}
private static void log(String msg) {
if (IS_DEBUG) {
Log.i(TAG, msg);
}
}
/**
* 查询失败的订单
* skuType :,BillingClient.SkuType.INAPP; BillingClient.SkuType.SUBS
* 0:PurchaseState.UNSPECIFIED_STATE:未知状态
* 1:PurchaseState.PURCHASED:付款完成
* 2:PurchaseState.PENDING:购买正在等待付款完成。
*/
public void queryFailOrder(Activity activity, String skuType) {
MyToash.Log("pay", "-----查询需要补货的商品:");
if (mBillingClient != null) {
// Purchase.PurchasesResult result = mBillingClient.queryPurchases(skuType);
// if (BillingClient.BillingResponseCode.OK == result.getResponseCode()) {
// for (Purchase purchase : result.getPurchasesList()) {
// //PURCHASED --已购买
// if (Purchase.PurchaseState.PURCHASED == purchase.getPurchaseState()) {
// //MyToash.Log("pay", "----------需要补货的商品:");
// //调用google去消费
// getConsumeGoods(activity, purchase, "");
// }
// }
// }
QueryPurchasesParams queryPurchasesParams = QueryPurchasesParams.newBuilder()
.setProductType(skuType)
.build();
mBillingClient.queryPurchasesAsync(queryPurchasesParams, new PurchasesResponseListener() {
@Override
public void onQueryPurchasesResponse(@NonNull BillingResult billingResult, @NonNull List<Purchase> list) {
if (list != null) {
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
for (Purchase purchase : list) {
//PURCHASED --已购买
if (Purchase.PurchaseState.PURCHASED == purchase.getPurchaseState()) {
MyToash.Log("pay", "----------需要补货的商品:");
//调用google去消费
getConsumeGoods(activity, purchase, "", skuType);
}
}
}
}
}
});
}
}
/**
* 公共消费商品接口
*/
public void getConsumeGoods(Activity activity, Purchase purchase, String price, String skuType) {
if (purchase != null) {
MyToash.Log("pay", "-----开始消耗商品:" + purchase.toString());
if (skuType == BillingClient.ProductType.INAPP) {
//普通消耗-本地发送书券成功之后,调用消耗接口
ConsumeParams.Builder consumeParams = ConsumeParams.newBuilder();
consumeParams.setPurchaseToken(purchase.getPurchaseToken());
//调用普通消耗-商品方法
consumeAsync(activity, consumeParams.build(), purchase, price);
} else {
//调用订阅消耗-商品方法
getConsumeSubscribeGoods(activity, purchase, price);
}
}
}
/**
* 公共订阅方法消费
*/
public void getConsumeSubscribeGoods(Activity activity, Purchase purchase, String price) {
if (purchase != null) {
if (purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED) {
if (!purchase.isAcknowledged()) {
//本地记录消费的商品
saveRecordLoaclConsumeGoods(activity, purchase, 1, BillingClient.ProductType.SUBS);
AcknowledgePurchaseParams acknowledgePurchaseParams =
AcknowledgePurchaseParams.newBuilder()
.setPurchaseToken(purchase.getPurchaseToken())
.build();
mBillingClient.acknowledgePurchase(acknowledgePurchaseParams, new AcknowledgePurchaseResponseListener() {
@Override
public void onAcknowledgePurchaseResponse(@NonNull BillingResult billingResult) {
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
MyToash.Log("pay", "-------消费订阅商品成功");
//移除
saveRecordLoaclConsumeGoods(activity, purchase, 2, BillingClient.ProductType.SUBS);
//请求本地接口
getHttpPay(activity, purchase, BillingClient.ProductType.SUBS);
} else {
MyToash.Log("pay", "-------消费订阅商品失败:" + billingResult.getResponseCode());
}
}
});
} else {
WaitDialogUtils.dismissDialog();
}
} else {
WaitDialogUtils.dismissDialog();
}
}
}
/**
* 正常流程-调用本地接口发送书券和书币
*/
public void getHttpPay(Activity activity, Purchase purchase, String skuType) {
if (purchase != null) {
String pId = purchase.getSkus().get(0);
String purchaseToken = purchase.getPurchaseToken();
String packageName = purchase.getPackageName();
//保存本地单据
saveLocalFaileOrder(activity, purchase, skuType);
//调用本地发书券接口
GoPay.httpPay(activity, pId, purchaseToken, "", packageName, skuType, new GooglePayActivity.UpPay() {
@Override
public void paySuccess(String success) {
MyToash.Log("pay", "--本地接口调用成功:" + success);
//书的id
String bookId = MmkvUtils.decodeString(BaseKey.SP_BOOKID);
MmkvUtils.encode(BaseKey.SP_BOOKID, "");
ShareUitls.putString(activity, "goods_id", null);
ShareUitls.putString(activity, "productId", null);
ShareUitls.putString(activity, "productToken", null);
if (!TextUtils.isEmpty(bookId)) {
//不是空刷新阅读器的支付页面
EventBus.getDefault().post(new RefreshRead());
}
//通知刷新页面
EventBus.getDefault().post(new RefreshMine());
//成功提示
MyToash.ToashSuccess(activity, success);
//删除本地的数据
deleteLocalFaileOrder(activity, purchaseToken, skuType);
WaitDialogUtils.dismissDialog();
}
@Override
public void payFail(String msg) {
//本地接口调用失败
MyToash.Log("pay", "----本地接口调用失败:" + msg);
WaitDialogUtils.dismissDialog();
}
});
}
}
/**
* 保存本地失败的单据
*
* @param activity
*/
private void saveLocalFaileOrder(Activity activity, Purchase purchase, String skuType) {
try {
String pId = purchase.getSkus().get(0);
String purchaseToken = purchase.getPurchaseToken();
String packageName = purchase.getPackageName();
//用户id
String uid = UserUtils.getUID();
List<String> list = new ArrayList<>();
//获取存储的值
String lastJson = MmkvUtils.decodeString(uid);
if (!lastJson.isEmpty()) {
//不是空,只有第一次请求失败保存
List<String> list2 = GsonUtil.GsonToList(lastJson, String.class);
for (int i = 0; i < list2.size(); i++) {
String data = list2.get(i);
list.add(data);
}
}
//没有相同单据,保存
HashMap map = new HashMap();
map.put("pId", pId);
map.put("purchaseToken", purchaseToken);
map.put("packageName", packageName);
map.put("skuType", skuType);
map.put("time", CommonPayUtilsKt.isSaveCurrentTime());
list.add(GsonUtil.BeanToJson(map));
String json = GsonUtil.BeanToJson(list);
//保存上传失败的订单
MmkvUtils.encode(uid, json);
MyToash.Log("pay", "----保存上传失败的订单json:" + json);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 删除本地失败的单据
*
* @param activity
*/
private void deleteLocalFaileOrder(Activity activity, String purchaseToken, String skuType) {
try {
//用户id
String uid = UserUtils.getUID();
String lastJson = MmkvUtils.decodeString(uid);
if (!lastJson.isEmpty()) {
MyToash.Log("pay", "----查询准备要删除上传失败的订单:" + lastJson);
//不是空
List<String> list = GsonUtil.GsonToList(lastJson, String.class);
//是否有相等的下标
int index = -1;
for (int i = 0; i < list.size(); i++) {
String data = list.get(i);
OrderFailBean bean = GsonUtil.GsonToBean(data, OrderFailBean.class);
String token = bean.getPurchaseToken();
if (purchaseToken.equals(token)) {
index = i;
}
}
//删除数据
if (index != -1) {
list.remove(index);
String json = GsonUtil.BeanToJson(list);
//保存数据
MmkvUtils.encode(uid, json);
MyToash.Log("pay", "----删除上传失败的订单:" + json);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 查询订单失败--调用本地接口发送书券和书币
*/
public void getLocalFailOrder(Activity activity) {
try {
//用户id
String uid = UserUtils.getUID();
List<String> list = new ArrayList<>();
//获取存储的值
String lastJson = MmkvUtils.decodeString(uid);
MyToash.Log("pay", "---查询失败的订单lastJson:" + lastJson);
if (!lastJson.isEmpty()) {
//不是空
list = GsonUtil.GsonToList(lastJson, String.class);
for (int i = 0; i < list.size(); i++) {
String data = list.get(i);
OrderFailBean bean = GsonUtil.GsonToBean(data, OrderFailBean.class);
String pId = bean.getpId();
long time = bean.getTime();
String purchaseToken = bean.getPurchaseToken();
String packageName = bean.getPackageName();
String skuType = bean.getSkuType();
if (CommonPayUtilsKt.isUploadLocalFailOrder(time)) {
//上传失败的单据
UploadLocalFailOrder(activity, uid, pId, packageName, purchaseToken, skuType);
Thread.sleep(1000);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 上传本地单据
*
* @param pId
* @param packageName
* @param purchaseToken
*/
private void UploadLocalFailOrder(Activity activity, String uid, String pId, String packageName, String purchaseToken, String skuType) {
try {
//调用本地发书券接口
GoPay.httpPay(activity, pId, purchaseToken, "", packageName, skuType, new GooglePayActivity.UpPay() {
@Override
public void paySuccess(String success) {
MyToash.Log("pay", "后台上传失败订单成功:" + success);
//WaitDialogUtils.dismissDialog();
MmkvUtils.encode(BaseKey.SP_BOOKID, "");
ShareUitls.putString(activity, "goods_id", null);
ShareUitls.putString(activity, "productId", null);
ShareUitls.putString(activity, "productToken", null);
//通知刷新页面
EventBus.getDefault().post(new RefreshMine());
//删除本地的数据
deleteLocalFaileOrder(activity, purchaseToken, skuType);
//移除消费无响应数据
deleteRecordLoaclConsumeGoods(activity, pId, purchaseToken, packageName);
}
@Override
public void payFail(String msg) {
//本地接口调用失败
try {
if (!TextUtils.isEmpty(msg)) {
int subIndex = msg.indexOf(",");
if (subIndex > 0) {
String code = msg.substring(0, subIndex);
String message = msg.substring(subIndex, msg.length());
if ("806".equals(code)) {
//删除本地的数据
deleteLocalFaileOrder(activity, purchaseToken, skuType);
//移除消费无响应数据
deleteRecordLoaclConsumeGoods(activity, pId, purchaseToken, packageName);
}
}
}
MyToash.Log("pay", "后台上传失败订单失败:" + msg);
} catch (Exception e) {
e.printStackTrace();
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 新-只删除消费商品无响应的状态
*
* @param activity
* @param lastJson
* @param pId
* @param purchaseToken
* @param packageName
*/
private void deleteRecordLoaclConsumeGoods(Activity activity, String pId, String purchaseToken, String packageName) {
try {
String uid_key = UserUtils.getUID() + CommonConstantUtils.CONSUME;
List<String> list = new ArrayList<>();
//获取存储的值
String lastJson = MmkvUtils.decodeString(uid_key);
//不是空
if (!lastJson.isEmpty()) {
int currentIndex = -1;
List<String> list3 = GsonUtil.GsonToList(lastJson, String.class);
for (int i = 0; i < list3.size(); i++) {
String data = list3.get(i);
list.add(data);
OrderFailBean bean = GsonUtil.GsonToBean(data, OrderFailBean.class);
String tokenKey = bean.getPurchaseToken() + CommonConstantUtils.CONSUME;
if ((tokenKey).equals(purchaseToken + CommonConstantUtils.CONSUME)) {
currentIndex = i;
}
}
if (currentIndex != -1) {
list.remove(currentIndex);
String json = GsonUtil.BeanToJson(list);
//保存上传失败的订单
MmkvUtils.encode(uid_key, json);
MyToash.Log("pay", "--移除消耗的商品json:" + json);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 调用支付之前创建订单
* goods_id:商品ID
* content_type:内容类型 1小说
*/
public void getCreateOrder(Activity activity, String goods_id, String content_type, CallCheckOrder CallCheckOrder) {
ReaderParams params = new ReaderParams(activity);
params.putExtraParams("goods_id", goods_id);
params.putExtraParams("content_type", content_type);
params.putExtraParams("book_id", MmkvUtils.decodeString(BaseKey.SP_BOOKID));
String json = params.generateParamsJson();
//调用本地发书券接口
//MyToash.Log("lyy", "--json:" + json);
HttpUtils.getInstance().sendRequestRequestParams(activity, Api.mCreateOrderUrl, json, new HttpUtils.ResponseListener() {
@Override
public void onResponse(final String result) {
WaitDialogUtils.dismissDialog();
MyToash.Log("pay", "--创建订单成功" + result);
if (CallCheckOrder != null) {
CallCheckOrder.call("success");
}
}
@Override
public void onErrorResponse(String error) {
WaitDialogUtils.dismissDialog();
MyToash.Log("pay", "--创建订单失败" + error);
}
}
);
}
public interface CallCheckOrder {
void call(String result);
}
}