1.添加參考

2.添加組態檔
/// <summary> /// 微信 /// </summary> public class WeChat { // 小程式 public string WxOpenAppId { get; set; } public string WxOpenAppSecret { get; set; } //// 微信支付 //public string TenPayV3_AppId { get; set; } //public string TenPayV3_AppSecret { get; set; } //public string TenPayV3_MchId { get; set; } //public string TenPayV3_Key { get; set; } //public string TenPayV3_CertPath { get; set; } //public string TenPayV3_CertSecret { get; set; } //public string TenPayV3_TenpayNotify { get; set; } //public string TenPayV3_RechargeTenpayNotify { get; set; } //public string TenPayV3_RefundNotify { get; set; } //public string TenPayV3_RechargeRefundNotify { get; set; } //public string TenPayV3_WxOpenTenpayNotify { get; set; } /// <summary> /// 是否是開發環境 /// </summary> public string IsDevelopment { get; set; } }
3.在appsetting.jso里面配置引數
"WeChat": {
// 小程式
"WxOpenAppId": "",
"WxOpenAppSecret": ""
////微信支付
//"TenPayV3_AppId": "", //商戶平臺系結了微信小程式就可以直接用這個
//"TenPayV3_AppSecret": "", //商戶平臺系結了微信小程式就可以直接用這個
//"TenPayV3_MchId": "", //
//"TenPayV3_Key": "", //
//"TenPayV3_CertPath": "", //(新)支付證書物理路徑,如:D:\\cert\\apiclient_cert.p12
//"TenPayV3_CertSecret": "", //(新)支付證書密碼(原始密碼和 MchId 相同)
//"TenPayV3_TenpayNotify": "https://river.runtoinfo.com/agr/api/TenPayV3/PayNotifyUrl",
////http://YourDomainName/TenpayV3/PayNotifyUrl
////如果不設定TenPayV3_WxOpenTenpayNotify,默認在 TenPayV3_TenpayNotify 的值最后加上 "WxOpen"
//"TenPayV3_WxOpenTenpayNotify": "https://river.runtoinfo.com/agr/api/TenPayV3/PayNotifyUrlWxOpen" //http://YourDomainName/TenpayV3/PayNotifyUrlWxOpen
}
4.配置program
builder.Services.AddMemoryCache();
//微信 builder.Services.AddSenparcGlobalServices(builder.Configuration)//Senparc.CO2NET 全域注冊 .AddSenparcWeixinServices(builder.Configuration);//Senparc.Weixin 注冊 //添加 微信支付api證書 //var mac_id = builder.Configuration["WeChat:TenPayV3_MchId"]; //builder.Services.AddCertHttpClient(mac_id + "_", mac_id, AppDomain.CurrentDomain.BaseDirectory + "/wwwroot/" + builder.Configuration["WeChat:TenPayV3_CertPath"]); var senparcSetting = builder.Services.BuildServiceProvider().GetRequiredService<IOptions<SenparcSetting>>(); IRegisterService register = RegisterService.Start(senparcSetting.Value).UseSenparcGlobal();// 啟動 CO2NET 全域注冊,必須! var senparcWeixinSetting = builder.Services.BuildServiceProvider().GetRequiredService<IOptions<SenparcWeixinSetting>>(); //register.UseSenparcWeixin(senparcWeixinSetting.Value, senparcSetting.Value);//微信全域注冊,必須! register.UseSenparcWeixin(senparcWeixinSetting.Value, weixinRegister => { #region 微信相關配置 /* 微信配置開始 * * 建議按照以下順序進行注冊,尤其須將快取放在第一位! */ #region 微信快取(按需,必須放在配置開頭,以確保其他可能依賴到快取的注冊程序使用正確的配置) //注意:如果使用非本地快取,而不執行本塊注冊代碼,將會收到“當前擴展快取策略沒有進行注冊”的例外 // DPBMARK_END // 微信的 Memcached 快取,如果不使用則注釋掉(開啟前必須保證配置有效,否則會拋錯) -- DPBMARK Memcached //if (UseMemcached(senparcSetting.Value, out _)) //{ // app.UseEnyimMemcached(); // weixinRegister.UseSenparcWeixinCacheMemcached(); //} // DPBMARK_END #endregion /* 微信配置結束 */ #endregion });//微信全域注冊,必須! //注冊 Token容器 應用憑證 var con = AccessTokenContainer.RegisterAsync(builder.Configuration["WeChat:WxOpenAppId"], builder.Configuration["WeChat:WxOpenAppSecret"]); con.Wait();
本文來自博客園,作者:康Sir7,轉載請注明原文鏈接:https://www.cnblogs.com/kangsir7/p/16476463.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/499229.html
標籤:.NET技术
