简介
今天我们就来使用NCF框架对接一下支付宝支付
参考文档
支付宝支付开发文档:https://opendocs.alipay.com/open/204
NCF仓库地址:https://github.com/NeuCharFramework/NCF (要Star哦)
步骤
1.下载NCF
2.创建自己的XNCF模块
3.引用Nuget包ML.ThirdParty & ML.Common.SDK & AopSdk
4.建立自己的Controller,并创建接口方法
5.建立PaymentService
6.调用代码返回数据给前端
7.前端拿数据吊起支付
案例
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="amount">金额</param> 5 /// <param name="userId">用户Id</param> 6 /// <returns></returns> 7 [HttpPost] 8 public async Task<IActionResult> AliPaymentAsync(string amount,string userId) 9 { 10 try 11 { 12 SenparcTrace.Log($"amount---{amount},userId----{userId}"); 13 PaymentService paymentService = new PaymentService(); 14 var obj = await paymentService.PaymentParam(amount, userId); 15 return Success(obj); 16 } 17 catch (Exception ex) 18 { 19 return Fail(ex.Message); 20 } 21 }
5.建立PaymentService
1 using Senparc.CO2NET.Trace; 2 using Senparc.Ncf.Core.Enums; 3 using Senparc.Ncf.Repository; 4 using Senparc.Ncf.Service; 5 using System; 6 using System.Collections.Generic; 7 using System.Linq; 8 using System.Threading.Tasks; 9 using ML.Xncf.Admin.Models.DatabaseModel; 10 using ML.Xncf.Admin.Models.DatabaseModel.Dto; 11 using Senparc.Ncf.Utility; 12 using Microsoft.EntityFrameworkCore; 13 using System.Text; 14 using ML.Xncf.Admin.Models; 15 using Aop.Api; 16 using Aop.Api.Request; 17 using Aop.Api.Response; 18 using Aop.Api.Util; 19 using Aop.Api.Domain; 20 using System.Web; 21 using ML.Xncf.Admin.Models.DatabaseModel.VO; 22 using ML.Xncf.Admin.Param; 23 24 namespace ML.Xncf.Admin.Services 25 { 26 public class PaymentService 27 { 28 public IAopClient client; 29 30 protected const string strPrivateKey = @"bxOYPgAyIlE94ofz9v75nM20VM5cacea3hRb4q2qVCuw6p3tJkXwmRLn1zrGvCvZbxOYPgAyIlE94ofz9v75nM20VM5cacea3hRb4q2qVCuw6p3tJkXwmRLn1zrGvCvZbxOYPgAyIlE94ofz9v75nM20VM5cacea3hRb4q2qVCuw6p3tJkXwmRLn1zrGvCvZbxOYPgAyIlE94ofz9v75nM20VM5cacea3hRb4q2qVCuw6p3tJkXwmRLn1zrGvCvZbxOYPgAyIlE94ofz9v75nM20VM5cacea3hRb4q2qVCuw6p3tJkXwmRLn1zrGvCvZbxOYPgAyIlE94ofz9v75nM20VM5cacea3hRb4q2qVCuw6p3tJkXwmRLn1zrGvCvZbxOYPgAyIlE94ofz9v75nM20VM5cacea3hRb4q2qVCuw6p3tJkXwmRLn1zrGvCvZbxOYPgAyIlE94ofz9v75nM20VM5cacea3hRb4q2qVCuw6p3tJkXwmRLn1zrGvCvZbxOYPgAyIlE94ofz9v75nM20VM5cacea3hRb4q2qVCuw6p3tJkXwmRLn1zrGvCvZbxOYPgAyIlE94ofz9v75nM20VM5cacea3hRb4q2qVCuw6p3tJkXwmRLn1zrGvCvZbxOYPgAyIlE94ofz9v75nM20VM5cacea3hRb4q2qVCuw6p3tJkXwmRLn1zrGvCvZbxOYPgAyIlE94ofz9v75nM20VM5cacea3hRb4q2qVCuw6p3tJkXwmRLn1zrGvCvZbxOYPgAyIlE94ofz9v75nM20VM5cacea3hRb4q2qVCuw6p3tJkXwmRLn1zrGvCvZbxOYPgAyIlE94ofz9v75nM20VM5cacea3hRb4q2qVCuw6p3tJkXwmRLn1zrGvCvZbxOYPgAyIlE94ofz9v75nM20VM5cacea3hRb4q2qVCuw6p3tJkXwmRLn1zrGvCvZbxOYPgAyIlE94ofz9v75nM20VM5cacea3hRb4q2qVCuw6p3tJkXwmRLn1zrGvCvZbxOYPgAyIlE94ofz9v75nM20VM5cacea3hRb4q2qVCuw6p3tJkXwmRLn1zrGvCvZbxOYPgAyIlE94ofz9v75nM20VM5cacea3hRb4q2qVCuw6p3tJkXwmRLn1zrGvCvZbxOYPgAyIlE94ofz9v75nM20VM5cacea3hRb4q2qVCuw6p3tJkXwmRLn1zrGvCvZbxOYPgAyIlE94ofz9v75nM20VM5cacea3hRb4q2qVCuw6p3tJkXwmRLn1zrGvCvZbxOYPgAyIlE94ofz9v75nM20VM5cacea3hRb4q2qVCuw6p3tJkXwmRLn1zrGvCvZbxOYPgAyIlE94ofz9v75nM20VM5cacea3hRb4q2qVCuw6p3tJkXwmRLn1zrGvCvZbxOYPgAyIlE94ofz9v75nM20VM5cacea3hRb4q2qVCuw6p3tJkXwmRLn1zrGvCvZbxOYPgAyIlE94ofz9v75nM20VM5cacea3hRb4q2qVCuw6p3tJkXwmRLn1zrGvCvZbxOYPgAyIlE94ofz9v75nM20VM5cacea3hRb4q2qVCuw6p3tJkXwmRLn"; 31 protected const string strPublicKey = @"bxOYPgAyIlE94ofz9v75nM20VM5cacea3hRb4q2qVCuw6p3tJkXwmRLn1zrGvCvZbxOYPgAyIlE94ofz9v75nM20VM5cacea3hRb4q2qVCuw6p3tJkXwmRLn1zrGvCvZbxOYPgAyIlE94ofz9v75nM20VM5cacea3hRb4q2qVCuw6p3tJkXwmRLn1zrGvCvZbxOYPgAyIlE94ofz9v75nM20VM5cacea3hRb4q2qVCuw6p3tJkXwmRLn1zrGvCvZbxOYPgAyIlE94ofz9v75nM20VM5cacea3hRb4q2qVCuw6p3tJkXwmRLn1zrGvCvZbxOYPgAyIlE94ofz9v75nM20VM5cacea3hRb4q2qVCuw6p3tJkXwmRLn1zrGvCvZ"; 32 33 public PaymentService() 34 { 35 client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", "9880981436596309", strPrivateKey, "json", "1.0", "RSA2", strPublicKey, "UTF-8", false); 36 } 37 38 /// <summary> 39 /// 支付宝参数 40 /// </summary> 41 /// <returns></returns> 42 public async Task<object> PaymentParam(string amount,string userId) 43 { 44 try 45 { 46 //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称如:alipay.trade.app.pay 47 AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest(); 48 //SDK已经封装掉了公共参数,这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。 49 AlipayTradeAppPayModel model = new AlipayTradeAppPayModel(); 50 model.Body = "购买会员," + userId; 51 string strOutTradeNo = DateTime.Now.ToString("yyyyMMddHHmmss"); 52 model.OutTradeNo = strOutTradeNo; 53 model.Subject = "购买会员"; 54 55 if (!string.IsNullOrEmpty(amount)) 56 { 57 model.TotalAmount = amount; 58 } 59 else 60 { 61 model.TotalAmount = "0.01"; 62 } 63 model.ProductCode = "QUICK_MSECURITY_PAY"; 64 model.TimeoutExpress = "30m"; 65 request.SetBizModel(model); 66 request.SetNotifyUrl("http://www.alibaba.com/api/alipay/notice"); 67 //这里和普通的接口调用不同,使用的是sdkExecute 68 AlipayTradeAppPayResponse response = client.SdkExecute(request); 69 //HttpUtility.HtmlEncode是为了输出到页面时防止被浏览器将关键参数html转义,实际打印到日志以及http传输不会有这个问题 70 SenparcTrace.Log($"支付宝支付结果信息为未转义-----{response.Body}"); 71 SenparcTrace.Log($"支付宝支付结果信息为-----{HttpUtility.HtmlEncode(response.Body)}"); 72 return new BaseResult<object>(200,"请求成功", HttpUtility.HtmlEncode(response.Body).Replace("&", "&")); 73 } 74 catch (Exception ex) 75 { 76 return new BaseResult<object>(201, "请求失败", ex.Message); 77 } 78 } 79 80 } 81 82 }
6.调用代码返回数据(即4中返回的obj的数据)给前端
7.前端拿数据吊起支付
结语
按照上面的方法让你瞬间解决支付宝APP支付的难题,欢迎大家交流,欢迎Star,欢迎关注