Skip to content

Commit

Permalink
Enable the new IAP flow
Browse files Browse the repository at this point in the history
Summary: This diff enables the new IAP flow guarded by a GK, which supports autologging events for both store kit 1 and store kit 2.

Reviewed By: xta0

Differential Revision: D63791563

fbshipit-source-id: 3557b690be357fa65c022eae1dc055d0c76c107c
  • Loading branch information
ryantobinmeta authored and facebook-github-bot committed Oct 11, 2024
1 parent 892ecbf commit 54e7b92
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 15 deletions.
9 changes: 8 additions & 1 deletion FBSDKCoreKit/FBSDKCoreKit/AppEvents/FBSDKAppEvents.m
Original file line number Diff line number Diff line change
Expand Up @@ -954,9 +954,16 @@ - (void)fetchServerConfiguration:(FBSDKCodeBlock)callback
self.serverConfiguration = serverConfiguration;

if ([self.settings isAutoLogAppEventsEnabled] && self.serverConfiguration.implicitPurchaseLoggingEnabled) {
[self.paymentObserver startObservingTransactions];
[self.featureChecker checkFeature:FBSDKFeatureIAPLoggingSK2 completionBlock:^(BOOL enabled) {
if (enabled) {
[self.transactionObserver startObserving];
} else {
[self.paymentObserver startObservingTransactions];
}
}];
} else {
[self.paymentObserver stopObservingTransactions];
[self.transactionObserver stopObserving];
}
[self.featureChecker checkFeature:FBSDKFeatureRestrictiveDataFiltering completionBlock:^(BOOL enabled) {
if (enabled) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ final class IAPEventResolver: NSObject {
iapSKProductRequestFactory: IAPSKProductsRequestFactory()
)

weak var delegate: IAPEventResolverDelegate?
var request: IAPSKProductsRequesting?
var delegate: IAPEventResolverDelegate?

private var isSubscriptionsEnabled: Bool {
guard let dependencies = try? Self.getDependencies() else {
Expand Down Expand Up @@ -232,12 +233,12 @@ extension IAPEventResolver {
return
}
let productID = transaction.payment.productIdentifier
let request = dependencies.iapSKProductRequestFactory.createRequestWith(
request = dependencies.iapSKProductRequestFactory.createRequestWith(
productIdentifier: productID,
transaction: transaction
)
request.delegate = self
request.start()
request?.delegate = self
request?.start()
}
}

Expand All @@ -249,6 +250,7 @@ extension IAPEventResolver: SKProductsRequestDelegate {
let transaction = iapRequest.transaction else {
return
}
self.request = nil
let product = response.products.first
resolveEventFor(transaction: transaction, product: product)
}
Expand All @@ -258,8 +260,13 @@ extension IAPEventResolver: SKProductsRequestDelegate {
let transaction = iapRequest.transaction else {
return
}
self.request = nil
resolveEventFor(transaction: transaction, product: nil)
}

func requestDidFinish(_ request: SKRequest) {
self.request = nil
}
}

// MARK: - SKProduct
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ final class IAPTransactionLogger: NSObject, IAPTransactionLogging {
static var defaultDependencies: TypeDependencies? = .init(
eventLogger: AppEvents.shared
)
var resolver: IAPEventResolver?
let dateFormatter = DateFormatter()
let maxParameterValueLength = 100

Expand Down Expand Up @@ -170,25 +171,29 @@ extension IAPTransactionLogger {

extension IAPTransactionLogger: IAPEventResolverDelegate {
func logTransaction(_ transaction: SKPaymentTransaction) {
let resolver = IAPEventResolver()
resolver.delegate = self
resolver.resolveEventFor(transaction: transaction)
resolver = IAPEventResolver()
resolver?.delegate = self
resolver?.resolveEventFor(transaction: transaction)
}

func didResolveNew(event: IAPEvent) {
logNewEvent(event)
resolver = nil
}

func didResolveRestored(event: IAPEvent) {
logRestoredEvent(event)
resolver = nil
}

func didResolveFailed(event: IAPEvent) {
logInitiatedCheckoutOrFailedEvent(event)
resolver = nil
}

func didResolveInitiatedCheckout(event: IAPEvent) {
logInitiatedCheckoutOrFailedEvent(event)
resolver = nil
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ final class AppEventsPublishATETests: XCTestCase {
blocklistEventsManager: TestBlocklistEventsManager(),
redactedEventsManager: TestRedactedEventsManager(),
sensitiveParamsManager: TestSensitiveParamsManager(),
transactionObserver: IAPTransactionObserver.shared
transactionObserver: TestTransactionObserver()
)

appEvents.publishATE()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ final class AppEventsTests: XCTestCase {
var settings: TestSettings!
var onDeviceMLModelManager: TestOnDeviceMLModelManager!
var paymentObserver: TestPaymentObserver!
var transactionObserver: TestTransactionObserver!
var appEventsStateStore: TestAppEventsStateStore!
var metadataIndexer: TestMetadataIndexer!
var appEventsConfigurationProvider: TestAppEventsConfigurationProvider!
Expand Down Expand Up @@ -69,6 +70,7 @@ final class AppEventsTests: XCTestCase {
onDeviceMLModelManager = TestOnDeviceMLModelManager()
onDeviceMLModelManager.integrityParametersProcessor = integrityParametersProcessor
paymentObserver = TestPaymentObserver()
transactionObserver = TestTransactionObserver()
metadataIndexer = TestMetadataIndexer()

graphRequestFactory = TestGraphRequestFactory()
Expand Down Expand Up @@ -138,6 +140,7 @@ final class AppEventsTests: XCTestCase {
appEventsUtility = nil
internalUtility = nil
capiReporter = nil
transactionObserver = nil

resetTestHelpers()

Expand Down Expand Up @@ -178,7 +181,7 @@ final class AppEventsTests: XCTestCase {
blocklistEventsManager: blocklistEventsManager,
redactedEventsManager: redactedEventsManager,
sensitiveParamsManager: sensitiveParamsManager,
transactionObserver: IAPTransactionObserver.shared
transactionObserver: transactionObserver
)

appEvents.configureNonTVComponents(
Expand Down Expand Up @@ -1650,17 +1653,53 @@ final class AppEventsTests: XCTestCase {
appEvents.fetchServerConfiguration(nil)
appEventsConfigurationProvider.firstCapturedBlock?()
serverConfigurationProvider.capturedCompletionBlock?(serverConfiguration, nil)
featureManager.completeCheck(forFeature: .iapLoggingSK2, with: false)
XCTAssertTrue(
paymentObserver.didStartObservingTransactions,
"fetchConfiguration should start payment observing if the configuration allows it"
"fetchConfiguration should start payment observing if the configuration allows it and SK2 is disabled"
)
XCTAssertFalse(
paymentObserver.didStopObservingTransactions,
"fetchConfiguration shouldn't stop payment observing if the configuration allows it"
)
XCTAssertFalse(
transactionObserver.didStartObserving,
"fetchConfiguration should not start transaction observing if the configuration allows it and SK2 is disabled"
)
XCTAssertFalse(
transactionObserver.didStopObserving,
"fetchConfiguration should not stop transaction observing if the configuration allows it"
)
}

func testFetchingConfigurationStartsTransactionObservingIfConfigurationAllowed() {
settings.isAutoLogAppEventsEnabled = true
let serverConfiguration = ServerConfigurationFixtures.configuration(
withDictionary: ["implicitPurchaseLoggingEnabled": true]
)
appEvents.fetchServerConfiguration(nil)
appEventsConfigurationProvider.firstCapturedBlock?()
serverConfigurationProvider.capturedCompletionBlock?(serverConfiguration, nil)
featureManager.completeCheck(forFeature: .iapLoggingSK2, with: true)
XCTAssertTrue(
transactionObserver.didStartObserving,
"fetchConfiguration should start transaction observing if the configuration allows it and SK2 is enabled"
)
XCTAssertFalse(
transactionObserver.didStopObserving,
"fetchConfiguration should not stop transaction observing if the configuration allows it"
)
XCTAssertFalse(
paymentObserver.didStartObservingTransactions,
"fetchConfiguration should not start payment observing if the configuration allows it and SK2 is enabled"
)
XCTAssertFalse(
paymentObserver.didStopObservingTransactions,
"fetchConfiguration shouldn't stop payment observing if the configuration allows it"
)
}

func testFetchingConfigurationStopsPaymentObservingIfConfigurationDisallowed() {
func testFetchingConfigurationStopsPaymentAndTransactionObservingIfConfigurationDisallowed() {
settings.isAutoLogAppEventsEnabled = true
let serverConfiguration = ServerConfigurationFixtures.configuration(
withDictionary: ["implicitPurchaseLoggingEnabled": 0]
Expand All @@ -1670,11 +1709,19 @@ final class AppEventsTests: XCTestCase {
serverConfigurationProvider.capturedCompletionBlock?(serverConfiguration, nil)
XCTAssertFalse(
paymentObserver.didStartObservingTransactions,
"Fetching a configuration shouldn't start payment observing if the configuration disallows it"
"Fetching a configuration shouldn't start payment observing if auto log app events is disabled"
)
XCTAssertTrue(
paymentObserver.didStopObservingTransactions,
"Fetching a configuration should stop payment observing if the configuration disallows it"
"Fetching a configuration should stop payment observing if auto log app events is disabled"
)
XCTAssertFalse(
transactionObserver.didStartObserving,
"Fetching a configuration shouldn't start transaction observing if auto log app events is disabled"
)
XCTAssertTrue(
transactionObserver.didStopObserving,
"Fetching a configuration should stop transaction observing if auto log app events is disabled"
)
}

Expand All @@ -1694,6 +1741,14 @@ final class AppEventsTests: XCTestCase {
paymentObserver.didStopObservingTransactions,
"Fetching a configuration should stop payment observing if auto log app events is disabled"
)
XCTAssertFalse(
transactionObserver.didStartObserving,
"Fetching a configuration shouldn't start transaction observing if auto log app events is disabled"
)
XCTAssertTrue(
transactionObserver.didStopObserving,
"Fetching a configuration should stop transaction observing if auto log app events is disabled"
)
}

func testFetchingConfigurationIncludingSKAdNetworkIfSKAdNetworkReportEnabled() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ enum TestCoreKitComponents {
blocklistEventsManager: _EventsProcessing = TestBlocklistEventsManager(),
redactedEventsManager: _EventsProcessing = TestRedactedEventsManager(),
sensitiveParamsManager: _AppEventsParameterProcessing = TestSensitiveParamsManager(),
transactionObserver: _TransactionObserving = IAPTransactionObserver.shared
transactionObserver: _TransactionObserving = TestTransactionObserver()
) -> CoreKitComponents {
CoreKitComponents(
accessTokenExpirer: accessTokenExpirer,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
*/

import Foundation

@objcMembers
final class TestTransactionObserver: NSObject, _TransactionObserving {
var didStartObserving = false
var didStopObserving = false

func startObserving() {
didStartObserving = true
}

func stopObserving() {
didStopObserving = true
}
}

0 comments on commit 54e7b92

Please sign in to comment.