AppleReceiptVerifier.NET 0.3.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package AppleReceiptVerifier.NET --version 0.3.0
NuGet\Install-Package AppleReceiptVerifier.NET -Version 0.3.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AppleReceiptVerifier.NET" Version="0.3.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AppleReceiptVerifier.NET --version 0.3.0
#r "nuget: AppleReceiptVerifier.NET, 0.3.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install AppleReceiptVerifier.NET as a Cake Addin
#addin nuget:?package=AppleReceiptVerifier.NET&version=0.3.0

// Install AppleReceiptVerifier.NET as a Cake Tool
#tool nuget:?package=AppleReceiptVerifier.NET&version=0.3.0

📝 What is AppleReceiptVerifier.NET?

Build status Nuget package Nuget package downloads

AppleReceiptVerifier.NET is a library used to verify the App Store receipts with the App Store verification service.

You can read more about receipt verification on the official documentation website.

✔ī¸ Which .NET versions and OS platforms are supported?

The library targets .NET Standard 2.0 and .NET 5 which makes it compatible with the following .NET implementations:

The library is platform-agnostic and can run on any OS supported by the selected .NET implementation.

ℹī¸ Usage

To use the library you need an iOS or MacOS application that has in-app purchases. You also need to get the app shared secret from the App Store Connect (how?). Next, you need to decide whether you want to use the library's DI integration. If not, refer to the Standalone section below. Otherwise, scroll down to the DI Integration section.

đŸ“Ļ Standalone

The verifier can be used without the DI container. Simply instantiate it like this:

string receipt = "[[very long base64encoded string]]";
var verifierOptions = new AppleReceiptVerifierOptions()
{
    AppSecret = "your app shared secret",
    AcceptTestEnvironmentReceipts = false // don't trust test environment receipts in production!
};
var httpClient = new HttpClient();
var verifier = new AppleReceiptVerifier(verifierOptions, httpClient);
var response = await verifier.VerifyReceiptAsync(receipt, true);
if (!response.IsValid)
{
    Console.WriteLine("Failed to verify the receipt: " + response.ErrorDescription);
}
else
{
    Console.WriteLine("Receipt is valid");
}

⚙ī¸ DI Integration

The library supports integration with the Microsoft.Extensions.DependencyInjection DI implementation.

If you want to use a single verifier to verify receipts from a single app, take the following steps:

  1. Register a verifier in your Startup.cs.

    services.AddAppleReceiptVerifier(c =>
    {
        c.AppSecret = "your app shared secret";
        c.AcceptTestEnvironmentReceipts = false; // don't trust test environment receipts in production!
    });
    
  2. Inject IAppleReceiptVerifier in your controller or a service:

     public class ExampleController
     {
         readonly IAppleReceiptVerifier _verifier;
    
         public ExampleController(IAppleReceiptVerifier verifier)
         {
             _verifier = verifier;
         }
    
         public async Task<IActionResult> CheckReceiptAsync(string receipt)
         {
             var response = await _verifier.VerifyReceiptAsync(receipt, true);
             if (!response.IsValid)
             {
                 return BadRequest(response.ErrorDescription);
             }
             else
             {
                 return Ok();
             }
         }
     }
    

If you want to support multiple verifiers, for multiple applications, things are a little bit more tricky:

  1. Register multiple verifiers in your Startup.cs, each with a unique name:

     services.AddAppleReceiptVerifier(c =>
     {
       c.AppSecret = "first app shared secret";
       c.AcceptTestEnvironmentReceipts = true;
     }, "App1");
    
     services.AddAppleReceiptVerifier(c =>
     {
       c.AppSecret = "second app shared secret";
       c.AcceptTestEnvironmentReceipts = true;
     }, "App2");
    
  2. Inject IAppleReceiptVerifierResolver in your constructor and resolve the registered verifiers by their names:

     public class ExampleController
     {
         readonly IAppleReceiptVerifier _verifier1;
         readonly IAppleReceiptVerifier _verifier2;
    
         public ExampleController(IAppleReceiptVerifierResolver verifierResolver)
         {
             _verifier1 = verifierResolver.Resolve("App1");
             _verifier2 = verifierResolver.Resolve("App2");
         }
    
         public async Task<IActionResult> CheckReceiptAsync(string receipt, string appName)
         {
             var response = await (appName switch
             {
                 "App1" => _verifier1.VerifyReceiptAsync(receipt, true),
                 "App2" => _verifier2.VerifyReceiptAsync(receipt, true),
                 _ => throw new ArgumentOutOfRangeException(nameof(appName), appName, "Invalid app name.")
             });
             if (!response.IsValid)
             {
                 return BadRequest(response.ErrorDescription);
             }
             else
             {
                 return Ok();
             }
         }
     }
    

🤔 Advanced usage

Please refer to the official documentation to learn more about how different receipt fields can be used.

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on AppleReceiptVerifier.NET:

Package Downloads
ReceiptVerifierMiddlewareEndpoint

ReceiptVerifierMiddleware adds an endpoint to retrieve receipt info via AppleReceiptVerifier.NET

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.2 3,852 11/23/2023
1.0.1 27,257 10/30/2022
1.0.0 549 10/25/2022
0.5.1 6,050 3/2/2022
0.5.0 477 2/18/2022
0.4.0 5,862 11/24/2021
0.3.1 3,113 1/4/2021
0.3.0 517 12/24/2020

* Tidied the surface API.