Exemple #1
0
 public AppleReceiptVerificationResult(string errorMessage, IapVerificationResultStatus status,
                                       AppleAppReceipt receipt)
 {
     Message = errorMessage;
     Status  = status;
     Receipt = receipt;
 }
Exemple #2
0
 public AppleReceiptVerificationResult(IapVerificationResultStatus status,
                                       AppleAppReceipt receipt,
                                       string message)
 {
     Receipt = receipt;
     Status  = status;
     Message = message;
 }
Exemple #3
0
        public async Task <AppleReceiptVerificationResult> VerifyAppleReceiptAsync(string receiptData)
        {
            // 1. Validate incoming arguments
            if (string.IsNullOrEmpty(receiptData))
            {
                _log.Information("receiptData cannot be empty");
                return(new AppleReceiptVerificationResult("receiptData cannot be empty",
                                                          IapVerificationResultStatus.WrongArgument));
            }
            // 2. Prevalidate Receipt (Optional)
            try
            {
                byte[]          data    = Convert.FromBase64String(receiptData);
                AppleAppReceipt receipt = _receiptParserService.GetAppleReceiptFromBytes(data);

                // a. Validate bundle ID
                if (receipt != null && !_settings.AllowedBundleIds.Contains(receipt.BundleId))
                {
                    _log.Information("Receipt has wrong bundle ID {bundle_id}", receipt.BundleId);
                    return(new AppleReceiptVerificationResult($"Receipt has wrong bundle ID {receipt.BundleId}",
                                                              IapVerificationResultStatus.WrongArgument));
                }

                if (_customValidation != null)
                {
                    // if custom Validator implemented
                    AppleReceiptVerificationResult validationResult = _customValidation.ValidateReceipt(receipt);
                    if (validationResult == null || validationResult.Status != IapVerificationResultStatus.Ok)
                    {
                        // and custom validation doesn't passed - failed
                        return(validationResult);
                    }
                }
            }
            catch (Exception e)
            {
                _log.Error(e,
                           "Something went wrong in receipt prevalidation. Seems to be invalid receipt data. Skip this step.");
            }
            // 3. Validate Receipt in Apple (verification in IAP)
            try
            {
                _log.Debug("Start receipt verification in IAP...");
                IAPVerificationRequest request =
                    new IAPVerificationRequest(receiptData, _settings.VerifyReceiptSharedSecret);
                IAPVerificationResult iapVerificationResult =
                    await _restService.ValidateAppleReceiptAsync(request).ConfigureAwait(false);

                if (iapVerificationResult == null)
                {
                    return(new AppleReceiptVerificationResult("IAP receipt verification failed. Apple returned empty receipt.",
                                                              IapVerificationResultStatus.InternalVerificationFailed));
                }
                IapVerificationResultStatus iapStatus = iapVerificationResult.StatusCode;
                // 1.If status <> 0 - failed
                if (iapStatus != IapVerificationResultStatus.Ok)
                {
                    return(new AppleReceiptVerificationResult("IAP receipt verification failed",
                                                              iapStatus,
                                                              iapVerificationResult.Receipt));
                }

                // 2. If there is no information about receipt - failed.
                if (iapVerificationResult.Receipt == null)
                {
                    _log.Information("IAP Receipt Verification failed due empty receipt");
                    return(new AppleReceiptVerificationResult("IAP Receipt Verification failed due empty receipt.",
                                                              IapVerificationResultStatus.InternalVerificationFailed));
                }

                _log.Information("IAPReceipt Verification passed.");
                return(new AppleReceiptVerificationResult(iapVerificationResult.StatusCode,
                                                          iapVerificationResult.Receipt, "Everything is OK."));
            }
            catch (Exception e)
            {
                _log.Error(e, "Something went wrong in IAP receipt verification");
                return(new AppleReceiptVerificationResult("Something went wrong in IAP receipt verification",
                                                          IapVerificationResultStatus.InternalVerificationBroken));
            }
        }
Exemple #4
0
 public AppleReceiptVerificationResult(string message, IapVerificationResultStatus status)
 {
     Message = message;
     Status  = status;
     Receipt = null;
 }