> ## Documentation Index
> Fetch the complete documentation index at: https://docs.cog888.net/llms.txt
> Use this file to discover all available pages before exploring further.

# SyncCredit 範例

> 單一錢包異動類型 Payload 差異比較與測試案例

## SyncCredit API 共用 Payload 結構

所有類型都使用相同的 `dt/callback/sync_credit` API，Payload 格式如下：

```json theme={null}
{
  "platform_id": "產品代碼",
  "uuid": "唯一識別碼",
  "account": "玩家帳號",
  "token": "MD5 驗證碼",
  "info": [
    {
      "type": 異動類型編號,
      "credit": 額度變化 (正=入款, 負=扣款),
      "order_id": "注單編號",
      "canceled_id": "被取消的交易ID (僅取消類使用)",
      "transaction_id": "交易ID",
      "proposal_id": "提案號",
      "ip": "玩家IP",
      "target_wallet": 目標錢包 (0=一般, 1=傳統提案錢包),
      "rolling_unit": 轉碼單位,
      "ratio": 寄碼,
      "order_detail": {
        "room_account": "廳帳號",
        "in_proposal": 是否在傳統提案中 (1=否, 2=是),
        "table_id": "桌台ID",
        "gmcode": "局號",
        "game_type": "遊戲類型",
        "play_type": 玩法,
        "amount": 投注金額,
        "valid_amount": 有效投注額,
        "win": 輸贏值,
        "flag": 狀態標記,
        "create_time": 建立時間,
        "reckon_time": 結算時間,
        "last_update_time": 最後更新時間,
        "round_detail": "局結果詳情"
      }
    }
  ]
}
```

***

## 各類型關鍵差異

| 類型            | `credit` 方向    | `canceled_id` | 特殊處理                                                                                |
| ------------- | -------------- | ------------- | ----------------------------------------------------------------------------------- |
| **8 派彩**      | 正值（贏）或 0（輸）    | 空             | 正常結算，包含輸贏金額                                                                         |
| **10 重算局**    | 差額（可正可負）       | 空             | 因現場異常，系統後台調整原派彩差額                                                                   |
| **11 取消局**    | 正值（退還投注）       | 空             | 因現場異常，系統後台退還所有該局投注                                                                  |
| **12 添增注單**   | 贏面為正，輸面為負，即輸贏值 | 空             | 因現場異常，系統後台添加注單，有直接為結算狀態                                                             |
| **13 取消注單**   | 正值（退還投注）       | 空             | 因現場異常，系統後台取消注單                                                                      |
| **23 下注失敗還款** | 正值             | 原交易 ID        | 投注方未收到下注的回覆，該筆交易不成立。貴幣需核對原交易 ID 是否有執行過下注扣款，有執行過的話需要回滾                               |
| **26 修改注單**   | 差額（可正可負）       | 空             | 1. 只有傳統注單會操作，因現場異常，系統後台修改注單  2. 用來更新對沖有效投注額                                         |
| **29 額度回沖**   | 正值             | 原交易 ID        | 通用回沖機制，用於對費失敗等場景，跟 type 23 類似，代表投注方未收到扣款的回覆，該筆交易不成立，貴幣需核對原交易 ID 是否有執行過扣款，有執行過的話需要回滾 |

***

## 額外說明：Type 26 對沖同步機制

在測試案例一中，派彩 (Type 8) 和重算局 (Type 10) 都會伴隨額外的 **Type 26 (credit=0)** 記錄。這是因為**對沖計算 (Hedge Calculation)** 機制。

### 觸發條件

當玩家在同一局有**對沖投注**（例如同時買莊和買閒）時，系統在派彩或重算時會自動產生 Type 26 記錄：

| 遊戲類型                 | 對沖玩法                               |
| -------------------- | ---------------------------------- |
| 百家樂 (BAC) / 牛牛 (NIU) | playtype 1 (莊) ↔ playtype 2 (閒)    |
| 輪盤 (ROU)             | playtype 151-156 之間互為對沖            |
| 點數百家樂 (DTB)          | 多組對沖玩法 (1↔2, 4↔5, 6↔7, 8↔9, 10↔11) |

### 邏輯說明

當處理有對沖注單的派彩/重算時，系統會：

1. 檢查同一局是否有對沖注單存在
2. 若有對沖，則重新計算雙方的**有效投注額 (valid\_amount)**
3. 插入 Type 26 記錄通知單一錢包方 valid\_amount 的變化

### Type 26 對沖同步的欄位

| 欄位             | 值         | 說明             |
| -------------- | --------- | -------------- |
| `type`         | 26        | 修改注單           |
| `credit`       | **固定為 0** | 不影響額度，僅同步有效投注額 |
| `valid_amount` | 調整後的值     | 重新計算後的有效投注額    |

