1. 程式人生 > >Unity IAP iOS 內購以及獲取交易收據進行驗證

Unity IAP iOS 內購以及獲取交易收據進行驗證

1. 準備工作,先到iTunesConnect 上進行新增內購專案和沙盒測試賬號

建立APP 內購專案

在這裡插入圖片描述

選擇其中一種型別,進行建立

在這裡插入圖片描述

填寫內購專案的內容(其中的產品 ID 記住,在接下來有很多地方需要用到)

在這裡插入圖片描述

螢幕快照一定要選到,不然會顯示該內購專案元資料丟失

在這裡插入圖片描述

狀態顯示為黃色:準備提交才是正確的

在這裡插入圖片描述

2. 建立沙盒賬號

在這裡插入圖片描述
在這裡插入圖片描述
在建立測試員的時候,記住那個郵箱不能填你已經有的 AppleID 的郵箱,填好後會有一篇驗證資訊發到你剛剛填寫的郵箱,驗證後就可以用了

3. unity 裡面的操作

將 IAP 相關的檔案匯入 unity 中

在這裡插入圖片描述

點選 in-App-Purchasing進去

在這裡插入圖片描述
保證 Analytics 和in-App-Purchasing是開啟狀態就可以了

新增 IAPButton

在這裡插入圖片描述

填寫 IAPButton 裡面的資訊

在這裡插入圖片描述
在這裡插入圖片描述

  • 新建一個指令碼


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Purchasing;
using UnityEngine.UI;
using System.Text;
using System;
using MiniJSON;
using UnityEngine.Purchasing.Security;


public class PurchaseManager : MonoBehaviour
, IStoreListener { private IStoreController controller; ConfigurationBuilder builder; private IStoreController m_Controller; private IAppleExtensions m_AppleExtensions; void Start() { var module = StandardPurchasingModule.Instance(); builder = ConfigurationBuilder.Instance(module); builder.
AddProduct("產品 ID", ProductType.Subscription); UnityPurchasing.Initialize(this, builder); } /// <summary> /// Called when Unity IAP is ready to make purchases. /// </summary> public void OnInitialized(IStoreController controller, IExtensionProvider extensions) { this.controller = controller; m_Controller = controller; m_AppleExtensions = extensions.GetExtension<IAppleExtensions> (); m_AppleExtensions.RegisterPurchaseDeferredListener (OnDeferred); } /// <summary> /// Called when Unity IAP encounters an unrecoverable initialization error. /// /// Note that this will not be called if Internet is unavailable; Unity IAP /// will attempt initialization until it becomes available. /// </summary> public void OnInitializeFailed(InitializationFailureReason error) { } /// <summary> /// Called when a purchase completes. /// /// May be called at any time after OnInitialized(). /// </summary> //這個函式就是購買成功後自動呼叫的函式 public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs e) { Debug.Log ("IAP成功"); if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.tvOS) { string transactionReceipt = m_AppleExtensions.GetTransactionReceiptForProduct (e.purchasedProduct); Debug.Log (transactionReceipt); //這裡的transactionReceipt是 Base64加密後的交易收據,將這個收據傳送給遠端伺服器就會得到訂單資訊的 Json 資料 } return PurchaseProcessingResult.Complete; } /// <summary> /// Called when a purchase fails. /// </summary> public void OnPurchaseFailed(Product item, PurchaseFailureReason r) { Debug.Log ("IAP失敗"); Debug.Log ("失敗原因:"+r.ToString()); } //將這個函式掛載到需要進行內購的 UI(Button)上 public void OnPurchaseClicked(string productId) { controller.InitiatePurchase(productId); } private void OnDeferred (Product item) { Debug.Log ("Purchase deferred: " + item.definition.id); } }

在要進行內購的 Button 上進行掛載方法

在這裡插入圖片描述

4. 打包到 Xcode 裡面進行沙盒測試

在執行之前記得把 IAP 開啟,還有將真機的 AppleID登出再進行測試

在這裡插入圖片描述

5. 驗證訂單資訊

在購買成功後自動呼叫的函式裡面

public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs e)
	{
		Debug.Log ("IAP成功");
		if (Application.platform == RuntimePlatform.IPhonePlayer ||
			Application.platform == RuntimePlatform.tvOS) {
			string transactionReceipt = m_AppleExtensions.GetTransactionReceiptForProduct (e.purchasedProduct);
			Debug.Log (transactionReceipt);
			//這裡的transactionReceipt是 Base64加密後的交易收據,將這個收據傳送給遠端伺服器就會得到訂單資訊的 Json 資料

		}    
		return PurchaseProcessingResult.Complete;
	}

在正式伺服器中(已上線Appstore),傳送transactionReceipt到蘋果的正式伺服器( https://buy.itunes.apple.com/verifyReceipt )驗證
驗證後返回的Json 資料如下:

{
    "receipt": {
        "original_purchase_date_pst": "2017-11-15 15:25:20 America/Los_Angeles",
        "purchase_date_ms": "1510788320209",
        "unique_identifier": "0ea7808637555b2c633eb07aa1cb0894c821a6f9",
        "original_transaction_id": "1000000352597239",
        "bvrs": "0",
        "transaction_id": "1000000352597239",
        "quantity": "1",
        "unique_vendor_identifier": "01B57C2E-9E91-42FF-9B0D-4983175D6694",
        "item_id": "1141751870",
        "original_purchase_date": "2017-11-15 23:25:20 Etc/GMT",
        "product_id": "100.gold.coins",
        "purchase_date": "2017-11-15 23:25:20 Etc/GMT",
        "is_trial_period": "false",
        "purchase_date_pst": "2017-11-15 15:25:20 America/Los_Angeles",
        "bid": "com.unity3d.unityiap.demo",
        "original_purchase_date_ms": "1510788320209"
    },
    "status": 0
}