-
Notifications
You must be signed in to change notification settings - Fork 316
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Created StoreKit2TransactionFetcher
#2539
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
Sources/Purchasing/StoreKit2/StoreKit2TransactionFetcher.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// | ||
// Copyright RevenueCat Inc. All Rights Reserved. | ||
// | ||
// Licensed under the MIT License (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://opensource.org/licenses/MIT | ||
// | ||
// StoreKit2TransactionFetcher.swift | ||
// | ||
// Created by Nacho Soto on 5/24/23. | ||
|
||
import Foundation | ||
import StoreKit | ||
|
||
protocol StoreKit2TransactionFetcherType: Sendable { | ||
|
||
@available(iOS 15.0, tvOS 15.0, macOS 12.0, watchOS 8.0, *) | ||
var unfinishedVerifiedTransactions: [StoreTransaction] { get async } | ||
|
||
@available(iOS 15.0, tvOS 15.0, macOS 12.0, watchOS 8.0, *) | ||
var hasPendingConsumablePurchase: Bool { get async } | ||
|
||
} | ||
|
||
@available(iOS 15.0, tvOS 15.0, macOS 12.0, watchOS 8.0, *) | ||
final class StoreKit2TransactionFetcher: StoreKit2TransactionFetcherType { | ||
|
||
var unfinishedVerifiedTransactions: [StoreTransaction] { | ||
get async { | ||
return await StoreKit.Transaction | ||
.unfinished | ||
.compactMap { $0.verifiedTransaction } | ||
.map { StoreTransaction(sk2Transaction: $0) } | ||
.extractValues() | ||
} | ||
} | ||
|
||
var hasPendingConsumablePurchase: Bool { | ||
get async { | ||
return await StoreKit.Transaction | ||
.unfinished | ||
.compactMap { $0.verifiedTransaction } | ||
.map(\.productType) | ||
.map { StoreProduct.ProductType($0) } | ||
.contains { $0.productCategory == .nonSubscription } | ||
} | ||
} | ||
|
||
} | ||
|
||
// MARK: - | ||
|
||
@available(iOS 15.0, tvOS 15.0, macOS 12.0, watchOS 8.0, *) | ||
extension StoreKit.VerificationResult where SignedType == StoreKit.Transaction { | ||
|
||
var underlyingTransaction: StoreKit.Transaction { | ||
switch self { | ||
case let .unverified(transaction, _): return transaction | ||
case let .verified(transaction): return transaction | ||
} | ||
} | ||
|
||
var verifiedTransaction: StoreKit.Transaction? { | ||
switch self { | ||
case let .verified(transaction): return transaction | ||
case .unverified: return nil | ||
} | ||
} | ||
|
||
} |
File renamed without changes.
File renamed without changes.
122 changes: 122 additions & 0 deletions
122
Tests/StoreKitUnitTests/StoreKit2/StoreKit2TransactionFetcherTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
// | ||
// Copyright RevenueCat Inc. All Rights Reserved. | ||
// | ||
// Licensed under the MIT License (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://opensource.org/licenses/MIT | ||
// | ||
// StoreKit2TransactionFetcherTests.swift | ||
// | ||
// Created by Nacho Soto on 5/24/23. | ||
|
||
import Nimble | ||
@testable import RevenueCat | ||
import StoreKitTest | ||
import XCTest | ||
|
||
@available(iOS 15.0, tvOS 15.0, watchOS 8.0, macOS 12.0, *) | ||
@MainActor | ||
class StoreKit2TransactionFetcherTests: StoreKitConfigTestCase { | ||
|
||
private var fetcher: StoreKit2TransactionFetcher! | ||
|
||
@MainActor | ||
override func setUp() async throws { | ||
try await super.setUp() | ||
try AvailabilityChecks.iOS15APIAvailableOrSkipTest() | ||
|
||
self.fetcher = .init() | ||
} | ||
|
||
// MARK: - unfinishedVerifiedTransactions | ||
|
||
func testNoUnfinishedVerifiedTransactions() async { | ||
let transactions = await self.fetcher.unfinishedVerifiedTransactions | ||
expect(transactions).to(beEmpty()) | ||
} | ||
|
||
func testOneUnfinishedVerifiedTransaction() async throws { | ||
let transaction = try await self.createTransaction(finished: false) | ||
let result = await self.fetcher.unfinishedVerifiedTransactions | ||
|
||
expect(result) == [transaction] | ||
} | ||
|
||
func testOneUnfinishedConsumablePurchase() async throws { | ||
let transaction = try await self.createTransactionForConsumableProduct(finished: false) | ||
let result = await self.fetcher.unfinishedVerifiedTransactions | ||
|
||
expect(result) == [transaction] | ||
} | ||
|
||
func testMultipleUnfinishedVerifiedTransaction() async throws { | ||
let transaction1 = try await self.createTransaction(productID: Self.product1, finished: false) | ||
let transaction2 = try await self.createTransaction(productID: Self.product2, finished: false) | ||
|
||
let result = await self.fetcher.unfinishedVerifiedTransactions | ||
expect(result).to(haveCount(2)) | ||
expect(result).to(contain([transaction1, transaction2])) | ||
} | ||
|
||
func testFiltersOutFinishedTransaction() async throws { | ||
_ = try await self.createTransaction(productID: Self.product1, finished: true) | ||
let transaction = try await self.createTransaction(productID: Self.product2, finished: false) | ||
|
||
let result = await self.fetcher.unfinishedVerifiedTransactions | ||
expect(result) == [transaction] | ||
} | ||
|
||
// MARK: - hasPendingConsumablePurchase | ||
|
||
func testHasNoPendingConsumablePurchase() async throws { | ||
let result = await self.fetcher.hasPendingConsumablePurchase | ||
expect(result) == false | ||
} | ||
|
||
func testHasNoPendingConsumablePurchaseWithNormalProduct() async throws { | ||
_ = try await self.createTransaction(finished: false) | ||
|
||
let result = await self.fetcher.hasPendingConsumablePurchase | ||
expect(result) == false | ||
} | ||
|
||
func testHasNoPendingConsumablePurchaseWithFinishedConsumable() async throws { | ||
_ = try await self.createTransactionForConsumableProduct(finished: true) | ||
|
||
let result = await self.fetcher.hasPendingConsumablePurchase | ||
expect(result) == false | ||
} | ||
|
||
func testHasPendingConsumablePurchase() async throws { | ||
_ = try await self.createTransactionForConsumableProduct(finished: false) | ||
|
||
let result = await self.fetcher.hasPendingConsumablePurchase | ||
expect(result) == true | ||
} | ||
|
||
} | ||
|
||
@available(iOS 15.0, tvOS 15.0, watchOS 8.0, macOS 12.0, *) | ||
private extension StoreKit2TransactionFetcherTests { | ||
|
||
func createTransaction( | ||
productID: String? = nil, | ||
finished: Bool | ||
) async throws -> StoreTransaction { | ||
return StoreTransaction( | ||
sk2Transaction: try await self.simulateAnyPurchase(productID: productID, | ||
finishTransaction: finished) | ||
) | ||
} | ||
|
||
func createTransactionForConsumableProduct(finished: Bool) async throws -> StoreTransaction { | ||
return try await self.createTransaction(productID: Self.consumable, finished: finished) | ||
} | ||
|
||
static let product1 = "com.revenuecat.monthly_4.99.1_week_intro" | ||
static let product2 = "com.revenuecat.annual_39.99_no_trial" | ||
static let consumable = "com.revenuecat.consumable" | ||
|
||
} |
File renamed without changes.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see we are not using
verifiedTransaction
here. Should we filter to only consider verified transactions here as well? Not sure when a purchase will be unverified in SK2 though, so this might be ok.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm yeah I think might as well, since we won't try to post unverified ones anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.