public static string ReceiptSignature(this CreateFiscalCheckRequest parameters)
        {
            //------------------------ TEST ---------------------------------------------------------
            // проверка
            //using (var sha256 = SHA256.Create())
            //{
            //    var Shop_IDP = sha256.ComputeHash(Encoding.UTF8.GetBytes(parameters.ShopID));
            //    var PaymentAttemptID = sha256.ComputeHash(Encoding.UTF8.GetBytes(parameters.PaymentAttemptID));
            //    var Subtotal_P = sha256.ComputeHash(Encoding.UTF8.GetBytes(parameters.Subtotal.ToString("F2", CultureInfo.InvariantCulture)));
            //    var Receipt = sha256.ComputeHash(Encoding.UTF8.GetBytes(parameters.Receipt));
            //    var Password = sha256.ComputeHash(Encoding.UTF8.GetBytes(parameters.Password));

            //    Console.WriteLine($"ShopID={Byte2HexString(Shop_IDP)}");
            //    Console.WriteLine($"PaymentAttemptID={Byte2HexString(PaymentAttemptID)}");
            //    Console.WriteLine($"Subtotal={Byte2HexString(Subtotal_P)}");
            //    Console.WriteLine($"Receipt={Byte2HexString(Receipt)}");
            //    Console.WriteLine($"Password={Byte2HexString(Password)}");
            //}
            //--------------------------------------------------------------------------------------

            using (var sha256 = SHA256.Create())
            {
                var parts = parameters.GetParts()
                            .Select(p => p.SignPart(sha256))
                            .ToArray();

                var signature = string.Join("&", parts).Sign(sha256).ToUpper();
                return(signature);
            }
        }
        public static string Signature(this CreateFiscalCheckRequest parameters)
        {
            //------------------------ TEST ---------------------------------------------------------
            // проверка
            //using (var md5 = MD5.Create())
            //{
            //    var shopID = md5.ComputeHash(Encoding.UTF8.GetBytes(parameters.ShopID));
            //    var paymentAttemptID = md5.ComputeHash(Encoding.UTF8.GetBytes(parameters.PaymentAttemptID));
            //    var subtotal = md5.ComputeHash(Encoding.UTF8.GetBytes(parameters.Subtotal.ToString("F2", CultureInfo.InvariantCulture)));
            //    var password = md5.ComputeHash(Encoding.UTF8.GetBytes(parameters.Password));

            //    Console.WriteLine($"ShopID={Byte2HexString(shopID)}");
            //    Console.WriteLine($"PaymentAttemptID={Byte2HexString(paymentAttemptID)}");
            //    Console.WriteLine($"Subtotal={Byte2HexString(subtotal)}");
            //    Console.WriteLine($"Password={Byte2HexString(password)}");
            //}
            //--------------------------------------------------------------------------------------

            using (var md5 = MD5.Create())
            {
                var parts = parameters.GetParts()
                            .Select(p => p.SignPart(md5))
                            .ToArray();

                //var signature = string.Join("&", parts).Sign(md5).ToUpper();
                var signature = string.Concat(parts).Sign(md5).ToUpper();
                return(signature);
            }
        }
        private static IEnumerable <string> GetParts(this CreateFiscalCheckRequest parameters)
        {
            // ShopID
            yield return(parameters.ShopID);

            // PaymentAttemptID
            yield return(parameters.PaymentAttemptID);

            // Subtotal
            yield return(parameters.Subtotal.ToString("F2", CultureInfo.InvariantCulture));

            // password
            yield return(parameters.Password);
        }
        private static IEnumerable <KeyValuePair <string, string> > ToKeyValuePairs(CreateFiscalCheckRequest original)
        {
            yield return(new KeyValuePair <string, string>("ShopID", original.ShopID));

            yield return(new KeyValuePair <string, string>("PaymentAttemptID", original.PaymentAttemptID));

            yield return(new KeyValuePair <string, string>("Subtotal", original.Subtotal.ToString("F2", CultureInfo.InvariantCulture)));

            yield return(new KeyValuePair <string, string>("Signature", original.Signature));

            yield return(new KeyValuePair <string, string>("Receipt", original.Receipt));

            yield return(new KeyValuePair <string, string>("ReceiptSignature", original.ReceiptSignature));
        }
Example #5
0
        internal bool CreateFiscalCheck(ref FiscalizationData fiscal)
        {
            try
            {
                var customer = new Сustomer
                {
                    id    = fiscal.СustomerId,
                    phone = fiscal.СustomerPhone,
                    email = fiscal.СustomerEmail,
                    name  = fiscal.СustomerName,
                    inn   = fiscal.СustomerInn
                };

                var lines = new Lines
                {
                    name     = fiscal.СustomerName,
                    price    = fiscal.PaidTotal,
                    qty      = 1,
                    sum      = fiscal.PaidTotal,
                    vat      = VAT.NotVAT,
                    payattr  = PayAttribute.PrepaidExpense,
                    lineattr = 10,
                };

                var payments = new Payments
                {
                    kind   = PaymentsKind.PaymentByAdditionalMeansPayment,
                    type   = PaymentsType.ExternalAcquiring,
                    amount = fiscal.PaidTotal
                };

                var receipt = new Receipt
                {
                    customer = customer,
                    lines    = new List <Lines> {
                        lines
                    },
                    taxmode  = Taxmode.SimplifiedTaxSystemIncomeMinusExpense,
                    payments = new List <Payments> {
                        payments
                    },
                    total = fiscal.PaidTotal
                };

                fiscal.Receipt = JsonConvert.SerializeObject(receipt);

                var createFiscalCheckRequest = new CreateFiscalCheckRequest
                {
                    ShopID           = fiscal.ShopID,
                    PaymentAttemptID = fiscal.PaymentAttemptID,
                    Subtotal         = fiscal.PaidTotal,
                    Receipt          = fiscal.Receipt,
                    Password         = _password
                };

                createFiscalCheckRequest.Receipt          = Base64Encode(fiscal.Receipt);
                createFiscalCheckRequest.ReceiptSignature = createFiscalCheckRequest.ReceiptSignature();
                createFiscalCheckRequest.Signature        = createFiscalCheckRequest.Signature();

                var requestContent    = RequestFormedHelper.ToFormUrlEncodedContent(createFiscalCheckRequest);
                var responseXmlString = GetResponse(unitellerPayUrl, requestContent);
                var response          = DeserializeXml <XmlResponse>(responseXmlString);

                if (response == null || !string.Equals(response.Result, "0"))
                {
                    var errorMessage = (response == null || string.IsNullOrEmpty(response.ErrorMessage)) ? "CreateFiscalCheckError" : response.ErrorMessage;
                    logger.Error($"UnitellerProcessor.CreateFiscalCheck ShopId = {fiscal.ShopID}, OrderID = {fiscal.OrderID}, СustomerId = {fiscal.СustomerId}, " +
                                 $"Error = {errorMessage}");
                    fiscal.ErrorMessage = errorMessage;
                    return(false);
                }

                return(true);
            }
            catch (Exception ex)
            {
                logger.Error(ex, $"UnitellerProcessor.CreateFiscalCheck Exception ShopId = {fiscal.ShopID}, OrderID = {fiscal.OrderID}, СustomerId = {fiscal.СustomerId}");
                fiscal.ErrorMessage = ex.Message;
                return(false);
            }
        }
 public static FormUrlEncodedContent ToFormUrlEncodedContent(CreateFiscalCheckRequest original)
 {
     return(new FormUrlEncodedContent(ToKeyValuePairs(original)));
 }