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)); }
/// <summary> /// 查询退款 /// </summary> /// <remarks>提交退款申请后,通过该接口查询退款状态。退款有一定延时,用零钱支付的退款20分钟内到账,银行卡支付的退款3个工作日后重新查询退款状态。 out_refund_no、out_trade_no、transaction_id、refund_id四个参数必填一个</remarks> /// <param name="inputObj">提交给查询退款API的参数</param> /// <param name="timeOut"> 接口超时时间</param> /// <returns></returns> internal WeChatPayData RefundQuery(WeChatPayData inputObj, int timeOut = 6) { string url = payHost + "/pay/refundquery"; //检测必填参数 if (!inputObj.IsSet("out_refund_no") && !inputObj.IsSet("out_trade_no") && !inputObj.IsSet("transaction_id") && !inputObj.IsSet("refund_id")) { throw new WeChatException("退款查询接口中,out_refund_no、out_trade_no、transaction_id、refund_id四个参数必填一个!"); } inputObj.SetValue("appid", option.AppId); //公众账号ID inputObj.SetValue("mch_id", option.MchID); //商户号 inputObj.SetValue("nonce_str", Guid.NewGuid().ToString("N").ToLower()); //随机字符串 inputObj.SetValue("sign_type", WeChatPayData.SIGN_TYPE_MD5); //签名类型 inputObj.SetValue("sign", inputObj.MakeSign(option)); //签名 string xml = inputObj.ToXml(); logger.LogDebug("WxPayApi", "RefundQuery request : " + xml); string response = httpService.Post(xml, url, false, timeOut);//调用HTTP通信接口以提交数据到API logger.LogDebug("WxPayApi", "RefundQuery response : " + response); //将xml格式的结果转换为对象以返回 WeChatPayData result = new WeChatPayData(); result.FromXml(response); return(result); }
/// <summary> /// 撤销订单API接口 /// </summary> /// <param name="inputObj"> 提交给撤销订单API接口的参数,out_trade_no和transaction_id必填一个</param> /// <param name="timeOut">接口超时时间</param> /// <returns></returns> internal WeChatPayData Reverse(WeChatPayData inputObj, int timeOut = 6) { string url = payHost + "/secapi/pay/reverse"; //检测必填参数 if (!inputObj.IsSet("out_trade_no") && !inputObj.IsSet("transaction_id")) { throw new WeChatException("撤销订单API接口中,参数out_trade_no和transaction_id必须填写一个!"); } inputObj.SetValue("appid", option.AppId); //公众账号ID inputObj.SetValue("mch_id", option.MchID); //商户号 inputObj.SetValue("nonce_str", Guid.NewGuid().ToString("N").ToLower()); //随机字符串 inputObj.SetValue("sign_type", "MD5"); //签名类型 inputObj.SetValue("sign", inputObj.MakeSign(option)); //签名 string xml = inputObj.ToXml(); logger.LogDebug("WxPayApi", "Reverse request : " + xml); string response = httpService.Post(xml, url, true, timeOut); logger.LogDebug("WxPayApi", "Reverse response : " + response); WeChatPayData result = new WeChatPayData(); result.FromXml(response); return(result); }
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); }
/// <summary> /// 关闭订单 /// </summary> /// <param name="inputObj">提交给关闭订单API的参数</param> /// <param name="timeOut">接口超时时间</param> /// <returns></returns> internal WeChatPayData CloseOrder(WeChatPayData inputObj, int timeOut = 6) { string url = payHost + "/pay/closeorder"; //检测必填参数 if (!inputObj.IsSet("out_trade_no")) { throw new WeChatException("关闭订单接口中,out_trade_no必填!"); } inputObj.SetValue("appid", option.AppId); //公众账号ID inputObj.SetValue("mch_id", option.MchID); //商户号 inputObj.SetValue("nonce_str", Guid.NewGuid().ToString("N").ToLower()); //随机字符串 inputObj.SetValue("sign_type", WeChatPayData.SIGN_TYPE_MD5); //签名类型 inputObj.SetValue("sign", inputObj.MakeSign(option)); //签名 string xml = inputObj.ToXml(); string response = httpService.Post(xml, url, false, timeOut); WeChatPayData result = new WeChatPayData(); result.FromXml(response); return(result); }