private string BuildRequest(TransmiParameterModel model, EnumWePayTradeType tradeType) { SortedDictionary <string, string> dicParam = CreateParam(model, tradeType); string signString = CreateURLParamString(dicParam); string key = tradeType == EnumWePayTradeType.APP ? WepayConfig.WEPAY_APP_KEY : WepayConfig.WEPAY_WEB_KEY; string preString = signString + "&key=" + key; string sign = MD5Helper.Sign(preString, WepayConfig.WEPAY_CHARTSET).ToUpper(); dicParam.Add("sign", sign); return(BuildForm(dicParam)); }
/// <summary> /// 统一下单 /// </summary> /// <returns></returns> private string UnifiedOrder(string orderNo, string productName, int totalFee, string customerIP, EnumWePayTradeType tradeType) { string requestXml = this.BuildRequest(orderNo, productName, totalFee, customerIP, tradeType); string resultXml = HTTPHelper.Post(WepayConfig.WEPAY_PAY_URL, requestXml); var dic = FromXml(resultXml); string returnCode = ""; dic.TryGetValue("return_code", out returnCode); if (returnCode == "SUCCESS") { if (tradeType == EnumWePayTradeType.APP) { var prepay_id = GetValueFromDic <string>(dic, "prepay_id"); if (!string.IsNullOrEmpty(prepay_id)) { return(BuildAppPay(prepay_id)); } else { throw new Exception("支付错误:" + GetValueFromDic <string>(dic, "err_code_des")); } } else if (tradeType == EnumWePayTradeType.NATIVE) { string codeUrl = ""; dic.TryGetValue("code_url", out codeUrl); if (!string.IsNullOrEmpty(codeUrl)) { return(codeUrl); } else { throw new Exception("未找到对应的二维码链接"); } } else { throw new Exception("JSAPI & WAP 未实现"); } } else { throw new Exception("后台统一下单失败"); } }
public string BuildWePay(string orderNo, string productName, int totalFee, string customerIP, EnumWePayTradeType tradeType) { return(this.UnifiedOrder(orderNo, productName, totalFee, customerIP, tradeType)); }
private static SortedDictionary <string, string> CreateParam(string orderNo, string productName, decimal totalFee, string customerIP, EnumWePayTradeType tradeType) { SortedDictionary <string, string> dic = new SortedDictionary <string, string>(); dic.Add("appid", tradeType == EnumWePayTradeType.APP ? WepayConfig.WEPAY_APP_APPID : WepayConfig.WEPAY_WEB_APPID); //账号ID dic.Add("mch_id", tradeType == EnumWePayTradeType.APP ? WepayConfig.WEPAY_APP_MCH_ID : WepayConfig.WEPAY_WEB_MCH_ID); //商户号 dic.Add("nonce_str", Guid.NewGuid().ToString().Replace("-", "")); //随机字符串 dic.Add("body", productName); //商品描述 dic.Add("out_trade_no", orderNo); //商户订单号 dic.Add("total_fee", totalFee.ToString()); //总金额 dic.Add("spbill_create_ip", customerIP); //终端IP dic.Add("notify_url", tradeType == EnumWePayTradeType.APP ? WepayConfig.WEPAY_APP_NOTIFY_URL : WepayConfig.WEPAY_WEB_NOTIFY_URL); //通知地址 dic.Add("trade_type", tradeType.ToString()); //交易类型 return(dic); }
private string BuildRequest(string orderNo, string productName, int totalFee, string customerIP, EnumWePayTradeType tradeType) { SortedDictionary <string, string> dicParam = CreateParam(orderNo, productName, totalFee, customerIP, tradeType); string signString = CreateURLParamString(dicParam); string key = tradeType == EnumWePayTradeType.APP ? WepayConfig.WEPAY_APP_KEY : WepayConfig.WEPAY_WEB_KEY; string preString = signString + "&key=" + key; string sign = MD5Helper.Sign(preString, WepayConfig.WEPAY_CHARTSET).ToUpper(); dicParam.Add("sign", sign); return(BuildForm(dicParam)); }
/// <summary> /// 统一下单 /// </summary> /// <returns></returns> private string UnifiedOrder(TransmiParameterModel model, EnumWePayTradeType tradeType) { string requestXml = this.BuildRequest(model, tradeType); string resultXml = HTTPHelper.Post(WepayConfig.WEPAY_PAY_URL, requestXml); var dic = FromXml(resultXml); string returnCode = ""; var resultCode = ""; dic.TryGetValue("result_code", out resultCode); dic.TryGetValue("return_code", out returnCode); if (returnCode == "SUCCESS" && resultCode == "SUCCESS") { if (tradeType == EnumWePayTradeType.APP) { var prepay_id = GetValueFromDic <string>(dic, "prepay_id"); if (!string.IsNullOrEmpty(prepay_id)) { return(BuildAppPay(prepay_id)); } else { throw new Exception("支付错误:" + GetValueFromDic <string>(dic, "err_code_des")); } } else if (tradeType == EnumWePayTradeType.NATIVE) { string codeUrl = ""; dic.TryGetValue("code_url", out codeUrl); if (!string.IsNullOrEmpty(codeUrl)) { return(codeUrl); } else { throw new Exception("未找到对应的二维码链接"); } } else if (tradeType == EnumWePayTradeType.JSAPI) { var prepay_id = GetValueFromDic <string>(dic, "prepay_id"); if (!string.IsNullOrEmpty(prepay_id)) { return(BuildWatPay(prepay_id)); } else { throw new Exception("支付错误:" + GetValueFromDic <string>(dic, "err_code_des")); } } else { throw new Exception("WAP 未实现"); } } else { throw new Exception("后台统一下单失败"); } }
public string BuildWePay(TransmiParameterModel model, EnumWePayTradeType tradeType) { return(this.UnifiedOrder(model, tradeType)); }
private static SortedDictionary <string, string> CreateParam(TransmiParameterModel model, EnumWePayTradeType tradeType) { SortedDictionary <string, string> dic = new SortedDictionary <string, string>(); dic.Add("appid", tradeType == EnumWePayTradeType.APP ? WepayConfig.WEPAY_APP_APPID : WepayConfig.WEPAY_WEB_APPID); //账号ID dic.Add("mch_id", tradeType == EnumWePayTradeType.APP ? WepayConfig.WEPAY_APP_MCH_ID : WepayConfig.WEPAY_WEB_MCH_ID); //商户号 dic.Add("nonce_str", Guid.NewGuid().ToString().Replace("-", "")); //随机字符串 dic.Add("body", model.productName); //商品描述 dic.Add("out_trade_no", model.orderNo); //商户订单号 dic.Add("total_fee", model.totalFee.ToString()); //总金额 dic.Add("spbill_create_ip", model.customerIP); //终端IP dic.Add("notify_url", tradeType == EnumWePayTradeType.APP ? WepayConfig.WEPAY_APP_NOTIFY_URL : WepayConfig.WEPAY_WEB_NOTIFY_URL); //通知地址 dic.Add("trade_type", tradeType.ToString()); //交易类型 if (tradeType.Equals(EnumWePayTradeType.JSAPI)) { dic.Add("openid", model.openId); } return(dic); }