-
-
Notifications
You must be signed in to change notification settings - Fork 11
refactor: deprecate validateReceipt in favor of verifyPurchase #45
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
146 changes: 146 additions & 0 deletions
146
packages/apple/Tests/OpenIapTests/ValidateReceiptTests.swift
This file contains hidden or 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,146 @@ | ||
| import XCTest | ||
| @testable import OpenIAP | ||
|
|
||
| @available(iOS 15.0, macOS 14.0, *) | ||
| final class ValidateReceiptTests: XCTestCase { | ||
|
|
||
| @MainActor | ||
| func testVerifyPurchaseReturnsIOSResult() async throws { | ||
| let iosResult = ReceiptValidationResultIOS( | ||
| isValid: true, | ||
| jwsRepresentation: "jws-token", | ||
| latestTransaction: nil, | ||
| receiptData: "base64-receipt" | ||
| ) | ||
| let module = FakeOpenIapModule(validateResult: .receiptValidationResultIos(iosResult)) | ||
| let store = OpenIapStore(module: module) | ||
|
|
||
| let result = try await store.verifyPurchase(sku: "test.sku") | ||
|
|
||
| XCTAssertTrue(result.isValid) | ||
| XCTAssertEqual("jws-token", result.jwsRepresentation) | ||
| XCTAssertEqual("base64-receipt", result.receiptData) | ||
| } | ||
|
|
||
| @MainActor | ||
| func testVerifyPurchaseThrowsForAndroidVariant() async { | ||
| let androidPayload = ReceiptValidationResultAndroid( | ||
| autoRenewing: false, | ||
| betaProduct: false, | ||
| cancelDate: nil, | ||
| cancelReason: nil, | ||
| deferredDate: nil, | ||
| deferredSku: nil, | ||
| freeTrialEndDate: 0, | ||
| gracePeriodEndDate: 0, | ||
| parentProductId: "parent", | ||
| productId: "android.sku", | ||
| productType: "subs", | ||
| purchaseDate: 0, | ||
| quantity: 1, | ||
| receiptId: "receipt-123", | ||
| renewalDate: 0, | ||
| term: "P1M", | ||
| termSku: "plan-monthly", | ||
| testTransaction: false | ||
| ) | ||
| let module = FakeOpenIapModule(validateResult: .receiptValidationResultAndroid(androidPayload)) | ||
| let store = OpenIapStore(module: module) | ||
|
|
||
| do { | ||
| _ = try await store.verifyPurchase(sku: "android.sku") | ||
| XCTFail("Expected featureNotSupported when Android result is returned on Apple platform") | ||
| } catch let error as PurchaseError { | ||
| XCTAssertEqual(.featureNotSupported, error.code) | ||
| } catch { | ||
| XCTFail("Unexpected error type: \(error)") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @available(iOS 15.0, macOS 14.0, *) | ||
| private final class FakeOpenIapModule: OpenIapModuleProtocol { | ||
| private let validateResult: ReceiptValidationResult | ||
|
|
||
| init(validateResult: ReceiptValidationResult) { | ||
| self.validateResult = validateResult | ||
| } | ||
|
|
||
| // MARK: - Connection Management | ||
| func initConnection() async throws -> Bool { true } | ||
| func endConnection() async throws -> Bool { true } | ||
|
|
||
| // MARK: - Product Management | ||
| func fetchProducts(_ params: ProductRequest) async throws -> FetchProductsResult { .products(nil) } | ||
| func getPromotedProductIOS() async throws -> ProductIOS? { nil } | ||
|
|
||
| // MARK: - Purchase Management | ||
| func requestPurchase(_ params: RequestPurchaseProps) async throws -> RequestPurchaseResult? { nil } | ||
| func requestPurchaseOnPromotedProductIOS() async throws -> Bool { false } | ||
| func restorePurchases() async throws -> Void { () } | ||
| func getAvailablePurchases(_ options: PurchaseOptions?) async throws -> [Purchase] { [] } | ||
|
|
||
| // MARK: - Transaction Management | ||
| func finishTransaction(purchase: PurchaseInput, isConsumable: Bool?) async throws -> Void { () } | ||
| func getPendingTransactionsIOS() async throws -> [PurchaseIOS] { [] } | ||
| func clearTransactionIOS() async throws -> Bool { true } | ||
| func isTransactionVerifiedIOS(sku: String) async throws -> Bool { false } | ||
| func getTransactionJwsIOS(sku: String) async throws -> String? { nil } | ||
| func currentEntitlementIOS(sku: String) async throws -> PurchaseIOS? { nil } | ||
| func latestTransactionIOS(sku: String) async throws -> PurchaseIOS? { nil } | ||
|
|
||
| // MARK: - Validation | ||
| func getReceiptDataIOS() async throws -> String? { "receipt" } | ||
| func validateReceiptIOS(_ props: ReceiptValidationProps) async throws -> ReceiptValidationResultIOS { | ||
| guard case let .receiptValidationResultIos(ios) = validateResult else { | ||
| throw PurchaseError(code: .featureNotSupported, message: "Android validation not supported", productId: props.sku) | ||
| } | ||
| return ios | ||
| } | ||
|
|
||
| func validateReceipt(_ props: ReceiptValidationProps) async throws -> ReceiptValidationResult { | ||
| validateResult | ||
| } | ||
|
|
||
| func verifyPurchase(_ props: ReceiptValidationProps) async throws -> ReceiptValidationResult { | ||
| validateResult | ||
| } | ||
|
|
||
| // MARK: - Store Information | ||
| func getStorefrontIOS() async throws -> String { "US" } | ||
| func getAppTransactionIOS() async throws -> AppTransaction? { nil } | ||
|
|
||
| // MARK: - Subscription Management | ||
| func getActiveSubscriptions(_ subscriptionIds: [String]?) async throws -> [ActiveSubscription] { [] } | ||
| func hasActiveSubscriptions(_ subscriptionIds: [String]?) async throws -> Bool { false } | ||
| func subscriptionStatusIOS(sku: String) async throws -> [SubscriptionStatusIOS] { [] } | ||
| func isEligibleForIntroOfferIOS(groupID: String) async throws -> Bool { true } | ||
|
|
||
| // MARK: - Refunds (iOS 15+) | ||
| func beginRefundRequestIOS(sku: String) async throws -> String? { nil } | ||
|
|
||
| // MARK: - Misc | ||
| func syncIOS() async throws -> Bool { true } | ||
| func presentCodeRedemptionSheetIOS() async throws -> Bool { true } | ||
| func showManageSubscriptionsIOS() async throws -> [PurchaseIOS] { [] } | ||
| func deepLinkToSubscriptions(_ options: DeepLinkOptions?) async throws -> Void { () } | ||
|
|
||
| // MARK: - Event Listeners | ||
| func purchaseUpdatedListener(_ listener: @escaping PurchaseUpdatedListener) -> Subscription { | ||
| Subscription(eventType: .purchaseUpdated) | ||
| } | ||
|
|
||
| func purchaseErrorListener(_ listener: @escaping PurchaseErrorListener) -> Subscription { | ||
| Subscription(eventType: .purchaseError) | ||
| } | ||
|
|
||
| func promotedProductListenerIOS(_ listener: @escaping PromotedProductListener) -> Subscription { | ||
| Subscription(eventType: .promotedProductIos) | ||
| } | ||
|
|
||
| func removeListener(_ subscription: Subscription) { | ||
| subscription.onRemove?() | ||
| } | ||
|
|
||
| func removeAllListeners() {} | ||
| } |
This file contains hidden or 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.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
🧩 Analysis chain
Verify removal of macOS from availability checks.
The availability checks for
environment,countryCodeIOS, andstorefrontCountryCodeIOShave been modified to exclude macOS, while other parts of the codebase (such asgetAppTransactionIOS()andmapAppTransaction()in the relevant code snippets) still declare@available(iOS 16.0, macOS 14.0, tvOS 16.0, watchOS 9.0, *). This inconsistency could indicate either:On macOS, these properties will now always fall back to
nil(forenvironment) ortransaction.storefrontCountryCode(for the country code fields), potentially causing data loss or inconsistent behavior.Please verify whether
Transaction.environmentandTransaction.storefront.countryCodeare actually available on macOS 14.0+ in StoreKit 2:Also applies to: 131-136, 161-166
🌐 Web query:
💡 Result:
The StoreKit 2 Transaction.environment property is available on macOS 13.0 and later. [1][2]
Sources:
🌐 Web query:
💡 Result:
Short answer: Transaction.storefront (the storefront identifier / 3‑letter countryCode) is part of StoreKit 2 and is available on Apple platforms that support StoreKit 2 — i.e. use the Storefront APIs introduced with StoreKit 2 (see StoreKit docs). In practice StoreKit 2 requires modern OS versions (StoreKit2 generally targets iOS 15 / macOS 12+; AppTransaction types were documented as @available starting macOS 13 for some signed-receipt APIs), so expect storefront info to be available on macOS builds running the StoreKit 2–capable OS. [1][2][3]
Sources
Restore macOS availability declarations that were incorrectly removed.
Verification confirms that both
Transaction.environmentandTransaction.storefront.countryCodeare available on macOS 13.0+ in StoreKit 2. The removal of macOS support from these three availability checks unnecessarily breaks functionality on macOS.environmentshould includemacOS 13.0(ormacOS 14.0to match the enum's declared platform)countryCodeIOSandstorefrontCountryCodeIOSshould include the appropriate macOS versionWithout these declarations, these properties will incorrectly fall back to
nilor alternative values on macOS, causing data loss and inconsistent behavior.Also applies to: 131–136, 161–166
🤖 Prompt for AI Agents
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.
macOS guarded by enclosing @available; check remaining platforms
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.