示例#1
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);
        }