Repository: flutter
Issue: Flutter iOS in-app purchase restore purchases no receipts
Labels: platform-ios,p: in_app_purchase,package,has reproducible steps,P2,found in release: 3.10,found in release: 3.12,team-ios,triaged-ios

### Is there an existing issue for this?

- [X] I have searched the [existing issues](https://github.com/flutter/flutter/issues)
- [X] I have read the [guide to filing a bug](https://flutter.dev/docs/resources/bug-reports)

### Steps to reproduce

1. Complete at least one purchase with a sandbox user
2. "PurchaseDetails" not received in "_listenToPurchaseUpdated" after calling "InAppPurchase.instance.restorePurchases" method with the same sandbox user

### Expected results

_listenToPurchaseUpdated should receive data

### Actual results

_listenToPurchaseUpdated monitored data length is 0

### Code sample

<details><summary>Podfile.lock</summary>

```
PODS:
  - Flutter (1.0.0)
  - in_app_purchase_storekit (0.0.1):
    - Flutter
    - FlutterMacOS

DEPENDENCIES:
  - Flutter (from `Flutter`)
  - in_app_purchase_storekit (from `.symlinks/plugins/in_app_purchase_storekit/darwin`)

EXTERNAL SOURCES:
  Flutter:
    :path: Flutter
  in_app_purchase_storekit:
    :path: ".symlinks/plugins/in_app_purchase_storekit/darwin"

SPEC CHECKSUMS:
  Flutter: f04841e97a9d0b0a8025694d0796dd46242b2854
  in_app_purchase_storekit: 4fb7ee9e824b1f09107fbfbbce8c4b276366dc43

PODFILE CHECKSUM: 70d9d25280d0dd177a5f637cdb0f0b0b12c6a189

COCOAPODS: 1.12.1

```

</details>


<details><summary>pubspec.yaml</summary>

```yaml
name: xxx
description: A new Flutter project.
version: 1.0.0+1

environment:
  sdk: '>=3.0.5 <4.0.0'
dependencies:
  flutter:
    sdk: flutter
  cupertino_icons: ^1.0.2
  in_app_purchase: ^3.1.7

dev_dependencies:
  flutter_test:
    sdk: flutter
  flutter_lints: ^2.0.0
flutter:
  uses-material-design: true

```

</details>


<details><summary>Code sample</summary>

```dart
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:in_app_purchase/in_app_purchase.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  late StreamSubscription<List<PurchaseDetails>> _subscription;

  void _iosBuy() async {
    const Set<String> kIds = <String>{'xxx'};
    final ProductDetailsResponse response =
        await InAppPurchase.instance.queryProductDetails(kIds);
    if (response.notFoundIDs.isNotEmpty) {
      // Handle the error.
    }
    List<ProductDetails> products = response.productDetails;
    print(products);
    // await InAppPurchase.instance.restorePurchases();
    final PurchaseParam purchaseParam = PurchaseParam(
      productDetails: products[0],
    );
    await InAppPurchase.instance.buyNonConsumable(purchaseParam: purchaseParam);
    print('restorePurchases');
  }

  void _restorePurchases() async {
    InAppPurchase.instance.restorePurchases();
  }

  //支付过程监听
  _listenToPurchaseUpdated(List<PurchaseDetails> purchaseDetailsList) async {
    print('purchase length: ${purchaseDetailsList.length}');
    for (var purchaseDetails in purchaseDetailsList) {
      // print(purchaseDetails.status);
      var status = purchaseDetails.status;
      if (status == PurchaseStatus.pending) {
        print('paying');
      } else {
        if (status == PurchaseStatus.error) {
          print('pay error');
          print(purchaseDetails.error);
        } else if (status == PurchaseStatus.purchased ||
            status == PurchaseStatus.restored) {
          print('pay success');
        } else if (status == PurchaseStatus.canceled) {
          print('pay cancel');
        }
        if (purchaseDetails.pendingCompletePurchase) {
          print('pendingCompletePurchase');
          await InAppPurchase.instance.completePurchase(purchaseDetails);
        }
      }
    }
  }

  @override
  void initState() {
    final Stream<List<PurchaseDetails>> purchaseUpdated =
        InAppPurchase.instance.purchaseStream;
    _subscription = purchaseUpdated.listen((purchaseDetailsList) {
      _listenToPurchaseUpdated(purchaseDetailsList);
    }, onDone: () {
      _subscription.cancel();
    }, onError: (error) {
      // handle error here.
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'pay',
              style: Theme.of(context).textTheme.headlineMedium,
            ),
          ],
        ),
      ),
      floatingActionButton: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          FloatingActionButton(
            onPressed: _iosBuy,
            child: const Text('buy'),
          ),
          const SizedBox(width: 20),
          FloatingActionButton(
            onPressed: _restorePurchases,
            child: const Text('restore'),
          ),
        ],
      ),
    );
  }
}

```

</details>


### Screenshots or Video

<details>
<summary>Screenshots / Video demonstration</summary>

[Upload media here]

</details>


### Logs

<details>Click the buy button and complete the payment log output</summary>

```console
Restarted application in 422ms.
flutter: [Instance of 'AppStoreProductDetails']
flutter: restorePurchases
flutter: purchase length: 1
flutter: paying
flutter: purchase length: 1
flutter: pay success
flutter: pendingCompletePurchase
```

</details>

<details>Log output of clicking the restore button</summary>

```console
[Restarted application in 422ms.
flutter: [Instance of 'AppStoreProductDetails']
flutter: restorePurchases
flutter: purchase length: 1
flutter: paying
flutter: purchase length: 1
flutter: pay success
flutter: pendingCompletePurchase](flutter: purchase length: 0)
```

</details>


### Flutter Doctor output

<details><summary>Doctor output</summary>

```console
╰─➤  flutter doctor --verbose
[✓] Flutter (Channel stable, 3.10.5, on macOS 13.4.1 22F82 darwin-arm64, locale zh-Hans-CN)
    • Flutter version 3.10.5 on channel stable at /usr/local/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 796c8ef792 (3 weeks ago), 2023-06-13 15:51:02 -0700
    • Engine revision 45f6e00911
    • Dart version 3.0.5
    • DevTools version 2.23.1
    • Pub download mirror https://pub.flutter-io.cn
    • Flutter download mirror https://storage.flutter-io.cn

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.2)
    • Android SDK at /Users/dly/Library/Android/sdk/
    • Platform android-33, build-tools 33.0.2
    • ANDROID_HOME = /Users/dly/Library/Android/sdk/
    • ANDROID_SDK_ROOT = /Users/dly/Library/Android/sdk/
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 14.3.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14E300c
    • CocoaPods version 1.12.1

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2022.2)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694)

[✓] VS Code (version 1.79.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.66.0

[✓] VS Code (version 1.79.0-insider)
    • VS Code at /Applications/Visual Studio Code - Insiders.app/Contents
    • Flutter extension version 3.64.0

[✓] Connected device (3 available)
    • ***iPhone (mobile) • 00008101-00040D2434A2001E • ios            • iOS 16.5.1 20F75
    • macOS (desktop)     • macos                     • darwin-arm64   • macOS 13.4.1 22F82 darwin-arm64
    • Chrome (web)        • chrome                    • web-javascript • Google Chrome 114.0.5735.198

[✓] Network resources
    • All expected network resources are available.

• No issues found!
```

</details>