connectionInPay method
Implementation
Future connectionInPay(String? id, String? payId, String? price,
{String? inType}) async {
if (await checkInPay(first: false) == false && !showLoading) {
if (inType != null) {
payInType = inType;
} else {
payInType = null;
}
if (!showLoading) {
showLoading = true;
delegate.showLoadingDialog();
}
final bool isAvailable = await connection.isAvailable();
if (isAvailable) {
Set<String> _kProductIds = {payId!};
ProductDetailsResponse productDetailResponse =
await connection.queryProductDetails(_kProductIds);
if (productDetailResponse.error == null) {
List<ProductDetails> products = productDetailResponse.productDetails;
if (products.isNotEmpty) {
return RechargeDao.rechargeOrder(id).then((res) async {
if (res.result) {
bool buy = false;
try {
PurchaseParam purchaseParam =
PurchaseParam(productDetails: products[0]);
buy = await connection.buyConsumable(
purchaseParam: purchaseParam,
autoConsume: !Platform.isAndroid);
AppConfig.printLog('buyConsumable: $buy');
if (buy) {
orderId = res.data.id;
payPrice = price;
SpUtil.putString(RechargeConfig.inappOrderId, orderId!);
SpUtil.putString(RechargeConfig.inappPrice, payPrice!);
return true;
}
} catch (e) {
delegate.logEvent('${payInType ?? ''}发起支付失败');
AppConfig.printLog(e);
popDialog();
showInPay = true;
delegate.showDialogBase(1).then((value) async {
showInPay = false;
if (value == true) {
queryPastPurchases(refresh: true);
}
});
RechargeDao.rechargeFailLog('', '', orderId, e.toString());
return false;
}
}
delegate.logEvent('${payInType ?? ''}统一下单失败');
popDialog();
delegate.showErrorToast(res);
return false;
});
}
}
}
delegate.logEvent('${payInType ?? ''}支付不存在');
popDialog();
delegate.showToast(2);
return false;
} else {
return false;
}
}