# トランザクションを設定する

> Adapty にアプリのエンドユーザーの新しいトランザクションを作成し、アクセスレベルを付与します。このメソッドで作成されたトランザクションはアナリティクスおよびイベントフィードに表示され、すべてのインテグレーションに送信されます。
>
> **既存のサブスクリプションの更新:** 新しい `subscription_renewed` イベントを発生させずに `billing_issue_detected_at` または `renew_status_changed_at` を更新するには、既存の `store_transaction_id` を同じ `store_original_transaction_id` の下で再利用してください。新しい `store_transaction_id` を送信すると新しいトランザクションとして扱われ、`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 サーバーサイド API
  version: 1.0.0
servers:
  - url: https://api.adapty.io
    description: 本番サーバー
paths:
  /api/v2/server-side-api/purchase/set/transaction/:
    post:
      summary: トランザクションを設定する
      description: |
        Adapty にアプリのエンドユーザーの新しいトランザクションを作成し、アクセスレベルを付与します。このメソッドで作成されたトランザクションはアナリティクスおよびイベントフィードに表示され、すべてのインテグレーションに送信されます。

        **既存のサブスクリプションの更新:** 新しい `subscription_renewed` イベントを発生させずに `billing_issue_detected_at` または `renew_status_changed_at` を更新するには、既存の `store_transaction_id` を同じ `store_original_transaction_id` の下で再利用してください。新しい `store_transaction_id` を送信すると新しいトランザクションとして扱われ、`subscription_renewed` イベントが発火します。
      operationId: setTransaction
      tags:
        - Purchase
      security:
        - apikeyAuth: []
      parameters:
        - name: adapty-customer-user-id
          in: header
          required: false
          schema:
            type: string
          description: お客様のシステムにおける顧客の一意の ID です。`adapty-customer-user-id` または `adapty-profile-id` のいずれかが必須です。
        - name: adapty-profile-id
          in: header
          required: false
          schema:
            type: string
          description: お客様のシステムにおけるプロファイルの一意の ID です。匿名プロファイルを扱う場合に最適です。`adapty-customer-user-id` または `adapty-profile-id` のいずれかが必須です。
      responses:
        "200":
          description: トランザクションが正常に記録されました
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ProfileResponse"
        "400":
          description: リクエストが不正です
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ErrorResponse"
        "401":
          description: 認証エラー
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ErrorResponse"
        "404":
          description: プロファイルが見つかりません
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ErrorResponse"
        "500":
          description: 内部サーバーエラー
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/TransactionDTORequest"
            examples:
              one_time_purchase:
                summary: 買い切り購入の例
                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: サブスクリプションの例
                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: エラーの発生源
              errors:
                type: array
                items:
                  type: string
                description: エラーメッセージの配列
        error_code:
          type: string
          description: エラーの短縮名
        status_code:
          type: integer
          description: 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
        profile_id:
          type: string
          format: uuid
          description: Adapty プロファイル ID
        customer_user_id:
          type: string
          nullable: true
          description: お客様のシステムにおけるユーザーの ID
        total_revenue_usd:
          type: number
          format: float
          description: プロファイルで獲得した USD 換算の総収益を表す浮動小数点値
        segment_hash:
          type: string
          description: 内部パラメータ
        timestamp:
          type: integer
          format: int64
          description: レスポンス時刻（ミリ秒）。競合状態の解決に使用します
        custom_attributes:
          type: array
          items:
            $ref: "#/components/schemas/CustomAttribute"
          description: プロファイルに設定できるカスタム属性は最大 30 個です
        access_levels:
          type: array
          items:
            $ref: "#/components/schemas/AccessLevel"
          description: アクセスレベルオブジェクトの配列。顧客がアクセスレベルを持たない場合は空配列
        subscriptions:
          type: array
          items:
            $ref: "#/components/schemas/Subscription"
          description: サブスクリプションオブジェクトの配列。顧客がサブスクリプションを持たない場合は空配列
        non_subscriptions:
          type: array
          items:
            $ref: "#/components/schemas/NonSubscription"
          description: 非サブスクリプションオブジェクトの配列。顧客が購入履歴を持たない場合は空配列
      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: One-time Purchase
      description: 買い切り購入のトランザクションデータ
      properties:
        purchase_type:
          type: string
          enum:
            - one_time_purchase
          description: 購入の種別
        store:
          type: string
          description: 購入が行われたストア。一般的な値には `app_store`、`play_store`、`stripe`、`paddle`、またはカスタムストア識別子が含まれます
        environment:
          type: string
          enum:
            - Production
            - Sandbox
          default: Production
          description: 購入が行われた環境
        store_product_id:
          type: string
          description: ストアにおけるプロダクト ID
        store_transaction_id:
          type: string
          maxLength: 50
          description: ストアにおけるトランザクション ID。最大 50 文字。PayPal および一部のカスタムストアのトランザクション ID はこれを超える場合があります。その場合は送信前に値をトリミングまたはハッシュ化してください。そうしないとリクエストが失敗します。
        store_original_transaction_id:
          type: string
          description: ストアにおける元のトランザクション ID
        is_family_shared:
          type: boolean
          default: false
          description: ファミリー共有の購入かどうか
        price:
          $ref: "#/components/schemas/PriceDTO"
        purchased_at:
          type: string
          format: date-time
          description: 購入が行われた日時
        variation_id:
          type: string
          nullable: true
          description: A/B テスト用のバリエーション ID
        offer:
          $ref: "#/components/schemas/OfferDTO"
          nullable: true
        refunded_at:
          type: string
          format: date-time
          nullable: true
          description: 購入が返金された日時
        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: キャンセルの理由
      required:
        - purchase_type
        - store
        - store_product_id
        - store_transaction_id
        - store_original_transaction_id
        - price
        - purchased_at
    SubscriptionDTO:
      type: object
      title: Subscription
      description: サブスクリプション購入のトランザクションデータ
      properties:
        purchase_type:
          type: string
          enum:
            - subscription
          description: 購入の種別
        store:
          type: string
          description: 購入が行われたストア。一般的な値には `app_store`、`play_store`、`stripe`、`paddle`、またはカスタムストア識別子が含まれます
        environment:
          type: string
          enum:
            - Production
            - Sandbox
          default: Production
          description: 購入が行われた環境
        store_product_id:
          type: string
          description: ストアにおけるプロダクト ID
        store_transaction_id:
          type: string
          maxLength: 50
          description: ストアにおけるトランザクション ID。最大 50 文字。PayPal および一部のカスタムストアのトランザクション ID はこれを超える場合があります。その場合は送信前に値をトリミングまたはハッシュ化してください。そうしないとリクエストが失敗します。
        store_original_transaction_id:
          type: string
          description: ストアにおける元のトランザクション ID
        is_family_shared:
          type: boolean
          default: false
          description: ファミリー共有の購入かどうか
        price:
          $ref: "#/components/schemas/PriceDTO"
        purchased_at:
          type: string
          format: date-time
          description: 購入が行われた日時
        variation_id:
          type: string
          nullable: true
          description: A/B テスト用のバリエーション ID
        offer:
          $ref: "#/components/schemas/OfferDTO"
          nullable: true
        refunded_at:
          type: string
          format: date-time
          nullable: true
          description: 購入が返金された日時
        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: キャンセルの理由
        originally_purchased_at:
          type: string
          format: date-time
          description: サブスクリプションが最初に購入された日時
        expires_at:
          type: string
          format: date-time
          description: サブスクリプションの有効期限
        renew_status:
          type: boolean
          description: サブスクリプションが自動更新されるかどうか
        renew_status_changed_at:
          type: string
          format: date-time
          nullable: true
          description: 更新ステータスが変更された日時
        billing_issue_detected_at:
          type: string
          format: date-time
          nullable: true
          description: 請求問題が検出された日時
        grace_period_expires_at:
          type: string
          format: date-time
          nullable: true
          description: グレース期間の終了日時
      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: キーは 30 文字以下の文字列である必要があります。英字、数字、ダッシュ、ピリオド、アンダースコアのみ使用できます
        value:
          oneOf:
            - type: string
            - type: number
          description: 属性値は 50 文字以下である必要があります。文字列と浮動小数点数のみ使用できます
      required:
        - key
        - value
    AccessLevel:
      type: object
      properties:
        access_level_id:
          type: string
          description: アクセスレベルの識別子
        store:
          type: string
          description: アクセスレベルが購入されたストア
        store_product_id:
          type: string
          description: ストアにおけるプロダクト ID
        store_base_plan_id:
          type: string
          nullable: true
          description: ストアにおけるベースプラン ID
        store_transaction_id:
          type: string
          description: ストアにおけるトランザクション ID
        store_original_transaction_id:
          type: string
          description: ストアにおける元のトランザクション ID
        offer:
          allOf:
            - $ref: "#/components/schemas/OfferDTO"
          nullable: true
          description: プロモーションオファーまたは初回オファーが適用された場合のオファー詳細
        starts_at:
          type: string
          format: date-time
          nullable: true
          description: アクセスレベルの開始日時
        purchased_at:
          type: string
          format: date-time
          description: アクセスレベルが購入された日時
        originally_purchased_at:
          type: string
          format: date-time
          description: アクセスレベルが最初に購入された日時
        expires_at:
          type: string
          format: date-time
          nullable: true
          description: アクセスレベルの有効期限
        renewal_cancelled_at:
          type: string
          format: date-time
          nullable: true
          description: 更新がキャンセルされた日時
        billing_issue_detected_at:
          type: string
          format: date-time
          nullable: true
          description: 請求問題が検出された日時
        is_in_grace_period:
          type: boolean
          description: アクセスレベルがグレース期間中かどうか
        cancellation_reason:
          type: string
          nullable: true
          description: キャンセルの理由
    Subscription:
      type: object
      properties:
        store:
          type: string
          description: サブスクリプションが購入されたストア
        store_product_id:
          type: string
          description: ストアにおけるプロダクト ID
        store_base_plan_id:
          type: string
          nullable: true
          description: ストアにおけるベースプラン ID
        store_transaction_id:
          type: string
          description: ストアにおけるトランザクション ID
        store_original_transaction_id:
          type: string
          description: ストアにおける元のトランザクション ID
        offer:
          allOf:
            - $ref: "#/components/schemas/OfferDTO"
          nullable: true
          description: プロモーションオファーまたは初回オファーが適用された場合のオファー詳細
        environment:
          type: string
          description: 環境（サンドボックス、本番）
        purchased_at:
          type: string
          format: date-time
          description: サブスクリプションが購入された日時
        originally_purchased_at:
          type: string
          format: date-time
          description: サブスクリプションが最初に購入された日時
        expires_at:
          type: string
          format: date-time
          nullable: true
          description: サブスクリプションの有効期限
        renewal_cancelled_at:
          type: string
          format: date-time
          nullable: true
          description: 更新がキャンセルされた日時
        billing_issue_detected_at:
          type: string
          format: date-time
          nullable: true
          description: 請求問題が検出された日時
        is_in_grace_period:
          type: boolean
          description: サブスクリプションがグレース期間中かどうか
        cancellation_reason:
          type: string
          nullable: true
          description: キャンセルの理由
    NonSubscription:
      type: object
      properties:
        purchase_id:
          type: string
          format: uuid
          description: 購入の一意の識別子
        store:
          type: string
          description: 購入が行われたストア
        store_product_id:
          type: string
          description: ストアにおけるプロダクト ID
        store_base_plan_id:
          type: string
          nullable: true
          description: ストアにおけるベースプラン ID
        store_transaction_id:
          type: string
          description: ストアにおけるトランザクション ID
        store_original_transaction_id:
          type: string
          description: ストアにおける元のトランザクション ID
        purchased_at:
          type: string
          format: date-time
          description: 購入が行われた日時
        environment:
          type: string
          description: 環境（サンドボックス、本番）
        is_refund:
          type: boolean
          description: 返金かどうか
        is_consumable:
          type: boolean
          description: 消耗型アイテムの購入かどうか
    PriceDTO:
      type: object
      properties:
        country:
          type: string
          description: 国コード
        currency:
          type: string
          description: 通貨コード
        value:
          type: number
          format: float
          description: 価格の値
      required:
        - country
        - currency
        - value
    OfferDTO:
      type: object
      properties:
        category:
          type: string
          enum:
            - introductory
            - promotional
            - offer_code
            - win_back
          description: オファーのカテゴリ
        type:
          type: string
          enum:
            - free_trial
            - pay_as_you_go
            - pay_up_front
          description: オファーの種別
        id:
          type: string
          nullable: true
          description: オファー ID
      required:
        - category
        - type
  securitySchemes:
    apikeyAuth:
      type: apiKey
      name: Authorization
      in: header
      default: Api-Key {Your secret API key}
      description: |
        API リクエストは、**Authorization** ヘッダーにシークレット API キーを `Api-Key {your_secret_api_key}` の形式（例: `Api-Key secret_live_...`）で指定して認証する必要があります。このキーは Adapty ダッシュボード → **App Settings** → **General** タブ → **API keys** セクションで確認できます。
```
