Handling idempotent endpoints in flutter API makes the request twice on connection timeout

77 Views Asked by At

I have a button when I click it call a payment API that transfers money from the user to us then there is a second API that buys a product for them, now the problem I face is that the first API that transfers money from them to us will work perfectly but the second API might then face connection timeout and not give them the product, sometimes it will face connection timeout and still give them the product and I wont know because I did not get response that product purchased successfully, they usually give the users the products before sending the response of product purchased no I wont know if the product purchased successfully when it faced connection timeout or it faced connection timeout and they did not get the product and I wrote a function that retry the API call if connection timeout but this might end up buying the product twice for them. this is the code I wrote to retry the API call on connection timeout or connection error.

 Future<Response> testDisconnect(back) async {
    debugPrint("meeow");
    debugPrint(back);
    try {
      return await dio.post("apiurl",
          data: {"me": ",e"},
          options: Options(
            headers: {
              'Content-Type': 'application/json',
              'Accept': 'application/json'
            },
          ));
    } on DioException catch (e) {
      debugPrint(e.type.toString());
      return testDisconnect(back);
    }
  }
0

There are 0 best solutions below