Skip to content

Commit

Permalink
PurchasesOrchestrator: update CustomerInfoManager cache after pro…
Browse files Browse the repository at this point in the history
…cessing transactions

This is the last piece needed after #2612. Transactions handled from `StoreKit.Transaction.updates` were ignoring the result `CustomerInfo`.
On example where this was noticed is when redeeming promo codes: we posted the transaction, but `PurchasesDelegate` was not notified of the change.
  • Loading branch information
NachoSoto committed Jun 20, 2023
1 parent c9b36e5 commit 835db1a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
17 changes: 14 additions & 3 deletions Sources/Purchasing/Purchases/PurchasesOrchestrator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -829,24 +829,35 @@ extension PurchasesOrchestrator: StoreKit2TransactionListenerDelegate {
updatedTransaction transaction: StoreTransactionType
) async throws {
let storefront = await self.storefront(from: transaction)
let subscriberAttributes = self.unsyncedAttributes
let adServicesToken = self.attribution.unsyncedAdServicesToken

_ = try await Async.call { completed in
let result: Result<CustomerInfo, BackendError> = await Async.call { completed in
self.transactionPoster.handlePurchasedTransaction(
StoreTransaction.from(transaction: transaction),
data: .init(
appUserID: self.appUserID,
presentedOfferingID: nil,
unsyncedAttributes: self.unsyncedAttributes,
unsyncedAttributes: subscriberAttributes,
aadAttributionToken: adServicesToken,
storefront: storefront,
source: .init(
isRestore: self.allowSharingAppStoreAccount,
initiationSource: .queue
)
)
) { result in
completed(result.mapError(\.asPurchasesError))
completed(result)
}
}

self.handlePostReceiptResult(result,
subscriberAttributes: subscriberAttributes,
adServicesToken: adServicesToken)

if let error = result.error {
throw error
}
}

private func storefront(from transaction: StoreTransactionType) async -> StorefrontType? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,22 @@ class StoreKit1ObserverModeIntegrationTests: BaseStoreKitObserverModeIntegration
try await self.verifyEntitlementWentThrough(info)
}

func testPurchaseOutsideTheAppUpdatesCustomerInfoDelegate() async throws {
try self.testSession.buyProduct(productIdentifier: Self.monthlyNoIntroProductID)

try await asyncWait(
until: {
await self.purchasesDelegate.customerInfo?.entitlements.active.isEmpty == false
},
timeout: .seconds(4),
pollInterval: .milliseconds(100),
description: "Delegate should be notified"
)

let customerInfo = try XCTUnwrap(self.purchasesDelegate.customerInfo)
try await self.verifyEntitlementWentThrough(customerInfo)
}

@available(iOS 15.0, tvOS 15.0, watchOS 8.0, macOS 12.0, *)
func testSK2RenewalsPostReceiptOnlyOnceWhenSK1IsEnabled() async throws {
try XCTSkipIf(Self.storeKit2Setting.isEnabledAndAvailable, "Test only for SK1")
Expand Down
5 changes: 4 additions & 1 deletion Tests/StoreKitUnitTests/PurchasesOrchestratorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,9 @@ class PurchasesOrchestratorTests: StoreKitConfigTestCase {
expect(self.backend.invokedPostReceiptData) == true
expect(self.backend.invokedPostReceiptDataParameters?.transactionData.source.isRestore) == false
expect(self.backend.invokedPostReceiptDataParameters?.transactionData.source.initiationSource) == .queue
expect(self.customerInfoManager.invokedCacheCustomerInfo) == true
expect(self.customerInfoManager.invokedCacheCustomerInfoParameters?.appUserID) == Self.mockUserID
expect(self.customerInfoManager.invokedCacheCustomerInfoParameters?.info) === self.mockCustomerInfo
}

@available(iOS 15.0, tvOS 15.0, watchOS 8.0, macOS 12.0, *)
Expand All @@ -858,7 +861,7 @@ class PurchasesOrchestratorTests: StoreKitConfigTestCase {
)
fail("Expected error")
} catch {
expect(error).to(matchError(expectedError.asPurchasesError))
expect(error).to(matchError(expectedError))
}

expect(transaction.finishInvoked) == false
Expand Down

0 comments on commit 835db1a

Please sign in to comment.