# Paddle の購入を検証する

> Adapty ダッシュボードのアプリ設定に登録された Paddle の認証情報を使用して、指定された Paddle トークンで購入を検証します。
> 購入が有効な場合、Paddle からトランザクション履歴が、指定された customer_user_id を持つ Adapty のプロファイルにインポートされます。
> この customer_user_id を持つプロファイルが存在しない場合は、新たに作成されます。

## OpenAPI

```yaml
/api-specs/adapty-api.yaml post /api/v2/server-side-api/purchase/paddle/token/validate/
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/paddle/token/validate/:
    post:
      summary: Paddle の購入を検証する
      description: |
        Adapty ダッシュボードのアプリ設定に登録された Paddle の認証情報を使用して、指定された Paddle トークンで購入を検証します。
        購入が有効な場合、Paddle からトランザクション履歴が、指定された customer_user_id を持つ Adapty のプロファイルにインポートされます。
        この customer_user_id を持つプロファイルが存在しない場合は、新たに作成されます。
      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: 購入が正常に検証されました
          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: リクエストが不正です
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ErrorResponse"
              examples:
                no_products_found:
                  summary: プロダクトが見つかりません
                  value:
                    errors:
                      - No products found
                    error_code: no_products_found
                    status_code: 400
                paddle_api_key_not_found:
                  summary: Paddle API キーが見つかりません
                  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: Paddle の認証情報が無効または購入が見つかりません
                  value:
                    errors:
                      - Invalid Paddle credentials or purchase not found
                    error_code: invalid_paddle_credentials_or_purchase_not_found
                    status_code: 400
        "401":
          description: 認証エラー
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ErrorResponse"
              example:
                errors:
                  - Invalid API key
                error_code: unauthorized
                status_code: 401
        "500":
          description: 内部サーバーエラー
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ErrorResponse"
components:
  schemas:
    PaddleValidationRequest:
      type: object
      properties:
        customer_user_id:
          type: string
          description: お客様のシステムにおけるユーザーの ID
        paddle_token:
          type: string
          description: |
            一意の購入を表す Paddle オブジェクトのトークン。
            トランザクション ID（txn_...）またはサブスクリプション ID（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: エラーの発生源
              errors:
                type: array
                items:
                  type: string
                description: エラーメッセージの配列
        error_code:
          type: string
          description: エラーの短縮名
        status_code:
          type: integer
          description: HTTP ステータスコード
      required:
        - errors
        - error_code
        - status_code
    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
    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: 消耗型アイテムの購入かどうか
    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** セクションで確認できます。
```