### 範例

案例一中同時下注「買莊 100」和「買閒 100」，這兩筆互為對沖：

* 處理買莊 (playtype=1) 的派彩時 → 產生買閒的 Type 26 (credit=0)
* 處理買閒 (playtype=2) 的派彩時 → 產生買莊的 Type 26 (credit=0)

***

## 測試案例

### 案例一：正常流程 → 重算局 → 取消局

**涵蓋類型：8 派彩、10 重算局、11 取消局**

**開牌設定：**

* 原始開牌（莊贏）：閒 \[♠ 2, ♥3] = 5 點，莊 \[♦K, ♣9] = 9 點 → **莊贏**
* 重算開牌（閒贏）：閒 \[♠ 9, ♥8] = 7 點，莊 \[♦4, ♣2] = 6 點 → **閒贏**

| 步驟 | 動作        | Type | credit | 說明               |
| -- | --------- | ---- | ------ | ---------------- |
| 1  | 下注 100 買莊 | 7    | -100   | 扣款               |
| 2  | 下注 100 買閒 | 7    | -100   | 扣款               |
| 3  | 開牌莊贏，派彩   | 8    | +200   | 買莊贏（1:1，退還本金+獎金） |
| 4  | (同上)      | 8    | 0      | 買閒輸              |
| 5  | 後台重算為閒贏   | 10   | -200   | 買莊（原贏變輸，扣回派彩）    |
| 6  | (同上)      | 10   | +200   | 買閒（原輸變贏，補發派彩）    |
| 7  | 後台取消局     | 11   | +100   | 退還買莊             |
| 8  | (同上)      | 11   | +100   | 退還買閒             |

**Payload：**

