Joh*_*Doe 2 payment stripe-payments react-stripe-elements
我正在努力让 Stripe 在我的服务器上工作。
所以,在客户端,我有这个代码(在努力让元素工作之后):
this.props.stripe.createPaymentMethod({ type: 'card', card: cardElement, billing_details: { name: name } }).then((paymentMethod) => {
// server request with customer_id and paymentMethod.id);
});
Run Code Online (Sandbox Code Playgroud)
这工作正常。现在,在服务器 (NodeJS) 上,我想为我的客户添加一个固定费用的新订阅。
这是我所拥有的:
const paymentIntent = await stripe.paymentIntents.create({
amount: 1000,
currency: 'usd',
payment_method_types: ['card'],
customer: req.body.customer_id,
metadata: { integration_check: 'accept_a_payment' },
});
const paymentIntentConfirmation = await stripe.paymentIntents.confirm(paymentIntent.id, { payment_method: req.body.paymentMethod_id });
const newSubscription = await stripe.subscriptions.create({
customer: req.body.customer_id,
items: [{ plan: premium_plan.id, quantity: 1 }],
default_payment_method: req.body.paymentMethod_id,
});
const attachPaymentToCustomer = await stripe.paymentMethods.attach(req.body.paymentMethod_id, { customer: req.body.customer_id });
const updateCustomerDefaultPaymentMethod = await stripe.customers.update(req.body.customer_id, {
invoice_settings: {
default_payment_method: req.body.paymentMethod_id,
},
});
Run Code Online (Sandbox Code Playgroud)
因此,如果我不将付款附加给客户,则会收到以下错误消息:
'The customer does not have a payment method with the ID pm_1Gx9m1HVGJbiGjghYhrkt6j. The payment method must be attached to the customer.'
Run Code Online (Sandbox Code Playgroud)
如果我这样做,我会收到以下错误消息:
'This PaymentMethod was previously used without being attached to a Customer or was detached from a Customer, and may not be used again.'
Run Code Online (Sandbox Code Playgroud)
那么,我如何添加该死的付款方式,以便当我检索我的客户时,它显示该客户已更新为他刚刚订阅的服务的新订阅,以及他的付款方式(在这种情况下为 CC)。
非常感谢这里对沮丧用户的任何帮助!
总的来说,到目前为止,实施 Stripe 是一个非常痛苦的经历。似乎没有任何效果。我使用打字稿,但有很多错误。该文档不是很有帮助,也没有很好的解释。“创建源”、“创建令牌”、“创建付款意图”、“创建设置意图”,我应该如何理解所有这些东西之间的区别?我想添加该死的在线订阅,这应该是 Internet 服务的标准程序。为什么有这么多不同的指导方针,带有令牌,带有来源等......
您可以在此处进行一些更改以使其正常工作,为了开始订阅 [1],您无需创建和确认 PaymentIntent。这是在发票中自动创建的,因为它们是为付款而创建的。所以步骤大致是(你已经做了很多,但只是为了有一个端到端的例子):
这在 [1] 中有详细概述。这是一些启动订阅的示例代码,您当然可以用您自己的 ID 替换创建产品和价格的调用:
const customer = await stripe.customers.create({
name: "Foo Bartley"
});
const paymentMethod = await stripe.paymentMethods.create(
{
type: 'card',
card: {
number: '4242424242424242',
exp_month: 6,
exp_year: 2021,
cvc: '314',
},
}
);
const product = await stripe.products.create(
{name: 'Gold Special'}
);
const price = await stripe.prices.create(
{
unit_amount: 1111,
currency: 'eur',
recurring: {interval: 'month'},
product: product.id,
}
);
// Everything above here is just setting up this demo
const attachPaymentToCustomer = await stripe.paymentMethods.attach(
paymentMethod.id, // <-- your payment method ID collected via Stripe.js
{ customer: customer.id } // <-- your customer id from the request body
);
const updateCustomerDefaultPaymentMethod = await stripe.customers.update(
customer.id, { // <-- your customer id from the request body
invoice_settings: {
default_payment_method: paymentMethod.id, // <-- your payment method ID collected via Stripe.js
},
});
const newSubscription = await stripe.subscriptions.create({
customer: customer.id, // <-- your customer id from the request body
items: [{ plan: price.id, quantity: 1 }], // <-- plans and prices are compatible Prices is a newer API
default_payment_method: paymentMethod.id, // <-- your payment method ID collected via Stripe.js
});
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助!
[1] https://stripe.com/docs/billing/subscriptions/fixed-price#create-subscription
[2] https://stripe.com/docs/billing/subscriptions/fixed-price#manage-payment-authentication