public static string SerializeSync <T>(this IXmlSerializer serializer, T source)
        {
            var sb = new StringBuilder();

            using (var writer = new StringWriter(sb)) {
                serializer
                .SerializeAsync(source, writer)
                .ConfigureAwait(false)
                .GetAwaiter()
                .GetResult();
                writer.Flush();
            }

            return(sb.ToString());
        }
示例#2
0
        public async Task <UnifiedOrderResponse> UnifiedOrderAsync(UnifiedOrderRequest order)
        {
            if (order == null)
            {
                throw new ArgumentException("order cannot be null", nameof(order));
            }

            if (order.EmbedTradeType == TradeType.App)
            {
                if ((order.Detail ?? "").Length > 8192)
                {
                    throw new ValidationException("detail field cannot exceed 8192 characters");
                }

                if (order.EmbedDetail != null)
                {
                    foreach (var d in order.EmbedDetail.GoodsDetail)
                    {
                        if (string.IsNullOrEmpty(d.GoodsCategory))
                        {
                            throw new ValidationException("goods_category is required");
                        }
                        if (string.IsNullOrEmpty(d.Body))
                        {
                            throw new ValidationException("body is required");
                        }
                    }
                }
            }

            if (order.EmbedTradeType == TradeType.Jsapi)
            {
                if ((order.Detail ?? "").Length > 6000)
                {
                    throw new ValidationException("detail field cannot exceed 6000 characters");
                }

                if (order.OpenId == null)
                {
                    throw new ValidationException($"{nameof(order.OpenId)}is reqiured");
                }
            }

            if (order.EmbedTradeType == TradeType.Native)
            {
                if ((order.Detail ?? "").Length > 6000)
                {
                    throw new ValidationException("detail field cannot exceed 6000 characters");
                }
            }

            order.Preparing(_paymentContext);

            _signatureService
            .GenNonce(nonce => { order.NonceStr = nonce; })
            .GenSign(_paymentContext.MchKey, order, sign => { order.Sign = sign; });

            _validationProvider.Validate(order);

            var response = await _httpClient.PostAsync <UnifiedOrderResponse>("api_url", async writer => {
                await _xmlSerializer.SerializeAsync(order, writer);
            });

            return(response);
        }