stripe_flow.dart 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /// Stripe 流程封装(flutter_stripe 11.x)
  2. ///
  3. /// 覆盖三种入口:
  4. /// 1. PaymentSheet(标准卡支付)
  5. /// 2. Apple Pay(confirmPlatformPayPaymentIntent)
  6. /// 3. Google Pay(confirmPlatformPayPaymentIntent)
  7. library;
  8. import 'package:flutter_stripe/flutter_stripe.dart';
  9. import '../../models/payment.dart';
  10. /// Stripe 封装:初始化 + PaymentSheet + Apple/Google Pay
  11. class StripeFlow {
  12. StripeFlow._();
  13. static final StripeFlow instance = StripeFlow._();
  14. /// 设置发布密钥(App 启动时调用;真实接入以后端下发为准)
  15. void init(String publishableKey) {
  16. Stripe.publishableKey = publishableKey;
  17. }
  18. /// 是否 Mock 模式(后端下发 pk_test_mock 时,无法弹原生面板)
  19. static bool isMock(StripePaymentCreatedResponse created) =>
  20. created.publishableKey.startsWith('pk_test_mock');
  21. /// 初始化并展示 PaymentSheet
  22. /// [created] 后端 createStripePayment 响应
  23. Future<void> presentPaymentSheet(StripePaymentCreatedResponse created) async {
  24. // 真实密钥由后端下发时动态更新
  25. if (created.publishableKey.isNotEmpty) {
  26. Stripe.publishableKey = created.publishableKey;
  27. }
  28. await Stripe.instance.initPaymentSheet(
  29. paymentSheetParameters: SetupPaymentSheetParameters(
  30. merchantDisplayName: 'flutter_paydemo 演示商店',
  31. customerId: created.customer,
  32. paymentIntentClientSecret: created.paymentIntent,
  33. customerEphemeralKeySecret: created.ephemeralKey,
  34. // Apple / Google Pay 开关见对应方法
  35. applePay: const PaymentSheetApplePay(merchantCountryCode: 'US'),
  36. googlePay: const PaymentSheetGooglePay(
  37. merchantCountryCode: 'US',
  38. testEnv: true,
  39. ),
  40. ),
  41. );
  42. await Stripe.instance.presentPaymentSheet();
  43. }
  44. /// 探测 Apple Pay 是否可用(iOS/Safari)
  45. Future<bool> isApplePaySupported() {
  46. return Stripe.instance.isPlatformPaySupported();
  47. }
  48. /// 探测 Google Pay 是否可用(Android/Chrome)
  49. Future<bool> isGooglePaySupported() {
  50. return Stripe.instance.isPlatformPaySupported(
  51. googlePay: const IsGooglePaySupportedParams(testEnv: true),
  52. );
  53. }
  54. /// 确认 Apple Pay 支付
  55. Future<void> confirmApplePay(
  56. StripePaymentCreatedResponse created,
  57. String productName,
  58. int amountUsdCents,
  59. ) async {
  60. await Stripe.instance.confirmPlatformPayPaymentIntent(
  61. clientSecret: created.paymentIntent,
  62. confirmParams: PlatformPayConfirmParams.applePay(
  63. applePay: ApplePayParams(
  64. cartItems: [
  65. ApplePayCartSummaryItem.immediate(
  66. label: productName,
  67. amount: (amountUsdCents / 100).toStringAsFixed(2),
  68. ),
  69. ],
  70. merchantCountryCode: 'US',
  71. currencyCode: 'USD',
  72. ),
  73. ),
  74. );
  75. }
  76. /// 确认 Google Pay 支付
  77. Future<void> confirmGooglePay(
  78. StripePaymentCreatedResponse created,
  79. String productName,
  80. int amountUsdCents,
  81. ) async {
  82. await Stripe.instance.confirmPlatformPayPaymentIntent(
  83. clientSecret: created.paymentIntent,
  84. confirmParams: PlatformPayConfirmParams.googlePay(
  85. googlePay: GooglePayParams(
  86. testEnv: true,
  87. merchantName: productName,
  88. merchantCountryCode: 'US',
  89. currencyCode: 'USD',
  90. ),
  91. ),
  92. );
  93. }
  94. }