Stripe Payments使用NodeJS/Vue添加支付源/卡/方法



我正在构建一个简单的SaaS应用程序,使用NodeJS为API提供Express,并为UI提供Vue。我已经编写了添加客户、链接订阅和计划以及其他一些例程的代码。我们允许用户在不输入支付方式的情况下注册,所以现在,我需要为用户添加一种添加支付方式的方式。我已经看了太多的文档,以至于我的头都在转,Stripe的支持(或缺乏支持(也无济于事。

我在UI中尝试了从createSource、createToken和createPaymentMethod的一切,然后将其提交给API,在那里我尝试了从stripeapi.customer.createSource到stripe.paymentMethods.create的一切,但都无效。一切都会返回一个错误,要么是对象中缺少什么,要么是该对象不正确。我试着看看API的支付意图,然而,这似乎是大材小用,只是简单地向客户添加一张卡。

这是我的最新代码。

UI:创建元素

this.stripe = await loadStripe('pk_test_');
let stripeElem = this.stripe.elements();
this.card = stripeElem.create('card', { hideIcon: true, hidePostalCode: false, style: { base: { color: '#363636', fontSize: '22px', fontSmoothing: 'antialiased' }}});
this.card.mount(this.$refs.card);

UI:提交给API

await this.stripe.createSource(this.card, { type: 'card' } ).then((source) => {
this.$http.post(`/api/route`, source).then((response) => {
if (response.status === 200) {
} else {
}
}).catch(() => {
});

API

await stripeapi.customers.createSource(customer_id, { source: card });

此代码生成以下对象:

{ source:
{ id: 'src_1HLFsEDfvqoM1TxYXmFvlcK9',
object: 'source',
amount: null,
card:
{ exp_month: 1,
exp_year: 2022,
last4: '4242',
country: 'US',
brand: 'Visa',
address_zip_check: 'unchecked',
cvc_check: 'unchecked',
funding: 'credit',
three_d_secure: 'optional',
name: null,
address_line1_check: null,
tokenization_method: null,
dynamic_last4: null },
client_secret: 'src_client_secret_VILuqM6ZikLzp9nMq4gizfN8',
created: 1598653002,
currency: null,
flow: 'none',
livemode: false,
metadata: {},
owner:
{ address: [Object],
email: null,
name: null,
phone: null,
verified_address: null,
verified_email: null,
verified_name: null,
verified_phone: null },
statement_descriptor: null,
status: 'chargeable',
type: 'card',
usage: 'reusable' } }

此代码和对象产生此错误:

(node:352976) UnhandledPromiseRejectionWarning: Error: The source hash must include an 'object' key indicating what type of source to create.
at Function.generate (/data/api/node_modules/stripe/lib/Error.js:39:16)
at IncomingMessage.res.once (/data/api/docroot/node_modules/stripe/lib/StripeResource.js:190:33)
at Object.onceWrapper (events.js:286:20)
at IncomingMessage.emit (events.js:203:15)
at IncomingMessage.EventEmitter.emit (domain.js:448:20)
at endReadableNT (_stream_readable.js:1145:12)
at process._tickCallback (internal/process/next_tick.js:63:19)

我想做的就是获取一个元素,创建一个支付源/方法(不管它叫什么(,然后将其与客户关联。感谢您的帮助。我看了很多例子,但对我来说都不起作用。一切似乎都会产生关于对象的错误。

经过几个小时的开发,我终于明白了!API参考资料严重缺乏,但本文解释了要做什么:https://stripe.com/docs/payments/save-card-without-authentication

从本质上讲,您可以创建和装载元素。然后,您在UI中使用createPaymentMethod并将卡片元素传递给它。从那里,您将paymentMethod.id字符串提交给API,然后使用strip.paymentMethods.attach通过传递paymentMethod.id和Stripe客户ID将其附加到客户。

前端HTML

<div ref="card" class="credit-card"></div>

前端创建和装载

this.stripe = await loadStripe('pk_test_YOURKEY');
let stripeElem = this.stripe.elements();
this.card = stripeElem.create('card', { hideIcon: true, hidePostalCode: false, style: { base: { color: '#363636', fontSize: '22px', fontSmoothing: 'antialiased' }}});
this.card.mount(this.$refs.card);

前端创建付款方式并提交给后端

await this.stripe.createPaymentMethod({ type: 'card', card: this.card }).then((method) => {
this.$http.post(`/users/billing/cards`, { id: method.paymentMethod.id }).then((response) => {
}).catch(() => {
});
}).catch(() => {
});

请注意:这段代码并不完整,它只是想给那些像我一样挣扎的人一个例子。

NodeJS错误消息显示:

源哈希必须包含一个指示要创建的源类型的"object"键。

也可以在这里找到,但我不确定,如果不是,这是一条虚假的错误消息。如果这确实适用,它将是object: 'card'而不是object: 'source';但我不这么认为。


Stripe有时有不止一种方法可以完成任务:

source肯定应该是客户端生成的卡令牌,
但您的客户端没有任何可以标记卡的代码。

为了参考,必须将这些结合起来:

  • https://stripe.com/docs/js/tokens_sources/create_token?type=cardElement
  • https://stripe.com/docs/api/cards/create

最新更新