NCF 如何写微信支付中的APP支付

简介

微信支付想必大家耳熟能祥,使用的地方太多了,今天我们用几分钟的时间来写一个微信支付的案例给到大家,希望大家能够使用NCF快速的对接APP支付

今天我们讲解到的是微信支付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> WxPayAsync(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/wxNotifyUrl";
23                 wxpayParam.trade_type = "APP";
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                 MakeCallPaymentParam mcpp = new MakeCallPaymentParam();
33                 mcpp.appid = "wx2y0svtzsngwduix6";
34                 mcpp.partnerid = "0165355078";
35                 mcpp.prepayid = dtWxPayResponse.Rows[0]["prepay_id"].ToString();
36                 mcpp.package = "Sign=WXPay";
37                 mcpp.noncestr = "";
38                 mcpp.timestamp = "";
39                 string strSign = "";
40                 var data = wxpay.MakeCallPayment(mcpp, "5hJpKkSaOoWVqET1Gfl7Q3RUEBLTu9kP", out strSign);
41                 string rp = XMLHelper.XMLToDataTable(WechatPayHelper.FormatParamToXML(data)).ToJson();
42                 return Success(rp);
43             }
44             catch (Exception ex)
45             {
46                 return Fail(ex.Message);
47             }
48         }        

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

6.前端拿数据吊起支付

 

 

 

结语

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

 

posted @ 2021-04-29 15:09  MartyZane  阅读(313)  评论(0编辑  收藏  举报