Example #1
0
 internal static string ToPostDataString(this WxPayApiType value)
 {
     return(Enum.GetName(typeof(WxPayApiType), value).ToUpper());
 }
        public async Task <OrderReceipt> PlaceOrder(string appID, string mechantID, string deviceInfo, string description, IEnumerable <GoodsDetails> details, string attachNote, string orderID, string currency, int totalAmount, string clientIP, DateTime?validFrom, DateTime?expireAt, string couponTag, string callbackUrl, WxPayApiType apiType, string productID, WxPayPaymentLimit paymentLimit, string userOpenID)
        {
            dynamic postData = new WxPayData(_configuration.APIKey);

            postData.appid            = appID;
            postData.mch_id           = mechantID;
            postData.device_info      = deviceInfo;
            postData.body             = description;
            postData.detail           = SerializeGoodsDetails(details);
            postData.attach           = attachNote;
            postData.out_trade_no     = orderID;
            postData.fee_type         = currency;
            postData.total_fee        = totalAmount;
            postData.spbill_create_ip = clientIP;
            postData.time_start       = ConvertToEast8TimeString(validFrom);
            postData.time_expire      = ConvertToEast8TimeString(expireAt);
            postData.goods_tag        = couponTag;
            postData.notify_url       = callbackUrl;
            postData.trade_type       = apiType.ToPostDataString();
            postData.product_id       = productID;
            postData.limit_pay        = paymentLimit.ToPostDataString();
            postData.openid           = userOpenID;

            postData.Sign();

            HttpContent postContent = new ByteArrayContent(postData.ToByteArray());
            var         response    = await _httpClient.PostAsync("unifiedorder", postContent);

            if (!response.IsSuccessStatusCode)
            {
                throw new WxPayResponseInvalidException();
            }

            var resContent = await response.Content.ReadAsStringAsync();

            var resData = WxPayData.FromXml(resContent, _configuration.APIKey);

            if (resData.IsReturnedValid)
            {
                if (resData.VerifySignature())
                {
                    return(OrderReceipt.FromWxPayData(resData));
                }
                else
                {
                    throw new WxPaySignatureInvalidException("PlaceOrder return error");
                }
            }
            else
            {
                throw new WxPayResponseInvalidException(resData.ReturnedError);
            }
        }