Cloudflare 官方宣布了stripe JS 包对 Cloudflare Workers 的普遍可用性。用户现在可在项目中直接使用原生 Stripe SDK,可通过在项目中安装 stripe:npm i stripe 以进行使用。

Cloudflare officially announced the general availability of the stripe JS package for Cloudflare Workers. Users can now directly use the native Stripe SDK in the project, and can use it by installing stripe: npm i stripe in the project.

The announcement pointed out that processing payments in the application is the key to establishing an online business. For many developers, the main choice for payment processing is Stripe. For developers who have deployed their sites to Cloudflare Pages, this universal availability provides them with an opportunity to use Stripe directly in their applications. “This week we announced Cloudflare Pages’ support for serverless functions, so just add a few lines of JavaScript code to your Pages project to accept payments for your digital products or process subscriptions for your membership website. No. Additional configuration is required, and it will be automatically expanded, just like your Pages site.”

Cloudflare has prepared a sample open source code library to show how to integrate Stripe Checkout into the Pages application. No additional configuration is required. The new Workers function of Pages allows users to deploy infinitely expandable functions by simply adding a new function folder to the project file. You can view the example demo at stripe.pages.dev, or view the open source code library.

After installing the SDK, users can start accepting payments directly in the app. The following example shows how to start a new Checkout session and redirect to the Checkout page hosted by Stripe:

const Stripe = require(“stripe”);

const stripe = Stripe(STRIPE_API_KEY, {
httpClient: Stripe.createFetchHttpClient()
});

export default {
async fetch(request) {
const session = await stripe.checkout.sessions.create({
line_items: [{
price_data: {
currency:’usd’,
product_data: {
name:’T-shirt’,
},
unit_amount: 2000,
},
quantity: 1,
}],
payment_method_types: [
‘card’,
],
mode:’payment’,
success_url: `${YOUR_DOMAIN}/success.html`,
cancel_url: `${YOUR_DOMAIN}/cancel.html`,
});

return Response.redirect(session.url)
}
}

According to the introduction, the Stripe SDK is natively supported in Cloudflare Workers, and what users can use will not be limited to payment processing. Any JavaScript examples currently in the extensive documentation of Stripe can be run directly in Workers without any changes. Specifically, by using Workers to handle a large number of available Stripe webhooks, users can better understand the operation of the existing system without starting any new infrastructure.

At the same time, Cloudflare also announced a new Workers template in cooperation with Stripe; this template can help users use best practices to get Stripe and Workers up and running. “In less than 5 minutes, you can start accepting payments for your next digital product or membership business.”

For more details, please check the official blog.

公告指出,在应用中处理支付是建立在线业务的关键。而对于许多开发者来说,处理支付的主要选择则是 Stripe。对于已经将站点部署到 Cloudflare Pages 的开发人员来说,此次的普遍可用为他们提供了一个可在应用程序中直接使用 Stripe 的机会。“本周我们宣布了 Cloudflare Pages 对无服务器函数的支持,因此,只需在你的 Pages 项目增加几行 JavaScript 代码,就能为你的数字产品接受付款,或为你的会员网站处理订阅。不需额外配置,而且会自动扩展,一如你的 Pages 站点。”

Cloudflare 方面准备了一个示例开源代码库,展示如何将 Stripe Checkout 集成到 Pages 应用中。不需要额外配置,Pages 的新 Workers 函数功能允许用户只需在项目文件中添加一个新的函数文件夹,就可以部署可无限扩展的函数。可在 stripe.pages.dev 查看实例演示,或查看开源代码库

安装了 SDK 后,用户就可以开始在应用程序中直接接受支付。下面的例子展示了如何启动一个新的 Checkout 会话,并重定向到 Stripe 托管的 Checkout 页面:

const Stripe = require("stripe");

const stripe = Stripe(STRIPE_API_KEY, {
  httpClient: Stripe.createFetchHttpClient()
});

export default {
  async fetch(request) {
    const session = await stripe.checkout.sessions.create({
      line_items: [{
        price_data: {
          currency: 'usd',
          product_data: {
            name: 'T-shirt',
          },
          unit_amount: 2000,
        },
        quantity: 1,
      }],
      payment_method_types: [
        'card',
      ],
      mode: 'payment',
      success_url: `${YOUR_DOMAIN}/success.html`,
      cancel_url: `${YOUR_DOMAIN}/cancel.html`,
    });

    return Response.redirect(session.url)
  }
}

根据介绍,在 Cloudflare Workers 中原生支持 Stripe SDK,用户可以利用的也将不仅限于支付处理。目前在 Stripe 的大量文档中的任何 JavaScript 示例都可在 Workers 中直接运行,而无需进行任何更改。具体而言,通过使用 Workers 来处理大量可用的 Stripe webhook,用户可以更好地掌握现有系统的运行情况,无需启动任何新的基础设施。

同时,Cloudflare 还宣布了一个与 Stripe 合作的新 Workers 模板;该模板可帮助用户使用最佳实践来启动并运行 Stripe 和 Workers。“不到 5 分钟,你就能开始为你的下一个数字产品或会员业务接受支付了。”

更多详情可查看官方博客

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注