如何通过 Stripe redirectToCheckout 函数传递元数据

bra*_*ice 2 stripe-payments

我已经了解了 Stripe 付款意图的基础知识。我正在出售我的原创艺术品。我希望能够将元数据发送到结账流程中。API 说我可以使用此处显示的 payment_intents_data.metadata 来做到这一点:https ://support.stripe.com/questions/using-metadata-with-checkout-sessions

const result = stripe.redirectToCheckout({
            sessionId: session,
            metadata: {
              id: this.props.id,
              title: this.props.title
            }
            });
Run Code Online (Sandbox Code Playgroud)

我收到一条错误消息,指出 stripe.redirectToCheckout 没有元数据。sessionId 工作正常。

如何传递元数据?我在 api 中看到这是可能的,但没有如何做到这一点的示例。

Neo*_*ist 7

我与 Stripe 的集成是通过 AWS API Gateway 使用 AWS Node.js Lambda 函数以 TypeScript 和 Angular 编写的。由于对支付服务的需求不大,我选择不为我的应用程序编写 Stripe 支付页面,而是选择使用Stripe Checkout。我的应用程序用户界面代码通过准备一个对象并将其传递给 来调用Stripe Checkoutoptionsstripe.redirectToCheckout(options?)

\n

Stripe Checkout 的局限性

\n

目前,Stripe Checkout不支持在函数参数metadata内使用,据我所知,没有办法解决此限制。库中定义的参数接口仅提供以下字段:optionsredirectToCheckout(options)optionscheckout.d.ts@stripe\\stripe.js

\n
interface RedirectToCheckoutClientOptions {\n/**\n * The URL to which Stripe should send customers when payment is complete.\n * If you\xe2\x80\x99d like access to the Checkout Session for the successful payment, read more about it in our guide on [fulfilling your payments with webhooks](https://stripe.com/docs/payments/checkout/fulfillment#webhooks).\n */\nsuccessUrl: string;\n\n/**\n * The URL to which Stripe should send customers when payment is canceled.\n */\ncancelUrl: string;\n\n/**\n * An array of objects representing the items that your customer would like to purchase.\n * These items are shown as line items in the Checkout interface and make up the total amount to be collected by Checkout.\n */\nlineItems?: Array<{\n  /**\n   * The ID of the price that the customer would like to purchase. SKU or plan IDs may also be used.\n   */\n  price?: string;\n\n  /**\n   * The quantity of units for the item.\n   */\n  quantity?: number;\n}>;\n\n/**\n * An array of objects representing the items that your customer would like to purchase.\n * These items are shown as line items in the Checkout interface and make up the total amount to be collected by Checkout.\n *\n * @deprecated\n */\nitems?: Array<{\n  /**\n   * The ID of the SKU that the customer would like to purchase\n   */\n  sku?: string;\n\n  /**\n   * The ID of the plan that the customer would like to subscribe to.\n   */\n  plan?: string;\n\n  /**\n   * The quantity of units for the item.\n   */\n  quantity?: number;\n}>;\n\n/**\n * The mode of the Checkout Session. Required if using lineItems.\n */\nmode?: \'payment\' | \'subscription\';\n\n/**\n * A unique string to reference the Checkout session.\n * This can be a customer ID, a cart ID, or similar.\n * It is included in the `checkout.session.completed` webhook and can be used to fulfill the purchase.\n */\nclientReferenceId?: string;\n\n/**\n * The email address used to create the customer object.\n * If you already know your customer\'s email address, use this attribute to prefill it on Checkout.\n */\ncustomerEmail?: string;\n\n/**\n * Specify whether Checkout should collect the customer\xe2\x80\x99s billing address.\n * If set to `required`, Checkout will attempt to collect the customer\xe2\x80\x99s billing address.\n * If not set or set to `auto` Checkout will only attempt to collect the billing address when necessary.\n */\nbillingAddressCollection?: \'auto\' | \'required\';\n\n/**\n * Provides configuration for Checkout to collect a shipping address from a customer.\n */\nshippingAddressCollection?: {\n  /**\n   * An array of two-letter ISO country codes representing which countries\n   * Checkout should provide as options for shipping locations. The codes are\n   * expected to be uppercase. Unsupported country codes: AS, CX, CC, CU, HM, IR, KP, MH, FM, NF, MP, PW, SD, SY, UM, VI.\n   */\n  allowedCountries: string[];\n};\n\n/**\n * The [IETF language tag](https://en.wikipedia.org/wiki/IETF_language_tag) of the locale to display Checkout in.\n * Default is `auto` (Stripe detects the locale of the browser).\n */\nlocale?: CheckoutLocale;\n\n/**\n * Describes the type of transaction being performed by Checkout in order to customize relevant text on the page, such as the **Submit** button.\n * `submitType` can only be specified when using using line items or SKUs, and not subscriptions.\n * The default is `auto`.\n */\nsubmitType?: \'auto\' | \'book\' | \'donate\' | \'pay\';\n}\n
Run Code Online (Sandbox Code Playgroud)\n

两个API

\n

令人困惑的部分(至少对我来说)是 Stripe 文档没有清楚地描述(至少)有两个不同的 API,它们具有不同的方法,但设计时不能协同工作。

\n

更大且功能齐全的 API 用于 Node.js(或其他常见后端服务器)集成,其中用户界面对应用程序的 Node.js 后端进行支付调用,而后者又调用 Stripe 进行支付处理。

\n

Stripe Checkout API 非常有限,适合直接从客户端集成(例如,为了处理支付,该 API“最初”不需要任何类型的后端来调用 Stripe 支付处理方法)。我将Stripe Checkout语句限定为“最初”不需要 Node.js 后端的原因是,在 Stripe 调出其简单/标准支付屏幕并且用户进行支付后,我不建议尝试完成支付流程通过您的用户界面代码,而是实施 Stripe Webhooks(使用 Node.js 后端(或者在我的例子中是 AWS 网关后面的 AWS Lambda 函数))。您向 Stripe 注册的 Webhook 端点,将在处理付款时调用。就我而言,一旦redirectToCheckout(options)函数重定向到我的应用程序成功或取消的 URL,用户界面就会被编码为轮询我的 AWS 端点 Lambda 函数之一,该函数最终确认或不确认付款已完成。

\n

假暂停

\n

我犯的一个错误(我的学习经验)是尝试直接在我的用户界面应用程序中使用 Node.js Stripe 库...(我能够将Stripe Checkout库包含到我的应用程序中,为什么不导入 Node .js Stripe 库?)我认为它可能能够工作,但是 Stripe 库期望可用的依赖项,因为它假设 Node.js 安装也需要导入到您的用户界面中......我很快就放弃了这种方法,因为从长远来看,我似乎是在购买依赖维护噩梦......其他更勇敢的开发人员可能愿意/足够熟练地管理依赖项......但不是我。

\n

推荐:

\n

如果您需要进行metadataStripe 付款,我建议您不要使用Stripe Checkout redirectToCheckout(options)功能...而是实现您自己的付款页面,该页面会传递metadata到您的后端,而后端又使用完整的 Stripe API 来处理您的付款。

\n

请求增强

\n

也许在某个时候,Stripe 会扩展Stripe Checkout功能以支持传递metadata,并且还将支持信用卡以外的支付类型......

\n

至少,我建议 Stripe 在两个 API 的文档中自由放置交叉引用链接(或警告),这样很明显,这些 API 不是为应用程序客户端的组合使用而设计的。

\n