Esempio n. 1
0
        /// <summary>
        /// 统一下单
        /// </summary>
        /// <param name="request">提交给统一下单API的参数</param>
        /// <param name="config"></param>
        /// <param name="timeOut">超时时间</param>
        /// <returns>成功时返回,其他抛异常</returns>
        public static async Task <WechatpayData> CreateOrderAsync(WechatpayData request, WechatpayConfig config, int timeOut = 6)
        {
            //检测必填参数
            if (!request.IsSet("out_trade_no"))
            {
                throw new Exception("缺少统一支付接口必填参数out_trade_no!");
            }
            else if (!request.IsSet("body"))
            {
                throw new Exception("缺少统一支付接口必填参数body!");
            }
            else if (!request.IsSet("total_fee"))
            {
                throw new Exception("缺少统一支付接口必填参数total_fee!");
            }
            else if (!request.IsSet("trade_type"))
            {
                throw new Exception("缺少统一支付接口必填参数trade_type!");
            }

            //关联参数
            if (request.GetValue("trade_type").ToString() == "JSAPI" && !request.IsSet("openid"))
            {
                throw new Exception("统一支付接口中,缺少必填参数openid!trade_type为JSAPI时,openid为必填参数!");
            }
            if (request.GetValue("trade_type").ToString() == "NATIVE" && !request.IsSet("product_id"))
            {
                throw new Exception("统一支付接口中,缺少必填参数product_id!trade_type为JSAPI时,product_id为必填参数!");
            }

            //异步通知url未设置,则使用配置文件中的url
            if (!request.IsSet("notify_url"))
            {
                throw new Exception("缺少统一支付接口必填参数notify_url");
            }

            var result = await HttpService.ExecuteAsync(request, config, WechatConstants.UnifiedOrderUrl, false, timeOut);

            var data = HttpService.GetAppData(config, result.GetValue("prepay_id"), request.GetValue("trade_type")?.ToString(), request.GetValue("code_url")?.ToString());

            result.SetValue("body", data.ToXml());
            return(result);
        }
Esempio n. 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="inputObj"></param>
        /// <param name="config"></param>
        /// <param name="url"></param>
        /// <param name="isUseCert"></param>
        /// <param name="timeout"></param>
        /// <returns></returns>
        public static async Task <WechatpayData> ExecuteAsync(WechatpayData inputObj, WechatpayConfig config, string url, bool isUseCert = false, int timeout = 6)
        {
            if (config == null || string.IsNullOrEmpty(config.AppId) || string.IsNullOrEmpty(config.MchId))
            {
                throw new Exception("收款账号配置不能为空");
            }
            if (string.IsNullOrEmpty(config.SignKey))
            {
                throw new Exception("密钥配置不能为空");
            }
            inputObj.SetValue("appid", config.AppId);                                      //公众账号ID
            inputObj.SetValue("mch_id", config.MchId);                                     //商户号
            inputObj.SetValue("nonce_str", GenerateNonceStr());                            //随机字符串
            inputObj.SetValue("sign_type", config.SignType);                               //签名类型
            inputObj.SetValue("sign", inputObj.MakeSign(config.SignType, config.SignKey)); //签名

            string response = await PostAsync(inputObj.ToXml(), url, isUseCert, timeout, config.CertPath, config.CertPassword);

            var result = new WechatpayData();

            //若接口调用失败会返回xml格式的结果
            if (response.Substring(0, 5) == "<xml>")
            {
                result.FromXml(response);
                if (result.GetValue("return_code").ToString() != WechatConstants.SUCCESS)
                {
                    throw new Exception(result.GetValue("return_msg").ToString());
                }
                //验证签名,不通过会抛异常
                result.CheckSign(config.SignType, config.SignKey);
            }
            //接口调用成功则返回非xml格式的数据
            else
            {
                result.SetValue("return_code", "SUCCESS");
                result.SetValue("return_msg", "");
                result.SetValue("result_code", "SUCCESS");
                result.SetValue("body", response);
            }

            return(result);
        }