【问题标题】:Create Stripe Connect Customer with Payment Method on Platform Account在平台帐户上使用付款方式创建 Stripe Connect 客户
【发布时间】:2021-05-15 18:30:04
【问题描述】:

我正在关注文档 here 关于使其他企业能够直接接受付款。

我已成功创建关联帐户并接受客户在关联帐户上的付款,但我还无法保存该客户的付款信息以供将来付款。

在整个文档中,尤其是here,它假定您已经在平台帐户上使用付款方式创建了一个客户,然后您应该尝试将付款方式复制到连接的帐户。

在我将客户克隆到连接的帐户之前,我终生无法弄清楚如何在平台帐户上使用付款信息创建客户。

根据文档,我从客户端开始,其中 accountID 是连接帐户的 ID:


const [stripePromise, setstripePromise] = useState(() =>
    loadStripe(process.env.NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY, {
      stripeAccount: uid.accountID,
    })
  );

我的 Stripe 元素是使用 stripePromise 创建的。

在尝试保存卡详细信息时,我在客户端执行此操作:

我认为这是我的错误所在我在尝试创建平台支付方式时使用了关联的帐户凭据。

  const handleRememberMe = async () => {
    const { token } = await stripe.createToken(
      elements.getElement(CardElement)
    );
    console.log(token);

    const res = await fetchPostJSON("/api/pay_performer", {
      accountID: accountID,
      amount: value,
      cardToken: token,
    });

该 API 调用转到“/api/pay_performer”:

//Handle onboarding a new connect user for x

require("dotenv").config();
import Stripe from "stripe";
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY, {
  apiVersion: "2020-08-27",
});
import setCookie from "../../../utils/cookies";

export default async function createPayment(req, res) {
  if (req.method === "POST") {
// Connected account ID and the token generated on the client to be saved.
    const { accountID, amount, cardToken } = req.body;
    console.log(`API side cardToken: ${cardToken.used}`);
    try {
      // Trying to create a customer with that token.
      const customer = await stripe.customers.create({
        email: "test2@example.com",
        source: cardToken.card,
      });
      customer.sources = cardToken.card;
// Beginning the cloning process for the connected account.
      const token = await stripe.tokens.create(
        {
          customer: customer.id,
        },
        {
          stripeAccount: accountID,
        }
      );
      const clonedCustomer = await stripe.customers.create(
        {
          source: token.id,
        },
        {
          stripeAccount: accountID,
        }
      );
      console.log(`Default Source: ${clonedCustomer.default_source}`);

      console.log(`AccountID: ${accountID}, Amount: ${amount}`);
      const paymentIntent = await stripe.paymentIntents.create(
        {
          payment_method_types: ["card"],
          payment_method: clonedCustomer.default_source
            ? clonedCustomer.default_source
            : null,
          amount: amount * 100,
          currency: "usd",
          application_fee_amount: 1,
          customer: clonedCustomer.id,
        },
        {
          stripeAccount: accountID,
        }
      );

      const secret = paymentIntent.client_secret;
      console.log(secret);
      const payment_method = paymentIntent.payment_method;
      return res.status(200).send({ secret, payment_method });
    } catch (e) {
      console.log(e);
      return res.status(500).send({ error: e.message });
    }
  }
}

但我收到一个错误,即提供的令牌不是有效的令牌。我假设这是因为我使用客户端上已连接帐户的凭据创建了令牌,然后尝试将其应用于平台帐户。

我如何有一个单独的 UI 来首先创建平台客户,然后在购买时将其克隆到连接的帐户。

我不应该将令牌传递给服务器,而只通过 HTTPS 传递卡信息吗?非常迷茫,文档没有帮助!

感谢任何帮助!

【问题讨论】:

    标签: stripe-payments


    【解决方案1】:

    为了创建客户、收集卡信息并将 PaymentMethod 附加到他们,例如,您可以使用 SetupIntent 和 Elements [1] 或创建 PaymentMethod [2]。核心点是,如果系统最初应该在平台上存储该信息,那么连接的帐户还没有发挥作用。概括地说,这些步骤是(在任何地方提供已连接帐户的帐户 ID,因为这只发生在平台帐户上),使用平台的密钥:

    1. 创建客户
    2. 使用 Stripe.js 和 Elements 收集卡片数据
    3. 在后端将 pm_xxx 附加到客户

    [1]https://stripe.com/docs/payments/save-and-reuse

    [2]https://stripe.com/docs/js/payment_methods/create_payment_method

    【讨论】:

    • 这正是我需要的,谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-28
    • 2015-03-01
    • 1970-01-01
    • 2019-12-31
    • 1970-01-01
    • 2020-09-30
    • 2021-03-26
    相关资源
    最近更新 更多