NCF 如何写微信支付中的小程序支付

简介

上一篇给大家说了APP支付的对接,今天我们来使用NCF快速的对接小程序支付

今天我们讲解到的是微信支付V2开发文档中的使用方法

 

参考文档

微信支付V2开发文档:https://pay.weixin.qq.com/wiki/doc/api/index.html

NCF仓库地址:https://github.com/NeuCharFramework/NCF (要Star哦)

 

步骤

1.下载NCF

2.创建自己的XNCF模块

3.引用Nuget包ML.ThirdParty & ML.Common.SDK

4.建立自己的Controller,并创建接口方法

5.调用代码返回数据给前端

6.前端拿数据吊起支付

 

案例

1.直接到Github上下载NCF的源码master分支

2.请参考NCF开发文档中的创建XNCF模块

3.项目中引入

1     <ItemGroup>
2         <PackageReference Include="ML.Common.SDK" Version="1.0.2.8" />
3         <PackageReference Include="ML.ThirdParty" Version="1.0.2" />
4     </ItemGroup>

4.建立自己的Controller,并创建方法

1 //引用相关命名空间
2 using ML.Common.SDK;
3 using ML.ThirdParty.Wechat;
 1 /// <summary>
 2 /// 微信小程序支付
 3 /// </summary>
 4 /// <param name="openId">用户的微信OpenId</param>
 5 /// <param name="userId">用户Id</param>
 6 /// <param name="body">内容</param>
 7 /// <param name="totalFee">支付的总金额</param>
 8 /// <returns></returns>
 9 [HttpPost]
10 public async Task<IActionResult> WxSPPayAsync(string openId,string userId, string body, int totalFee)
11 {
12     try
13     {
14         //ML 方案
15         WechatPayHelper wxpay = new WechatPayHelper();
16         WechatPaymentParam wxpayParam = new WechatPaymentParam();
17         wxpayParam.appid = "wx2y0svtzsngwduix6";
18         wxpayParam.mch_id = "0165355078";
19         wxpayParam.attach = $"userId={userId};totalFee={totalFee.ToString()}";
20         wxpayParam.body = body;
21         wxpayParam.total_fee = totalFee.ToString();
22         wxpayParam.notify_url = "https://www.weixin.com/api/v1/payment/wxSPNotifyUrl";
23         wxpayParam.trade_type = "JSAPI";
24         wxpayParam.openid = openId;
25         string strOutTradeNo = "";
26         var result = wxpay.Payment(wxpayParam, "5hJpKkSaOoWVqET1Gfl7Q3RUEBLTu9kP", out strOutTradeNo);
27         SenparcTrace.Log($"result----{result},sign={strOutTradeNo}");
28         result = result.Replace("<![CDATA[", "").Replace("]]>", "").Replace("\n", "");
29         DataTable dtWxPayResponse = XMLHelper.XMLToDataTable(result);
30         var response = dtWxPayResponse.ToJson().Replace("}]", $",\"timestamp\":\"{DateTimeHelper.ConvertToJavaScriptUnix(DateTime.Now)}\"}}]");
31 
32         MakeCallMiniProgramPaymentParam mcmppp = new MakeCallMiniProgramPaymentParam();
33         mcmppp.appId = "wx2y0svtzsngwduix6";
34         mcmppp.package = $"prepay_id={dtWxPayResponse.Rows[0]["prepay_id"].ToString()}";
35         mcmppp.nonceStr = "";
36         mcmppp.timeStamp = "";
37         string strSign = "";
38         var data = wxpay.MakeCallMiniProgramPayment(mcmppp, "5hJpKkSaOoWVqET1Gfl7Q3RUEBLTu9kP", out strSign);
39         string rp = XMLHelper.XMLToDataTable(WechatPayHelper.FormatParamToXML(data)).ToJson();
40 
41         return Success(rp);
42     }
43     catch (Exception ex)
44     {
45         return Fail(ex.Message);
46     }
47 }

5.调用代码返回数据(即4中返回的rp的数据)给前端

6.前端拿数据吊起支付

 

 

 

结语

 按照上面的方法让你瞬间解决微信小程序支付的难题,欢迎大家交流,欢迎Star,欢迎关注

posted @ 2021-05-01 18:44  MartyZane  阅读(293)  评论(0编辑  收藏  举报