【问题标题】:how to check if iap transactions are finished?如何检查iap交易是否完成?
【发布时间】:2021-09-13 13:44:46
【问题描述】:

我正在使用https://pub.dev/packages/flutter_inapp_purchase 这个包进行应用内购买。我出售了积分,但用户可以获得比他们支付的更多的积分,如此视频所示:

https://youtu.be/Bo5MQdi5wGY

我该如何修复这个错误?请帮帮我。谢谢。

所有相关的代码行都在下面;

代码:

Future<void> initPlatformState() async {

await Purchases.setDebugLogsEnabled(true);
await Purchases.setup("RowkOdfhhydoMREZKRckltCNUNzJqgnj");
String platformVersion;
try {
  platformVersion = await FlutterInappPurchase.instance.platformVersion;
} on PlatformException {
  platformVersion = 'Failed to get platform version.';
}
var result = await FlutterInappPurchase.instance.initConnection;
print('result: $result');
if (!mounted) return;

setState(() {
  _platformVersion = platformVersion;
});

try {
  String msg = await FlutterInappPurchase.instance.consumeAllItems;
  print('consumeAllItems: $msg');

} catch (err) {
  print('consumeAllItems error: $err');
}
_conectionSubscription =
    FlutterInappPurchase.connectionUpdated.listen((connected) {
      print('connected: $connected');

    });




_purchaseUpdatedSubscription =
    FlutterInappPurchase.purchaseUpdated.listen((productItem) {
      // String deviceId = await PlatformDeviceId.getDeviceId;

      dispose();
      initPlatformState();

      //FlutterInappPurchase.instance.finishTransaction(productItem);



      if (productItem.productId == "oddinbetcredit10") {

        print("CREDİT 10 !!!");

        addCredit(deviceid, "10").then((value) => {

          tipController.updateCredit(),
        Fluttertoast.showToast(
        msg: "Successfully loaded 10 credit !...",
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.SNACKBAR,
        timeInSecForIosWeb: 1,
        backgroundColor: Colors.red,
        textColor: Colors.white,
        fontSize: 16.0
        )
        });

      } else if (productItem.productId == "oddinbetcredit20") {
        print("CREDİT 20 !!!");

        addCredit(deviceid, "20").then((value) => {



          tipController.updateCredit(),
        Fluttertoast.showToast(
        msg: "20 credits successfully loaded !...",
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.SNACKBAR,
        timeInSecForIosWeb: 1,
        backgroundColor: Colors.red,
        textColor: Colors.white,
        fontSize: 16.0
        )
        });


      } else if (productItem.productId == "oddinbetcredit50") {
        print("CREDİT 50 !!!");

        addCredit(deviceid, "50").then((value) => {

          tipController.updateCredit(),
        Fluttertoast.showToast(
        msg: "50 credits successfully loaded !...",
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.SNACKBAR,
        timeInSecForIosWeb: 1,
        backgroundColor: Colors.red,
        textColor: Colors.white,
        fontSize: 16.0
        )
        });


      } else if (productItem.productId == "oddinbetcredit100") {
        print("CREDİT 100 !!!");

        addCredit(deviceid, "100").then((value) => {

          tipController.updateCredit(),
        Fluttertoast.showToast(
        msg: "100 crdits successfully loaded!...",
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.SNACKBAR,
        timeInSecForIosWeb: 1,
        backgroundColor: Colors.red,
        textColor: Colors.white,
        fontSize: 16.0
        )

        });


      }  else if (productItem.productId == "oddinbetcreditvip"){


        isPremium = true;
        Fluttertoast.showToast(
            msg: "VIP successfully loaded!...",
            toastLength: Toast.LENGTH_SHORT,
            gravity: ToastGravity.SNACKBAR,
            timeInSecForIosWeb: 1,
            backgroundColor: Colors.red,
            textColor: Colors.white,
            fontSize: 16.0
        );


      }
    });

_purchaseErrorSubscription =
    FlutterInappPurchase.purchaseError.listen((purchaseError) {
      print('purchase-error: $purchaseError');
    });

}

