Stripe Connect账户下Angular应用Apple Pay启用问题及时机咨询
问题描述
我在Angular应用中基于Stripe实现Google Pay、Apple Pay等钱包支付功能,采用Stripe Connect账户模式,支付全流程由后端通过PaymentIntent管理,具体流程如下:
- 通过后端创建交易
- 将ClientSecret返回给Angular前端
- 使用ClientSecret创建Stripe PaymentElement
- Webhook捕获
charge.succeed事件后完成订单
目前遇到的问题:Google Pay可正常使用,但苹果设备上仅能选择信用卡支付,无法唤起Apple Pay。我已在Stripe控制台完成域名注册,但查阅文档后发现需要通过API为关联账户创建域名。现咨询两个问题:
- 域名应在何时创建?
- 是否可在后端创建交易的方法中执行该操作?
后端创建交易的代码
private string CreateStripeTransaction(string paymentId, string orderId, string type = "MENU") { var keys = GetPaymentKeys(paymentId); StripeConfiguration.ApiKey = GetStripeKey(); var amount = GetOrderAmount(orderId); var service = new PaymentIntentService(); var options = new PaymentIntentCreateOptions { Amount = amount, Currency = "eur", Metadata = new Dictionary<string, string> { { "integration_check", "accept_a_payment" }, { "OrderId", orderId }, { "PaymentId", paymentId }, { "Company", _company }, { "Type", type } }, }; var requestOptions = new RequestOptions { StripeAccount = keys.publicKey }; var intent = service.Create(options, requestOptions); return JsonSerializer.Serialize(new { client_secret = intent.ClientSecret }); }
前端消费ClientSecret的代码
async initStripe() { this.stripe = await loadStripe( environment.STRIPE, { stripeAccount: this.publicKey, } ); this.paymentService .createTransaction(this.paymentId, this.orderId, this.type, 'stripe') .subscribe((data: any) => { const clientSecret = data.client_secret; const elements = this.stripe.elements({ clientSecret }); const options: StripePaymentElementOptions = { layout: "accordion" }; this.card = elements.create('payment', options); this.card.mount(this.cardElement.nativeElement); this.card.on('change', this.cardHandler); }); }
解决方案
1. 域名创建的时机
域名关联操作不需要每次创建交易都执行,只需为关联账户创建一次即可。建议在以下时机执行:
- 首次为某个关联账户配置支付功能时
- 关联账户的域名发生变更时
重复创建相同域名不会触发错误(Stripe API会返回成功但不重复创建),但从性能角度考虑,优先只执行一次。
2. 是否可在后端创建交易的方法中执行?
可以,但更推荐提前完成域名关联。如果一定要在交易创建流程中执行,需先检查关联账户是否已存在目标域名,不存在则创建。以下是修改后的代码示例:
private string CreateStripeTransaction(string paymentId, string orderId, string type = "MENU") { var keys = GetPaymentKeys(paymentId); var connectedAccountId = keys.accountId; // 注意:这里需替换为关联账户的实际ID(以acct_开头),而非公钥 StripeConfiguration.ApiKey = GetStripeKey(); // 检查并创建关联账户的域名 var domainService = new AccountDomainService(); var existingDomains = domainService.List(new AccountDomainListOptions(), new RequestOptions { StripeAccount = connectedAccountId }); var targetDomain = "your-production-domain.com"; // 替换为已验证的实际域名 var domainExists = existingDomains.Data.Any(d => d.Domain == targetDomain); if (!domainExists) { var domainOptions = new AccountDomainCreateOptions { Domain = targetDomain, Enabled = true }; domainService.Create(domainOptions, new RequestOptions { StripeAccount = connectedAccountId }); } // 原PaymentIntent创建逻辑 var amount = GetOrderAmount(orderId); var service = new PaymentIntentService(); var options = new PaymentIntentCreateOptions { Amount = amount, Currency = "eur", Metadata = new Dictionary<string, string> { { "integration_check", "accept_a_payment" }, { "OrderId", orderId }, { "PaymentId", paymentId }, { "Company", _company }, { "Type", type } }, }; var requestOptions = new RequestOptions { StripeAccount = connectedAccountId }; var intent = service.Create(options, requestOptions); return JsonSerializer.Serialize(new { client_secret = intent.ClientSecret }); }
额外注意事项
- 确保
connectedAccountId是关联账户的实际ID(格式为acct_xxxxxx),原代码中的keys.publicKey属于参数误用,需修正。 - 域名必须是已在Stripe控制台完成验证的域名,且网站已配置HTTPS。
- Apple Pay生效还需确保设备满足Apple Pay的使用条件(如绑定了银行卡、系统版本支持等)。
内容的提问来源于stack exchange,提问作者NiceToMytyuk
相关产品推荐
相关产品推荐

