/// <summary> /// 支付成功回调 /// </summary> /// <param name="request"></param> /// <param name="config"></param> /// <returns></returns> public static WechatNotifyResponse Notify(WechatpayData request, WechatpayConfig config) { if (config == null || string.IsNullOrEmpty(config.AppId)) { throw new Exception("收款账号不能为空"); } //验证签名,不通过会抛异常 if (!request.CheckSign(config.SignType, config.SignKey)) { throw new Exception("签名校验失败"); } var result = request.ToObject <WechatNotifyResponse>(); if (result.ReturnCode != WechatConstants.SUCCESS && result.ResultCode != WechatConstants.SUCCESS) { throw new Exception(result.ReturnMsg); } return(result); }
/// <summary> /// /// </summary> /// <param name="inputObj"></param> /// <param name="config"></param> /// <param name="url"></param> /// <param name="isUseCert"></param> /// <param name="timeout"></param> /// <returns></returns> public static async Task <WechatpayData> ExecuteAsync(WechatpayData inputObj, WechatpayConfig config, string url, bool isUseCert = false, int timeout = 6) { if (config == null || string.IsNullOrEmpty(config.AppId) || string.IsNullOrEmpty(config.MchId)) { throw new Exception("收款账号配置不能为空"); } if (string.IsNullOrEmpty(config.SignKey)) { throw new Exception("密钥配置不能为空"); } inputObj.SetValue("appid", config.AppId); //公众账号ID inputObj.SetValue("mch_id", config.MchId); //商户号 inputObj.SetValue("nonce_str", GenerateNonceStr()); //随机字符串 inputObj.SetValue("sign_type", config.SignType); //签名类型 inputObj.SetValue("sign", inputObj.MakeSign(config.SignType, config.SignKey)); //签名 string response = await PostAsync(inputObj.ToXml(), url, isUseCert, timeout, config.CertPath, config.CertPassword); var result = new WechatpayData(); //若接口调用失败会返回xml格式的结果 if (response.Substring(0, 5) == "<xml>") { result.FromXml(response); if (result.GetValue("return_code").ToString() != WechatConstants.SUCCESS) { throw new Exception(result.GetValue("return_msg").ToString()); } //验证签名,不通过会抛异常 result.CheckSign(config.SignType, config.SignKey); } //接口调用成功则返回非xml格式的数据 else { result.SetValue("return_code", "SUCCESS"); result.SetValue("return_msg", ""); result.SetValue("result_code", "SUCCESS"); result.SetValue("body", response); } return(result); }