@@ -47,6 +47,8 @@ void UTrivialKartGameInstance::Init()
4747 FOnReadUserFileCompleteDelegate::CreateUObject (this , &UTrivialKartGameInstance::OnCloudWriteComplete));
4848 }
4949 InitiateAutoLogin ();
50+ // Check unacknowledged purchases on start or foreground
51+ FCoreDelegates::ApplicationHasEnteredForegroundDelegate.AddUObject (this , &UTrivialKartGameInstance::CheckPendingPurchases);
5052}
5153
5254void UTrivialKartGameInstance::Shutdown ()
@@ -60,6 +62,8 @@ void UTrivialKartGameInstance::Shutdown()
6062 CloudInterface->ClearOnReadUserFileCompleteDelegate_Handle (ReadSaveHandle);
6163 CloudInterface->ClearOnWriteUserFileCompleteDelegate_Handle (WriteSaveHandle);
6264 }
65+ FCoreDelegates::ApplicationHasEnteredForegroundDelegate.RemoveAll (this );
66+ Super::Shutdown ();
6367}
6468
6569void UTrivialKartGameInstance::InitiateAutoLogin ()
@@ -161,6 +165,45 @@ void UTrivialKartGameInstance::StartPurchasing(const FUniqueOfferId& OfferID, co
161165 }
162166}
163167
168+ void UTrivialKartGameInstance::CheckPendingPurchases ()
169+ {
170+ if (const IOnlineSubsystem* Subsystem = Online::GetSubsystem (GetWorld ()))
171+ {
172+ if (const IOnlineIdentityPtr IdentityInterface = Online::GetIdentityInterface (GetWorld ()))
173+ {
174+ if (const TSharedPtr<const FUniqueNetId> UserId = IdentityInterface->GetUniquePlayerId (0 ); UserId.IsValid ())
175+ {
176+ if (const IOnlinePurchasePtr PurchaseInterface = Online::GetPurchaseInterface (GetWorld ()); PurchaseInterface.IsValid ())
177+ {
178+ PurchaseInterface->QueryReceipts (*UserId, true ,
179+ FOnQueryReceiptsComplete::CreateWeakLambda (this ,
180+ [this , PurchaseInterface, UserId](const FOnlineError& OnlineError)
181+ {
182+ if (!OnlineError.WasSuccessful ())
183+ return ;
184+
185+ TArray<FPurchaseReceipt> Receipts;
186+ PurchaseInterface->GetReceipts (*UserId, Receipts);
187+
188+ for (const FPurchaseReceipt& Receipt : Receipts)
189+ {
190+ for (const FPurchaseReceipt::FReceiptOfferEntry& Offer : Receipt.ReceiptOffers )
191+ {
192+ if (OnPurchaseReceived.IsBound ())
193+ {
194+ OnPurchaseReceived.Broadcast (Offer.OfferId , 1 );
195+ FPlatformMisc::LowLevelOutputDebugStringf (TEXT (" Pending purchase restored - Offer ID:: %s" ), *Offer.OfferId );
196+ }
197+ // The Purchase Token is passed as the ReceiptId to tell the platform which purchase to finalize (consume/acknowledge).
198+ PurchaseInterface->FinalizePurchase (*UserId, Receipt.TransactionId );
199+ }
200+ }
201+ }));
202+ }
203+ }
204+ }
205+ }
206+ }
164207UTrivialKartSaveGame* UTrivialKartGameInstance::LoadGame ()
165208{
166209#if WITH_EDITOR
@@ -230,6 +273,8 @@ void UTrivialKartGameInstance::OnLoginCompleted(int32 LocalUserNum, bool bWasSuc
230273 CloudInterface->ReadUserFile (*IdentityInterface->GetUniquePlayerId (0 ), " TrivialKartCloudSave" );
231274 }
232275 }
276+ // Check unacknowledged purchases on start or foreground
277+ CheckPendingPurchases ();
233278 }
234279}
235280
0 commit comments