|
|
@@ -7,23 +7,26 @@ import androidx.lifecycle.MutableLiveData;
|
|
|
|
|
|
import com.android.billingclient.api.BillingClient;
|
|
|
import com.android.billingclient.api.BillingResult;
|
|
|
+import com.android.billingclient.api.Purchase;
|
|
|
import com.atmob.app.lib.base.BaseViewModel;
|
|
|
+import com.atmob.app.lib.handler.RxHttpHandler;
|
|
|
import com.atmob.app.lib.livedata.SingleLiveEvent;
|
|
|
import com.atmob.common.data.KVUtils;
|
|
|
import com.atmob.common.logging.AtmobLog;
|
|
|
import com.atmob.common.runtime.ActivityUtil;
|
|
|
import com.atmob.common.runtime.ContextUtil;
|
|
|
import com.atmob.voiceai.R;
|
|
|
+import com.atmob.voiceai.data.api.AtmobApi;
|
|
|
import com.atmob.voiceai.data.api.bean.GoodsBean;
|
|
|
import com.atmob.voiceai.data.api.bean.PayOptionsBean;
|
|
|
import com.atmob.voiceai.data.api.response.OrderPayResponse;
|
|
|
import com.atmob.voiceai.data.repositories.MemberRepository;
|
|
|
import com.atmob.voiceai.helper.ErrorHelper;
|
|
|
import com.atmob.voiceai.sdk.billing.GPBillingClient;
|
|
|
+import com.atmob.voiceai.utils.BoxingUtil;
|
|
|
import com.atmob.voiceai.utils.SpannableUtil;
|
|
|
import com.atmob.voiceai.utils.ToastUtil;
|
|
|
|
|
|
-import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
@@ -33,6 +36,7 @@ import atmob.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
|
|
|
import atmob.reactivex.rxjava3.annotations.NonNull;
|
|
|
import atmob.reactivex.rxjava3.core.Observable;
|
|
|
import atmob.reactivex.rxjava3.core.ObservableSource;
|
|
|
+import atmob.reactivex.rxjava3.core.Single;
|
|
|
import atmob.reactivex.rxjava3.core.SingleObserver;
|
|
|
import atmob.reactivex.rxjava3.core.SingleSource;
|
|
|
import atmob.reactivex.rxjava3.disposables.Disposable;
|
|
|
@@ -61,7 +65,6 @@ public class SubscriptionPageViewModel extends BaseViewModel {
|
|
|
private GoodsBean checkGoodsBean;
|
|
|
private boolean isRequestSubmitOrder;
|
|
|
|
|
|
- private final HashMap<String, GoodsBean> orderIdItemMap = new HashMap<>();
|
|
|
|
|
|
@Inject
|
|
|
public SubscriptionPageViewModel(MemberRepository memberRepository, GPBillingClient gpBillingClient) {
|
|
|
@@ -125,7 +128,22 @@ public class SubscriptionPageViewModel extends BaseViewModel {
|
|
|
}
|
|
|
|
|
|
public void onRestoreClick() {
|
|
|
+ queryPurchase()
|
|
|
+ .subscribe(new SingleObserver<List<Purchase>>() {
|
|
|
+ @Override
|
|
|
+ public void onSubscribe(@NonNull Disposable d) {
|
|
|
+ addDisposable(d);
|
|
|
+ }
|
|
|
|
|
|
+ @Override
|
|
|
+ public void onSuccess(@NonNull List<Purchase> purchases) {
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onError(@NonNull Throwable e) {
|
|
|
+
|
|
|
+ }
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -201,13 +219,13 @@ public class SubscriptionPageViewModel extends BaseViewModel {
|
|
|
@Override
|
|
|
public void onSuccess(@NonNull OrderPayResponse orderPayResponse) {
|
|
|
isRequestSubmitOrder = false;
|
|
|
- orderIdItemMap.put(orderPayResponse.getOutTradeNo(), checkGoodsBean);
|
|
|
requestPayment(checkGoodsBean, orderPayResponse.getOutTradeNo());
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void onError(@NonNull Throwable e) {
|
|
|
isRequestSubmitOrder = false;
|
|
|
+ ErrorHelper.errorThrowableToast(e, ToastUtil.LENGTH_SHORT);
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
@@ -225,7 +243,30 @@ public class SubscriptionPageViewModel extends BaseViewModel {
|
|
|
@Override
|
|
|
public void onSuccess(@NonNull BillingResult billingResult) {
|
|
|
AtmobLog.d(TAG, "requestPayment onSuccess: " + billingResult);
|
|
|
+ gpBillingClient.registerFlowListener(this, orderNo, purchase -> queryOrderStatus(bean, orderNo, purchase));
|
|
|
+ }
|
|
|
|
|
|
+ @Override
|
|
|
+ public void onError(@NonNull Throwable e) {
|
|
|
+
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ private void queryOrderStatus(GoodsBean bean, String orderNo, Purchase purchase) {
|
|
|
+ memberRepository.orderStatus(orderNo, purchase.getPurchaseToken())
|
|
|
+ .subscribe(new SingleObserver<Boolean>() {
|
|
|
+ @Override
|
|
|
+ public void onSubscribe(@NonNull Disposable d) {
|
|
|
+ addDisposable(d);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onSuccess(@NonNull Boolean aBoolean) {
|
|
|
+ if (BoxingUtil.boxing(aBoolean)) {
|
|
|
+ ToastUtil.show(R.string.pay_success, ToastUtil.LENGTH_SHORT);
|
|
|
+ finishEvent.call();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -240,9 +281,17 @@ public class SubscriptionPageViewModel extends BaseViewModel {
|
|
|
}
|
|
|
|
|
|
|
|
|
+ private Single<List<Purchase>> queryPurchase() {
|
|
|
+ return gpBillingClient.queryPurchase(BillingClient.ProductType.SUBS)
|
|
|
+ .filter(purchases -> purchases != null && !purchases.isEmpty())
|
|
|
+ .toSingle()
|
|
|
+ .compose(RxJavaUtil.SingleSchedule.io2Main());
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
protected void onCleared() {
|
|
|
super.onCleared();
|
|
|
gpBillingClient.endUrgentConnection();
|
|
|
+ gpBillingClient.unregisterAllFlowListener(this);
|
|
|
}
|
|
|
}
|