Magicodes.Pay,是心莱科技团队提供的统一支付库,相关库均使用.NET标准库编写,支持.NET Framework以及.NET Core。目前支持如下支付方式和功能:git
支付宝APP支付github
支付宝Wap支付小程序
支付宝国际支付微信小程序
支持分帐微信
微信小程序支付app
微信APP支付async
统一支付回调处理ide
支持日志函数注入(不依赖日志库)函数
支持支付配置函数注入,以便于支持自定义配置获取逻辑,以应用于不一样的场景(好比从配置文件、用户设置获取配置,或者多租户支持)微信支付
目前此库咱们在不少项目上已经进行了验证,因为项目赶工,许多功能咱们并无添加、迁移或者重构过来,在后续的过程当中,咱们会逐步来完成这些工做。同时,在Magicodes.Admin开源库中,咱们也编写了相关的Demo和实现。
Magicodes.Pay开源库地址:
https://github.com/xin-lai/magicodes.pay
Magicodes.Admin开源库地址:https://gitee.com/xl_wenqiang/Magicodes.Admin.Core
整个支付实现这块,咱们在Magicodes.Admin开源库中已经提供了统一支付的Demo,并且咱们将会提供根据请求头来自动调用相关支付的功能。以下图所示:
在各个业务支付场景中,咱们能够很是方便的调用此统一支付,以下图所示:
这是目前的下个版本的规划:
支付宝PC支付
微信H5支付
提供默认的回调管理逻辑,支持回调处理函数的注入
具体功能咱们会根据项目的状况来迭代,若是你有好的建议或者意见,能够关注咱们的公众号“magiccodes”来提交您的意见或者意见。
相关库的配置相对比较简单,通常均使用相关Builder类来配置自定义日志逻辑、配置获取逻辑等,具体能够查阅Builder目录下的代码。
部分代码以下所示:
if (WeChatPayApi == null) { throw new UserFriendlyException("支付未开放,请联系管理员!"); } var appPayInput = new WeChat.Pay.Dto.AppPayInput { Body = input.Body, OutTradeNo = input.OutTradeNo, Attach = input.CustomData, TotalFee = input.TotalAmount, SpbillCreateIp = _clientInfoProvider?.ClientIpAddress }; try { var appPayOutput = WeChatPayApi.AppPay(appPayInput); return Task.FromResult(appPayOutput); } catch (Exception ex) { throw new UserFriendlyException(ex.Message); }
if (AlipayAppService == null) { throw new UserFriendlyException("支付未开放,请联系管理员!"); } var appPayInput = new Alipay.Dto.AppPayInput { Body = input.Body, Subject = input.Subject, TradeNo = input.OutTradeNo, PassbackParams = input.CustomData, TotalAmount = input.TotalAmount }; try { var appPayOutput = await AlipayAppService.AppPay(appPayInput); return appPayOutput.Response.Body; } catch (Exception ex) { throw new UserFriendlyException(ex.Message); }
if (GlobalAlipayAppService == null) { throw new UserFriendlyException("支付未开放,请联系管理员!"); } var payInput = new Alipay.Global.Dto.PayInput { Body = input.Body, Subject = input.Subject, TradeNo = input.OutTradeNo, //PassbackParams = input.CustomData, TotalFee = input.TotalAmount, }; try { return await GlobalAlipayAppService.Pay(payInput); } catch (Exception ex) { throw new UserFriendlyException(ex.Message); }
统一回调处理逻辑和回调处理地址
上图的PayAction参考:
void PayAction(string key, string outTradeNo, string transactionId, int totalFee, JObject data) { using (var paymentCallbackManagerObj = iocManager.ResolveAsDisposable<PaymentCallbackManager>()) { var paymentCallbackManager = paymentCallbackManagerObj?.Object; if (paymentCallbackManager == null) { throw new ApplicationException("支付回调管理器异常,没法执行回调!"); } AsyncHelper.RunSync(async () => await paymentCallbackManager.ExecuteCallback(key, outTradeNo, transactionId, totalFee, data)); } }
完整回调代码请参考此代码:https://gitee.com/xl_wenqiang/Magicodes.Admin.Core/blob/develop/src/unity/Magicodes.Pay/Startup/PayStartup.cs
回调逻辑参考: