RevenueCat / purchases-ios

In-app purchases and subscriptions made easy. Support for iOS, watchOS, tvOS, macOS, and visionOS.
https://www.revenuecat.com/
MIT License
2.2k stars 294 forks source link

Throw error for invalid Apple In App Subscription Key in debug POST /receipt requests #4005

Closed fire-at-will closed 3 days ago

fire-at-will commented 6 days ago

Motivation

Making purchases with Store Kit 2 requires developers to upload an In App Purchase Key to RevenueCat. Currently, we log a warning when a developer tries to make a purchase with an invalid In App Purchase Key, but the problem can easily be missed because the purchase still completes, and if offline entitlements are enabled, entitlements can be unlocked, but the RevenueCat backend will never be able to validate and register the purchase because we can't verify the purchase with Apple.

Ultimately, this could lead to developers upgrading from SDK version 4.X to 5.X, not realize that they need to upload their in app purchase key, and ship a build to production that can't properly make purchases.

Description

This PR modifies the purchase() function to throw an error if the app is running in a DEBUG build and the RevenueCat backend returns an error response because the developer has an invalid or missing In App Purchase Key. When it throws an exception, the following error message is logged with the warn level:

Failed to post the transaction to RevenueCat's backend because your Apple In-App Purchase Key is invalid or not present. This error is thrown only in debug builds; in production, it will fail silently. You must configure an In-App Purchase Key. Please see https://rev.cat/in-app-purchase-key-configuration for more info.

The PR accomplishes this behavior by modifying the CustomerInfoResponseHandler class. To ensure that the error is only thrown for POST /receipt calls and not GET /subscriber calls, a new initializer parameter failIfInvalidSubscriptionKeyDetectedInDebug has been introduced to control the throwing behavior when the error is received from the backend.

fire-at-will commented 3 days ago

@MarkVillacampa thanks for the review! 🙌 I've made the following changes: