private byte[]? DecryptApplePayData(byte[] keyMaterial, ApplePayPaymentToken token)
        {
            try
            {
                if (token.PaymentData?.Data == null)
                {
                    throw new InvalidOperationException("Required payment data was not found on Payment Token JSON.");
                }

                return(ApplePayHelper.DecryptCipherDataUsingAesGcmAlgorithm(keyMaterial, Convert.FromBase64String(token.PaymentData.Data)));
            }
            catch (Exception keyDerivationException)
            {
                MessageBox.Show(
                    $"Apple Pay decryption failed:\r\n\r\n{keyDerivationException}",
                    "Apple Pay Key Decryption Failure",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
                return(null);
            }
        }
        private bool VerifyApplePayPaymentProcessingCertificate(X509Certificate2 paymentProcessingCertificate, ApplePayPaymentToken token)
        {
            try
            {
                if (token.PaymentData?.Header?.PublicKeyHash == null)
                {
                    throw new InvalidOperationException("Required header data was not found on Payment Token JSON.");
                }

                ApplePayHelper.ValidatePaymentProcessingCertificate(
                    paymentProcessingCertificate,
                    token.PaymentData.Header.PublicKeyHash);

                return(true);
            }
            catch (Exception paymentProcessingCertificateException)
            {
                MessageBox.Show(
                    $"Payment Processing Certificate validation failed:\r\n\r\n{paymentProcessingCertificateException}",
                    "Apple Pay Payment Processing Certificate Failure",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
                return(false);
            }
        }
        private byte[]? DeriveApplePayKeyMaterial(X509Certificate2 paymentProcessingCertificate, ApplePayPaymentToken token)
        {
            try
            {
                if (token.PaymentData?.Header?.EphemeralPublicKey == null)
                {
                    throw new InvalidOperationException("Required header data was not found on Payment Token JSON.");
                }

                return(ApplePayHelper.DeriveKeyMaterialUsingEllipticCurveDiffieHellmanAlgorithm(
                           paymentProcessingCertificate,
                           token.PaymentData.Header.EphemeralPublicKey));
            }
            catch (Exception keyDerivationException)
            {
                MessageBox.Show(
                    $"Apple Pay key derivation failed:\r\n\r\n{keyDerivationException}",
                    "Apple Pay Key Derivation Failure",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
                return(null);
            }
        }
        private bool VerifyApplePaySignature(X509Certificate2 rootCertificateAuthority, ApplePayPaymentToken token)
        {
            try
            {
                if (token.PaymentData?.Signature == null ||
                    token.PaymentData?.Data == null ||
                    token.PaymentData?.Header?.EphemeralPublicKey == null ||
                    token.PaymentData?.Header?.TransactionId == null)
                {
                    throw new InvalidOperationException("Required signature data was not found on Payment Token JSON.");
                }

                ApplePayHelper.VerifyApplePaySignature(
                    rootCertificateAuthority,
                    token.PaymentData.Signature,
                    token.PaymentData.Data,
                    token.PaymentData.Header.EphemeralPublicKey,
                    token.PaymentData.Header.TransactionId.ToByteArray(),
                    token.PaymentData.Header.ApplicationData?.ToByteArray(),
                    _ValidateSigningTimeCheckBox.Checked ? 300 : (int?)null);

                return(true);
            }
            catch (Exception signatureValidationException)
            {
                MessageBox.Show(
                    $"Payment Token Signature validation failed:\r\n\r\n{signatureValidationException}",
                    "Apple Pay Signature Verification Failure",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
                return(false);
            }
        }