purchaseUpdated method

Future purchaseUpdated(
  1. PurchaseDetails purchaseDetail
)

Implementation

Future purchaseUpdated(PurchaseDetails purchaseDetail) async {
  if (purchaseDetail.status == PurchaseStatus.pending) {
    if (purchaseDetail.productID.startsWith(RechargeConfig.subStart)) {
      delegate.logEvent('发起订阅');
    } else {
      delegate.logEvent('${payInType ?? ''}发起支付');
    }
    AppConfig.printLog('PurchaseStatus.pending');
  } else if (purchaseDetail.status == PurchaseStatus.canceled) {
    popDialog();
    AppConfig.printLog('PurchaseStatus.canceled');
    if (purchaseDetail.productID.startsWith(RechargeConfig.subStart)) {
      delegate.logEvent('取消订阅');
    } else {
      delegate.logEvent('${payInType ?? ''}取消支付');
    }
    delegate.showToast(1);
    var res = await RechargeDao.rechargeCancel(orderId);
    if (res.result) {
      completePurchaseInPay(purchaseDetail);
    }
    orderId = '';
  } else if (purchaseDetail.status == PurchaseStatus.error) {
    popDialog();
    AppConfig.printLog(
        'PurchaseStatus.error:${purchaseDetail.error?.message}');
    if (purchaseDetail.error?.message == 'BillingResponse.itemAlreadyOwned') {
      queryPastPurchases();
    } else {
      if (purchaseDetail.productID.startsWith(RechargeConfig.subStart)) {
        delegate.logEvent('支付失败');
      } else {
        delegate.logEvent('${payInType ?? ''}支付失败');
      }
      showInPay = true;
      delegate.showDialogBase(1).then((value) async {
        showInPay = false;
      });
      var res = await RechargeDao.rechargeFailLog(
          '', '', orderId, purchaseDetail.error?.message);
      if (res.result) {
        completePurchaseInPay(purchaseDetail);
      }
    }
  } else if (purchaseDetail.status == PurchaseStatus.purchased ||
      purchaseDetail.status == PurchaseStatus.restored) {
    AppConfig.printLog('PurchaseStatus.purchased: ${purchaseDetail.status}');
    if (purchaseDetail.status == PurchaseStatus.restored) {
      restored = false;
    }
    if (orderId?.isEmpty != false) {
      orderId = SpUtil.getString(RechargeConfig.inappOrderId);
      payPrice = SpUtil.getString(RechargeConfig.inappPrice);
    }
    if (purchaseDetail.productID.startsWith(RechargeConfig.subStart)) {
      delegate.logEvent('订阅成功');
      SpUtil.putString(RechargeConfig.inappCode, purchaseDetail.productID);
      if (Platform.isIOS) {
        SpUtil.putString(RechargeConfig.inappToken,
            purchaseDetail.verificationData.serverVerificationData);
        await checkInPayPurchaseVip(
            purchaseDetail.verificationData.serverVerificationData,
            orderId,
            purchaseDetail.productID,
            price: payPrice,
            first: purchaseDetail.status != PurchaseStatus.restored,
            purchaseDetail: purchaseDetail,
            index: purchaseDetail.status == PurchaseStatus.restored
                ? maxIndex
                : 1);
      } else {
        SpUtil.putString(
            RechargeConfig.inappToken,
            (purchaseDetail as GooglePlayPurchaseDetails)
                .billingClientPurchase
                .originalJson);
        SpUtil.putString(RechargeConfig.inappSign,
            purchaseDetail.billingClientPurchase.signature);
        await checkInPayPurchaseVip(
            purchaseDetail.billingClientPurchase.originalJson,
            orderId,
            purchaseDetail.productID,
            sign: purchaseDetail.billingClientPurchase.signature,
            price: payPrice,
            first: purchaseDetail.status != PurchaseStatus.restored,
            purchaseDetail: purchaseDetail,
            index: purchaseDetail.status == PurchaseStatus.restored
                ? maxIndex
                : 1);
      }
    } else {
      delegate.logEvent('${payInType ?? ''}支付成功');
      SpUtil.putString(RechargeConfig.inappCode, purchaseDetail.productID);
      if (Platform.isIOS) {
        SpUtil.putString(RechargeConfig.inappToken,
            purchaseDetail.verificationData.serverVerificationData);
        await checkInPayPurchase(
            purchaseDetail.verificationData.serverVerificationData,
            orderId,
            purchaseDetail.productID,
            price: payPrice,
            first: purchaseDetail.status != PurchaseStatus.restored,
            purchaseDetail: purchaseDetail,
            index: purchaseDetail.status == PurchaseStatus.restored
                ? maxIndex
                : 1);
      } else {
        SpUtil.putString(
            RechargeConfig.inappToken,
            (purchaseDetail as GooglePlayPurchaseDetails)
                .billingClientPurchase
                .originalJson);
        SpUtil.putString(RechargeConfig.inappSign,
            purchaseDetail.billingClientPurchase.signature);
        await checkInPayPurchase(
            purchaseDetail.billingClientPurchase.originalJson,
            orderId,
            purchaseDetail.productID,
            sign: purchaseDetail.billingClientPurchase.signature,
            price: payPrice,
            first: purchaseDetail.status != PurchaseStatus.restored,
            purchaseDetail: purchaseDetail,
            index: purchaseDetail.status == PurchaseStatus.restored
                ? maxIndex
                : 1);
      }
    }
  } else {
    popDialog();
  }
}