> ## Documentation Index
> Fetch the complete documentation index at: https://docs.anyway.sh/llms.txt
> Use this file to discover all available pages before exploring further.

# 创建付款链接

> 为已有产品添加价格和可分享的付款链接。

请使用属于该产品所在组织、且为全部权限或勾选了 `products:payment_links:write` 权限（控制台中“可创建和修改”下的**支付链接**）的 API 密钥进行认证。只有 `Owner` 和 `Admin` 角色可以授予该权限。

* `id` 是你组织内的产品 ID，无论该产品是通过 API 还是在控制台创建的。其他组织的产品或不存在的产品返回 `404`。
* 每次调用都会为该产品新建一个价格及其付款链接，因此一个产品可以有多个价格。
* 必填请求头 `Idempotency-Key`（1–255 个 ASCII 字符，范围为 `0x21` 至 `0x7E`，不允许空格），规则与结账会话相同。相同的键加相同的请求体，返回第一次创建的链接（`201`），不会重复创建。相同的键但请求体不同，或用于另一个产品，返回 `409`。如果该键创建的链接之后被删除，重试也会返回 `409`，请换一个新的键。
* `pricingType` 为 `ONE_TIME` 或 `SUBSCRIPTION`。`pricing` 是以主单位表示的字符串，如 `"9.99"`。不传 `pricing` 即为灵活的“自定义金额”，仅限 `ONE_TIME`，此时 `minAmount` 和 `maxAmount` 可选。
* `SUBSCRIPTION` 价格必须提供 `billingInterval`（`DAY`、`MONTH` 或 `YEAR`）。`billingIntervalCount` 可选，默认为 `1`。
* `successUrl` 是客户付款后跳转的绝对 HTTPS 地址。不传则客户看到 Anyway 的收据页。
* 款项通过 Anyway 平台账户收取。通过 API 创建的链接仅支持银行卡付款，不支持加密货币付款链接。
* 未知字段会被拒绝，返回 `400`。
* 请原样分享返回的 `paymentLinkUrl`。响应结构与[读取产品付款链接](https://docs.anyway.sh/zh/api-reference/endpoint/get-product-payment-links.md)中的条目相同。

> [!NOTE]
>
> 结账会话无法销售灵活金额。若要让客户自行决定付款金额，请在此创建灵活的一次性付款链接。

| 状态码 | 错误码 | 含义 |
| - | - | - |
| `400` | | 校验失败：货币无效、订阅缺少计费周期、灵活价格的订阅、金额小于或等于 0、`successUrl` 不是绝对 HTTPS 地址、未知字段，或缺少 `Idempotency-Key` 或其无效。 |
| `401` | | API 密钥缺失或无效。 |
| `403` | `INSUFFICIENT_SCOPE` | 密钥缺少 `products:payment_links:write`。 |
| `403` | `ORG_FROZEN` | 组织的收款已被冻结。 |
| `403` | `CARD_USD_VERIFICATION_REQUIRED` | 创建美元银行卡付款链接前需先完成企业认证。 |
| `404` | | 产品不存在，或属于其他组织。 |
| `409` | `PRODUCT_CONFLICT` | `Idempotency-Key` 已用于不同的请求体或另一个产品，或它创建的链接已被删除。 |
| `413` | `PAYLOAD_TOO_LARGE` | 请求体超过 64 KB。 |
| `503` | `CARD_USD_CAPABILITY_UNAVAILABLE` | 企业认证状态暂时无法获取，请稍后重试。 |


## OpenAPI

````yaml api-reference/openapi.json POST /v1/product/{id}/payment-link
openapi: 3.0.3
info:
  description: >-
    Organization-scoped commerce API for merchant backends: create and manage
    checkout sessions, and query commerce records.
  title: Anyway Merchant API
  contact: {}
  version: '1.0'
  x-source: anyway-backend-go
servers:
  - url: https://merchant-api-prod.anyway.sh
security: []
paths:
  /v1/product/{id}/payment-link:
    post:
      tags:
        - products
      summary: Create a payment link
      description: >-
        Creates a new price for an existing product and its card payment link.
        Requires Full access or the `products:payment_links:write` permission.
        The product can be created by API or in the dashboard. Send a unique
        `Idempotency-Key` (required): the same key with the same body returns
        the link created the first time (201); the same key with a different
        body or product, or a link removed since, returns 409 PRODUCT_CONFLICT.
        Unknown request fields return 400. Crypto payment links are not
        available through the API. Checkout sessions can't sell flexible
        amounts; a flexible one-time payment link can.
      parameters:
        - description: Product ID
          name: id
          in: path
          required: true
          schema:
            type: string
        - description: >-
            Unique creation request key: 1-255 ASCII characters in the range
            0x21-0x7E (no spaces or control characters)
          name: Idempotency-Key
          in: header
          required: true
          schema:
            type: string
            minLength: 1
            maxLength: 255
      requestBody:
        required: true
        description: Price and payment link
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/product.CreatePaymentLinkRequest'
      responses:
        '201':
          description: Created
          content:
            application/json:
              schema:
                $ref: >-
                  #/components/schemas/response.ApiResponse-product_PaymentLinkResponse
        '400':
          description: >-
            Bad Request. Invalid currency, a subscription without a billing
            interval, a flexible subscription, an amount of 0 or less, a
            successUrl that is not an absolute HTTPS URL, an unknown field, or
            an invalid Idempotency-Key.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/response.ApiResponse-any'
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/response.ApiResponse-any'
        '403':
          description: >-
            Forbidden. The API key lacks the required permission
            (INSUFFICIENT_SCOPE), or the organization is frozen.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/response.ApiResponse-any'
        '404':
          description: >-
            Not Found. The product does not exist or belongs to another
            organization.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/response.ApiResponse-any'
        '409':
          description: >-
            Conflict. The Idempotency-Key was already used with a different body
            (PRODUCT_CONFLICT).
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/response.ApiResponse-any'
        '413':
          description: Request body larger than 64 KB (`PAYLOAD_TOO_LARGE`).
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/response.ApiResponse-any'
      security:
        - APIKeyAuth: []
components:
  schemas:
    product.CreatePaymentLinkRequest:
      type: object
      required:
        - pricingType
        - currency
      properties:
        pricingType:
          $ref: '#/components/schemas/product.PricingType'
        currency:
          type: string
          description: Currency code, for example USD
        pricing:
          type: string
          description: >-
            Fixed amount in major units, for example "9.99". Omit for a flexible
            (pay what you want) amount, which is ONE_TIME only.
        minAmount:
          description: Optional minimum for a flexible amount (ONE_TIME only)
          type: number
        maxAmount:
          description: Optional maximum for a flexible amount (ONE_TIME only)
          type: number
        billingInterval:
          $ref: '#/components/schemas/product.BillingInterval'
        billingIntervalCount:
          type: integer
          description: Number of intervals between charges (SUBSCRIPTION). Defaults to 1.
        pricingName:
          type: string
          description: Name of the price
        successUrl:
          type: string
          description: >-
            Absolute HTTPS URL to send the customer to after paying. Defaults to
            the Anyway receipt.
    response.ApiResponse-product_PaymentLinkResponse:
      type: object
      properties:
        data:
          $ref: '#/components/schemas/product.PaymentLinkResponse'
        error:
          $ref: '#/components/schemas/response.ErrorDetail'
        message:
          type: string
        success:
          type: boolean
    response.ApiResponse-any:
      type: object
      properties:
        data: {}
        error:
          $ref: '#/components/schemas/response.ErrorDetail'
        message:
          type: string
        success:
          type: boolean
    product.PricingType:
      type: string
      enum:
        - SUBSCRIPTION
        - ONE_TIME
      x-enum-varnames:
        - PricingTypeSubscription
        - PricingTypeOneTime
    product.BillingInterval:
      type: string
      enum:
        - DAY
        - MONTH
        - YEAR
      x-enum-varnames:
        - BillingIntervalDay
        - BillingIntervalMonth
        - BillingIntervalYear
    product.PaymentLinkResponse:
      type: object
      properties:
        amount:
          type: number
          nullable: true
        createdAt:
          type: string
        currency:
          type: string
        flexibleAmount:
          type: boolean
        isActive:
          type: boolean
        name:
          type: string
        paymentLinkUrl:
          type: string
        pricingId:
          type: string
        pricingType:
          type: string
        provider:
          type: string
        title:
          type: string
        description:
          type: string
        minAmount:
          type: number
          nullable: true
        maxAmount:
          type: number
          nullable: true
        successUrl:
          type: string
          description: >-
            Where the customer is sent after paying. Empty when the Anyway
            receipt is used.
    response.ErrorDetail:
      type: object
      properties:
        code:
          type: string
        message:
          type: string
  securitySchemes:
    APIKeyAuth:
      description: 'Merchant API key (format: ak_BASE64)'
      type: apiKey
      name: X-API-Key
      in: header

````

This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.