Example #1
0
        public string SendPaymentATOL(ServiceConfig m_Config, kkmServiceRequest m_request, CultureInfo FormatProvider, out string UUID, out string exeptionMessage, out string status)
        {
            UUID            = "Не присвоен";
            exeptionMessage = "";
            status          = "";

            Logs.LogAdd("SendPaymentATOL()Пытаюсь отправить чек с групповой: " + m_request.groupOperId);

            exeptionMessage = GetToken();
            if (!string.IsNullOrEmpty(exeptionMessage))
            {
                return(exeptionMessage);
            }

            try
            {
                string url = m_Config.url + "/possystem/v4/" + m_Config.codeGroup + "/sell";


                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);


                request.Method      = "POST";
                request.ContentType = "application/json; charset=utf-8";
                request.Headers.Add("Token", token);

                string summ = m_request.summ;
                //m_request.groupOperId = new Random().Next(100000);
                // m_request.groupOperId = 319979252;
                m_request.email = "*****@*****.**";

                decimal dSumm = Convert.ToDecimal(summ, ni);

                AtoRequest atolRequest = new AtoRequest();
                atolRequest.external_id    = m_request.groupOperId.ToString();
                atolRequest.receipt        = new Receipt();
                atolRequest.receipt.client = new Client()
                {
                    email = m_request.email
                };
                atolRequest.receipt.company = new Company()
                {
                    email = m_Config.emailCompany, inn = m_Config.inn, payment_address = m_Config.paymentAddr
                };
                atolRequest.receipt.payments = new List <Payment>();
                atolRequest.receipt.payments.Add(new Payment()
                {
                    type = 1, sum = dSumm
                });
                atolRequest.receipt.total = dSumm;
                atolRequest.service       = new Service()
                {
                    callbackUrl = m_Config.callBackUrl
                };
                atolRequest.timestamp     = timestamp;
                atolRequest.receipt.items = new List <Item>();
                atolRequest.receipt.items.Add(new Item()
                {
                    name = m_Config.nameServices, price = dSumm, quantity = 1, sum = dSumm, vat = new Vat()
                    {
                        type = "vat20"
                    }
                });

                //m_request.groupOperId = new Random().Next(100000);
                //m_request.groupOperId = 319979251;
                m_request.email = "*****@*****.**";

                string pData   = @"{ ""external_id"":""" + m_request.groupOperId.ToString() + @""", 
                                        ""receipt"":
                                        {""client"":{""email"":""" + m_request.email + @"""},
                                        ""company"":
                                        {""email"":""" + m_Config.emailCompany + @""",
                                        ""inn"":""" + m_Config.inn + @""",
                                        ""payment_address"":""" + m_Config.paymentAddr + @"""},
                                        ""items"":
                                        [{""name"":""" + m_Config.nameServices + m_request.accountNumber + @""",
                                        ""price"":" + summ + @", 
                                        ""quantity"":1,
                                        ""sum"":" + summ + @",
                                        ""payment_method"":""full_payment"",
                                        ""payment_object"":""service"",
                                        ""vat"":{""type"":""vat20""}}],
                                        ""payments"":
                                        [{""type"":1,
                                        ""sum"":" + summ + @"}],
                                        ""total"":" + summ + @"},
                                        ""service"":
                                        {""callbackUrl"":""" + m_Config.callBackUrl + @"""}, 
                                        ""timestamp"":""" + timestamp + @"""}";
                string jSonStr = pData;

                // Serializer.ToJson<AtoRequest>(atolRequest);
                byte[] ByteArr = Encoding.UTF8.GetBytes(jSonStr);
                request.ContentLength = ByteArr.Length;
                request.GetRequestStream().Write(ByteArr, 0, ByteArr.Length);

                DataJSONPayload dataJSONPayload;

                if (m_Config.debugLevel > 20)
                {
                    Logs.LogAdd("Отправленный чек: " + jSonStr);
                }

                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    using (StreamReader myStreamReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                    {
                        DataContractJsonSerializer jsonFormatter = new DataContractJsonSerializer(typeof(DataJSONPayload));
                        dataJSONPayload = (DataJSONPayload)jsonFormatter.ReadObject(myStreamReader.BaseStream);
                        UUID            = dataJSONPayload.uuid;
                        status          = dataJSONPayload.status;
                    }
                }
                if (m_Config.debugLevel > 3)
                {
                    Logs.LogAdd("Отправленная групповая: " + m_request.groupOperId + Environment.NewLine +
                                "Полученный token: " + token + Environment.NewLine +
                                "Полученный UID: " + UUID);
                }
            }
            catch (WebException ex)
            {
                var temp = ex.Response as HttpWebResponse;
                Logs.LogAdd("ОШИБКА: " + ex.Message);
                string content;

                using (var r = new StreamReader(ex.Response.GetResponseStream()))
                    content = r.ReadToEnd();

                try
                {
                    using (var mm = new MemoryStream(Encoding.UTF8.GetBytes(content)))
                    {
                        using (StreamReader myStreamReader = new StreamReader(mm, Encoding.UTF8))
                        {
                            DataContractJsonSerializer jsonFormatter = new DataContractJsonSerializer(typeof(DataJSONPayload));
                            var errorDataJSONPayload = (DataJSONPayload)jsonFormatter.ReadObject(myStreamReader.BaseStream);
                            if (errorDataJSONPayload.error != null)
                            {
                                exeptionMessage = errorDataJSONPayload.error.text;
                            }
                        }
                    }
                }
                catch (Exception)
                {
                }

                switch (temp.StatusCode)
                {
                case HttpStatusCode.Unauthorized:
                    Logs.LogAdd("ОШИБКА: " + content
                                + Environment.NewLine + "Номер групповой операции: " + m_request.groupOperId + " UUID: " + m_UUID);
                    break;
                }
            }
            catch (Exception ex)
            {
                Logs.LogAdd("ОШИБКА: " + ex.Message + " ОШИБКА 2 : ");
            }
            return(UUID);
        }
Example #2
0
 public ClientObject(TcpClient tcpClient, ServiceConfig m_Config)
 {
     client        = tcpClient;
     this.m_Config = m_Config;
 }