Skip to content
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

Fixed crash on async SK1 cancelled purchase #1869

Merged
merged 1 commit into from
Aug 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 4 additions & 14 deletions Sources/Misc/Purchases+async.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ extension Purchases {
func purchaseAsync(product: StoreProduct) async throws -> PurchaseResultData {
return try await withCheckedThrowingContinuation { continuation in
purchase(product: product) { transaction, customerInfo, error, userCancelled in
continuation.resume(with: Result(customerInfo, error?.ignoreIfPurchaseCancelled(userCancelled))
continuation.resume(with: Result(customerInfo, error)
.map { PurchaseResultData(transaction, $0, userCancelled) })
}
}
Expand All @@ -76,7 +76,7 @@ extension Purchases {
func purchaseAsync(package: Package) async throws -> PurchaseResultData {
return try await withCheckedThrowingContinuation { continuation in
purchase(package: package) { transaction, customerInfo, error, userCancelled in
continuation.resume(with: Result(customerInfo, error?.ignoreIfPurchaseCancelled(userCancelled))
continuation.resume(with: Result(customerInfo, error)
.map { PurchaseResultData(transaction, $0, userCancelled) })
}
}
Expand All @@ -87,7 +87,7 @@ extension Purchases {
return try await withCheckedThrowingContinuation { continuation in
purchase(product: product,
promotionalOffer: promotionalOffer) { transaction, customerInfo, error, userCancelled in
continuation.resume(with: Result(customerInfo, error?.ignoreIfPurchaseCancelled(userCancelled))
continuation.resume(with: Result(customerInfo, error)
.map { PurchaseResultData(transaction, $0, userCancelled) })
}
}
Expand All @@ -98,7 +98,7 @@ extension Purchases {
return try await withCheckedThrowingContinuation { continuation in
purchase(package: package,
promotionalOffer: promotionalOffer) { transaction, customerInfo, error, userCancelled in
continuation.resume(with: Result(customerInfo, error?.ignoreIfPurchaseCancelled(userCancelled))
continuation.resume(with: Result(customerInfo, error)
.map { PurchaseResultData(transaction, $0, userCancelled) })
}
}
Expand Down Expand Up @@ -208,13 +208,3 @@ extension Purchases {
#endif

}

private extension Error {

/// This allows `async` APIs to return `userCancelled` in ``PurchaseResultData`` instead of throwing errors.
/// - Returns: `nil` if `cancelled` is `true`
func ignoreIfPurchaseCancelled(_ cancelled: Bool) -> Self? {
return cancelled ? nil : self
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,37 @@ class PurchasesPurchasingTests: BasePurchasesTests {
expect(receivedUnderlyingError?.code) == SKError.Code.paymentCancelled.rawValue
}

@MainActor
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.2, *)
func testUserCancelledTrueIfSK1AsyncPurchaseCancelled() throws {
try AvailabilityChecks.iOS13APIAvailableOrSkipTest()

let product = StoreProduct(sk1Product: MockSK1Product(mockProductIdentifier: "com.product.id1"))

var receivedError: NSError?

_ = Task {
do {
_ = try await self.purchases.purchase(
product: product
)
} catch {
receivedError = error as NSError
}
}

expect(self.storeKitWrapper.payment).toEventuallyNot(beNil())

let transaction = MockTransaction()
transaction.mockPayment = try XCTUnwrap(self.storeKitWrapper.payment)
transaction.mockState = .failed
transaction.mockError = NSError(domain: SKErrorDomain, code: SKError.Code.paymentCancelled.rawValue)
self.storeKitWrapper.delegate?.storeKitWrapper(self.storeKitWrapper, updatedTransaction: transaction)

expect(receivedError).toEventuallyNot(beNil())
expect(receivedError).to(matchError(ErrorCode.purchaseCancelledError))
}

func testDoNotSendEmptyReceiptWhenMakingPurchase() {
self.receiptFetcher.shouldReturnReceipt = false

Expand Down