如何将Stripe客户的PaymentMethod自动应用于订阅支付?
Stripe自动支付配置补充问询
业务场景
- 企业客户购买实物商品,移动端收集信息后,服务器成功创建Stripe Customer与PaymentIntent(已在Stripe后台验证);
- 支付完成后,Webhook捕获
charge.succeeded事件,尝试借此将PaymentMethod设置为自动支付(含确认及重定向URL),但全部失败; - 创建订阅后,期望使用该客户的PaymentMethod完成订阅支付,但支付状态显示未完成,需手动确认。
服务端处理流程及问题
- 创建PaymentIntent代码
Stripe.apiKey = API_SECRET_KEY; long totalCharge = calcTotalCharge(purchaseRequest.getRequestedProducts()); PaymentIntentCreateParams paymentIntentCreateParams = PaymentIntentCreateParams.builder() .setCustomer(customer.getId()) .setAmount(totalCharge) .setCurrency("usd") .setDescription(OFFICE_PURCHASE) .setSetupFutureUsage(SetupFutureUsage.OFF_SESSION) .setAutomaticPaymentMethods(PaymentIntentCreateParams.AutomaticPaymentMethods.builder() .setEnabled(true) .build()) .build(); PaymentIntent paymentIntent = PaymentIntent.create(paymentIntentCreateParams); SetupIntentCreateParams setupIntentParms = SetupIntentCreateParams.builder() .setCustomer(customer.getId()) .addPaymentMethodType("card") .build(); SetupIntent setupIntent = SetupIntent.create(setupIntentParms);
使用Stripe Elements完成订单,但此时无法设置确认或自动支付,因尚未获取PaymentMethod。
- Webhook处理异常
抛出异常:java.lang.RuntimeException: com.stripe.exception.InvalidRequestException: Received unknown parameters: enabled, returnUrl, confirm; code: parameter_unknown; request-id: req_My6nCQVFVNbsSgtry
对应代码:
try { PaymentIntent paymentIntent = PaymentIntent.retrieve(charge.getPaymentIntent()); Map<String, Object> automaticPaymentMethods = new HashMap<>(); automaticPaymentMethods.put("enabled", true); automaticPaymentMethods.put("confirm", true); automaticPaymentMethods.put("returnUrl", "https://cnn.com"); // 仅为满足Stripe要求,无实际作用 logger.info("webhook updating paymentIntent.automatic payment method as {} ", paymentIntent); } catch (StripeException e) { throw new RuntimeException(e); }
- 核心疑问
如何将客户PaymentMethod设置为自动确认支付(订阅用户无法手动确认),同时对自定义URL Scheme或Universal Link存在疑问。
更新内容
已调整支付完成后Webhook的处理代码,不再尝试设置automaticPaymentMethods,改为更新客户的默认PaymentMethod,但问题仍未解决:
PaymentIntent paymentIntent = PaymentIntent.retrieve(charge.getPaymentIntent()); paymentIntent.getAutomaticPaymentMethods().setEnabled(true); String paymentMethod = charge.getPaymentMethod(); String customerId = charge.getCustomer(); Long chargeAmount = charge.getAmountCaptured(); // 用paymentMethod更新待处理订单 try { Customer customer = Customer.retrieve(customerId); customer.update(CustomerUpdateParams.builder() .setInvoiceSettings(InvoiceSettings.builder() .setDefaultPaymentMethod(paymentMethod) .build()) .build()); } catch (StripeException se) { logger.error("unable to customer {} the paymentMethod {}", customerId, paymentMethod); }
内容的提问来源于stack exchange,提问作者sonoerin
相关产品推荐
相关产品推荐

