AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题 / 79275511
Accepted
Andyally
Andyally
Asked: 2024-12-12 22:40:02 +0800 CST2024-12-12 22:40:02 +0800 CST 2024-12-12 22:40:02 +0800 CST

我可以通过一次通话确认付款并保存卡以供将来订阅使用吗?

  • 772

有没有办法创建 Stipe 订阅(我使用 Java 和 React),这将需要添加卡以用于将来的订阅付款,并在前端确认期间通过一次调用立即收取第一笔金额。现在我能够研究的是,您首先需要创建订阅并通过首先调用前端进行确认 SetupIntent,clientSecret确认stripe.confirmCardSetup成功后 -stripe.confirmCardPayment从中clientSecret​​检索 启动subscription.getLatestInvoiceObject().getPaymentIntentObject().getClientSecret();

是否可以在拨打电话时将卡详细信息与订阅关联,stripe.confirmCardPayment而不是confirmCardSetup先拨打电话?我认为,如果您将其confirmCardPayment与clientSecret订阅关联,则无需先确认卡设置,卡详细信息将自动保存用于订阅?

stripe-payments
  • 1 1 个回答
  • 17 Views

1 个回答

  • Voted
  1. Best Answer
    archiverat
    2024-12-13T06:35:48+08:002024-12-13T06:35:48+08:00

    Stripe 公共文档有一个非常好的演练,描述了处理此问题的推荐方法:

    https://docs.stripe.com/billing/subscriptions/build-subscriptions?platform=web&ui=elements&lang=java#create-subscription

    简而言之,流程如下:

    • 您的后端创建订阅并使用扩展传latest_invoice.payment_intent.client_secret回前端。
      • 该payment_settings.save_default_payment_method参数可用于告诉 Stripe 稍后自动保存付款方式。https ://docs.stripe.com/api/subscriptions/create#create_subscription-payment_settings-save_default_payment_method
    • 您的前端使用您从后端获取的客户端密钥来安装支付元素。
    • 提交前端 Stripe 付款元素后,您可以使用 stripe.confirmPayment 立即确认付款详情并尝试向卡扣款。这也是 Stripe 自动保存付款方式的地方,前提是您在步骤 1 中传递了正确的参数。

    以下是相关的代码片段,向您展示如何配置服务器代码以保存默认付款方式并传回客户端机密。

    // Set your secret key. Remember to switch to your live secret key in production.
    // See your keys here: https://dashboard.stripe.com/apikeys
    Stripe.apiKey = "your_test_key";
    
    post(
      "/create-subscription",
      (request, response) -> {
        response.type("application/json");
        String customerId = request.cookie("customer");
        CreateSubscriptionRequest postBody = gson.fromJson(
          request.body(),
          CreateSubscriptionRequest.class
        );
        String priceId = postBody.getPriceId();
    
        // Automatically save the payment method to the subscription
        // when the first payment is successful
        SubscriptionCreateParams.PaymentSettings paymentSettings =
          SubscriptionCreateParams.PaymentSettings
            .builder()
            .setSaveDefaultPaymentMethod(SaveDefaultPaymentMethod.ON_SUBSCRIPTION)
            .build();
    
        // Create the subscription. Note we're expanding the Subscription's
        // latest invoice and that invoice's payment_intent
        // so we can pass it to the front end to confirm the payment
        SubscriptionCreateParams subCreateParams = SubscriptionCreateParams
          .builder()
          .setCustomer(customerId)
          .addItem(
            SubscriptionCreateParams
              .Item.builder()
              .setPrice(priceId)
              .build()
          )
          .setPaymentSettings(paymentSettings)
          .setPaymentBehavior(SubscriptionCreateParams.PaymentBehavior.DEFAULT_INCOMPLETE)
          .addAllExpand(Arrays.asList("latest_invoice.payment_intent"))
          .build();
    
        Subscription subscription = Subscription.create(subCreateParams);
    
        Map<String, Object> responseData = new HashMap<>();
        responseData.put("subscriptionId", subscription.getId());
        responseData.put("clientSecret", subscription.getLatestInvoiceObject().getPaymentIntentObject().getClientSecret());
        return StripeObject.PRETTY_PRINT_GSON.toJson(responseData);
      }
    );
    
    • 1

相关问题

  • Stripe Reporting API - 尝试使用 Stripe CLI 检索报告数据

  • 如何在服务器端处理 Stripe Webhook 签名密钥轮换

  • 如何从react stripe PaymentElement获取stripe卡号、cvc和到期日期

  • Stripe 付款:结帐前步骤

  • Stripe 订阅中 12 次等额付款(无按比例分配)的“cancel_at”和“按比例分配行为”的值是多少?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    为什么这个简单而小的 Java 代码在所有 Graal JVM 上的运行速度都快 30 倍,但在任何 Oracle JVM 上却不行?

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    何时应使用 std::inplace_vector 而不是 std::vector?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Marko Smith

    我正在尝试仅使用海龟随机和数学模块来制作吃豆人游戏

    • 1 个回答
  • Martin Hope
    Aleksandr Dubinsky 为什么 InetAddress 上的 switch 模式匹配会失败,并出现“未涵盖所有可能的输入值”? 2024-12-23 06:56:21 +0800 CST
  • Martin Hope
    Phillip Borge 为什么这个简单而小的 Java 代码在所有 Graal JVM 上的运行速度都快 30 倍,但在任何 Oracle JVM 上却不行? 2024-12-12 20:46:46 +0800 CST
  • Martin Hope
    Oodini 具有指定基础类型但没有枚举器的“枚举类”的用途是什么? 2024-12-12 06:27:11 +0800 CST
  • Martin Hope
    sleeptightAnsiC `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它? 2024-11-09 07:18:53 +0800 CST
  • Martin Hope
    The Mad Gamer 何时应使用 std::inplace_vector 而不是 std::vector? 2024-10-29 23:01:00 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST
  • Martin Hope
    MarkB 为什么 GCC 生成有条件执行 SIMD 实现的代码? 2024-02-17 06:17:14 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve