谷歌支付

This commit is contained in:
gongduoxiang
2024-08-06 10:26:59 +08:00
parent 3a12d848e1
commit 1deb197af6
11 changed files with 475 additions and 354 deletions

View File

@@ -43,6 +43,7 @@ public class GoogleBillingManage implements PurchasesUpdatedListener {
public GoogleBillingManage(Activity activity) {
this.mContext = activity;
queryFailOrder();
}
public void initGooglePay() {
@@ -117,6 +118,82 @@ public class GoogleBillingManage implements PurchasesUpdatedListener {
}
}
/**
* 查询失败的订单
* skuType :,BillingClient.SkuType.INAPP 内购 BillingClient.SkuType.SUBS订阅
* 0:PurchaseState.UNSPECIFIED_STATE未知状态
* 1:PurchaseState.PURCHASED付款完成
* 2:PurchaseState.PENDING购买正在等待付款完成。
*/
public void queryFailOrder(/*String skuType*/) {
Log.i(TAG, "-查询失败订单(支付成功未消耗)");
if (billingClient != null) {
billingClient.queryPurchasesAsync(BillingClient.SkuType.INAPP, new PurchasesResponseListener() {
@Override
public void onQueryPurchasesResponse(@NonNull BillingResult result, @NonNull List<Purchase> list) {
if (BillingClient.BillingResponseCode.OK == result.getResponseCode()) {
for (Purchase purchase : list) {
//PURCHASED --已购买
if (Purchase.PurchaseState.PURCHASED == purchase.getPurchaseState()) {
//todo 这里可以同步发送消息到后台 看订单是否下发了 没有就下发一下
//调用google去消费
getConsumeGoods(purchase);
}
}
}
}
});
}
}
/**
* 公共消费商品接口
*/
private void getConsumeGoods(Purchase purchase) {
if (purchase != null) {
Log.i(TAG, "-----开始消耗商品:" + purchase.toString());
ConsumeParams.Builder consumeParams = ConsumeParams.newBuilder();
consumeParams.setPurchaseToken(purchase.getPurchaseToken());
//调用消耗商品方法
consumeAsync(consumeParams.build(), purchase);
}
}
/**
* 消耗商品
* 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(ConsumeParams consumeParams, Purchase purchase) {
if (billingClient == null) {
return;
}
billingClient.consumeAsync(consumeParams, new ConsumeResponseListener() {
@Override
public void onConsumeResponse(@NonNull BillingResult billingResult, @NonNull String s) {
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
Log.i(TAG, "-----消耗商品成功");
String pId = purchase.getSkus().get(0);
// String price = CommonOkHttpUtils.INSTANCE.getPayPrice(pId);
Log.i(TAG, "-----消耗商品成功 ");
String productToken = purchase.getPurchaseToken();
} else {
Log.i(TAG, "-----消耗商品失败" + billingResult.toString() + "---" + s + "--code:" + billingResult.getResponseCode());
}
}
});
}
public boolean getGoogleService() {
int code = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(mContext);
return code == 0;