.NET Core之微信支付之公众号、H5支付详解

本篇主要记录微信支付中公众号及H5支付全过程。

准备篇

公众号或者服务号(并开通微信支付功能)、商户平台中开通JSAPI支付、H5支付。

配置篇

公众号或者服务号中 -------开发-------开发者工具---------web开发者工具-------绑定为开发者

公众号或者服务号中 -------公众号设置--------功能设置   :填写业务域名、JS安全域名、网页授权域名 示例:pay.one.com

商户平台中--------产品中心-------开发配置------JSAPI支付授权目录填写:    支付填写:pay.one.com

.NET Core之微信支付之公众号、H5支付详解

.NET Core之微信支付之公众号、H5支付详解

若对配置还有疑问,可参考官方文档:

开发篇

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<WeChatPayController> _logger; private IHttpContextAccessor _accessor; public WeChatPayController(IWeChatPayClient client, IHttpContextAccessor accessor, ILogger<WeChatPayController> logger) { _client = client; _accessor = accessor; _logger = logger; } /// <summary> /// 公众号支付 /// </summary> /// <returns></returns> [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); } /// <summary> /// 公众号支付 /// </summary> /// <param></param> /// <returns></returns> [HttpPost] public async Task<IActionResult> 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,而经用户同意后可拿到 微信头像、昵称、性别等其他信息。

具体可参阅文档:

页面:

@using Newtonsoft.Json @model WeChatPayPubPayViewModel @{ ViewData["Title"] = "公众号支付-统一下单"; } <nav aria-label="breadcrumb"> <ol> <li><a asp-controller="WeChatPay" asp-action="Index">微信支付</a></li> <li aria-current="page">@ViewData["Title"]</li> </ol> </nav> <br /> <div> <div> <form asp-controller="WeChatPay" asp-action="PubPay"> <div asp-validation-summary="All"></div> <div> <label asp-for="OutTradeNo"></label> <input type="text" asp-for="OutTradeNo" value="@Model?.OutTradeNo" /> </div> <div> <label asp-for="Body"></label> <input type="text" asp-for="Body" value="@Model?.Body" /> </div> <div> <label asp-for="TotalFee"></label> <input type="text" asp-for="TotalFee" value="@Model?.TotalFee" /> </div> <div> <label asp-for="SpbillCreateIp"></label> <input type="text" asp-for="SpbillCreateIp" value="@Model?.SpbillCreateIp" /> </div> <div> <label asp-for="NotifyUrl"></label> <input type="text" asp-for="NotifyUrl" value="@Model?.NotifyUrl" /> </div> <div> <label asp-for="TradeType"></label> <input type="text" asp-for="TradeType" value="@Model?.TradeType" /> </div> <div> <label asp-for="OpenId"></label> <input type="text" asp-for="OpenId" value="@Model?.OpenId" /> </div> <button type="submit">提交请求</button> <button type="button">立即支付</button> </form> <hr /> <form> <div> <label>Response:</label> <textarea rows="10">@ViewData["response"]</textarea> </div> <div> <label>Parameter:</label> <textarea rows="3">@ViewData["parameter"]</textarea> </div> </form> </div> </div> @section Scripts { @{await Html.RenderPartialAsync("_ValidationScriptsPartial"); } } <script src="https://www.jb51.net/~/lib/jquery/dist/jquery.min.js"></script> <script type="text/javascript"> $(function () { $("#PayNow").on('click', function () { const local = "http://pay.one.com/WeChatPay/PayBack/"; window.location.href ='https://open.weixin.qq.com/connect/oauth2/authorize?appid=@ViewBaig.AppId&redirect_uri=' + encodeURIComponent(local)+'&response_type=code&scope=snsapi_base&state=a#wechat_redirect'; }); }); </script>

此时:PayBack Action如下:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wdsgxd.html