| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- 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<String?> getPlatformVersion() async {
- final version = await methodChannel.invokeMethod<String>('getPlatformVersion');
- return version;
- }
- @override
- Future<Map<String, dynamic>> purchase({
- required String productId,
- String? appAccountToken,
- }) async {
- try {
- final result = await methodChannel.invokeMethod<Map<dynamic, dynamic>>(
- 'purchase',
- {
- 'appleId': productId,
- 'appAccountToken': appAccountToken,
- },
- );
- return result?.cast<String, dynamic>() ?? {
- 'success': false,
- 'error': 'No result returned',
- };
- } catch (e) {
- return {
- 'success': false,
- 'error': e.toString(),
- };
- }
- }
- @override
- Future<Map<String, dynamic>> restore() async {
- try {
- final result = await methodChannel.invokeMethod<Map<dynamic, dynamic>>(
- 'restore',
- );
- return result?.cast<String, dynamic>() ?? {
- 'success': false,
- 'error': 'No result returned',
- };
- } catch (e) {
- return {
- 'success': false,
- 'error': e.toString(),
- };
- }
- }
- @override
- Future<bool> check(String appleId) async {
- try {
- // 调用原生方法并处理可能的空值
- final result = await methodChannel.invokeMethod<bool>('check',
- {'appleId': appleId},
- );
- return result ?? false; // 如果结果为 null,返回 false
- } on PlatformException catch (e) {
- print('检查试用资格失败: ${e.message}');
- return false; // 发生错误时返回 false
- } catch (e) {
- print('未知错误: $e');
- return false;
- }
- }
- }
|