在Flutter中对接Apple Pay,可以通过以下步骤实现:
1. 添加依赖
在 pubspec.yaml 中添加 flutter_stripe 或 pay 插件:
dependencies:
  flutter_stripe: ^10.0.0
  # 或使用 pay 插件
  pay: ^2.0.0
运行 flutter pub get 安装依赖。
2. 配置 iOS 项目
- 在 ios/Runner/Info.plist中添加 Apple Pay 权限:
<key>NSApplePayUsageDescription</key>
<string>用于支付订单</string>
- 在 Xcode 中为项目启用 Apple Pay 并配置 Merchant ID。
3. 使用 flutter_stripe 实现
初始化 Stripe:
import 'package:flutter_stripe/flutter_stripe.dart';
void main() {
  Stripe.publishableKey = 'YOUR_STRIPE_PUBLISHABLE_KEY';
  runApp(MyApp());
}
创建支付表单:
Future<void> payWithApplePay() async {
  try {
    final paymentMethod = await Stripe.instance.createApplePayPaymentMethod(
      const ApplePayPresentParams(
        merchantId: 'YOUR_MERCHANT_ID',
        currency: 'USD',
        items: [
          ApplePayCartSummaryItem(
            label: '商品名称',
            amount: '10.00',
          ),
        ],
      ),
    );
    
    // 将 paymentMethod.id 发送到你的服务器完成支付
    await processPaymentOnServer(paymentMethod.id);
  } catch (e) {
    print('支付失败: $e');
  }
}
4. 使用 pay 插件实现
import 'package:pay/pay.dart';
final _paymentItems = [
  PaymentItem(
    label: '商品名称',
    amount: '10.00',
    status: PaymentItemStatus.final_price,
  )
];
void applePayButton() {
  ApplePayButton(
    paymentConfiguration: PaymentConfiguration.fromJsonString('{}'),
    paymentItems: _paymentItems,
    onPaymentResult: (result) {
      // 处理支付结果
    },
  );
}
5. 服务器端处理
- 使用 Stripe 或其他支付服务商处理支付确认。
- 验证支付状态并完成订单。
注意事项:
- 测试:在 Sandbox 环境中使用测试账户。
- 证书:确保 Merchant ID 和 SSL 证书配置正确。
- 审核:提交 App Store 时需说明 Apple Pay 用途。
通过以上步骤,即可在 Flutter 中集成 Apple Pay 支付功能。