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

> Xác thực giao dịch mua bằng token Paddle được cung cấp, sử dụng thông tin xác thực Paddle 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ừ Paddle 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.

## OpenAPI

```yaml
/api-specs/adapty-api.yaml post /api/v2/server-side-api/purchase/paddle/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/v2/server-side-api/purchase/paddle/token/validate/:
    post:
      summary: Xác thực giao dịch mua Paddle
      description: |
        Xác thực giao dịch mua bằng token Paddle được cung cấp, sử dụng thông tin xác thực Paddle 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ừ Paddle 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.
      operationId: validatePaddlePurchase
      tags:
        - Paddle
      security:
        - apikeyAuth: []
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/PaddleValidationRequest"
            example:
              customer_user_id: <YOUR_CUSTOMER_USER_ID>
              paddle_token: live_7d279f61a3339fed520f7cd8c08
      responses:
        "200":
          description: Giao dịch mua được xác thực thành công
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ProfileResponse"
              example:
                data:
                  app_id: 14c3d623-2f3a-455a-aa86-ef83dff6913b
                  profile_id: 3286abd3-48b0-4e9c-a5f6-ac0a006804a6
                  customer_user_id: Jane.doe@example.com
                  total_revenue_usd: 0
                  segment_hash: 8f45947bad31ab0c
                  timestamp: 1736436751469
                  custom_attributes:
                    - key: favourite_sport
                      value: yoga
                  access_levels: []
                  subscriptions:
                    - purchase_id: 5a7ab471-2299-45f7-ad69-1d395c1256e3
                      store: app_store
                      store_product_id: 1year.premium
                      store_base_plan_id: null
                      store_transaction_id: "30002109551456"
                      store_original_transaction_id: "30002109551456"
                      purchased_at: "2022-10-12T09:42:50+00:00"
                      environment: Production
                      is_refund: false
                      is_consumable: false
                  non_subscriptions: []
        "400":
          description: Yêu cầu không hợp lệ
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ErrorResponse"
              examples:
                no_products_found:
                  summary: Không tìm thấy sản phẩm
                  value:
                    errors:
                      - No products found
                    error_code: no_products_found
                    status_code: 400
                paddle_api_key_not_found:
                  summary: Không tìm thấy API key Paddle
                  value:
                    errors:
                      - Paddle API key not found
                    error_code: paddle_api_key_not_found
                    status_code: 400
                invalid_paddle_credentials_or_purchase_not_found:
                  summary: Thông tin xác thực Paddle không hợp lệ hoặc không tìm thấy giao dịch mua
                  value:
                    errors:
                      - Invalid Paddle credentials or purchase not found
                    error_code: invalid_paddle_credentials_or_purchase_not_found
                    status_code: 400
        "401":
          description: Không được phép
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ErrorResponse"
              example:
                errors:
                  - Invalid API key
                error_code: unauthorized
                status_code: 401
        "500":
          description: Lỗi máy chủ nội bộ
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ErrorResponse"
components:
  schemas:
    PaddleValidationRequest:
      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
        paddle_token:
          type: string
          description: |
            Token của đối tượng Paddle đại diện cho một giao dịch mua duy nhất.
            Có thể là ID giao dịch (txn_...) hoặc ID gói đăng ký (sub_...)
      required:
        - customer_user_id
        - paddle_token
    ProfileResponse:
      type: object
      properties:
        data:
          $ref: "#/components/schemas/Profile"
      required:
        - data
    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
    Profile:
      type: object
      properties:
        app_id:
          type: string
          format: uuid
          description: ID nội bộ của ứng dụng của bạn
        profile_id:
          type: string
          format: uuid
          description: ID hồ sơ người dùng Adapty
        customer_user_id:
          type: string
          nullable: true
          description: ID người dùng của bạn trong hệ thống của bạn
        total_revenue_usd:
          type: number
          format: float
          description: Giá trị float biểu thị tổng doanh thu tính bằng USD kiếm được trong hồ sơ người dùng
        segment_hash:
          type: string
          description: Tham số nội bộ
        timestamp:
          type: integer
          format: int64
          description: Thời gian phản hồi tính bằng mili giây, cần để giải quyết điều kiện tranh chấp
        custom_attributes:
          type: array
          items:
            $ref: "#/components/schemas/CustomAttribute"
          description: Tối đa 30 thuộc tính tùy chỉnh được phép đặt cho hồ sơ người dùng
        access_levels:
          type: array
          items:
            $ref: "#/components/schemas/AccessLevel"
          description: Mảng các đối tượng mức độ truy cập. Mảng rỗng nếu khách hàng không có mức độ truy cập nào
        subscriptions:
          type: array
          items:
            $ref: "#/components/schemas/Subscription"
          description: Mảng các đối tượng gói đăng ký. Mảng rỗng nếu khách hàng không có gói đăng ký nào
        non_subscriptions:
          type: array
          items:
            $ref: "#/components/schemas/NonSubscription"
          description: Mảng các đối tượng sản phẩm không phải gói đăng ký. Mảng rỗng nếu khách hàng không có giao dịch mua nào
      required:
        - app_id
        - profile_id
        - customer_user_id
        - total_revenue_usd
        - segment_hash
        - timestamp
        - custom_attributes
        - access_levels
        - subscriptions
        - non_subscriptions
    CustomAttribute:
      type: object
      properties:
        key:
          type: string
          maxLength: 30
          description: Khóa phải là chuỗi không quá 30 ký tự. Chỉ cho phép chữ cái, số, dấu gạch ngang, dấu chấm và dấu gạch dưới
        value:
          oneOf:
            - type: string
            - type: number
          description: Giá trị thuộc tính không được quá 50 ký tự. Chỉ cho phép chuỗi và số thực làm giá trị
      required:
        - key
        - value
    AccessLevel:
      type: object
      properties:
        access_level_id:
          type: string
          description: Định danh mức độ truy cập
        store:
          type: string
          description: Cửa hàng nơi mức độ truy cập được mua
        store_product_id:
          type: string
          description: ID sản phẩm trong cửa hàng
        store_base_plan_id:
          type: string
          nullable: true
          description: ID gói cơ bản trong cửa hàng
        store_transaction_id:
          type: string
          description: ID giao dịch trong cửa hàng
        store_original_transaction_id:
          type: string
          description: ID giao dịch gốc trong cửa hàng
        offer:
          allOf:
            - $ref: "#/components/schemas/OfferDTO"
          nullable: true
          description: Thông tin ưu đãi, nếu có áp dụng ưu đãi hoặc ưu đãi giới thiệu
        starts_at:
          type: string
          format: date-time
          nullable: true
          description: Thời điểm mức độ truy cập bắt đầu
        purchased_at:
          type: string
          format: date-time
          description: Thời điểm mức độ truy cập được mua
        originally_purchased_at:
          type: string
          format: date-time
          description: Thời điểm mức độ truy cập được mua lần đầu
        expires_at:
          type: string
          format: date-time
          nullable: true
          description: Thời điểm mức độ truy cập hết hạn
        renewal_cancelled_at:
          type: string
          format: date-time
          nullable: true
          description: Thời điểm gia hạn bị hủy
        billing_issue_detected_at:
          type: string
          format: date-time
          nullable: true
          description: Thời điểm phát hiện vấn đề thanh toán
        is_in_grace_period:
          type: boolean
          description: Liệu mức độ truy cập có đang trong thời gian ân hạn hay không
        cancellation_reason:
          type: string
          nullable: true
          description: Lý do hủy
    Subscription:
      type: object
      properties:
        store:
          type: string
          description: Cửa hàng nơi gói đăng ký được mua
        store_product_id:
          type: string
          description: ID sản phẩm trong cửa hàng
        store_base_plan_id:
          type: string
          nullable: true
          description: ID gói cơ bản trong cửa hàng
        store_transaction_id:
          type: string
          description: ID giao dịch trong cửa hàng
        store_original_transaction_id:
          type: string
          description: ID giao dịch gốc trong cửa hàng
        offer:
          allOf:
            - $ref: "#/components/schemas/OfferDTO"
          nullable: true
          description: Thông tin ưu đãi, nếu có áp dụng ưu đãi hoặc ưu đãi giới thiệu
        environment:
          type: string
          description: Môi trường (Sandbox, Production)
        purchased_at:
          type: string
          format: date-time
          description: Thời điểm gói đăng ký được mua
        originally_purchased_at:
          type: string
          format: date-time
          description: Thời điểm gói đăng ký được mua lần đầu
        expires_at:
          type: string
          format: date-time
          nullable: true
          description: Thời điểm gói đăng ký hết hạn
        renewal_cancelled_at:
          type: string
          format: date-time
          nullable: true
          description: Thời điểm gia hạn bị hủy
        billing_issue_detected_at:
          type: string
          format: date-time
          nullable: true
          description: Thời điểm phát hiện vấn đề thanh toán
        is_in_grace_period:
          type: boolean
          description: Liệu gói đăng ký có đang trong thời gian ân hạn hay không
        cancellation_reason:
          type: string
          nullable: true
          description: Lý do hủy
    NonSubscription:
      type: object
      properties:
        purchase_id:
          type: string
          format: uuid
          description: Định danh giao dịch mua duy nhất
        store:
          type: string
          description: Cửa hàng nơi thực hiện giao dịch mua
        store_product_id:
          type: string
          description: ID sản phẩm trong cửa hàng
        store_base_plan_id:
          type: string
          nullable: true
          description: ID gói cơ bản trong cửa hàng
        store_transaction_id:
          type: string
          description: ID giao dịch trong cửa hàng
        store_original_transaction_id:
          type: string
          description: ID giao dịch gốc trong cửa hàng
        purchased_at:
          type: string
          format: date-time
          description: Thời điểm thực hiện giao dịch mua
        environment:
          type: string
          description: Môi trường (Sandbox, Production)
        is_refund:
          type: boolean
          description: Liệu đây có phải là hoàn tiền hay không
        is_consumable:
          type: boolean
          description: Liệu đây có phải là giao dịch mua consumable hay không
    OfferDTO:
      type: object
      properties:
        category:
          type: string
          enum:
            - introductory
            - promotional
            - offer_code
            - win_back
          description: Danh mục ưu đãi
        type:
          type: string
          enum:
            - free_trial
            - pay_as_you_go
            - pay_up_front
          description: Loại ưu đãi
        id:
          type: string
          nullable: true
          description: ID ưu đãi
      required:
        - category
        - type
  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**.
```
