/// <summary> /// 处理支付平台通知 /// </summary> /// <param name="param">支付平台通知参数</param> /// <returns></returns> public override Result <Notification> HandleNotify(PaymentParam param) { var now = DateTime.Now; var input = param.ToXml(); var output = GetOutputXml("FAIL", "FAIL"); var notification = new Notification(Platform.Id, input, output); var result = new Result <Notification>(false, notification); OnTraced("微信支付 通知数据", input); if (param.GetString("return_code") == "SUCCESS" && param.GetString("result_code") != "SUCCESS") { if (VerifySign(param)) { result.Data.PayId = param.GetString("out_trade_no"); //商户订单号 result.Data.PayNo = param.GetString("transaction_id"); //微信支付订单号 result.Data.Amount = param.GetInt("total_fee") / 100m; //订单总金额,单位为分 result.Data.PaidOn = now; //支付完成时间 微信返回的格式不好处理 time_end 格式为yyyyMMddHHmmss 如2009年12月25日9点10分10秒表示为20091225091010 result.Data.State = param.GetString("attach"); //支付平台原样返回 var status = OnNotified(notification); if (status) { result.Status = true; result.Data.SetOutput(GetOutputXml("SUCCESS", "OK")); OnTraced("微信支付 处理通知成功", result.Data.PayId); } else { result.Message = "微信支付 处理通知失败 OnNotified 返回 false"; OnTraced("微信支付 处理通知失败", "OnNotified 返回 false"); } } else { var err = param.GetString("err_code_des"); var msg = string.IsNullOrEmpty(err) ? param.GetString("return_msg") : err; result.Message = "微信支付 处理通知失败 验证返回数据签名失败 " + msg; OnTraced("微信支付 处理通知失败 验证返回数据签名失败", msg); } } else { var msg = param.GetString("return_msg"); result.Message = "微信支付 处理通知失败 接口返回数据信息 " + msg; OnTraced("微信支付 处理通知失败 接口返回数据信息", msg); } return(result); }
/// <summary> /// 处理支付平台通知 /// 支付宝是用POST方式发送通知信息 /// </summary> /// <param name="param">支付平台通知参数</param> /// <returns></returns> public override Result <Notification> HandleNotify(PaymentParam param) { var now = DateTime.Now; var input = param.ToXml(); var notification = new Notification(Platform.Id, input, "fail"); var result = new Result <Notification>(false, notification); OnTraced("支付宝支付 通知数据", input); var isValid = VerifySign(param); if (isValid) { result.Data.PayId = param.GetString("out_trade_no"); //商户订单号 result.Data.PayNo = param.GetString("trade_no"); //支付宝交易号 result.Data.Amount = param.GetDecimal("total_fee"); //支付金额 result.Data.PaidOn = param.GetDateTime("gmt_payment", now); //支付时间 result.Data.State = param.GetString("extra_common_param"); //扩展字段 支付平台原样返回 var tradeStatus = param.GetString("trade_status"); //交易状态 if (tradeStatus == "TRADE_FINISHED" || tradeStatus == "TRADE_SUCCESS") { var status = OnNotified(notification); if (status) { result.Status = true; result.Data.SetOutput("success"); OnTraced("支付宝支付 处理通知成功", result.Data.PayId); } else { result.Message = "支付宝支付 处理通知失败 OnNotified 返回 false"; OnTraced("支付宝支付 处理通知失败", "OnNotified 返回 false"); } } else { result.Message = "支付宝支付 处理通知失败 交易状态错误 " + tradeStatus; OnTraced("支付宝支付 处理通知失败 交易状态错误", tradeStatus); } } else { result.Message = "支付宝支付 处理通知失败 签名或通知验证失败"; OnTraced("支付宝支付 处理通知失败 处理通知失败 签名或通知验证失败", string.Empty); } return(result); }
/// <summary> /// 处理支付平台通知 /// </summary> /// <param name="param">第三方支付平台通知参数</param> /// <returns></returns> public override Result <Notification> HandleNotify(PaymentParam param) { var now = DateTime.Now; var input = param.ToXml(); var notification = new Notification(Platform.Id, input, "fail"); var result = new Result <Notification>(false, notification); var isValid = VerifySign(param); if (isValid) { var reqParam = new PaymentParam(); var notifyId = param.GetString("notify_id"); // 通知id reqParam.Add("notify_id", notifyId); reqParam.Add("partner", Platform.MerchantId); reqParam.Add("sign", CreateSign(reqParam)); //系统可选参数 reqParam.Add("sign_type", "MD5"); reqParam.Add("service_version", "1.0"); reqParam.Add("input_charset", "UTF-8"); reqParam.Add("sign_key_index", "1"); var url = "https://gw.tenpay.com/gateway/verifynotifyid.xml" + reqParam.ToQueryString(true); var xml = XElement.Load(url); var notifyParam = new PaymentParam(xml); result.Data.PayId = notifyParam.GetString("out_trade_no"); //系统支付单号 result.Data.PayNo = notifyParam.GetString("transaction_id"); // 财付通交易号,28位长的数值,其中前10位为商户号,之后8位为订单产生的日期; result.Data.Amount = notifyParam.GetInt("total_fee") / 100M; // 转换为元为单位; result.Data.PaidOn = notifyParam.GetDateTime("time_end", now); result.Data.State = param.GetString("attach"); //扩展字段 支付平台原样返回 var bankOrderNo = notifyParam.GetString("bank_billno"); // 银行订单号,若为财付通余额支付则为空 if (VerifySign(notifyParam) && notifyParam["retcode"] == "0" && notifyParam["trade_state"] == "0" && notifyParam["trade_mode"] == "1") { var status = OnNotified(notification); if (status) { result.Status = true; result.Data.SetOutput("success"); } else { result.Message = "TenPay OnNotified 返回false"; } } else { result.Message = "TenPay 通知验证失败"; } } else { result.Message = "TenPay 签名验证失败"; } return(result); }
private string Pay(Payment payment, string tradeType, string openId) { var param = new PaymentParam(); param.Add("appid", Platform.PublicKey); //微信分配的公众账号ID(企业号corpid即为此appId) param.Add("mch_id", Platform.MerchantId); //微信支付分配的商户号 param.Add("nonce_str", GenerateNonceStr()); //随机字符串,不长于32位 param.Add("body", payment.Subject); //商品简单描述 该字段须严格按照规范传递 param.Add("attach", payment.State); //附加数据 在查询API和支付通知中原样返回 该字段主要用于商户携带订单的自定义数据 param.Add("out_trade_no", payment.Id); //商户系统内部的订单号,32个字符内 可包含字母 param.Add("total_fee", (payment.Amount * 100).ToString("F0")); //订单总金额,单位为分 不能带小数 param.Add("spbill_create_ip", payment.IpAddress); //APP和网页支付提交用户端ip Native支付填调用微信支付API的机器IP param.Add("notify_url", Platform.NotifyUrl); param.Add("trade_type", tradeType); if (tradeType == "NATIVE") { param.Add("product_id", payment.Id);// trade_type = NATIVE 时 此参数必传 此id为二维码中包含的商品ID,商户自行定义 } if (tradeType == "JSAPI") { param.Add("openid", openId);// rade_type=JSAPI 时此参数必传 用户在商户appid下的唯一标识 } param.Add("sign", CreateSign(param)); PaymentParam data;//支付接口返回参数 using (Client) { var xml = param.ToXml();//微信接口接受xml格式的数据 OnTraced("微信支付 提交数据", xml); var hc = new StringContent(xml, Encoding.UTF8, "text/xml"); var rep = Client.PostAsync(Platform.GatewayUrl, hc).Result; var repXml = XElement.Load(rep.Content.ReadAsStreamAsync().Result); OnTraced("微信支付 返回数据", repXml.ToString()); data = new PaymentParam(repXml); } var s = string.Empty; if (data.GetString("return_code") == "SUCCESS" && data.GetString("result_code") == "SUCCESS") { if (VerifySign(data)) { if (tradeType == "APP") { var prePayParam = new PaymentParam(); var prePayId = data.GetString("prepay_id"); prePayParam.Add("appId", Platform.PublicKey); prePayParam.Add("partnerId", Platform.MerchantId); prePayParam.Add("prepayId", prePayId); prePayParam.Add("package", "Sign=WXPay"); prePayParam.Add("nonceStr", GenerateNonceStr()); prePayParam.Add("timeStamp", GenerateTimeStamp()); prePayParam.Add("sign", CreateSign(prePayParam)); s = prePayParam.ToJson(); } else if (tradeType == "JSAPI") { var prePayParam = new PaymentParam(); var prePayId = data.GetString("prepay_id"); prePayParam.Add("appId", Platform.PublicKey); prePayParam.Add("timeStamp", GenerateTimeStamp()); prePayParam.Add("nonceStr", GenerateNonceStr()); prePayParam.Add("package", "prepay_id=" + prePayId); prePayParam.Add("signType", "MD5"); prePayParam.Add("paySign", CreateSign(prePayParam)); s = prePayParam.ToJson(); } else if (tradeType == "NATIVE") { s = data.GetString("code_url");//用于生成微信客户端可识别的支付URL } } else { var err = data.GetString("err_code_des"); var msg = string.IsNullOrEmpty(err) ? data.GetString("return_msg") : err; OnTraced("微信支付 验证返回数据签名失败", msg); } } return(s); }