<AccordionGroup>
  <Accordion title="步驟 1-2：下注扣款 (Type 7)">
    ```json theme={null}
    {
      "account": "st",
      "platform_id": "H88",
      "info": [
        {
          "type": 7,
          "credit": -100,
          "order_id": "9D13260123191230010000001",
          "transaction_id": "9D13260123191230010000001-260123191230-0000001",
          "proposal_id": "958936",
          "order_detail": {
            "play_type": 1,
            "amount": 100,
            "gmcode": "9D13261231KK",
            "game_type": "BAC_CF",
            "table_id": "9D13",
            "room_account": "9D"
          }
        },
        {
          "type": 7,
          "credit": -100,
          "order_id": "9D13260123191230020000002",
          "transaction_id": "9D13260123191230020000002-260123191230-0000002",
          "proposal_id": "958936",
          "order_detail": {
            "play_type": 2,
            "amount": 100,
            "gmcode": "9D13261231KK",
            "game_type": "BAC_CF",
            "table_id": "9D13",
            "room_account": "9D"
          }
        }
      ]
    }
    ```
  </Accordion>

  <Accordion title="步驟 3-4：派彩 (Type 8) + 對沖同步 (Type 26)">
    以下為同一個 API 請求，`info` 陣列包含多個 Object，請依序處理。

    ```json theme={null}
    {
      "account": "st",
      "platform_id": "H88",
      "info": [
        {
          // [1] 買莊派彩：贏 100（退還本金 100 + 獎金 100 = 200）
          "type": 8,
          "credit": 200,
          "canceled_id": "",
          "order_id": "9D13260123191230010000001",
          "transaction_id": "9D13260123191230010000001-1-bac.9d-260123191950",
          "order_detail": {
            "play_type": 1,
            "amount": 100,
            "valid_amount": 100,
            "win": 100,
            "flag": 1,
            "round_detail": "34 51;13 25|3|9|5|0"
          }
        },
        {
          // [2] 對沖同步：更新買莊的有效投注額為 0（因跟買閒對沖）
          "type": 26,
          "credit": 0,
          "canceled_id": "",
          "order_id": "9D13260123191230010000001",
          "transaction_id": "9D13260123191230010000001-1-lobby1-260123191952",
          "order_detail": {
            "play_type": 1,
            "amount": 100,
            "valid_amount": 0,
            "win": 100,
            "flag": 1,
            "round_detail": "34 51;13 25|3|9|5|0"
          }
        },
        {
          // [3] 買閒派彩：輸 100（credit=0，下注時已扣款）
          "type": 8,
          "credit": 0,
          "canceled_id": "",
          "order_id": "9D13260123191230020000002",
          "transaction_id": "9D13260123191230020000002-2-bac.9d-260123191950",
          "order_detail": {
            "play_type": 2,
            "amount": 100,
            "valid_amount": 0,
            "win": -100,
            "flag": 1,
            "round_detail": "34 51;13 25|3|9|5|0"
          }
        }
      ]
    }
    ```
  </Accordion>

  <Accordion title="步驟 5-6：重算局 (Type 10) + 對沖同步 (Type 26)">
    以下為同一個 API 請求，`info` 陣列包含多個 Object，請依序處理。

    ```json theme={null}
    {
      "account": "st",
      "platform_id": "H88",
      "info": [
        {
          // [1] 對沖同步：更新買閒的有效投注額
          "type": 26,
          "credit": 0,
          "canceled_id": "",
          "order_id": "9D13260123191230020000002",
          "transaction_id": "9D13260123191230020000002-2-lobby1-260123193239",
          "order_detail": {
            "play_type": 2,
            "amount": 100,
            "valid_amount": 0,
            "win": -100,
            "flag": 1,
            "round_detail": "41 56;4 18|4|6|7|0"
          }
        },
        {
          // [2] 買莊重算：原本贏變輸，扣回派彩 200
          "type": 10,
          "credit": -200,
          "canceled_id": "",
          "order_id": "9D13260123191230010000001",
          "transaction_id": "9D13260123191230010000001-1-bac.9d-260123193239",
          "order_detail": {
            "play_type": 1,
            "amount": 100,
            "valid_amount": 0,
            "win": -100,
            "flag": 1,
            "round_detail": "41 56;4 18|4|6|7|0"
          }
        },
        {
          // [3] 對沖同步：更新買莊的有效投注額
          "type": 26,
          "credit": 0,
          "canceled_id": "",
          "order_id": "9D13260123191230010000001",
          "transaction_id": "9D13260123191230010000001-1-lobby1-260123193239",
          "order_detail": {
            "play_type": 1,
            "amount": 100,
            "valid_amount": 0,
            "win": -100,
            "flag": 1,
            "round_detail": "41 56;4 18|4|6|7|0"
          }
        },
        {
          // [4] 買閒重算：原本輸變贏，補發派彩 200
          "type": 10,
          "credit": 200,
          "canceled_id": "",
          "order_id": "9D13260123191230020000002",
          "transaction_id": "9D13260123191230020000002-2-bac.9d-260123193239",
          "order_detail": {
            "play_type": 2,
            "amount": 100,
            "valid_amount": 0,
            "win": 100,
            "flag": 1,
            "round_detail": "41 56;4 18|4|6|7|0"
          }
        }
      ]
    }
    ```
  </Accordion>

  <Accordion title="步驟 7-8：取消局 (Type 11) + 對沖同步 (Type 26)">
    以下為同一個 API 請求，`info` 陣列包含多個 Object，請依序處理。

    ```json theme={null}
    {
      "account": "st",
      "platform_id": "H88",
      "info": [
        {
          // [1] 對沖同步：更新買閒的有效投注額
          "type": 26,
          "credit": 0,
          "canceled_id": "",
          "order_id": "9D13260123191230020000002",
          "transaction_id": "9D13260123191230020000002-2-lobby1-260123194449",
          "order_detail": {
            "play_type": 2,
            "amount": 100,
            "valid_amount": 100,
            "win": 100,
            "flag": 1,
            "round_detail": "41 56;4 18|4|6|7|0"
          }
        },
        {
          // [2] 取消買莊：退還投注額 100（flag=-8 表示已取消）
          "type": 11,
          "credit": 100,
          "canceled_id": "",
          "order_id": "9D13260123191230010000001",
          "transaction_id": "9D13260123191230010000001-1-bac.9d-260123194448",
          "order_detail": {
            "play_type": 1,
            "amount": 0,
            "valid_amount": 0,
            "win": 0,
            "flag": -8,
            "round_detail": "41 56;4 18|4|6|7|0"
          }
        },
        {
          // [3] 取消買閒：扣回已派彩金額 100（flag=-8 表示已取消）
          "type": 11,
          "credit": -100,
          "canceled_id": "",
          "order_id": "9D13260123191230020000002",
          "transaction_id": "9D13260123191230020000002-2-bac.9d-260123194448",
          "order_detail": {
            "play_type": 2,
            "amount": 0,
            "valid_amount": 0,
            "win": 0,
            "flag": -8,
            "round_detail": "41 56;4 18|4|6|7|0"
          }
        }
      ]
    }
    ```
  </Accordion>
</AccordionGroup>

***

### 案例二：正常流程 → 取消注單

**涵蓋類型：8 派彩、13 取消注單**

**開牌設定：**

* 開牌（莊贏）：閒 \[♠ 4, ♥2] = 6 點，莊 \[♦7, ♣K] = 7 點 → **莊贏**

