1.下载官方工具类
2.解压复制 lib,business 文件夹到自己的项![这两个文件夹](https://i-blog.csdnimg.cn/blog_migrate/24c9b8dab0b1716ab93cd3312574275a.png)
3.后端进行配置
-
修改 lib=》DemoConfig 基本信息
-
配置二维码的参数 business=》NativePay.cs
3. 返回链接生成二维码返回前端
安装生成二维码的包ThoughtWorks.QRCode
生成二维码两种方式,流,图片
//示例一 public FileResult paymentInformation(){ string pay = WxPayAPI.NativePay.GetPayUrl(out_trade_no,productId,total_fee, body, attach, goods_tag); //初始化二维码生成工具 QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(); qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE; qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M; qrCodeEncoder.QRCodeVersion = 0; qrCodeEncoder.QRCodeScale = 4; //将字符串生成二维码图片 pay接口返回的url Bitmap image = qrCodeEncoder.Encode(pay, Encoding.Default); //保存为PNG到内存流 MemoryStream ms = new MemoryStream(); image.Save(ms, ImageFormat.Png); //保存为PNG到内存流 return File(ms.ToArray(), "image/png"); }
//示例二 public void paymentInformation(){ string pay = WxPayAPI.NativePay.GetPayUrl(out_trade_no,productId,total_fee, body, attach, goods_tag); //初始化二维码生成工具 QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(); qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE; qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M; qrCodeEncoder.QRCodeVersion = 0; qrCodeEncoder.QRCodeScale = 4; //将字符串生成二维码图片 Bitmap image = qrCodeEncoder.Encode(pay, Encoding.Default); //保存为PNG到内存流 MemoryStream ms = new MemoryStream(); image.Save(ms, ImageFormat.Png); //输出二维码图片 Response.BinaryWrite(ms.GetBuffer()); Response.End(); }
4.扫码支付回调
public async Task<string> PayNotifyUrl()
{
try
{
//接收从微信后台POST过来的数据
Stream s = Request.InputStream;
byte[] buffer = new byte[Request.InputStream.Length];
await s.ReadAsync(buffer, 0, buffer.Length); /
string xml = System.Text.Encoding.UTF8.GetString(buffer);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);
string return_code = xmlDoc.DocumentElement.GetElementsByTagName("return_code")[0].InnerText;
string out_trade_no = xmlDoc.DocumentElement.GetElementsByTagName("out_trade_no")[0].InnerText;//商户订单号
string transaction_id = xmlDoc.DocumentElement.GetElementsByTagName("transaction_id")[0].InnerText;//微信支付订单号
string nonce_str = xmlDoc.DocumentElement.GetElementsByTagName("nonce_str")[0].InnerText;//随机字符串
string total_fee = xmlDoc.DocumentElement.GetElementsByTagName("total_fee")[0].InnerText; //金额
string attach = xmlDoc.DocumentElement.GetElementsByTagName("attach")[0].InnerText;
//业务逻辑
//微信支付成功回调
if (return_code.ToUpper() == "SUCCESS")
{
return "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
}
else
{
return "<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[ERROR]]></return_msg></xml>"; //回调失败返回给微信
}
}
catch (Exception)
{
return "<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[ERROR]]></return_msg></xml>"; //回调失败返回给微信
}
}