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
回調邏輯參考: