import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; import 'apple_pay_platform_interface.dart'; /// An implementation of [ApplePayPlatform] that uses method channels. class MethodChannelApplePay extends ApplePayPlatform { /// The method channel used to interact with the native platform. @visibleForTesting final methodChannel = const MethodChannel('apple_pay'); @override Future getPlatformVersion() async { final version = await methodChannel.invokeMethod('getPlatformVersion'); return version; } @override Future> purchase({ required String productId, String? appAccountToken, }) async { try { final result = await methodChannel.invokeMethod>( 'purchase', { 'appleId': productId, 'appAccountToken': appAccountToken, }, ); return result?.cast() ?? { 'success': false, 'error': 'No result returned', }; } catch (e) { return { 'success': false, 'error': e.toString(), }; } } @override Future> restore() async { try { final result = await methodChannel.invokeMethod>( 'restore', ); return result?.cast() ?? { 'success': false, 'error': 'No result returned', }; } catch (e) { return { 'success': false, 'error': e.toString(), }; } } @override Future check(String appleId) async { try { // 调用原生方法并处理可能的空值 final result = await methodChannel.invokeMethod('check', {'appleId': appleId}, ); return result ?? false; // 如果结果为 null,返回 false } on PlatformException catch (e) { print('检查试用资格失败: ${e.message}'); return false; // 发生错误时返回 false } catch (e) { print('未知错误: $e'); return false; } } }