apple_pay_method_channel.dart 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import 'package:flutter/foundation.dart';
  2. import 'package:flutter/services.dart';
  3. import 'apple_pay_platform_interface.dart';
  4. /// An implementation of [ApplePayPlatform] that uses method channels.
  5. class MethodChannelApplePay extends ApplePayPlatform {
  6. /// The method channel used to interact with the native platform.
  7. @visibleForTesting
  8. final methodChannel = const MethodChannel('apple_pay');
  9. @override
  10. Future<String?> getPlatformVersion() async {
  11. final version = await methodChannel.invokeMethod<String>('getPlatformVersion');
  12. return version;
  13. }
  14. @override
  15. Future<Map<String, dynamic>> purchase({
  16. required String productId,
  17. String? appAccountToken,
  18. }) async {
  19. try {
  20. final result = await methodChannel.invokeMethod<Map<dynamic, dynamic>>(
  21. 'purchase',
  22. {
  23. 'appleId': productId,
  24. 'appAccountToken': appAccountToken,
  25. },
  26. );
  27. return result?.cast<String, dynamic>() ?? {
  28. 'success': false,
  29. 'error': 'No result returned',
  30. };
  31. } catch (e) {
  32. return {
  33. 'success': false,
  34. 'error': e.toString(),
  35. };
  36. }
  37. }
  38. @override
  39. Future<Map<String, dynamic>> restore() async {
  40. try {
  41. final result = await methodChannel.invokeMethod<Map<dynamic, dynamic>>(
  42. 'restore',
  43. );
  44. return result?.cast<String, dynamic>() ?? {
  45. 'success': false,
  46. 'error': 'No result returned',
  47. };
  48. } catch (e) {
  49. return {
  50. 'success': false,
  51. 'error': e.toString(),
  52. };
  53. }
  54. }
  55. @override
  56. Future<void> check() async {
  57. await methodChannel.invokeMethod<void>(
  58. 'check',
  59. );
  60. }
  61. }