# Thiết lập giao dịch

> Tạo giao dịch mới cho người dùng cuối trong ứng dụng của bạn trong Adapty và cung cấp mức độ truy cập. Giao dịch được tạo bởi phương thức này sẽ xuất hiện trong phân tích và Event Feed của bạn, đồng thời sẽ được gửi tới tất cả các tích hợp.
>
> **Cập nhật gói đăng ký hiện có:** Để cập nhật `billing_issue_detected_at` hoặc `renew_status_changed_at` mà không tạo sự kiện `subscription_renewed` mới, hãy sử dụng lại `store_transaction_id` hiện có (trong cùng `store_original_transaction_id`). Việc gửi bất kỳ `store_transaction_id` mới nào sẽ được coi là giao dịch mới và kích hoạt sự kiện `subscription_renewed`.

## OpenAPI

```yaml
/api-specs/adapty-api.yaml post /api/v2/server-side-api/purchase/set/transaction/
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/set/transaction/:
    post:
      summary: Thiết lập giao dịch
      description: |
        Tạo giao dịch mới cho người dùng cuối trong ứng dụng của bạn trong Adapty và cung cấp mức độ truy cập. Giao dịch được tạo bởi phương thức này sẽ xuất hiện trong phân tích và Event Feed của bạn, đồng thời sẽ được gửi tới tất cả các tích hợp.

        **Cập nhật gói đăng ký hiện có:** Để cập nhật `billing_issue_detected_at` hoặc `renew_status_changed_at` mà không tạo sự kiện `subscription_renewed` mới, hãy sử dụng lại `store_transaction_id` hiện có (trong cùng `store_original_transaction_id`). Việc gửi bất kỳ `store_transaction_id` mới nào sẽ được coi là giao dịch mới và kích hoạt sự kiện `subscription_renewed`.
      operationId: setTransaction
      tags:
        - Purchase
      security:
        - apikeyAuth: []
      parameters:
        - name: adapty-customer-user-id
          in: header
          required: false
          schema:
            type: string
          description: ID duy nhất của khách hàng trong hệ thống của bạn. Cần cung cấp `adapty-customer-user-id` hoặc `adapty-profile-id`.
        - name: adapty-profile-id
          in: header
          required: false
          schema:
            type: string
          description: ID duy nhất của hồ sơ người dùng trong hệ thống của bạn. Lựa chọn tốt nhất khi làm việc với hồ sơ ẩn danh. Cần cung cấp `adapty-customer-user-id` hoặc `adapty-profile-id`.
      responses:
        "200":
          description: Giao dịch được ghi lại thành công
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ProfileResponse"
        "400":
          description: Yêu cầu không hợp lệ
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ErrorResponse"
        "401":
          description: Không được phép
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ErrorResponse"
        "404":
          description: Không tìm thấy hồ sơ người dùng
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ErrorResponse"
        "500":
          description: Lỗi máy chủ nội bộ
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/TransactionDTORequest"
            examples:
              one_time_purchase:
                summary: Ví dụ sản phẩm mua một lần
                value:
                  purchase_type: one_time_purchase
                  store: app_store
                  environment: Production
                  store_product_id: premium_lifetime
                  store_transaction_id: "1000000123456789"
                  store_original_transaction_id: "1000000123456789"
                  is_family_shared: false
                  price:
                    country: US
                    currency: USD
                    value: 9.99
                  purchased_at: "2024-01-15T10:30:00Z"
                  variation_id: variation_123
                  offer: null
                  refunded_at: null
                  cancellation_reason: null
              subscription:
                summary: Ví dụ gói đăng ký
                value:
                  purchase_type: subscription
                  store: app_store
                  environment: Production
                  store_product_id: premium_monthly
                  store_transaction_id: "1000000123456789"
                  store_original_transaction_id: "1000000123456789"
                  is_family_shared: false
                  price:
                    country: US
                    currency: USD
                    value: 4.99
                  purchased_at: "2024-01-15T10:30:00Z"
                  originally_purchased_at: "2024-01-15T10:30:00Z"
                  expires_at: "2024-02-15T10:30:00Z"
                  renew_status: true
                  renew_status_changed_at: null
                  billing_issue_detected_at: null
                  grace_period_expires_at: null
                  variation_id: variation_123
                  offer:
                    category: introductory
                    type: free_trial
                    id: trial_offer_123
                  refunded_at: null
                  cancellation_reason: null
components:
  schemas:
    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
    TransactionDTORequest:
      oneOf:
        - $ref: "#/components/schemas/OneTimePurchaseDTO"
        - $ref: "#/components/schemas/SubscriptionDTO"
      discriminator:
        propertyName: purchase_type
    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
    OneTimePurchaseDTO:
      type: object
      title: Sản phẩm mua một lần
      description: Dữ liệu giao dịch cho sản phẩm mua một lần
      properties:
        purchase_type:
          type: string
          enum:
            - one_time_purchase
          description: Loại giao dịch mua
        store:
          type: string
          description: Cửa hàng nơi thực hiện giao dịch mua. Các giá trị phổ biến bao gồm `app_store`, `play_store`, `stripe`, `paddle`, hoặc bất kỳ định danh cửa hàng tùy chỉnh nào
        environment:
          type: string
          enum:
            - Production
            - Sandbox
          default: Production
          description: Môi trườ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_transaction_id:
          type: string
          maxLength: 50
          description: ID giao dịch trong cửa hàng. Tối đa 50 ký tự. ID giao dịch PayPal và một số cửa hàng tùy chỉnh có thể vượt quá giới hạn này — hãy cắt ngắn hoặc băm giá trị trước khi gửi, nếu không yêu cầu sẽ thất bại.
        store_original_transaction_id:
          type: string
          description: ID giao dịch gốc trong cửa hàng
        is_family_shared:
          type: boolean
          default: false
          description: Liệu giao dịch mua có được chia sẻ gia đình hay không
        price:
          $ref: "#/components/schemas/PriceDTO"
        purchased_at:
          type: string
          format: date-time
          description: Thời điểm thực hiện giao dịch mua
        variation_id:
          type: string
          nullable: true
          description: ID biến thể cho A/B test
        offer:
          $ref: "#/components/schemas/OfferDTO"
          nullable: true
        refunded_at:
          type: string
          format: date-time
          nullable: true
          description: Thời điểm giao dịch mua được hoàn tiền
        cancellation_reason:
          type: string
          enum:
            - billing_error
            - cancelled_by_developer
            - new_subscription_replace
            - price_increase
            - product_was_not_available
            - refund
            - unknown
            - upgraded
            - voluntarily_cancelled
            - adapty_revoked
          nullable: true
          description: Lý do hủy
      required:
        - purchase_type
        - store
        - store_product_id
        - store_transaction_id
        - store_original_transaction_id
        - price
        - purchased_at
    SubscriptionDTO:
      type: object
      title: Gói đăng ký
      description: Dữ liệu giao dịch cho gói đăng ký
      properties:
        purchase_type:
          type: string
          enum:
            - subscription
          description: Loại giao dịch mua
        store:
          type: string
          description: Cửa hàng nơi thực hiện giao dịch mua. Các giá trị phổ biến bao gồm `app_store`, `play_store`, `stripe`, `paddle`, hoặc bất kỳ định danh cửa hàng tùy chỉnh nào
        environment:
          type: string
          enum:
            - Production
            - Sandbox
          default: Production
          description: Môi trườ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_transaction_id:
          type: string
          maxLength: 50
          description: ID giao dịch trong cửa hàng. Tối đa 50 ký tự. ID giao dịch PayPal và một số cửa hàng tùy chỉnh có thể vượt quá giới hạn này — hãy cắt ngắn hoặc băm giá trị trước khi gửi, nếu không yêu cầu sẽ thất bại.
        store_original_transaction_id:
          type: string
          description: ID giao dịch gốc trong cửa hàng
        is_family_shared:
          type: boolean
          default: false
          description: Liệu giao dịch mua có được chia sẻ gia đình hay không
        price:
          $ref: "#/components/schemas/PriceDTO"
        purchased_at:
          type: string
          format: date-time
          description: Thời điểm thực hiện giao dịch mua
        variation_id:
          type: string
          nullable: true
          description: ID biến thể cho A/B test
        offer:
          $ref: "#/components/schemas/OfferDTO"
          nullable: true
        refunded_at:
          type: string
          format: date-time
          nullable: true
          description: Thời điểm giao dịch mua được hoàn tiền
        cancellation_reason:
          type: string
          enum:
            - billing_error
            - cancelled_by_developer
            - new_subscription_replace
            - price_increase
            - product_was_not_available
            - refund
            - unknown
            - upgraded
            - voluntarily_cancelled
            - adapty_revoked
          nullable: true
          description: Lý do hủy
        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
          description: Thời điểm gói đăng ký hết hạn
        renew_status:
          type: boolean
          description: Liệu gói đăng ký có tự động gia hạn hay không
        renew_status_changed_at:
          type: string
          format: date-time
          nullable: true
          description: Thời điểm trạng thái gia hạn thay đổi
        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
        grace_period_expires_at:
          type: string
          format: date-time
          nullable: true
          description: Thời điểm thời gian ân hạn hết hạn
      required:
        - purchase_type
        - store
        - store_product_id
        - store_transaction_id
        - store_original_transaction_id
        - price
        - purchased_at
        - originally_purchased_at
        - expires_at
        - renew_status
    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
    PriceDTO:
      type: object
      properties:
        country:
          type: string
          description: Mã quốc gia
        currency:
          type: string
          description: Mã tiền tệ
        value:
          type: number
          format: float
          description: Giá trị giá
      required:
        - country
        - currency
        - value
    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**.
```
