-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The `StoreTransaction` object serves as a wrapper for both `SKTransaction` and `StoreKit.Transaction`.
- Loading branch information
1 parent
efa0928
commit bd616d9
Showing
26 changed files
with
690 additions
and
127 deletions.
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
15 changes: 15 additions & 0 deletions
15
Sources/Flare/Classes/Helpers/AsyncSequence/AsyncSequence+Stream.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,15 @@ | ||
// | ||
// Flare | ||
// Copyright © 2023 Space Code. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
extension AsyncSequence { | ||
func toAsyncStream() -> AsyncStream<Element> { | ||
var asyncIterator = makeAsyncIterator() | ||
return AsyncStream<Element> { | ||
try? await asyncIterator.next() | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
Sources/Flare/Classes/Listeners/TransactionListener/ITransactionListener.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,14 @@ | ||
// | ||
// Flare | ||
// Copyright © 2023 Space Code. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import StoreKit | ||
|
||
protocol ITransactionListener: Sendable { | ||
func listenForTransaction() async | ||
|
||
@available(iOS 15.0, tvOS 15.0, macOS 12.0, watchOS 8.0, *) | ||
func handle(purchaseResult: StoreKit.Product.PurchaseResult) async throws -> StoreTransaction? | ||
} |
81 changes: 81 additions & 0 deletions
81
Sources/Flare/Classes/Listeners/TransactionListener/TransactionListener.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,81 @@ | ||
// | ||
// Flare | ||
// Copyright © 2023 Space Code. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import StoreKit | ||
|
||
// MARK: - TransactionListener | ||
|
||
@available(iOS 15.0, tvOS 15.0, macOS 12.0, watchOS 8.0, *) | ||
actor TransactionListener { | ||
// MARK: Types | ||
|
||
typealias TransactionResult = StoreKit.VerificationResult<StoreKit.Transaction> | ||
|
||
// MARK: Private | ||
|
||
private let updates: AsyncStream<TransactionResult> | ||
private var task: Task<Void, Never>? | ||
|
||
// MARK: Initialization | ||
|
||
init<S: AsyncSequence>(updates: S) where S.Element == TransactionResult { | ||
self.updates = updates.toAsyncStream() | ||
} | ||
|
||
// MARK: Private | ||
|
||
private func handle( | ||
transactionResult: TransactionResult, | ||
fromTransactionUpdate _: Bool | ||
) async throws -> StoreTransaction { | ||
switch transactionResult { | ||
case let .verified(transaction): | ||
return StoreTransaction( | ||
transaction: transaction, | ||
jwtRepresentation: transactionResult.jwsRepresentation | ||
) | ||
case let .unverified(transaction, verificationError): | ||
throw IAPError.verification( | ||
error: .unverified(productID: transaction.productID, error: verificationError) | ||
) | ||
} | ||
} | ||
} | ||
|
||
// MARK: ITransactionListener | ||
|
||
@available(iOS 15.0, tvOS 15.0, macOS 12.0, watchOS 8.0, *) | ||
extension TransactionListener: ITransactionListener { | ||
func listenForTransaction() async { | ||
task?.cancel() | ||
task = Task(priority: .utility) { [weak self] in | ||
guard let self = self else { return } | ||
|
||
for await update in self.updates { | ||
Task.detached { | ||
do { | ||
_ = try await self.handle(transactionResult: update, fromTransactionUpdate: true) | ||
} catch { | ||
debugPrint("[TransactionListener] Error occurred: \(error.localizedDescription)") | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
func handle(purchaseResult: Product.PurchaseResult) async throws -> StoreTransaction? { | ||
switch purchaseResult { | ||
case let .success(verificationResult): | ||
return try await handle(transactionResult: verificationResult, fromTransactionUpdate: false) | ||
case .userCancelled: | ||
throw IAPError.paymentCancelled | ||
case .pending: | ||
throw IAPError.paymentDefferred | ||
@unknown default: | ||
throw IAPError.unknown | ||
} | ||
} | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// | ||
// Flare | ||
// Copyright © 2023 Space Code. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
public enum VerificationError: Swift.Error { | ||
case unverified(productID: String, error: Error) | ||
} |
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
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
Oops, something went wrong.