【问题讨论】:

    标签: flutter in-app-purchase


    【解决方案1】:

    其实这是这个插件的一个BUG,在Github上还是开放的

    您获得更多积分的原因是因为 purchaseUpdated 侦听器在 iOS 上收到多个回调,因此您的 addCredit() 函数被多次调用。

    作为一种解决方法,您可以使用 transactionId 作为标志,并避免多次调用 purchaseUpdated 侦听器内的代码。

    添加支票

    if(lastTransactionId!=productItem.transactionId)  
    

    并在if语句体之外初始化lastTransactionId

     lastTransactionId = productItem.transactionId;
    

    完整代码:

    声明一个全局变量

    String lastTransactionId;
    

    然后用这个更新你的监听器代码

    _purchaseUpdatedSubscription =
        FlutterInappPurchase.purchaseUpdated.listen((productItem) {
          // String deviceId = await PlatformDeviceId.getDeviceId;
          if(lastTransactionId!=productItem.transactionId){  //Added this to avoid multiple callbacks for the same transaction
            
          dispose();
          initPlatformState();
    
          //FlutterInappPurchase.instance.finishTransaction(productItem);
    
    
    
          if (productItem.productId == "oddinbetcredit10") {
            print("CREDİT 10 !!!");
            addCredit(deviceid, "10").then((value) => {
              tipController.updateCredit(),
            Fluttertoast.showToast(
            msg: "Successfully loaded 10 credit !...",
            toastLength: Toast.LENGTH_SHORT,
            gravity: ToastGravity.SNACKBAR,
            timeInSecForIosWeb: 1,
            backgroundColor: Colors.red,
            textColor: Colors.white,
            fontSize: 16.0
            )
            });
    
          } else if (productItem.productId == "oddinbetcredit20") {
            print("CREDİT 20 !!!");
            addCredit(deviceid, "20").then((value) => {
              tipController.updateCredit(),
            Fluttertoast.showToast(
            msg: "20 credits successfully loaded !...",
            toastLength: Toast.LENGTH_SHORT,
            gravity: ToastGravity.SNACKBAR,
            timeInSecForIosWeb: 1,
            backgroundColor: Colors.red,
            textColor: Colors.white,
            fontSize: 16.0
            )
            });
          } else if (productItem.productId == "oddinbetcredit50") {
            print("CREDİT 50 !!!");
            addCredit(deviceid, "50").then((value) => {
              tipController.updateCredit(),
            Fluttertoast.showToast(
            msg: "50 credits successfully loaded !...",
            toastLength: Toast.LENGTH_SHORT,
            gravity: ToastGravity.SNACKBAR,
            timeInSecForIosWeb: 1,
            backgroundColor: Colors.red,
            textColor: Colors.white,
            fontSize: 16.0
            )
            });
    
          } else if (productItem.productId == "oddinbetcredit100") {
            print("CREDİT 100 !!!");
            addCredit(deviceid, "100").then((value) => {
              tipController.updateCredit(),
            Fluttertoast.showToast(
            msg: "100 crdits successfully loaded!...",
            toastLength: Toast.LENGTH_SHORT,
            gravity: ToastGravity.SNACKBAR,
            timeInSecForIosWeb: 1,
            backgroundColor: Colors.red,
            textColor: Colors.white,
            fontSize: 16.0
            )
            });
          }  else if (productItem.productId == "oddinbetcreditvip"){
            isPremium = true;
            Fluttertoast.showToast(
                msg: "VIP successfully loaded!...",
                toastLength: Toast.LENGTH_SHORT,
                gravity: ToastGravity.SNACKBAR,
                timeInSecForIosWeb: 1,
                backgroundColor: Colors.red,
                textColor: Colors.white,
                fontSize: 16.0
            );
          }
          }
          lastTransactionId = productItem.transactionId; //finally initialize your lastTransactionId with productItem.transactionId, outside the if statement
        });
    

    PS:你应该只调用一次await FlutterInappPurchase.instance.initConnection; 方法。

    我希望这个答案有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-07
      • 2022-09-22
      相关资源
      最近更新 更多