# 设置您的开发环境 熟悉 Stripe CLI 和我们的服务器端 SDK。 查看我们的[无代码文档](https://docs.stripe.com/no-code.md),使用我们合作伙伴目录中的[预构建解决方案](https://stripe.com/partners/directory),或聘请 [Stripe 认证专家](https://stripe.com/partners/directory?t=Consulting)。 您可以通过 Stripe 的服务器端 SDK 和命令行接口 (CLI) 与 Stripe 的 REST API 进行交互。从 Stripe CLI 开始,简化您的开发环境并进行 API 调用。 使用 SDK 可以避免编写样板代码。要开始从您的环境发送请求,请选择一种语言,按照其快速入门指南进行。 > #### Chrome 扩展程序 > > 建议您在自己的网站上构建与 Stripe 的支付集成(例如 [Elements](https://docs.stripe.com/payments/elements.md) 或 [Checkout](https://docs.stripe.com/payments/checkout.md))。然后,设置您的 Chrome 扩展程序,在用户准备好完成购买时使他们转至此支付页面。 > > 这种方法比尝试直接在扩展中处理付款更安全、更易于维护。 # Go 在本快速入门中,您将安装 [Stripe CLI](https://docs.stripe.com/stripe-cli.md)。它是一个必不可少的工具,让您可以通过命令行访问 Stripe 集成。您还将安装 [Stripe Go 服务器端 SDK](https://github.com/stripe/stripe-go),以便从 Go 编写的应用程序访问 Stripe API。 ## 所学内容 本快速指南中,您将学会如何: - 如何在不编写代码的情况下调用 Stripe API - 如何用 Go 模块管理第三方依赖 - 如何安装最新的 Stripe Go SDK v85.2.0 - 如何发送第一个 SDK 请求 ## 初始设置 首先,[创建 Stripe 账户](https://dashboard.stripe.com/register)或[登录](https://dashboard.stripe.com/login)。 ## 设置 Stripe CLI ### 安装 在命令行中,使用一个安装脚本或为您的操作系统下载并提取一个有版本号的归档文件,完成 CLI 的安装。 #### homebrew 要用 [homebrew](https://brew.sh/) 安装 Stripe CLI,请运行: ```bash brew install stripe/stripe-cli/stripe ``` 如果您在 Linux 版本的 homebrew 上运行该命令,则此命令将失败,但您可以使用此替代方法或按照 Linux 选项卡上的说明进行操作。 ```bash brew install stripe-cli ``` #### apt > 用于 CLI 的 Debian 构建版本可在 JFrog 的https://packages.stripe.dev,获取,该域名并非 Stripe 所有。当您访问此 URL 时,它将会重定向至 Jfrog 工件列表。 要在基于 Debian 和 Ubuntu 的发行版本上安装 Stripe CLI: 1. 将 Stripe CLI 的 GPG 密钥添加到 apt 源密钥环中: ```bash curl -s https://packages.stripe.dev/api/security/keypair/stripe-cli-gpg/public | gpg --dearmor | sudo tee /usr/share/keyrings/stripe.gpg > /dev/null ``` 1. 将 CLI 的 APT Repository 添加到 apt sources list: ```bash echo "deb [signed-by=/usr/share/keyrings/stripe.gpg] https://packages.stripe.dev/stripe-cli-debian-local stable main" | sudo tee -a /etc/apt/sources.list.d/stripe.list ``` 1. 更新软件包列表: ```bash sudo apt update ``` 1. 安装 CLI: ```bash sudo apt install stripe ``` #### yum > 用于 CLI 的 RPM 构建版本可在 JFrog 的https://packages.stripe.dev,获取,该域名并非 Stripe 所有。当您访问此 URL 时,它将会重定向至 Jfrog 工件列表。 要在基于 RPM 的发行版本上安装 Stripe CLI: 1. 将 CLI 的 yum repository 添加到 yum 源列表: ```bash echo -e "[Stripe]\nname=stripe\nbaseurl=https://packages.stripe.dev/stripe-cli-rpm-local/\nenabled=1\ngpgcheck=0" >> /etc/yum.repos.d/stripe.repo ``` 1. 安装 CLI: ```bash sudo yum install stripe ``` #### Scoop 要用 [Scoop](https://scoop.sh/) 安装 Stripe CLI,请运行: ```bash scoop bucket add stripe https://github.com/stripe/scoop-stripe-cli.git ``` ```bash scoop install stripe ``` #### macOS 要在没有 homebrew 的 macOS 上安装 Stripe CLI: 1. 从 [GitHub](https://github.com/stripe/stripe-cli/releases/latest) 下载您的 CPU 结构类型的最新的 `mac-os` tar.gz 文件。 1. 解压文件:`tar -xvf stripe_[X.X.X]_mac-os_[ARCH_TYPE].tar.gz`。 或者,将二进制文件安装在可以全局执行的位置(例如,`/usr/local/bin`)。 #### Linux 要在没有软件包管理器的 Linux 上安装 Stripe CLI: 1. 从 [GitHub](https://github.com/stripe/stripe-cli/releases/latest) 下载最新的 `linux` tar.gz 文件。 1. 解压文件:`tar -xvf stripe_X.X.X_linux_x86_64.tar.gz`. 1. 将 `./stripe` 移到您的执行路径。 #### Windows 要在没有 Scoop 的 Windows 上安装 Stripe CLI: 1. 从 [GitHub](https://github.com/stripe/stripe-cli/releases/latest) 下载最新的 `windows` zip 文件 1. 解压 `stripe_X.X.X_windows_x86_64.zip` 文件。 1. 将解压缩后的 `stripe.exe` 文件的路径添加到 `Path` 环境变量中。若要了解如何更新环境变量,请参阅 [Microsoft PowerShell 文档](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_environment_variables?view=powershell-7.3#saving-changes-to-environment-variables)。 > Windows 防病毒扫描程序偶尔会将 Stripe CLI 标记为不安全。这称为误报。有关详细信息,请参阅 GitHub 存储库中的[问题 #692](https://github.com/stripe/stripe-cli/issues/692)。 1. 运行解压的 `.exe` 文件。 #### Docker Stripe CLI 也可以作为 [Docker 镜像](https://hub.docker.com/r/stripe/stripe-cli)提供。要安装最新版本,请运行: ```bash docker run --rm -it stripe/stripe-cli:latest ``` ### 验证身份 登录您的 Stripe 用户[账户](https://docs.stripe.com/get-started/account/set-up.md)并进行身份验证,以生成一组受限密钥。如需了解更多信息,请参阅 [Stripe CLI 密钥和权限](https://docs.stripe.com/stripe-cli/keys.md)。 ```bash stripe login ``` 在浏览器中按键盘上的**回车键**以完成认证流程。 ```bash Your pairing code is: enjoy-enough-outwit-win This pairing code verifies your authentication with Stripe. Press Enter to open the browser or visit https://dashboard.stripe.com/stripecli/confirm_auth?t=THQdJfL3x12udFkNorJL8OF1iFlN8Az1 (^C to quit) ``` ### 确认设置 现在,CLI 已安装完毕,您可以向 [Create a product](https://docs.stripe.com/api/products/create.md) 发出一个 API 请求。 #### bash ```bash stripe products create \ --name="My First Product" \ --description="Created with the Stripe CLI" ``` 在响应对象中查找产品标识符( `id` 中)。保存它以备下一步使用。 如果一切正常,命令行将显示以下响应。 #### bash ```json { "id": "prod_LTenIrmp8Q67sa", // 标识符如下所示。 "object": "product", "active": true, "attributes": [], "created": 1668198126, "default_price": null, "description": "Created with the Stripe CLI", "identifiers": {}, "images": [], "livemode": false, "metadata": {}, "name": "My First Product", "package_dimensions": null, "price": null, "product_class": null, "shippable": null, "sku": "my-first-product-10", "statement_descriptor": null, "tax_code": null, "type": "service", "unit_label": null, "updated": 1668198126, "url": null } ``` 接下来,调用 [Create a price](https://docs.stripe.com/api/prices/create.md),附加一个 30 美元的价格。将 `product` 中的占位符替换为您的产品标识符(例如,`prod_LTenIrmp8Q67sa`)。 #### bash ```bash stripe prices create \ --unit-amount=3000 \ --currency=usd \ --product="{{PRODUCT_ID}}" ``` 如果一切正常,命令行将显示以下响应。 #### bash ```json { "id": "price_1KzlAMJJDeE9fu01WMJJr79o", // 标识符如下所示。 "object": "price", "active": true, "billing_scheme": "per_unit", "created": 1652636348, "currency": "usd", "livemode": false, "lookup_key": null, "metadata": {}, "nickname": null, "product": "prod_Lh9iTGZhb2mcBy", "recurring": null, "tax_behavior": "unspecified", "tiers_mode": null, "transform_quantity": null, "type": "one_time", "unit_amount": 3000, "unit_amount_decimal": "3000" } ``` ## 管理第三方依赖 建议用 [Go 模块](https://go.dev/blog/using-go-modules)管理第三方依赖项,这使您可以添加新库并将其包含在 Go 项目中。 ### 初始化 Go 如果您是在一个新目录中从头开始,那么首先需要创建一个 `go.mod` 文件来跟踪依赖项。例如: #### 初始化 Go ```bash go mod init stripe-example ``` ## 安装 Go 服务器端 SDK Stripe Go 服务器端 SDK 的最新版本是 v85.2.0。它支持 1.15+ 版 Go。 ### 安装库 从 [Go 模块](https://go.dev/blog/using-go-modules)安装库,这是一个 Go 包管理工具: ```bash go get github.com/stripe/stripe-go/v85 ``` 用 Go 模块向一个_新_项目安装库后,该库将作为依赖项自动添加到您的项目的 go.mod 文件中。例如: ```go.mod module stripe-example go 1.18 require github.com/stripe/stripe-go/v85 85.2.0 // indirect ``` ### 同步依赖 要使_已有_项目的托管依赖项集保持整洁,请运行以下命令[同步代码的依赖项](https://go.dev/doc/modules/managing-dependencies)。 ```bash go mod tidy ``` ## 运行您的第一个 SDK 请求 现在您已安装 Go SDK,可以通过几个 API 请求创建订阅[产品](https://docs.stripe.com/api/products/create.md)并附加[价格](https://docs.stripe.com/api/prices/create.md)。在此示例中,我们使用响应中返回的产品标识符来创建价格。 > #### 使用 API 密钥的最佳实践 > > 这个示例使用您的 Stripe 用户[账户](https://docs.stripe.com/get-started/account/set-up.md)在*沙盒* (A sandbox is an isolated test environment that allows you to test Stripe functionality in your account without affecting your live integration. Use sandboxes to safely experiment with new features and changes)环境中的默认密钥。只有您能看到这些值。请遵循[最佳实践](https://docs.stripe.com/keys-best-practices.md)来安全地管理您的密钥。 #### 创建产品和价格 ```go package main import ( "context" "fmt" "github.com/stripe/stripe-go/v85" ) func main() { // Don't embed any keys in production code. This is an example. // See https://docs.stripe.com/keys-best-practices. sc := stripe.NewClient("sk_test_BQokikJOvBiI2HlWgH4olfQ2") productParams := &stripe.ProductCreateParams{ Name: stripe.String("Starter Subscription"), Description: stripe.String("$12/Month subscription"), } starterProduct, _ := sc.V1Products.Create(context.TODO(), productParams) priceParams := &stripe.PriceCreateParams{ Currency: stripe.String(stripe.CurrencyUSD), Product: stripe.String(starterProduct.ID), Recurring: &stripe.PriceCreateRecurringParams{ Interval: stripe.String(stripe.PriceRecurringIntervalMonth), }, UnitAmount: stripe.Int64(1200), } starterPrice, _ := sc.V1Prices.Create(context.TODO(), priceParams) fmt.Println("Success! Here is your starter subscription product id: " + starterProduct.ID) fmt.Println("Success! Here is your starter subscription price id: " + starterPrice.ID) } ``` 将文件另存为 `create_price.go`。从命令行,`cd` 到包含您刚刚保存的文件的目录,然后运行: #### create_price.rb ```bash go run create_price.go ``` 如果一切正常,命令行将显示以下响应。保存这些标识符,以便在构建集成时使用。 #### bash ```bash Success! Here is your starter subscription product id: prod_0KxBDl589O8KAxCG1alJgiA6 Success! Here is your starter subscription price id: price_0KxBDm589O8KAxCGMgG7scjb ``` ## See also 快速入门到此结束。有关处理您刚创建的产品的付款的几种不同方式,请参阅下面的链接。 - [创建支付链接](https://docs.stripe.com/payment-links.md) - [Stripe 托管页面](https://docs.stripe.com/checkout/quickstart.md) - [高级集成](https://docs.stripe.com/payments/quickstart-checkout-sessions.md)