google支付修改

This commit is contained in:
18401019693
2022-09-28 13:43:53 +08:00
parent 0d473bdd9f
commit 8d98c6b5cc
12 changed files with 635 additions and 309 deletions

View File

@@ -145,7 +145,7 @@ dependencies {
//谷歌支付
//谷歌内购
api 'com.android.billingclient:billing:3.0.0'
api 'com.android.billingclient:billing:5.0.0'
implementation 'com.teprinciple:updateapputils:2.3.0'
api 'com.squareup.picasso:picasso:2.5.2'
api "com.immomo.cosmos.mediax:beautyutils:2.2.1_01071700"
@@ -184,4 +184,5 @@ dependencies {
api 'com.google.firebase:firebase-messaging:23.0.6'
api 'com.google.firebase:firebase-analytics:21.1.0'
// api 'com.huawei.hms:push:4.0.2.300'
implementation 'com.github.tajchert:WaitingDots:0.6.1'
}

View File

@@ -0,0 +1,64 @@
package com.yunbao.common.fragment;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import com.yunbao.common.R;
import com.yunbao.common.dialog.AbsDialogFragment;
import com.yunbao.common.utils.DeviceUtils;
import pl.tajchert.waitingdots.DotsTextView;
public class LoadingDialog extends AbsDialogFragment {
private DotsTextView dots;
private TextView hint;
private String showText = "";
public void setShowText(String showText) {
this.showText = showText;
}
@Override
protected int getLayoutId() {
return R.layout.view_loading_layout;
}
@Override
protected int getDialogStyle() {
return R.style.dialog2;
}
@Override
protected boolean canCancel() {
return true;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
dots = (DotsTextView) findViewById(R.id.dots);
hint = (TextView) findViewById(R.id.hint);
dots.start();
hint.setText(showText);
}
@Override
public void onDestroy() {
super.onDestroy();
dots.stop();
}
@Override
protected void setWindowAttributes(Window window) {
window.setWindowAnimations(R.style.bottomToTopAnim);
WindowManager.LayoutParams params = window.getAttributes();
params.width = DeviceUtils.getScreenWidth(getActivity())/2;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.gravity = Gravity.CENTER;
window.setAttributes(params);
}
}

View File

@@ -410,7 +410,7 @@ public class PayPresenter {
String allData="";
allData="OrderId=" +purchase.getOrderId()+
",PackageName="+purchase.getPackageName() +
",Sku=" +purchase.getSku()+
",Sku=" +"purchase.getSku()"+
",PurchaseTime=" +purchase.getPurchaseTime()+
",PurchaseToken=" +purchase.getPurchaseToken()+
",DeveloperPayload=" +purchase.getDeveloperPayload()+
@@ -436,7 +436,7 @@ public class PayPresenter {
}else{
tempBuffer.append("暂未支付:");
}
String details = String.format(Locale.getDefault(),"%s \n", purchase.getSku());
String details = String.format(Locale.getDefault(),"%s \n"," purchase.getSku()");
tempBuffer.append(details);
Log.d(TAG,tempBuffer.toString());
return true ;//自动消耗(只有当isSelf为true,并且支付状态为PURCHASED时该值才会生效)

View File

@@ -385,7 +385,7 @@ public class GoogleBillingUtil {
List<Purchase> purchaseList = queryPurchasesInApp(activity);
if(purchaseList!=null){
for(Purchase purchase : purchaseList){
int index = skuList.indexOf(purchase.getSku());
int index = skuList.indexOf("purchase.getSku()");
if(index!=-1){
if(developerPayloadList!=null&&index<developerPayloadList.size()){
consumeAsync(activity,purchase.getPurchaseToken(),developerPayloadList.get(index));
@@ -457,57 +457,57 @@ public class GoogleBillingUtil {
private List<Purchase> queryPurchases(String tag, String skuType)
{
if(mBillingClient==null)
{
return null;
}
if(!mBillingClient.isReady())
{
startConnection(tag);
}
else
{
Purchase.PurchasesResult purchasesResult = mBillingClient.queryPurchases(skuType);
if(purchasesResult!=null)
{
if(purchasesResult.getResponseCode()== BillingClient.BillingResponseCode.OK)
{
List<Purchase> purchaseList = purchasesResult.getPurchasesList();
if(purchaseList!=null&&!purchaseList.isEmpty())
{
for(Purchase purchase:purchaseList)
{
for(OnGoogleBillingListener listener : onGoogleBillingListenerList) {
boolean isSelf = listener.tag.equals(tag);//是否是当前页面
boolean isSuccess = listener.onRecheck(skuType, purchase, isSelf);//是否消耗或者确认
if(isSelf){
if (purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED) {
if(skuType.equals(BillingClient.SkuType.INAPP)) {
if(isSuccess){
consumeAsync(tag,purchase.getPurchaseToken());
}else if(isAutoAcknowledgePurchase){
if(!purchase.isAcknowledged()){
acknowledgePurchase(tag,purchase.getPurchaseToken());
}
}
}else if(skuType.equals(BillingClient.SkuType.SUBS)){
if(isAutoAcknowledgePurchase){
if(!purchase.isAcknowledged()){
acknowledgePurchase(tag,purchase.getPurchaseToken());
}
}
}
}else{
log("未支付的订单:"+purchase.getSku());
}
}
}
}
}
return purchaseList;
}
}
}
// if(mBillingClient==null)
// {
// return null;
// }
// if(!mBillingClient.isReady())
// {
// startConnection(tag);
// }
// else
// {
// Purchase.PurchasesResult purchasesResult = mBillingClient.queryPurchases(skuType);
// if(purchasesResult!=null)
// {
// if(purchasesResult.getResponseCode()== BillingClient.BillingResponseCode.OK)
// {
// List<Purchase> purchaseList = purchasesResult.getPurchasesList();
// if(purchaseList!=null&&!purchaseList.isEmpty())
// {
// for(Purchase purchase:purchaseList)
// {
// for(OnGoogleBillingListener listener : onGoogleBillingListenerList) {
// boolean isSelf = listener.tag.equals(tag);//是否是当前页面
// boolean isSuccess = listener.onRecheck(skuType, purchase, isSelf);//是否消耗或者确认
// if(isSelf){
// if (purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED) {
// if(skuType.equals(BillingClient.SkuType.INAPP)) {
// if(isSuccess){
// consumeAsync(tag,purchase.getPurchaseToken());
// }else if(isAutoAcknowledgePurchase){
// if(!purchase.isAcknowledged()){
// acknowledgePurchase(tag,purchase.getPurchaseToken());
// }
// }
// }else if(skuType.equals(BillingClient.SkuType.SUBS)){
// if(isAutoAcknowledgePurchase){
// if(!purchase.isAcknowledged()){
// acknowledgePurchase(tag,purchase.getPurchaseToken());
// }
// }
// }
// }else{
// log("未支付的订单:"+"purchase.getSku()");
// }
// }
// }
// }
// }
// return purchaseList;
// }
// }
// }
return null;
}
//endregion
@@ -526,7 +526,6 @@ public class GoogleBillingUtil {
}
return false;
}
/**
* 异步联网查询所有的订阅历史-无论是过期的、取消、等等的订单
* @param listener 监听器
@@ -771,7 +770,7 @@ public class GoogleBillingUtil {
boolean isSuccess = listener.onPurchaseSuccess(purchase,isSelf);//是否自动消耗
if(isSelf&&purchase.getPurchaseState()== Purchase.PurchaseState.PURCHASED){
//是当前页面,并且商品状态为支付成功,才会进行消耗与确认的操作
String skuType = getSkuType(purchase.getSku());
String skuType = getSkuType("purchase.getSku()");
if(BillingClient.SkuType.INAPP.equals(skuType)){
if(isSuccess){
//进行消耗
@@ -791,7 +790,7 @@ public class GoogleBillingUtil {
}
}
}else if(purchase.getPurchaseState()== Purchase.PurchaseState.PENDING){
log("待处理的订单:"+purchase.getSku());
log("待处理的订单:"+"purchase.getSku()");
}
}
}

View File

@@ -0,0 +1,185 @@
package com.yunbao.common.utils;
import android.app.Activity;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
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.Purchase;
import com.android.billingclient.api.PurchasesResponseListener;
import com.android.billingclient.api.PurchasesUpdatedListener;
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 java.util.ArrayList;
import java.util.List;
/**
* 新写Google内付工具类做简单的封装
* 支付流程初始化Google支付服务-->查询商品-->购买-->支付成功回调-->消耗商品
*/
public class GoogleBillingUtilNew {
private BillingClient billingClient;
private Activity mContext;
private String TAG = "GoogleBillingUtilNew";
private static GoogleBillingUtilNew googleBillingUtilNew;
public static GoogleBillingUtilNew getInstance() {
if (googleBillingUtilNew == null) {
googleBillingUtilNew = new GoogleBillingUtilNew();
}
return googleBillingUtilNew;
}
public void initGooglePay(Activity mContext) {
this.mContext = mContext;
billingClient = BillingClient.newBuilder(mContext)
.setListener(purchasesUpdatedListener)
.enablePendingPurchases()
.build();
//请求连接到GooglePay
billingClient.startConnection(new BillingClientStateListener() {
@Override
public void onBillingSetupFinished(@NonNull BillingResult billingResult) {
int code = billingResult.getResponseCode();
if (code != BillingClient.BillingResponseCode.OK) {
String msg = billingResult.getDebugMessage();
Log.e(TAG, "连接到GooglePay失败 code = " + code + " msg = " + msg);
return;
}
Log.e(TAG, "连接到GooglePay成功");
}
//连接失败
@Override
public void onBillingServiceDisconnected() {
Log.e(TAG, "连接到GooglePay失败请重试");
}
});
}
//查询商品
public void checkSku(String id) {
List<String> skuList = new ArrayList<>();
skuList.add(id);
SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder()
.setSkusList(skuList)
.setType(BillingClient.SkuType.INAPP);
billingClient.querySkuDetailsAsync(params.build(), new SkuDetailsResponseListener() {
@Override
public void onSkuDetailsResponse(@NonNull BillingResult billingResult, @Nullable List<SkuDetails> list) {
int code = billingResult.getResponseCode();
if (code != BillingClient.BillingResponseCode.OK || list == null || list.isEmpty()) {
String msg = billingResult.getDebugMessage();
Log.e(TAG, "查询商品失败 code = " + code + " msg = " + msg);
return;
}
Log.e(TAG, "查询商品成功");
buyIt(list.get(0));
}
});
}
//购买
private void buyIt(SkuDetails skuDetails) {
BillingFlowParams billingFlowParams = BillingFlowParams.newBuilder()
.setSkuDetails(skuDetails)
.build();
BillingResult billingResult = billingClient.launchBillingFlow(mContext, billingFlowParams);
int code = billingResult.getResponseCode();
if (code != BillingClient.BillingResponseCode.OK) {
String msg = billingResult.getDebugMessage();
Log.e(TAG, "购买商品失败 code = " + code + " msg = " + msg);
return;
}
Log.e(TAG, "购买商品" + skuDetails.toString());
}
private PurchasesUpdatedListener purchasesUpdatedListener = new PurchasesUpdatedListener() {
@Override
public void onPurchasesUpdated(@NonNull BillingResult billingResult, @Nullable List<Purchase> list) {
int code = billingResult.getResponseCode();
String msg = billingResult.getDebugMessage();
Log.e(TAG, "onPurchasesUpdatedcode = " + code + " msg = " + msg);
if (list != null) {
for (Purchase purchase : list) {
Log.e(TAG, "onPurchasesUpdated" + purchase.toString());
}
}
if (code == BillingClient.BillingResponseCode.OK && list != null) {
consume(list);
Log.e(TAG, "支付成功");
if (billingListener != null) {
billingListener.onPaySuccess(list);
}
} else if (code == BillingClient.BillingResponseCode.USER_CANCELED) {
Log.e(TAG, "支付取消");
if (billingListener != null) {
billingListener.onPayFailed(code, msg);
}
} else {
if (billingListener != null) {
billingListener.onPayFailed(code, msg);
}
Log.e(TAG, "支付失败code = " + code + " msg = " + msg);
}
}
};
//消耗商品
public void consume(List<Purchase> list) {
if (list == null || list.isEmpty() || billingClient == null) {
return;
}
for (Purchase purchase : list) {
billingClient.consumeAsync(ConsumeParams.newBuilder().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);
}
});
}
}
/**
* 查询商品订单
*/
public void queryPurchasesAsync(PurchasesResponseListener responseListener) {
if (billingClient == null) return;
//内购商品交易查询
//内购商品交易查询
QueryPurchasesParams inAppPurchasesQurey = QueryPurchasesParams.newBuilder()
.setProductType(BillingClient.ProductType.INAPP)
.build();
billingClient.queryPurchasesAsync(inAppPurchasesQurey, responseListener);
}
/**
* 定义接口返回支付结果
*/
public interface GoogleBillingListener {
void onPaySuccess(List<Purchase> list);
void onPayFailed(int code, String msg);
}
private GoogleBillingListener billingListener;
public GoogleBillingUtilNew setBillingListener(GoogleBillingListener billingListener) {
this.billingListener = billingListener;
return this;
}
}

View File

@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners
android:bottomLeftRadius="12dp"
android:bottomRightRadius="12dp"
android:topLeftRadius="12dp"
android:topRightRadius="12dp" />
<solid android:color="#222324" />
</shape>

View File

@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:dots="http://schemas.android.com/apk/res-auto"
android:layout_width="350dp"
android:layout_height="wrap_content"
android:background="@drawable/bg_loding_view">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:layout_gravity="center">
<TextView
android:id="@+id/hint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ding单"
android:textColor="@color/white"
android:textSize="14sp"
android:textStyle="bold" />
<pl.tajchert.waitingdots.DotsTextView
android:id="@+id/dots"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:textSize="24sp"
dots:autoplay="false"
dots:period="1000" />
</LinearLayout>
</FrameLayout>

View File

@@ -909,5 +909,7 @@
<string name="try_again_later">數據加載失敗,請稍後再試</string>
<string name="open_noble2">開通了</string>
<string name="enough_speakers">喇叭數量不足</string>
<string name="order_query">订单查询中</string>
<string name="order_query_success">订单无异常</string>
</resources>