Example #1
0
        public static PaymentACK Load(Stream source)
        {
            if (source.CanSeek && source.Length > MaxLength)
            {
                throw new ArgumentOutOfRangeException("PaymentACK messages larger than " + MaxLength + " bytes should be rejected", "source");
            }

            PaymentACK ack = new PaymentACK();

            Protobuf.ProtobufReaderWriter reader = new Protobuf.ProtobufReaderWriter(source);
            int key;

            while (reader.TryReadKey(out key))
            {
                switch (key)
                {
                case 1:
                    var bytes = reader.ReadBytes();
                    ack.Payment = PaymentMessage.Load(bytes);
                    break;

                case 2:
                    ack.Memo = reader.ReadString();
                    break;

                default:
                    break;
                }
            }
            return(ack);
        }
Example #2
0
        public async Task <PaymentACK> SubmitPaymentAsync(Uri paymentUrl, HttpClient httpClient)
        {
            bool own = false;

            if (paymentUrl == null)
            {
                paymentUrl = ImplicitPaymentUrl;
            }
            if (paymentUrl == null)
            {
                throw new ArgumentNullException("paymentUrl");
            }
            if (httpClient == null)
            {
                httpClient = new HttpClient();
                own        = true;
            }

            try
            {
                var request = new HttpRequestMessage(HttpMethod.Post, paymentUrl.OriginalString);
                request.Headers.Clear();
                request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(PaymentACK.MediaType));
                request.Content = new ByteArrayContent(this.ToBytes());
                request.Content.Headers.ContentType = new MediaTypeHeaderValue(PaymentMessage.MediaType);

                var result = await httpClient.SendAsync(request).ConfigureAwait(false);

                if (!result.IsSuccessStatusCode)
                {
                    throw new WebException(result.StatusCode + "(" + (int)result.StatusCode + ")");
                }

                if (result.Content.Headers.ContentType == null || !result.Content.Headers.ContentType.MediaType.Equals(PaymentACK.MediaType, StringComparison.OrdinalIgnoreCase))
                {
                    throw new WebException("Invalid contenttype received, expecting " + PaymentACK.MediaType + ", but got " + result.Content.Headers.ContentType);
                }
                var response = await result.Content.ReadAsStreamAsync().ConfigureAwait(false);

                return(PaymentACK.Load(response));
            }
            finally
            {
                if (own)
                {
                    httpClient.Dispose();
                }
            }
        }