Now I am develop in app purchase using this lib in flutter. After verify the receipt on the server side, I want to finish the purchase workflow. This is my code in flutter client side:
void _listenToPurchaseUpdated(List<PurchaseDetails> purchaseDetailsList, Context<PayState> ctx) {
purchaseDetailsList.forEach((PurchaseDetails purchaseDetails) async {
if (purchaseDetails.status == PurchaseStatus.pending) {
RestLog.logger("PurchaseStatus pending..." + ctx.state.payModel.isAvailable.toString());
_showPendingUI(ctx);
} else {
if (purchaseDetails.status == PurchaseStatus.error) {
RestLog.logger("PurchaseStatus error");
_handleError(purchaseDetails.error!, ctx);
} else if (purchaseDetails.status == PurchaseStatus.restored) {
RestLog.logger("purchase successful trigger verify");
PayVerifyModel payVerifyModel = PayVerifyModel(orderId: purchaseDetails.purchaseID, receipt: purchaseDetails.verificationData.serverVerificationData);
int result = await Pay.verifyUserPay(payVerifyModel);
if(result == 0){
RestLog.logger("verify success:" + result.toString());
await InAppPurchase.instance.completePurchase(purchaseDetails);
}else{
RestLog.logger("verify failed:" + result.toString());
}
}else if(purchaseDetails.status == PurchaseStatus.purchased ){
RestLog.logger("purchaseDetails purchased:" + purchaseDetails.productID);
}
if (purchaseDetails.pendingCompletePurchase) {
await InAppPurchase.instance.completePurchase(purchaseDetails);
}
}
});
}
after verify success, I am using this code to finish the purchase:
await InAppPurchase.instance.completePurchase(purchaseDetails);
but it seems did not work. It still runs in verify workflow the next time enter the purchase page, am I missing something? what should I do to make it work? This function InAppPurchase.instance.completePurchase did not return any status so I could not if it execute success.
I have tried to wrap a try catch in the outer level if the InAppPurchase.instance.completePurchase(purchaseDetails) code run into issue.But there is no error output.
I just do not known the transaction of purchase finished success. what should I do to get the result of complete transaction?