.net6开发企业二维码微信支付 电脑版发表于:2024/4/15 17:45 ![](https://img.tnblog.net/arcimg/hb/d0512362aed443efbb82e644f2d634e0.jpg) >#企业微信小程序支付流程 [TOC] 前提准备 ------------ tn2>首先企业结合上一篇文章,开通企业级Native支付。 可点开这个链接进行申请开通:https://pay.weixin.qq.com/index.php/extend/product/lists?tid=3 ![](https://img.tnblog.net/arcimg/hb/94562949c54e458883ae229da0af518f.png) ![](https://img.tnblog.net/arcimg/hb/9b33d41be3e04c59a9cfcbd66f4e3c42.png) ![](https://img.tnblog.net/arcimg/hb/f8426147e06c4656a5ee0cb3ab05b607.png) 创建生成二维码的工具类 ------------ tn2>首先安装`ZXing.Net`和`ZXing.Net.Bindings.Windows.Compatibility`两个包。 ```bash <PackageReference Include="ZXing.Net" Version="0.16.9" /> <PackageReference Include="ZXing.Net.Bindings.Windows.Compatibility" Version="0.16.12" /> ``` tn2>创建根据字符串生成二维码的工具类 ```csharp public static class QRCodeHelper { public static void GenerateQRCode(string link,string path) { var writer = new BarcodeWriter<Bitmap>(); writer.Format = BarcodeFormat.QR_CODE; QrCodeEncodingOptions options = new QrCodeEncodingOptions() { DisableECI = true, //设置内容编码 CharacterSet = "UTF-8", //设置二维码的宽度和高度 Width = 200, Height = 200, Margin = 1 //设置二维码的边距,单位不是固定像素 }; var render = new AlternateBitmapRenderer(); writer.Renderer = render; writer.Options = options; Bitmap map = writer.Write(link); map.Save(path); } } ``` 添加相关小程序信息 ------------ tn2>在`appsettings.json`中添加小程序相关信息。 ```json "WechatNativeOptions": { "Accounts": [ { "AppId": "你的appid", "AppSecret": "相关secret" } ], "SucceedNotifyUrl": "接收通知的地址" } ``` tn2>在`OptionTenpays`目录下创建`WechatNativeOptions`类实例。 ```csharp public partial class WechatNativeOptions : IOptions<WechatNativeOptions> { WechatNativeOptions IOptions<WechatNativeOptions>.Value => this; public Types.WechatAccount[] Accounts { get; set; } = Array.Empty<Types.WechatAccount>(); public string SucceedNotifyUrl { get; set; } = string.Empty; } public partial class WechatNativeOptions { public static class Types { public class WechatAccount { public string AppId { get; set; } = string.Empty; public string AppSecret { get; set; } = string.Empty; } } } ``` tn2>创建请求的金额实例 ```csharp /// <summary> /// 请求的金额 /// </summary> public class CreateOrderByNativeRequest { public int Amount { get; set; } } ``` tn2>在`Program`中注入生成二维码对应的小程序信息的类型。 ```csharp builder.Services.Configure<WechatNativeOptions>(builder.Configuration.GetSection(nameof(WechatNativeOptions))); ``` 支付与通知 ------------ ```csharp [HttpPost] [Route("nativeweb2")] public async Task<IActionResult> CreateOrderByNativeWeb2([FromBody] Models.CreateOrderByNativeRequest requestModel) { //读取微信支付配置文件 var tenpayAccountOptions = _wechatnativeOptions.Accounts?.FirstOrDefault(); var tenpayOptions = _tenpayOptions.Merchants?.FirstOrDefault(); var client = _wechatTenpayClientFactory.Create(tenpayOptions.MerchantId); var request = new CreatePayTransactionNativeRequest() { OutTradeNumber = "SAMPLE_OTN_" + DateTimeOffset.Now.ToString("yyyyMMddHHmmssfff"), AppId = tenpayAccountOptions.AppId, Description = "演示订单", NotifyUrl = _tenpayOptions.SucceedNotifyUrl, Amount = new CreatePayTransactionNativeRequest.Types.Amount() { Total = requestModel.Amount } }; var response = await client.ExecuteCreatePayTransactionNativeAsync(request, cancellationToken: HttpContext.RequestAborted); if (!response.IsSuccessful()) { _logger.LogWarning( "Native 下单失败(状态码:{0},错误代码:{1},错误描述:{2})。", response.GetRawStatus(), response.ErrorCode, response.ErrorMessage ); return new JsonResult(response); } var guid = Guid.NewGuid().ToString()+".png"; QRCodeHelper.GenerateQRCode(response.QrcodeUrl, Path.Combine(_wwwrootpath,"wwwroot", guid)); var url = Request.Host.Value + "/" + guid; return Ok(url); } ``` ```csharp [HttpGet] [Route("nativenotify")] public IActionResult GetNativeNotify() { var request = HttpContext.Request; var fulljsondata = JsonConvert.SerializeObject(request); // 这里可以添加更多的信息获取逻辑 Console.WriteLine("数据获取:"); Console.WriteLine(fulljsondata); // 返回请求的信息或者根据需要进行处理 return Ok(); } ```