Пример #1
0
        /// <summary>
        /// 统一下单功能,支持除付款码支付场景以外的预支付交易单生成。
        /// </summary>
        public async Task <XmlDocument> UnifiedOrderAsync(string appId, string mchId, string body, string orderNo, int totalFee,
                                                          string tradeType,
                                                          string openId = null)
        {
            var request = new WeChatPayParameters();

            request.AddParameter("appid", appId);
            request.AddParameter("mch_id", mchId);
            request.AddParameter("nonce_str", RandomHelper.GetRandom());
            request.AddParameter("body", body);
            request.AddParameter("out_trade_no", orderNo);
            request.AddParameter("total_fee", totalFee);
            request.AddParameter("spbill_create_ip", "127.0.0.1");
            request.AddParameter("notify_url", _abpWeChatPayOptions.NotifyUrl);
            request.AddParameter("openid", openId);
            request.AddParameter("trade_type", tradeType);

            var signStr = SignatureGenerator.Generate(request, MD5.Create(), _abpWeChatPayOptions.ApiKey);

            request.AddParameter("sign", signStr);

            var result = await WeChatPayApiRequester.RequestAsync(TargetUrl, request.ToXmlStr());

            if (result.SelectSingleNode("/xml/err_code") != null ||
                result.SelectSingleNode("/xml/return_code")?.InnerText != "SUCCESS" ||
                result.SelectSingleNode("/xml/return_msg")?.InnerText != "OK")
            {
                throw new UserFriendlyException($"调用微信支付接口失败。");
            }

            return(result);
        }
Пример #2
0
        protected virtual async Task <XmlDocument> RequestAndGetReturnValueAsync(string targetUrl, WeChatPayParameters requestParameters)
        {
            var result = await WeChatPayApiRequester.RequestAsync(targetUrl, requestParameters.ToXmlStr());

            if (result.SelectSingleNode("/xml/err_code") != null ||
                result.SelectSingleNode("/xml/return_code")?.InnerText != "SUCCESS" ||
                result.SelectSingleNode("/xml/return_msg")?.InnerText != "OK")
            {
                throw new UserFriendlyException($"调用微信支付接口失败,具体失败原因:{result.SelectSingleNode("/xml/err_code_des")?.InnerText}");
            }

            return(result);
        }
Пример #3
0
        protected virtual async Task <XmlDocument> RequestAndGetReturnValueAsync(string targetUrl, WeChatPayParameters requestParameters)
        {
            var result = await WeChatPayApiRequester.RequestAsync(targetUrl, requestParameters.ToXmlStr());

            if (result.SelectSingleNode("/xml/return_code")?.InnerText != "SUCCESS" ||
                result.SelectSingleNode("/xml/return_msg")?.InnerText != "OK")
            {
                var errMsg = $"微信支付接口调用失败,具体失败原因:{result.SelectSingleNode("/xml/err_code_des")?.InnerText ?? result.SelectSingleNode("/xml/return_msg")?.InnerText}";
                Logger.Log(LogLevel.Error, errMsg, targetUrl, requestParameters);

                var exception = new CallWeChatPayApiException(errMsg);
                exception.Data.Add(nameof(targetUrl), targetUrl);
                exception.Data.Add(nameof(requestParameters), requestParameters);

                throw exception;
            }

            return(result);
        }