Exemplo n.º 1
0
        /// <summary>
        /// 创建微信支付的package
        /// </summary>
        /// <param name="request">支付请求实例</param>
        /// <returns></returns>
        private String CreatePackage(WeiXinPayRequest request)
        {
            SortedDictionary <String, String> param = CreatePayParams(request);
            String tmpPackageStr = CommonUtil.CreateLinkString(param);
            String signValue     = CommonUtil.Sign(tmpPackageStr, Config.PartnerKey);

            param.Add("sign", signValue);

            return(CommonUtil.ArrayToXml(param));
        }
Exemplo n.º 2
0
        /// <summary>
        /// 生成js api支付所需的参数或者扫码支付的url
        /// </summary>
        /// <param name="request">支付请求实例</param>
        /// <returns></returns>
        public String PayAction(WeiXinPayRequest request)
        {
            try
            {
                String payparams = CreatePackage(request);
                if (request.Trade_type.Equals(WXTradeType.PRENATIVE))
                {
                    var tmpNative1Params = XmlToDic(payparams);
                    return(String.Format(Native1UrlFormat, tmpNative1Params["sign"], appid, Config.PartnerId, request.Productid, tmpNative1Params["time_stamp"], tmpNative1Params["nonce_str"]));
                }


                String res       = HttpRequestUtil.Send("POST", PayUrl, payparams);
                var    tmpResult = XmlToDic(res);
                if (tmpResult["return_code"] == Fail)
                {
                    return(tmpResult["return_msg"]);
                }

                var tmpSignResult = CommonUtil.FilterPara(tmpResult);
                if (!IsMD5Sign(tmpSignResult, tmpResult["sign"]))
                {
                    return("签名验证失败");
                }

                if (tmpResult["result_code"] == Fail)
                {
                    var errMsg = "";
                    if (!tmpResult.TryGetValue("err_code_des", out errMsg))
                    {
                        errMsg = GetErrMsg(tmpResult["err_code"]);
                    }

                    return(errMsg);
                }

                if (tmpResult["trade_type"].Equals(WXTradeType.JSAPI.ToString(), StringComparison.OrdinalIgnoreCase))
                {
                    return(JSApiPay(tmpResult["prepay_id"], request.IsMiniAppPay));
                }

                if (tmpResult["trade_type"].Equals(WXTradeType.NATIVE.ToString(), StringComparison.OrdinalIgnoreCase))
                {
                    return(NativePay(tmpResult["prepay_id"], tmpResult["code_url"]));
                }

                return("提交参数错误");
            }
            catch
            {
                return("提交参数错误");
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// 刷卡支付
        /// </summary>
        /// <param name="request"></param>
        public WeiXinPayResponse Micropay(WeiXinPayRequest request)
        {
            try
            {
                SortedDictionary <String, String> param = new SortedDictionary <String, String>();
                String noncestr  = CommonUtil.CreateNoncestr();
                String timeStamp = CommonUtil.GetTimestamp().ToString();
                if (timeStamp.Length >= 10)
                {
                    timeStamp = timeStamp.Substring(timeStamp.Length - 10);
                }
                else
                {
                    timeStamp = timeStamp.PadLeft(10, '0');
                }
                param.Add("appid", appid);
                if (isSubCommercial)
                {
                    if (Config.IsMiniAppPay)
                    {
                        param.Add("sub_appid", Config.AppId);
                    }
                    param.Add("sub_mch_id", Config.SubPartnerId);
                }
                param.Add("mch_id", Config.PartnerId);
                param.Add("nonce_str", noncestr);
                param.Add("body", request.ProductDesc);
                param.Add("out_trade_no", request.OutTradeNO);
                param.Add("total_fee", request.PayAmount.ToString());
                if (!string.IsNullOrEmpty(request.Attach))
                {
                    param.Add("attach", request.Attach);
                }
                param.Add("spbill_create_ip", request.ClientIP);
                param.Add("auth_code", request.AuthCode);
                param.Add("sign_type", CommonUtil.SIGN_TYPE_MD5);//签名类型
                String tmpPackageStr = CommonUtil.CreateLinkString(param);
                String signValue     = CommonUtil.Sign(tmpPackageStr, Config.PartnerKey);
                param.Add("sign", signValue);
                String tmpRes = HttpRequestUtil.Send("POST", MicropayUrl, CommonUtil.ArrayToXml(param), 600);

                return(ConvertToPayResponse(tmpRes));
            }
            catch (Exception ex)
            {
                return(new WeiXinPayResponse
                {
                    RetCode = Fail,
                    RetMsg = ex.Message
                });
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// 扫码支付返回给支付公司信息
        /// </summary>
        /// <param name="request">支付请求实例</param>
        /// <param name="retCode">返回值,SUCCESS成功,其他已retmsg为准</param>
        /// <param name="retMsg">错误提示信息</param>
        /// <returns></returns>
        public String NativeCallbackResponse(WeiXinPayRequest request, String retCode, String retMsg)
        {
            SortedDictionary <String, String> tmpParams = new SortedDictionary <String, String>();

            tmpParams.Add("return_code", retCode);
            tmpParams.Add("return_msg", retMsg);

            if (retCode == Fail)
            {
                return(CommonUtil.ArrayToXml(tmpParams));
            }

            request.Trade_type = WXTradeType.NATIVE;
            var tmpNativeRes = PayAction(request);

            if (!tmpNativeRes.Contains("prepay_id"))
            {
                tmpParams["return_code"] = Fail;
                tmpParams["return_msg"]  = tmpNativeRes;
                return(CommonUtil.ArrayToXml(tmpParams));
            }

            String noncestr = CommonUtil.CreateNoncestr();
            var    native   = JsonConvert.DeserializeObject <NativeRes>(tmpNativeRes);

            tmpParams.Add("appid", appid);
            tmpParams.Add("mch_id", Config.PartnerId);
            tmpParams.Add("nonce_str", noncestr);
            tmpParams.Add("prepay_id", native.prepay_id);
            tmpParams.Add("result_code", retCode);
            tmpParams.Add("err_code_des", retMsg);

            string sign = CreatePaySign(tmpParams);

            tmpParams.Add("sign", sign);

            return(CommonUtil.ArrayToXml(tmpParams));
        }
Exemplo n.º 5
0
        private SortedDictionary <String, String> CreatePayParams(WeiXinPayRequest request)
        {
            SortedDictionary <String, String> param = new SortedDictionary <String, String>();
            String noncestr  = CommonUtil.CreateNoncestr();
            String timestamp = CommonUtil.GetTimestamp().ToString();

            param.Add("appid", appid);
            param.Add("mch_id", Config.PartnerId);
            param.Add("nonce_str", noncestr);
            if (isSubCommercial)
            {
                if (Config.IsMiniAppPay)
                {
                    param.Add("sub_appid", Config.AppId);
                }
                param.Add("sub_mch_id", Config.SubPartnerId);
            }

            switch (request.Trade_type)
            {
            case WXTradeType.JSAPI:
                if (isSubCommercial && Config.IsMiniAppPay)
                {
                    param.Add("sub_openid", request.OpenId);
                }
                else
                {
                    param.Add("openid", request.OpenId);
                }
                break;

            case WXTradeType.PRENATIVE:
                param.Add("time_stamp", timestamp);
                param.Add("product_id", request.Productid);
                break;

            case WXTradeType.NATIVE:
                param.Add("product_id", request.Productid);
                break;
            }

            if (!request.Trade_type.Equals(WXTradeType.PRENATIVE))
            {
                param.Add("body", request.ProductDesc);
                param.Add("out_trade_no", request.OutTradeNO);
                param.Add("total_fee", request.PayAmount.ToString());
                param.Add("spbill_create_ip", request.ClientIP);
                param.Add("notify_url", request.NotifyUrl);
                param.Add("trade_type", request.Trade_type.ToString());

                if (!String.IsNullOrEmpty(request.Attach))
                {
                    param.Add("attach", request.Attach);
                }
                if (request.StartTime.HasValue && request.ExpireTime.HasValue)
                {
                    param.Add("time_start", request.StartTime.Value.ToString("yyyyMMddHHmmss"));
                    param.Add("time_expire", request.ExpireTime.Value.ToString("yyyyMMddHHmmss"));
                }
            }

            return(param);
        }
Exemplo n.º 6
0
        public override PayResponse Micropay(PayRequest request)
        {
            WeiXinPayRequest tmpRequest = request as WeiXinPayRequest;

            return(weixin.Micropay(tmpRequest));
        }
Exemplo n.º 7
0
        public override string NativeCallbackResponse(PayRequest request, string retCode, string retMsg)
        {
            WeiXinPayRequest tmp = request as WeiXinPayRequest;

            return(weixin.NativeCallbackResponse(tmp, retCode, retMsg));
        }
Exemplo n.º 8
0
        public override string PayAction(PayRequest request)
        {
            WeiXinPayRequest tmpRequest = request as WeiXinPayRequest;

            return(weixin.PayAction(tmpRequest));
        }