public string MakeSign(WeChatPayOptions option) { if (option.IsDebug) { string url = "https://api.mch.weixin.qq.com/sandboxnew/pay/getsignkey"; WeChatPayData signkeyPayData = new WeChatPayData(); signkeyPayData.SetValue("mch_id", option.MchID); //商户号 signkeyPayData.SetValue("nonce_str", Guid.NewGuid().ToString("N").ToLower()); //随机字符串 signkeyPayData.SetValue("sign", signkeyPayData.MakeSign("MD5", option.Key)); string response = (new HttpService(option)).Post(signkeyPayData.ToXml(), url, false, 6); //调用HTTP通信接口提交数据 WeChatPayData result = new WeChatPayData(); result.FromXml(response); if (result.GetValue("return_code")?.ToString() == "SUCCESS") { var key = result.GetValue("sandbox_signkey")?.ToString(); return(MakeSign(key)); } throw new WeChatPayException("获取沙盒签名失败" + result.ToJson()); } if ((this.GetValue("signType") ?? this.GetValue("sign_type"))?.ToString() != "MD5") { return(MakeSign(SIGN_TYPE_HMAC_SHA256, option.Key)); } return(MakeSign(option.Key)); }
internal WeChatPayData UnifiedOrder(WeChatPayData inputObj, int timeOut = 6) { logger.LogInformation($"下单地址:{payHost}/pay/unifiedorder"); string url = payHost + "/pay/unifiedorder"; //检测必填参数 if (!inputObj.IsSet("out_trade_no")) { throw new WeChatPayException("缺少统一支付接口必填参数out_trade_no!"); } else if (!inputObj.IsSet("body")) { throw new WeChatPayException("缺少统一支付接口必填参数body!"); } else if (!inputObj.IsSet("total_fee")) { throw new WeChatPayException("缺少统一支付接口必填参数total_fee!"); } else if (!inputObj.IsSet("trade_type")) { throw new WeChatPayException("缺少统一支付接口必填参数trade_type!"); } //关联参数 if (inputObj.GetValue("trade_type").ToString() == "JSAPI" && !inputObj.IsSet("openid")) { throw new WeChatPayException("统一支付接口中,缺少必填参数openid!trade_type为JSAPI时,openid为必填参数!"); } if (inputObj.GetValue("trade_type").ToString() == "NATIVE" && !inputObj.IsSet("product_id")) { throw new WeChatPayException("统一支付接口中,缺少必填参数product_id!trade_type为JSAPI时,product_id为必填参数!"); } //异步通知url未设置,则使用配置文件中的url if (!inputObj.IsSet("notify_url")) { throw new WeChatPayException("统一支付接口中,缺少必填参数notify_url!trade_type为JSAPI时,notify_url为必填参数!"); } logger.LogDebug(inputObj.ToJson()); string xml = inputObj.ToXml(); logger.LogDebug("统一下单请求参数 : " + xml); string response = httpService.Post(xml, url, false, timeOut); logger.LogDebug("统一下单返回结果: " + response); WeChatPayData result = new WeChatPayData(); result.FromXml(response); if (!result.IsSet("appid") || !result.IsSet("prepay_id") || result.GetValue("prepay_id").ToString() == "") { throw new WeChatPayException("统一下单下单失败"); } return(result); }
private string GetPayParapeters(WeChatPayData unifiedOrder) { WeChatPayData jsApiParam = new WeChatPayData(); jsApiParam.SetValue("appId", option.AppId); jsApiParam.SetValue("timeStamp", weChatPayApi.GenerateTimeStamp()); jsApiParam.SetValue("nonceStr", Guid.NewGuid().ToString("N").ToLower()); jsApiParam.SetValue("package", "prepay_id=" + unifiedOrder.GetValue("prepay_id")); jsApiParam.SetValue("signType", "MD5"); jsApiParam.SetValue("paySign", jsApiParam.MakeSign(option)); string parameters = jsApiParam.ToJson(); logger.LogDebug("Get jsApiParam : " + parameters); return(parameters); }