# Xác thực giao dịch mua Stripe

> Xác thực giao dịch mua bằng token Stripe được cung cấp, sử dụng thông tin xác thực Stripe trong Cài đặt ứng dụng của bạn trong Adapty Dashboard.
> Nếu giao dịch mua hợp lệ, lịch sử giao dịch sẽ được nhập từ Stripe vào hồ sơ người dùng trong Adapty với customer_user_id được chỉ định.
> Nếu trước đó không có hồ sơ nào với customer_user_id này — hồ sơ sẽ được tạo mới.
>
> Các sự kiện hồ sơ người dùng được tạo trong quá trình này và các giao dịch được nhập sẽ được tính vào MTR.

## OpenAPI

```yaml
/api-specs/adapty-api.yaml post /api/v1/sdk/purchase/stripe/token/validate/
openapi: 3.1.0
info:
  title: Adapty server-side API
  version: 1.0.0
servers:
  - url: https://api.adapty.io
    description: Máy chủ sản xuất
paths:
  /api/v1/sdk/purchase/stripe/token/validate/:
    post:
      summary: Xác thực giao dịch mua Stripe
      description: |
        Xác thực giao dịch mua bằng token Stripe được cung cấp, sử dụng thông tin xác thực Stripe trong Cài đặt ứng dụng của bạn trong Adapty Dashboard.
        Nếu giao dịch mua hợp lệ, lịch sử giao dịch sẽ được nhập từ Stripe vào hồ sơ người dùng trong Adapty với customer_user_id được chỉ định.
        Nếu trước đó không có hồ sơ nào với customer_user_id này — hồ sơ sẽ được tạo mới.

        Các sự kiện hồ sơ người dùng được tạo trong quá trình này và các giao dịch được nhập sẽ được tính vào MTR.
      operationId: validateStripePurchase
      tags:
        - Stripe
      security:
        - apikeyAuth: []
      requestBody:
        required: true
        content:
          application/vnd.api+json:
            schema:
              $ref: "#/components/schemas/StripeValidationRequest"
            example:
              data:
                type: stripe_receipt_validation_result
                attributes:
                  customer_user_id: <YOUR_CUSTOMER_USER_ID>
                  stripe_token: <YOUR_STRIPE_TOKEN>
      responses:
        "200":
          description: Giao dịch mua được xác thực thành công
          content:
            application/vnd.api+json:
              schema:
                $ref: "#/components/schemas/StripeValidationResponse"
              example:
                data: null
        "400":
          description: Yêu cầu không hợp lệ
          content:
            application/vnd.api+json:
              schema:
                $ref: "#/components/schemas/StripeErrorResponse"
              example:
                errors:
                  - detail: none is not an allowed value
                    source:
                      pointer: /data/attributes/stripe_token
                    status: "400"
        "401":
          description: Không được phép
          content:
            application/vnd.api+json:
              schema:
                $ref: "#/components/schemas/ErrorResponse"
        "500":
          description: Lỗi máy chủ nội bộ
          content:
            application/vnd.api+json:
              schema:
                $ref: "#/components/schemas/ErrorResponse"
components:
  schemas:
    StripeValidationRequest:
      type: object
      properties:
        data:
          type: object
          properties:
            type:
              type: string
              enum:
                - stripe_receipt_validation_result
              description: Loại tài nguyên
            attributes:
              type: object
              properties:
                customer_user_id:
                  type: string
                  description: ID người dùng của bạn trong hệ thống của bạn
                stripe_token:
                  type: string
                  description: |
                    Token của đối tượng Stripe đại diện cho một giao dịch mua duy nhất.
                    Có thể là token của Stripe Subscription (sub_XXX) hoặc Payment Intent (pi_XXX)
              required:
                - customer_user_id
                - stripe_token
          required:
            - type
            - attributes
      required:
        - data
    StripeValidationResponse:
      type: object
      properties:
        data:
          type: object
          nullable: true
          description: Dữ liệu phản hồi (null khi xác thực thành công)
      required:
        - data
    StripeErrorResponse:
      type: object
      properties:
        errors:
          type: array
          items:
            type: object
            properties:
              detail:
                type: string
                description: Thông tin mô tả về lỗi
              source:
                type: object
                properties:
                  pointer:
                    type: string
                    description: Tham chiếu đến vị trí chính xác trong tài liệu yêu cầu gây ra sự cố
                required:
                  - pointer
              status:
                type: string
                description: Mã trạng thái HTTP
            required:
              - detail
              - source
              - status
      required:
        - errors
    ErrorResponse:
      type: object
      properties:
        errors:
          type: array
          items:
            type: object
            properties:
              source:
                type: string
                nullable: true
                description: Nguồn gốc lỗi
              errors:
                type: array
                items:
                  type: string
                description: Mảng thông báo lỗi
        error_code:
          type: string
          description: Tên lỗi rút gọn
        status_code:
          type: integer
          description: Mã trạng thái HTTP
      required:
        - errors
        - error_code
        - status_code
  securitySchemes:
    apikeyAuth:
      type: apiKey
      name: Authorization
      in: header
      default: Api-Key {Your secret API key}
      description: |
        Các yêu cầu API phải được xác thực bằng secret API key của bạn trong header **Authorization**
        với giá trị `Api-Key {your_secret_api_key}`, ví dụ:
        `Api-Key secret_live_...`. Tìm key này trong Adapty Dashboard ->
        **App Settings** -> tab **General** -> phần **API keys**.
```
