# Unity IAP Server Side Verification
[TOC]
## 流程圖

### APP Side 步驟說明
#### 1. APP 向 Unity IAP 發出購買請求
#### 2. Unity IAP 向 App Store 發出購買請求
#### 3. 商店通知 Unity IAP 購買成功
#### 4. Unity IAP 通知 APP 返回 receipt 於購買事件
#### 5. 根據 IAP Button 設定的 consumePurchase 將返回結果暫時 Pending
#### 6. 此時將購買成功的 receipt 向自家 Server 發出 API 請求驗證購買憑據
#### 7. Server 向官方 API 驗證購買憑據後,返回結果告知 APP
#### 8. 此時如果返回結果同意,就 Confirm pending 的交易狀態
#### 9. Unity IAP 通知商店完成此次交易
### Server Side 步驟說明
#### 1. 接收 APP 發送的 receipt JSON string
> 依據,官方文檔說明 Apple 與 Google 在 App 端收到的 receipt 中的欄位是不一樣的
> https://docs.unity3d.com/cn/2020.1/Manual/UnityIAPPurchaseReceipts.html
>
> Apple receipt JSON 所有欄位,請參考:
> https://developer.apple.com/library/archive/releasenotes/General/ValidateAppStoreReceipt/Chapters/ReceiptFields.html#/apple_ref/doc/uid/TP40010573-CH106-SW1
>
> Google 請參考以下連結,關鍵字搜尋 Table 7 或 INAPP_PURCHASE_DATA
> https://developer.android.com/google/play/billing/billing_reference.html
#### 2. 由 Server 將 JSON string 依據以下 API 驗證需求,傳遞 receipt 中的資料給商店
> Server 端 POST 驗證API
>
> APPLE Server side verify API 請參考:
> https://developer.apple.com/documentation/appstorereceipts/verifyreceipt
>
> ※ 實現方式請參考:
> https://cg2010studio.com/2018/05/24/ios-iap-%E9%A9%97%E8%AD%89%E7%99%BC%E7%A5%A8-iap-verify-receipt/
>
> Google Server side verify API 請參考
> https://developers.google.com/android-publisher/api-ref/rest/v3/purchases.subscriptions/get
>
> ※ 實現方式請參考:
> https://stackoverflow.com/a/34005001
#### 3. 驗證無誤後回傳結果給 APP,並由 APP 解除 Panding 狀態,繼續執行購買後流程