Beispiel #1
0
        /// <summary>
        /// Creates the sample by square XML document.
        /// </summary>
        /// <param name="configuration">The configuration for external generator (<see cref="BySquareExternalEncoder"/>).</param>
        private BySquareXmlDocuments CreateSampleBySquareXmlDocument(ExternalGeneratorSettings configuration)
        {
            var account1 = new BankAccount
            {
                IBAN = "SK2911000000005902208743",
                BIC  = "TATRSKBX"
            };

            var account2 = new BankAccount
            {
                IBAN = "SK2011000000002619521810",
                BIC  = "TATRSKBX"
            };

            var payment = new Payment(account1, account2)
            {
                Amount      = 10,
                PaymentNote = "poznamka",
                OriginatorsReferenceInformation = "reference"
            };

            var pay = new Pay(payment);

            var bySquareXmlDocuments = new BySquareXmlDocuments(pay)
            {
                Username      = configuration.Username,
                Password      = configuration.Password,
                ServiceId     = configuration.ServiceId,
                ServiceUserId = configuration.ServiceUserId,
            };

            return(bySquareXmlDocuments);
        }
        /// <summary>
        /// Serializes the given document as XML.
        /// </summary>
        /// <param name="bySquareXmlDocuments">The by square XML documents.</param>
        public string SerializeAsXml(BySquareXmlDocuments bySquareXmlDocuments)
        {
            using var ms = new MemoryStream();
            using (var xtw = XmlWriter.Create(ms,
                                              new XmlWriterSettings()
            {
                ConformanceLevel = ConformanceLevel.Auto,
                CloseOutput = false,
                Indent = true,
                IndentChars = "    ",
                OmitXmlDeclaration = true,
            }))
            {
                var ds = new XmlSerializer(typeof(BySquareXmlDocuments));
                ds.Serialize(xtw, bySquareXmlDocuments);
                xtw.Flush();
            }

            ms.Position = 0;

            using var sr = new StreamReader(ms);
            {
                return(sr.ReadToEnd());
            }
        }
        /// <summary>
        /// Serializes given document as a QR string.
        /// </summary>
        /// <param name="document">A BySquareDocument instance.</param>
        /// <returns>
        /// QR string.
        /// </returns>
        /// <remarks>
        /// Sample response:
        /// <StringSetOfCodes>
        /// <PayBySquare>00048000BEH98QN092PSOB1EKLG0V9OQ0L0QAK2M303J09BRUEOSHJHRN8UH0JUEAVQLBQQ9L88MHK264TTJLOHE056AM3MCD3PB600</PayBySquare>
        /// <ItemsBySquare />
        /// </StringSetOfCodes>
        /// </remarks>
        public string Encode(BySquareDocument document)
        {
            // wrap in BySquareXmlDocuments object
            var url       = this.Configuration.Url;
            var documents = new BySquareXmlDocuments(document)
            {
                Username      = this.Configuration.Username,
                Password      = this.Configuration.Password,
                ServiceId     = this.Configuration.ServiceId,
                ServiceUserId = this.Configuration.ServiceUserId,
            };

            // send to bysquare.com web api generator
            var request = new HttpRequestMessage(HttpMethod.Post, url)
            {
                Content = new StringContent(this.Serializer.SerializeAsXml(documents), Encoding.UTF8, "text/xml")
            };

            try
            {
                using var client = new HttpClient();
                var response       = client.SendAsync(request, HttpCompletionOption.ResponseContentRead).Result;
                var responseString = response.Content.ReadAsStringAsync().Result;
                var setOfCodes     = this.Serializer.DeserializeSetOfCodes(responseString);
                return(setOfCodes.PayBySquare);
            }
            catch (Exception ex)
            {
                this.Logger?.LogError(ex, "bysquare.com Web API error");
                return(null);
            }
        }