google支付修改
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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时,该值才会生效)
|
||||
|
||||
@@ -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()");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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, "onPurchasesUpdated:code = " + 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user