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

getPromotionalOffer: return ErrorCode.ineligibleError if receipt is not found #2678

Merged
merged 1 commit into from
Jun 21, 2023
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
13 changes: 9 additions & 4 deletions Sources/Purchasing/Purchases/PurchasesOrchestrator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,15 @@ final class PurchasesOrchestrator {
return
}

receiptFetcher.receiptData(refreshPolicy: .onlyIfEmpty) { receiptData, receiptURL in
guard let receiptData = receiptData,
!receiptData.isEmpty else {
completion(.failure(ErrorUtils.missingReceiptFileError(receiptURL)))
self.receiptFetcher.receiptData(refreshPolicy: .onlyIfEmpty) { receiptData, receiptURL in
guard let receiptData = receiptData, !receiptData.isEmpty else {
let underlyingError = ErrorUtils.missingReceiptFileError(receiptURL)

// Promotional offers require existing purchases.
// If no receipt is found, this is most likely in sandbox with no purchases,
// so producing an "ineligible" error is better.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add this explanation to the public docs for getting a promotional error? To warn users this may happen

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. I'm getting a 503 when suggesting edits to the docs, but I'll try again later.
Screenshot 2023-06-21 at 09 05 44

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

completion(.failure(ErrorUtils.ineligibleError(error: underlyingError)))

return
}

Expand Down
11 changes: 11 additions & 0 deletions Tests/BackendIntegrationTests/StoreKitIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,17 @@ class StoreKit1IntegrationTests: BaseStoreKitIntegrationTests {
try await subscribe()
}

func testGetPromotionalOfferWithNoPurchasesReturnsIneligible() async throws {
let product = try await self.monthlyPackage.storeProduct
let discount = try XCTUnwrap(product.discounts.onlyElement)

do {
_ = try await Purchases.shared.promotionalOffer(forProductDiscount: discount, product: product)
} catch {
expect(error).to(matchError(ErrorCode.ineligibleError))
}
}

func testUserHasNoEligibleOffersByDefault() async throws {
let (_, created) = try await Purchases.shared.logIn(UUID().uuidString)
expect(created) == true
Expand Down
26 changes: 26 additions & 0 deletions Tests/StoreKitUnitTests/PurchasesOrchestratorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,32 @@ class PurchasesOrchestratorTests: StoreKitConfigTestCase {
expect(self.offerings.invokedPostOfferParameters?.offerIdentifier) == storeProductDiscount.offerIdentifier
}

func testGetPromotionalOfferFailsWithIneligibleIfNoReceiptIsFound() async throws {
self.receiptFetcher.shouldReturnReceipt = false

let product = try await self.fetchSk1Product()
let storeProductDiscount = MockStoreProductDiscount(offerIdentifier: "offerid1",
currencyCode: product.priceLocale.currencyCode,
price: 11.1,
localizedPriceString: "$11.10",
paymentMode: .payAsYouGo,
subscriptionPeriod: .init(value: 1, unit: .month),
numberOfPeriods: 2,
type: .promotional)

do {
_ = try await Async.call { completion in
self.orchestrator.promotionalOffer(forProductDiscount: storeProductDiscount,
product: StoreProduct(sk1Product: product),
completion: completion)
}
} catch {
expect(error).to(matchError(ErrorCode.ineligibleError))
}

expect(self.offerings.invokedPostOffer) == false
}

func testGetSK1PromotionalOfferFailsWithIneligibleDiscount() async throws {
self.customerInfoManager.stubbedCachedCustomerInfoResult = mockCustomerInfo
self.offerings.stubbedPostOfferCompletionResult = .failure(
Expand Down