首页 > 解决方案 > 已解决 - 条带克隆客户并在关联账户上创建直接费用

问题描述

我正在尝试在 Stripe 中的关联帐户上创建直接费用。我一直在浏览 Stripe 上的文档并在这里发帖,但似乎无法让它发挥作用。

客户及其付款方式存储在平台帐户中。我正在尝试在连接的帐户上克隆客户,但不想将他们的付款方式存储在连接的帐户上。根据文档,我可以通过不将其附加到我试图在已连接帐户上创建的客户来生成一次性付款意图。

尝试克隆客户时,出现错误:

StripeInvalidRequestError: The customer must have an active payment source attached.

在下面的代码中,我确保付款方式已附加到平台客户,当我转到控制台时,我看到了他们的付款方式并标记为“默认”。

任何帮助表示赞赏!


const customerId = 'cus_xxx'; // platform customer id
const paymentMethod = 'pm_xxxx'; // platform customer payment method

const getDefaultCard = async (customer) => {
  const { invoice_settings } = await stripe.customers.retrieve(customer);
  return invoice_settings ? invoice_settings.default_payment_method : null;
};

const getCards = async (customer) => {
  if (!customer) {
    return [];
  }
  const default_card = await getDefaultCard(customer);
  const { data } = await Stripe.stripe.paymentMethods.list({
    customer,
    type: "card",
  });
  if (!data) return [];
  return data.map(({ id, card: { last4, exp_month, exp_year, brand } }) => ({
    id,
    last4,
    exp_month,
    exp_year,
    brand,
    default: id === default_card,
  }));
};

// check to see if the current payment method is 
// attached to the platform customer
const cards = await getCards(customerId);

let found = cards.filter((card) => card.id === paymentMethod).length > 0;

// if the card is not found, attach it to the platform customer
if (!found) {
  const res = await Stripe.stripe.paymentMethods.attach(paymentMethod, {
    customer: customerId,
  });
}

const defaultCards = cards.filter((card) => card.default);

if (!defaultCards || !defaultCards.length) {
  // attach a default payment source to the user before
  // cloning to the connected account
  await stripe.customers.update(user.cus_id, {
    invoice_settings: {
      default_payment_method: paymentMethod,
    },
  });
}


// Get customer token - Results in ERROR
const token = await stripe.tokens.create(
  {
    customer: customerId,
    card: paymentMethod,
  },
  {
    stripeAccount,
  }
);

/** DOESN'T GET PAST TOKEN CREATION ABOVE **/

// clone customer
const newCustomer = await stripe.customers.create(
  {
    source: token.id,
    card: paymentMethod,
  },
  {
    stripeAccount,
  }
);

const amount = 1000;
const application_fee_amount = 100;

const intent = await Stripe.stripe.paymentIntents.create(
  {
    customer: newCustomer.id,
    amount,
    currency: "usd",
    payment_method_types: ["card"],
    payment_method: paymentMethod,
    description: "Test Connected Direct Charge",
    application_fee_amount,
  },
  {
    stripeAccount,
  }
);

// Now confirm payment
const result = await Stripe.stripe.paymentIntents.confirm(intent.id, {
  payment_method: paymentMethod,
});

标签: node.jsstripe-payments

解决方案


您似乎正在尝试将您平台帐户上的付款方式克隆为已连接帐户上的令牌。这是不可能的; Payment Methods 是一种较新的 Stripe API,与 Tokens(较旧的 API)不直接兼容。

您应该将平台上的付款方式克隆为已连接帐户上的付款方式,而不是使用如下代码:

const paymentMethod = await stripe.paymentMethods.create({
  customer: '{{CUSTOMER_ID}}',
  payment_method: '{{PAYMENT_METHOD_ID}}',
}, {
  stripeAccount: '{{CONNECTED_ACCOUNT_ID}}',
});

推荐阅读