You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET MAUI集成Stripe实现ApplePay、GooglePay支付技术求助

.NET MAUI 集成Stripe Apple Pay/Google Pay 实现指南

通用实现流程

不管是Apple Pay还是Google Pay,Stripe移动端支付的核心逻辑都是后端驱动,客户端负责唤起系统支付组件并传递结果:

  • 后端创建PaymentIntent:这是Stripe支付的核心凭证,必须由后端生成(避免客户端篡改金额等敏感数据)
  • 客户端初始化Stripe SDK:根据平台配置唤起Apple Pay/Google Pay原生组件
  • 客户端完成支付授权:用户确认支付后,Stripe SDK会自动处理与支付网关的交互,返回PaymentIntent状态
  • 后端验证支付结果:客户端将PaymentIntent的client_secret传回后端,后端调用Stripe API确认支付状态,再执行业务逻辑(如更新订单)

与Web端支付的核心差异

  • 交互方式:Web端通过Stripe.js在浏览器内唤起支付弹窗;移动端直接调用系统原生支付组件(Apple依赖Wallet框架,Google依赖Google Pay API),需要单独配置平台权限
  • 安全验证:Web端验证域名所有权;移动端需绑定App ID与Stripe Merchant ID(Apple),或在Google Play Console配置商户信息+SHA-1指纹(Google)
  • SDK集成:Web用Stripe.js,移动端需通过Stripe官方的.NET MAUI绑定包或社区包集成原生SDK,而非纯前端脚本

分平台实现步骤与代码示例

前置准备

  1. 安装依赖:
    • 后端:Stripe.NET NuGet包(用于创建PaymentIntent)
    • 客户端:Stripe.Maui NuGet包(Stripe官方提供的MAUI集成包)
  2. Stripe后台配置:
    • Apple Pay:上传Merchant ID证书,关联你的iOS App Bundle ID
    • Google Pay:启用Google Pay API,配置商户号,绑定Android App的SHA-1指纹

后端代码(创建PaymentIntent)

using Stripe;

public async Task<PaymentIntent> CreatePaymentIntent(long amount, string currency)
{
    StripeConfiguration.ApiKey = "你的Stripe Secret Key";
    
    var options = new PaymentIntentCreateOptions
    {
        Amount = amount, // 金额单位为最小货币单位,如100代表1美元
        Currency = currency,
        PaymentMethodTypes = new List<string> { "apple_pay", "google_pay" }
    };
    
    var service = new PaymentIntentService();
    return await service.CreateAsync(options);
}

.NET MAUI客户端代码

初始化Stripe

在MauiProgram.cs中注册Stripe服务:

using Stripe.Maui;

builder.Services.AddStripeMaui(options =>
{
    options.PublishableKey = "你的Stripe Publishable Key";
});

唤起Apple Pay(iOS)

using Stripe.Maui;
using System.Text.Json;

private async Task TriggerApplePay()
{
    // 调用后端获取PaymentIntent的client_secret
    var clientSecret = await _httpClient.GetStringAsync("https://你的后端地址/api/create-payment-intent");
    
    var applePayOpts = new ApplePayPaymentMethodCreateOptions
    {
        Currency = "usd",
        MerchantIdentifier = "你的Stripe Merchant ID"
    };
    
    var paymentSheetOpts = new PaymentSheetOptions
    {
        PaymentIntentClientSecret = clientSecret,
        ApplePay = applePayOpts
    };
    
    var paymentSheet = StripeMaui.Instance.PaymentSheet;
    var result = await paymentSheet.PresentAsync(paymentSheetOpts);
    
    if (result.Status == PaymentSheetResultStatus.Completed)
    {
        // 通知后端验证支付结果
        await _httpClient.PostAsync("https://你的后端地址/api/verify-payment", 
            new StringContent(JsonSerializer.Serialize(new { ClientSecret = clientSecret }), 
            System.Text.Encoding.UTF8, "application/json"));
    }
}

唤起Google Pay(Android)

using Stripe.Maui;
using System.Text.Json;

private async Task TriggerGooglePay()
{
    var clientSecret = await _httpClient.GetStringAsync("https://你的后端地址/api/create-payment-intent");
    
    var googlePayOpts = new GooglePayPaymentMethodCreateOptions
    {
        Currency = "usd",
        MerchantName = "你的商户名称"
    };
    
    var paymentSheetOpts = new PaymentSheetOptions
    {
        PaymentIntentClientSecret = clientSecret,
        GooglePay = googlePayOpts
    };
    
    var paymentSheet = StripeMaui.Instance.PaymentSheet;
    var result = await paymentSheet.PresentAsync(paymentSheetOpts);
    
    if (result.Status == PaymentSheetResultStatus.Completed)
    {
        await _httpClient.PostAsync("https://你的后端地址/api/verify-payment", 
            new StringContent(JsonSerializer.Serialize(new { ClientSecret = clientSecret }), 
            System.Text.Encoding.UTF8, "application/json"));
    }
}

平台特定配置

  • iOS:在Info.plist中添加Apple Pay权限配置:
<key>PKPaymentMerchantIdentifier</key>
<string>你的Merchant ID</string>
<key>NSPaymentProcessingUsageDescription</key>
<string>使用Apple Pay完成支付</string>
  • Android:在AndroidManifest.xml中启用Google Pay:
<meta-data
    android:name="com.google.android.gms.wallet.api.enabled"
    android:value="true" />

关键注意事项

  • 禁止客户端生成PaymentIntent:所有支付凭证必须由后端创建,防止恶意篡改金额
  • 必须后端验证支付状态:客户端返回的成功结果不可信,后端需调用Stripe的PaymentIntentService.GetAsync(clientSecret)确认状态
  • 测试环境限制:Apple Pay需要真机测试(模拟器不支持);Google Pay需在测试环境配置测试商户号,或使用Stripe提供的测试卡片
  • 异常处理:需捕获支付取消、网络错误、Stripe API异常等场景,给用户明确提示

内容的提问来源于stack exchange,提问作者crazydev610

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 12:23:22