For Unity IAP developers seeing CrossPlatformValidator throw InvalidPublicKeyException in v5.1.0+ on Apple platforms, this article explains why local receipt validation fails when StoreKit 2 is in use. It’s useful if you support iOS/iPadOS/tvOS/macOS and need to detect StoreKit 2 at runtime so receipt validation only runs on StoreKit 1 devices.
Symptoms
CrossPlatformValidatorreturnsInvalidPublicKeyExceptionwhen created.- Using Unity In-App Purchasing v5.1.0 or above.
- Using a device running iOS/iPadOS/tvOS < 15.0 or macOS < 12.0.
Cause
The issue is caused by attempting perform local receipt validation on a device using Apple StoreKit 2. The CrossPlatformValidator method is used to perform local receipt validation, but this is only needed for StoreKit 1 devices (iOS/iPadOS/tvOS < 15.0, macOS < 12.0)
Resolution
Add code to detect a StoreKit 2 device and not attempt to perform local receipt validation.
bool storeKit2 = false;
// If compiled for any Apple platform (including the Unity Editor playing on OSX),
// check if StoreKit 2 is being used.
// The first #IF is needed since UseStoreKit1() will return false on non-Apple
// platforms, since those platforms are not using StoreKit 1. Without this first
// check, non-Apple platforms would appear to be using StoreKit 2.
#if UNITY_IOS || UNITY_TVOS || UNITY_VISIONOS || UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX
storeKit2 = !Purchasing.Utilities.StoreKitSelector.UseStoreKit1();
#endif
if(!storeKit2) {
// Perform receipt validation
}