| 步驟 | 動作        | Type | credit | 說明                                |
| -- | --------- | ---- | ------ | --------------------------------- |
| 1  | 下注 100 買莊 | 7    | -100   | 扣款                                |
| 2  | 開牌莊贏，派彩   | 8    | +200   | 贏面（1:1，退還本金+獎金）                   |
| 3  | 後台取消該注單   | 13   | -100   | 扣回差額（已派彩 200 - 退還投注 100 = 扣回 100） |

**Payload：**

<AccordionGroup>
  <Accordion title="步驟 1：下注扣款 (Type 7)">
    ```json theme={null}
    {
      "account": "st",
      "platform_id": "H88",
      "info": [
        {
          "type": 7,
          "credit": -100,
          "order_id": "9D13260123195630010000004",
          "transaction_id": "9D13260123195630010000004-260123195630-0000004",
          "proposal_id": "958936",
          "order_detail": {
            "play_type": 1,
            "amount": 100,
            "gmcode": "9D13261231KO",
            "game_type": "BAC_CF",
            "table_id": "9D13",
            "room_account": "9D"
          }
        }
      ]
    }
    ```
  </Accordion>

  <Accordion title="步驟 2：派彩 (Type 8)">
    ```json theme={null}
    {
      "account": "st",
      "platform_id": "H88",
      "info": [
        {
          "type": 8,
          "credit": 200,
          "canceled_id": "",
          "order_id": "9D13260123195630010000004",
          "transaction_id": "9D13260123195630010000004-1-bac.9d-260123195703",
          "order_detail": {
            "play_type": 1,
            "amount": 100,
            "valid_amount": 100,
            "win": 100,
            "flag": 1,
            "round_detail": "36 50;7 29|3|7|6|0"
          }
        }
      ]
    }
    ```
  </Accordion>

  <Accordion title="步驟 3：取消注單 (Type 13)">
    ```json theme={null}
    {
      "account": "st",
      "platform_id": "H88",
      "info": [
        {
          // 取消注單：扣回差額 100（已派彩 200，取消後需扣回 100）
          "type": 13,
          "credit": -100,
          "canceled_id": "",
          "order_id": "9D13260123195630010000004",
          "transaction_id": "9D13260123195630010000004-1-bac.9d-260123195728",
          "order_detail": {
            "play_type": 1,
            "amount": 0,
            "valid_amount": 0,
            "win": 0,
            "flag": -8,
            "round_detail": "36 50;7 29|3|7|6|0"
          }
        }
      ]
    }
    ```
  </Accordion>
</AccordionGroup>

***

### 案例三：下注 → 下注失敗還款

**涵蓋類型：23 下注失敗還款**

| 步驟 | 動作              | Type | credit | canceled\_id |
| -- | --------------- | ---- | ------ | ------------ |
| 1  | 下注 100（模擬未收到回覆） | 7    | -100   | -            |
| 2  | 觸發下注失敗還款        | 23   | +100   | 步驟1的交易ID     |

**Payload：**

<AccordionGroup>
  <Accordion title="步驟 1：下注扣款 (Type 7)">
    ```json theme={null}
    {
      "account": "st",
      "platform_id": "H88",
      "info": [
        {
          "type": 7,
          "credit": -100,
          "order_id": "9D13260123200228010000005",
          "transaction_id": "9D13260123200228010000005-260123200228-0000005",
          "proposal_id": "958936",
          "order_detail": {
            "play_type": 1,
            "amount": 100,
            "gmcode": "9D13261231KQ",
            "game_type": "BAC_CF",
            "table_id": "9D13",
            "room_account": "9D"
          }
        }
      ]
    }
    ```
  </Accordion>

  <Accordion title="步驟 2：下注失敗還款 (Type 23)">
    ```json theme={null}
    {
      "account": "st",
      "platform_id": "H88",
      "info": [
        {
          // 下注失敗還款：canceled_id 為原下注的 transaction_id
          "type": 23,
          "credit": 100,
          "canceled_id": "9D13260123200228010000005-260123200228-0000005",
          "order_id": "9D13260123200228010000005",
          "transaction_id": "9D13260123200228010000005-260123200228-0000005RB",
          "order_detail": {
            "play_type": 1,
            "amount": 0,
            "valid_amount": 0,
            "win": 0,
            "flag": 0,
            "round_detail": ""
          }
        }
      ]
    }
    ```
  </Accordion>
</AccordionGroup>

***

## 測試總結

| 案例  | 涵蓋類型          |
| --- | ------------- |
| 案例一 | 8, 10, 11, 26 |
| 案例二 | 8, 13         |
| 案例三 | 23            |
