/// <summary> /// 查询交易订单 /// </summary> /// <returns></returns> public WeiXinPayResponse OrderQuery(PayOrderSearchRequest request) { SortedDictionary <String, String> param = new SortedDictionary <String, String>(); String noncestr = CommonUtil.CreateNoncestr(); var mchId = isSubCommercial ? Config.SubPartnerId : Config.PartnerId; 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); if (!string.IsNullOrEmpty(request.TransactionID)) { param.Add("transaction_id", request.TransactionID); } if (!string.IsNullOrEmpty(request.OutTradeNO)) { param.Add("out_trade_no", request.OutTradeNO); } String tmpPackageStr = CommonUtil.CreateLinkString(param); String signValue = CommonUtil.Sign(tmpPackageStr, Config.PartnerKey); param.Add("sign", signValue); String tmpRes = HttpRequestUtil.Send("POST", OrderQueryUrl, CommonUtil.ArrayToXml(param)); return(ConvertToPayResponse(tmpRes)); }
/// <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)); }
/// <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 }); } }
/// <summary> /// Native支付时,微信支付平台调用本地服务,返回给微信支付平台的XML内容 /// </summary> /// <returns></returns> public string GetParametersXMLStr(string pRetCode = "0", string pRetErrorMessage = "") { string msg; if (!IsValid(out msg)) { throw new WeiXinPayException(msg); } Dictionary <string, object> dic = new Dictionary <string, object>(Paras); dic["appkey"] = Channel.PaySignKey; dic["retcode"] = pRetCode; dic["reterrmsg"] = pRetErrorMessage; var temp = dic.Where(t => t.Key.ToLower() != "sign_method" && t.Key.ToLower() != "app_signature").Select(t => t); var nosign = CommonUtil.GetParametersStr(temp); dic["app_signature"] = FormsAuthentication.HashPasswordForStoringInConfigFile(nosign, "SHA1").ToLower(); return(CommonUtil.ArrayToXml(dic)); }
/// <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)); }
/// <summary> /// Native支付时,微信支付平台调用本地服务,返回给微信支付平台的XML内容 /// </summary> /// <returns></returns> public string GetParametersXMLStr(string pRetCode = "0", string pRetErrorMessage = "") { string msg; if (!IsValid(out msg)) { throw new WeiXinPayException(msg); } Dictionary <string, object> tempDic = new Dictionary <string, object>(); tempDic.Add("AppId", AppId); tempDic.Add("Package", this.Package); tempDic.Add("TimeStamp", this.TimeStamp); tempDic.Add("RetCode", pRetCode); tempDic.Add("RetErrMsg", pRetErrorMessage); tempDic.Add("NonceStr", this.NonceStr); tempDic["appkey"] = Channel.PaySignKey; var nosign = CommonUtil.GetParametersStr(tempDic); tempDic.Add("AppSignature", CommonUtil.Sha1(nosign)); tempDic.Add("SignMethod", SignMethod); tempDic.Remove("appkey"); return(CommonUtil.ArrayToXml(tempDic)); }
public RedpackResponse SendRedpack(RedpackRequest 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'); } var mchId = isSubCommercial ? Config.SubPartnerId : Config.PartnerId; string billno = mchId + DateTime.Now.ToString("yyyyMMdd") + timeStamp; param.Add("nonce_str", noncestr); param.Add("mch_billno", billno); param.Add("mch_id", mchId); param.Add("wxappid", appid); param.Add("send_name", request.SendName); param.Add("re_openid", request.OpenId); param.Add("total_amount", (request.Amount * 100).ToString()); param.Add("total_num", "1"); param.Add("wishing", request.Wishing); param.Add("client_ip", request.ClientIP); param.Add("act_name", request.ActivityName); param.Add("remark", request.Remark); String tmpPackageStr = CommonUtil.CreateLinkString(param); String signValue = CommonUtil.Sign(tmpPackageStr, Config.PartnerKey); param.Add("sign", signValue); ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); X509Certificate cer = new X509Certificate(Config.CertFilePath, mchId, X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.MachineKeySet); String tmpRes = HttpRequestUtil.Send("POST", RedpackUrl, CommonUtil.ArrayToXml(param), 10, cer); var tmpResult = XmlToDic(tmpRes); if (tmpResult["return_code"] == Fail) { return(new RedpackResponse { RetCode = Fail, RetMsg = tmpResult["return_msg"] }); } if (tmpResult["result_code"] == Fail) { var errMsg = ""; if (!tmpResult.TryGetValue("err_code_des", out errMsg)) { errMsg = GetErrMsg(tmpResult["err_code"]); } return(new RedpackResponse { RetCode = Fail, RetMsg = errMsg }); } return(new RedpackResponse { SendListId = tmpResult["send_listid"] }); } catch (Exception ex) { return(new RedpackResponse { RetCode = Fail, RetMsg = ex.Message }); } }
public WeiXinRefundResponse Refund(WeiXinRefundRequest request) { SortedDictionary <String, String> param = new SortedDictionary <String, String>(); String noncestr = CommonUtil.CreateNoncestr(); 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); } param.Add("out_trade_no", request.OutTradeNO); param.Add("transaction_id", request.TransactionID); param.Add("out_refund_no", request.RefundNO); param.Add("total_fee", request.PayAmount.ToString()); param.Add("refund_fee", request.RefundPayAmount.ToString()); String tmpPackageStr = CommonUtil.CreateLinkString(param); String signValue = CommonUtil.Sign(tmpPackageStr, Config.PartnerKey).ToLower(); param.Add("sign", signValue); X509Certificate2 cert = new X509Certificate2(Config.CertFilePath, Config.PartnerId, X509KeyStorageFlags.MachineKeySet); String tmpRes = HttpRequestUtil.Send("POST", RefundUrl, CommonUtil.ArrayToXml(param), 60, cert); var tmpResult = XmlToDic(tmpRes); WeiXinRefundResponse res = new WeiXinRefundResponse(); if (tmpResult["return_code"] == Fail) { return(new WeiXinRefundResponse { RetCode = Fail, RetMsg = tmpResult["return_msg"] }); } var tmpSignResult = CommonUtil.FilterPara(tmpResult); if (!IsMD5Sign(tmpSignResult, tmpResult["sign"])) { return(new WeiXinRefundResponse { RetCode = Fail, RetMsg = "签名验证失败" }); } if (tmpResult["result_code"] == Fail) { var errMsg = ""; if (!tmpResult.TryGetValue("err_code_des", out errMsg)) { errMsg = GetErrMsg(tmpResult["err_code"]); } return(new WeiXinRefundResponse { RetCode = Fail, RetMsg = errMsg }); } return(new WeiXinRefundResponse { RetCode = "0", RefundTime = DateTime.Now, OrderNO = tmpResult["out_trade_no"], TransactionID = tmpResult["transaction_id"], RefundNO = tmpResult["out_refund_no"], RefundId = tmpResult["refund_id"], RefundAmount = decimal.Parse(tmpResult["refund_fee"]) / 100 }); }