// Register DI services...
services.RegisterAppleReceiptParser();
...// ... and resolve the service later.
IAppleReceiptParserService parserService;
...// Get your base64 Apple Receipt
const string appleAppReceipt ="{receipt_base64_string}";// Convert to Bytesbyte[]data= Convert.FromBase64String(appleAppReceipt);// Get parsed receiptAppleAppReceiptreceipt= parserService.GetAppleReceiptFromBytes(data);
// (Optional) You can create implementation of custom validation process:
services.AddScoped<IAppleReceiptCustomVerificatorService,AppleReceiptCustomVerificatorService>();
...// Fill settings:
services.RegisterAppleReceiptVerificator(x =>{ x.VerifyReceiptSharedSecret ="XXXX";// Apple Shared Secret Key x.VerificationType = AppleReceiptVerificationType.Sandbox;// Verification Type: Sandbox / Production x.AllowedBundleIds =new[]{"com.mbaasy.ios.demo"};// Array with allowed bundle ids});
...// ... and resolve the service later.
IAppleReceiptVerificatorService verificator;
...// Usage option 1. Apple recommends you use that behaviour: https://developer.apple.com/documentation/storekit/in-app_purchase/validating_receipts_with_the_app_store// Like 'Check on prod and in case of 21004 check on sandbox'. // BUT I CANNOT RECOMMEND THAT WAY, because Production Server cannot switch to Sandbox based on Apple Response.// Intruder would be able to send Sandbox data to your Server and get the Success response.// I Recommend the second/third options.
AppleReceiptVerificationResult result =await verificator.VerifyAppleProductionReceiptAsync(appleAppReceipt).ConfigureAwait(false);if(result.Status == IAPVerificationResponseStatus.TestReceiptOnProd){result=await verificator.VerifyAppleSandBoxReceiptAsync(appleAppReceipt).ConfigureAwait(false);}// Usage option 2. Determine if the Server was requested from Preview environment// Or App belongs to Not published apps (based on version for example).varisPreviewEnvironmentOrAppIsBelongsToUnpublishedBasedOnSomePattern=true;result=isPreviewEnvironmentOrAppIsBelongsToUnpublishedBasedOnSomePattern?await verificator.VerifyAppleSandBoxReceiptAsync(appleAppReceipt).ConfigureAwait(false):await verificator.VerifyAppleProductionReceiptAsync(appleAppReceipt).ConfigureAwait(false);// Usage option 3. Btw, you still has previous option to setup usage in the configuration during a Server Init step.result=await verificator.VerifyAppleReceiptAsync(appleAppReceipt).ConfigureAwait(false);// OBSOLETE USAGE (Just for Backward Compatibity):varverificationStatus= verificationResult.Status;varverificationReceipt= verificationResult.Receipt;varverificationMessage= verificationResult.Message;// USAGE (Full Apple Response Info):varverificationStatus= verificationResult.AppleVerificationResponse.StatusCode;varverificationReceipt= verificationResult.AppleVerificationResponse.Receipt;varverificationLatestReceiptInfo= verificationResult.AppleVerificationResponse.LatestReceiptInfo;varverificationPendingRenewalInfo= verificationResult.AppleVerificationResponse.PendingRenewalInfo;varverificationMessage= verificationResult.Message;