Unity 2019.4.28.f1
In App Purchasing Version 3.1.0
UnityでIn App Purchasing を使用し、アプリ内課金を実装していますが、決済を完了させる為のデータを複数保持できなくて困っています。
アプリ内課金成功の通知 ProcessPurchase()では Pendingを返却し、サーバー等での処理が正常に完了した後、IStoreController.ConfirmPendingPurchase() を使用して決済を完了させています。
IStoreController.ConfirmPendingPurchase() で決済を完了させる為には、ProcessPurchase() のパラメーターである PurchaseEventArgs.purchasedProduct が必要な為、PurchaseEventArgs.purchasedProduct.transactionID をキーとして Dictionary<string, Product> に保持しています。
ところが、PurchaseEventArgs.purchasedProduct が参照型のデータの為か、ProcessPurchase() が呼ばれる度に、Dictionaryの内容が書き換えられてしまいます。
例)3回 ProcessPurchase()が呼ばれた際の Dictionary の内容
トランザクションID-A ⇒ トランザクションID-CのProductデータ
トランザクションID-B ⇒ トランザクションID-CのProductデータ
トランザクションID-C ⇒ トランザクションID-CのProductデータ
iOSで自動更新サブスクリプションが更新された際や、復元を行った際に、ProcessPurchase()が非同期に複数回呼ばれる為、PurchaseEventArgs.purchasedProduct を複数保持しておく必要があると考えています。
単体で保持すると、ProcessPurchase()が非同期に呼ばれた場合、一連のトランザクション処理中に、PurchaseEventArgs.purchasedProduct の内容が書き換わる可能性があると思われる為です。
PurchaseEventArgs.purchasedProduct を複数保持したいのですが、可能なのでしょうか?
以下、参考コードです。
html
1public class Purchaser : SingletonMonoBehaviour<Purchaser>, IStoreListener 2{ 3 private Dictionary<string, Product> pendingProducts; // PendingとなったProduct の保存用 4 5 void Start() 6 { 7 pendingProducts = new Dictionary<string, Product>(); 8 } 9 10 // アプリ内課金の成功通知。 11 // UnityIAPでアプリ内課金が成功した際に呼び出される。 12 public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args) 13 { 14 Product pendingProduct = args.purchasedProduct; 15 16 pendingProducts[pendingProduct.transactionID] = pendingProduct; // purchasedProduct を Dictionary に保存 17 18 return PurchaseProcessingResult.Pending; // アプリ内課金の決済を保留にする 19 } 20 21 // アプリ内課金の決済完了。 22 // サーバー側での処理が完了したのち、コールする。 23 public bool FinishPendingPurchase(string transactionId) 24 { 25 Product pendingProduct = pendingProducts[transactionId]; // purchasedProduct を Dictionary から取得 26 27 _StoreController.ConfirmPendingPurchase(pendingProduct); // アプリ内課金の決済を完了させる 28 } 29}
あなたの回答
tips
プレビュー