.NETCore中微信支付之公眾號、H5支付的示例分析-創新互聯
小編給大家分享一下.NET Core中微信支付之公眾號、H5支付的示例分析,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
準備篇
公眾號或者服務號(并開通微信支付功能)、商戶平臺中開通JSAPI支付、H5支付。
配置篇
公眾號或者服務號中 -------開發-------開發者工具---------web開發者工具-------綁定為開發者
公眾號或者服務號中 -------公眾號設置--------功能設置 :填寫業務域名、JS安全域名、網頁授權域名 示例:pay.one.com
商戶平臺中--------產品中心-------開發配置------JSAPI支付授權目錄填寫:http://pay.one.com/ http://pay.one.com/WeChatPay/PubPay/-----H5支付填寫:pay.one.com
若對配置還有疑問,可參考官方文檔:
https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842
https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_7&index=6
開發篇
JSAPI支付
本Demo是基于Payment 的SDK開發。具體詳情可參考: https://github.com/Essensoft/Payment
首先 使用Nuget安裝payment:
Install-Package :Essensoft.AspNetCore.Payment.WeChatPay -Version 2.3.2
建一個Model: WeChatPayPubPayViewModel
public class WeChatPayPubPayViewModel { [Required] [Display(Name = "out_trade_no")] public string OutTradeNo { get; set; } [Required] [Display(Name = "body")] public string Body { get; set; } [Required] [Display(Name = "total_fee")] public int TotalFee { get; set; } [Required] [Display(Name = "spbill_create_ip")] public string SpbillCreateIp { get; set; } [Required] [Display(Name = "notify_url")] public string NotifyUrl { get; set; } [Required] [Display(Name = "trade_type")] public string TradeType { get; set; } [Required] [Display(Name = "openid")] public string OpenId { get; set; } }
WeChatPayController:
//微信支付請求客戶端(用于處理請求與響應) private readonly IWeChatPayClient _client; private readonly ILogger_logger; private IHttpContextAccessor _accessor; public WeChatPayController(IWeChatPayClient client, IHttpContextAccessor accessor, ILogger logger) { _client = client; _accessor = accessor; _logger = logger; } /// /// 公眾號支付 /// ///[HttpGet] public IActionResult PubPay() { WeChatPayPubPayViewModel payModel=new WeChatPayPubPayViewModel() { Body = "微信公眾號支付測試", OutTradeNo = DateTime.Now.ToString("yyyyMMddHHmmssfff"), TotalFee = 1,//分 單位 SpbillCreateIp = "127.0.0.1", NotifyUrl = "http://pay.one.com/notify/wechatpay/unifiedorder", TradeType = "JSAPI", OpenId = "" //此處需進行授權 獲取OpenId }; return View(payModel); } /// /// 公眾號支付 /// /// ///[HttpPost] public async Task PubPay(WeChatPayPubPayViewModel viewModel) { if(string.IsNullOrEmpty(viewModel.OpenId)) { ViewData["response"] = "請返回上級重新進入此頁面以獲取新數據"; return View(); } var request = new WeChatPayUnifiedOrderRequest { Body = viewModel.Body, OutTradeNo = viewModel.OutTradeNo, TotalFee = viewModel.TotalFee, SpbillCreateIp = viewModel.SpbillCreateIp, NotifyUrl = viewModel.NotifyUrl, TradeType = viewModel.TradeType, OpenId = viewModel.OpenId //此處需進行授權 獲取OpenId }; var response = await _client.ExecuteAsync(request);if (response.ReturnCode == "SUCCESS" && response.ResultCode == "SUCCESS") { var req = new WeChatPayH5CallPaymentRequest { Package = "prepay_id=" + response.PrepayId }; var parameter = await _client.ExecuteAsync(req); // 將參數(parameter)給 公眾號前端 讓他在微信內H5調起支付(https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_7&index=6) ViewData["parameter"] = JsonConvert.SerializeObject(parameter); ViewData["response"] = response.Body; return View(); } ViewData["response"] = response.Body; return View(); }
注意:公眾號或者微信內支付,需要授權獲取到用戶的OpenId。所以,此處我們還需要進行微信授權,而授權方式有兩種,一種是靜默授權、一種是需要用戶同意,區別是 靜默授權只能拿到Openid,而經用戶同意后可拿到 微信頭像、昵稱、性別等其他信息。
具體可參閱文檔: https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842
頁面:
@using Newtonsoft.Json @model WeChatPayPubPayViewModel @{ ViewData["Title"] = "公眾號支付-統一下單"; }