团餐订单
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

287 lines
9.9 KiB

  1. using BPA.Kitchen.Core;
  2. using BPA.KitChen.GroupMealOrder.Core.CacheOption;
  3. using BPA.KitChen.GroupMealOrder.Core.Common;
  4. using BPA.KitChen.GroupMealOrder.Core.Entity;
  5. using BPA.KitChen.GroupMealOrder.SqlSugar;
  6. using BPA.KitChen.WeChat.WechatServer.Dtos;
  7. using BPA.KitChen.WeChat.WechatServer.Service;
  8. using Essensoft.Paylink.WeChatPay.V2.Response;
  9. using Furion.DynamicApiController;
  10. using Furion.FriendlyException;
  11. using Furion.JsonSerialization;
  12. using Microsoft.AspNetCore.Authorization;
  13. using Microsoft.AspNetCore.Mvc;
  14. using Microsoft.Extensions.Caching.Memory;
  15. namespace Black_B_Project.Application.WechatServer.Service
  16. {
  17. /// <summary>
  18. /// 微信接口汇总
  19. /// </summary>
  20. [ApiDescriptionSettings("WECHAT"), AllowAnonymous]
  21. public class WechatAppService : IDynamicApiController
  22. {
  23. private readonly IWechatPayService _IWechatPay;
  24. private readonly IWechatNotityService _IWechatNotityService;
  25. private readonly IWechatService _WechatService;
  26. private readonly IWeChatPayProfitService _IProfitService;
  27. private readonly IMemoryCache _memoryCache;
  28. public WechatAppService(
  29. IWechatPayService IWechatPay,
  30. IWechatService wechatService,
  31. IWechatNotityService IWechatNotityService,
  32. IWeChatPayProfitService IProfitService,
  33. IMemoryCache memoryCache
  34. )
  35. {
  36. _IWechatPay = IWechatPay;
  37. _WechatService = wechatService;
  38. _IWechatNotityService = IWechatNotityService;
  39. _IProfitService = IProfitService;
  40. _memoryCache = memoryCache;
  41. }
  42. /// <summary>
  43. /// 用户登录,获取openid
  44. /// </summary>
  45. /// <param name="vm"></param>
  46. /// <returns></returns>
  47. [HttpPost]
  48. public WeChatUserInfo wxlogin(DecryptVM vm)
  49. {
  50. return _WechatService.WxLogin(vm);
  51. }
  52. [HttpPost]
  53. public WeChatUserInfo Decryptuserinfo(DecryptVM vm)
  54. {
  55. if (string.IsNullOrEmpty(vm.session_key))
  56. {
  57. throw Oops.Oh("session_key不能为空!");
  58. }
  59. if (string.IsNullOrEmpty(vm.aesiv))
  60. {
  61. throw Oops.Oh("aesiv不能为空!");
  62. }
  63. if (string.IsNullOrEmpty(vm.encryptedData))
  64. {
  65. throw Oops.Oh("encryptedData不能为空!");
  66. }
  67. WeChatUserInfo userinfo = _WechatService.Decryptuserinfo(vm);
  68. return userinfo;
  69. }
  70. /// <summary>
  71. /// 获取手机号
  72. /// </summary>
  73. /// <param name="code"></param>
  74. /// <returns></returns>
  75. [HttpGet("/api/wechat/user/phone")]
  76. public async Task<string> WxPhone(string code)
  77. {
  78. WechatPhoneMsg phonemsg = await _WechatService.WxPhone(code);
  79. if (phonemsg.errcode==0)
  80. {
  81. return phonemsg.phone_info.purePhoneNumber;
  82. }
  83. else
  84. {
  85. throw Oops.Bah(phonemsg.errmsg);
  86. }
  87. }
  88. /// <summary>
  89. /// 调用jsapi支付,返回支付信息
  90. /// </summary>
  91. /// <returns></returns>
  92. [HttpPost]
  93. public Task<IActionResult> Wechatpay( string ordernum)
  94. {
  95. return _IWechatPay.PayLinkWechatPay(null, ordernum);
  96. }
  97. /// <summary>
  98. /// 充值
  99. /// </summary>
  100. /// <param name="input"></param>
  101. /// <returns></returns>
  102. [HttpPost]
  103. public Task<IActionResult> Rechargepay(RechargeInput input)
  104. {
  105. return _IWechatPay.RechargePay(input);
  106. }
  107. /// <summary>
  108. /// 调用Native支付,返回支付信息url
  109. /// </summary>
  110. /// <returns></returns>
  111. [HttpPost]
  112. public string Nativepay(decimal amount, string orderid)
  113. {
  114. return _IWechatPay.NativePay(amount, orderid);
  115. }
  116. /***
  117. * @param transaction_id 微信订单号(优先使用)
  118. * @param out_trade_no 商户订单号
  119. * @param total_fee 订单总金额
  120. * @param refund_fee 退款金额
  121. * @return 退款结果(xml格式)
  122. */
  123. /// <summary>
  124. /// 申请退款完整业务流程逻辑
  125. /// </summary>
  126. /// <param name="input">商户</param>
  127. /// <returns></returns>
  128. [HttpPost]
  129. public async Task<IActionResult> Wechatrefund(RefundInput input)
  130. {
  131. // BPALog.WriteLog($"Wechatrefund请求参数", param: input);
  132. if (string.IsNullOrEmpty(input.AppId))
  133. {
  134. throw Oops.Oh("AppId不能为空!");
  135. }
  136. var config = ApolloApplication.Configs.Find(x => x.Key == input.AppId);
  137. if (config==null)
  138. {
  139. throw Oops.Oh("AppId不存在!");
  140. }
  141. config.WechatConfig.OrderAddress = "https://bpa.black-pa.com:21527/order";
  142. // MyDataResult<OrderAllInfoDto> res = OrderWeApiHepler.GetOrderInfoByNum<OrderAllInfoDto>(config.WechatConfig.OrderAddress,input.OrderNumberId);
  143. var order = await SqlSugarDb.Db.Queryable<BPA_WeighOrder>()
  144. .FirstAsync(x => x.OrderNumber == input.OrderNumberId);
  145. if (order!=null)
  146. {
  147. int totalFee = Convert.ToInt32(order.TotalAmount * 100);
  148. RefundDto refundDto = await _IWechatPay.Refund(
  149. new() {
  150. out_trade_no = input.OrderNumberId,
  151. refund_fee = totalFee,
  152. total_fee = totalFee,
  153. },config
  154. );//提交退款申请给API,接收返回数据
  155. return new JsonResult(refundDto);
  156. }
  157. else
  158. {
  159. throw Oops.Oh("数据接口查询失败:订单不存在");
  160. }
  161. }
  162. /// <summary>
  163. /// 对账单请求处理
  164. /// </summary>
  165. /// <param name="input"></param>
  166. /// <returns></returns>
  167. [NonUnify]
  168. [Route("wechat/pay/downloadbill")]
  169. public static string Downloadbill(BillInput input)
  170. {
  171. var config = ApolloApplication.Configs.Find(x => x.Key == input.appid);
  172. string nonceStr = WechatCommon.GenerateNonceStr();
  173. string appid = config.WechatPayConfig.AppId;
  174. string mchid = config.WechatPayConfig.MchId;
  175. string mchkey = config.WechatPayConfig.APIKey;
  176. string strA = $"appid={appid}&bill_date={input.billdate}&bill_type={input.billtype}&mch_id={mchid}&nonce_str={nonceStr}&key=" + mchkey;
  177. string sign = WechatCommon.MD5(strA).ToUpper();
  178. var json = new
  179. {
  180. input.appid,
  181. input.billdate,
  182. input.billtype,
  183. mchid,
  184. nonceStr,
  185. sign
  186. };
  187. return JSON.Serialize(json);
  188. }
  189. ///<summary>
  190. /// 微信支付通知
  191. /// </summary>
  192. /// <returns></returns>
  193. [NonUnify]
  194. [Route("wechat/paynotity")]
  195. public Task<IActionResult> PayNotity()
  196. {
  197. return _IWechatNotityService.PayNotity();
  198. }
  199. /// <summary>
  200. /// 微信支付退款通知
  201. /// </summary>
  202. [Route("wechat/refundnotity")]
  203. [NonUnify]
  204. public async Task<IActionResult> RefundNotity()
  205. {
  206. return await _IWechatNotityService.RefundNotity();
  207. }
  208. /// <summary>
  209. /// 微信支付-付款码
  210. /// </summary>
  211. /// <param name="input"></param>
  212. /// <returns></returns>
  213. [Route("wechat/pay/micro")]
  214. [NonUnify]
  215. public async Task<IActionResult> MicropayPay(MicropayInput input)
  216. {
  217. return await _IWechatPay.MicropayPay(input);
  218. }
  219. /// <summary>
  220. /// 查询
  221. /// </summary>
  222. /// <param name="ordernumid"></param>
  223. /// <param name="appId"></param>
  224. /// <returns></returns>
  225. [Route("wechat/queryorder")]
  226. public async Task<IActionResult> PayOrderQuery(string ordernumid,string appId)
  227. {
  228. var config = ApolloApplication.Configs.Find(x => x.Key == appId);
  229. // MyDataResult<OrderAllInfoDto> info = OrderWeApiHepler.GetOrderInfoByNum<OrderAllInfoDto>(config.WechatConfig.OrderAddress, ordernumid);
  230. var order = await SqlSugarDb.Db.Queryable<BPA_WeighOrder>()
  231. .FirstAsync(x => x.OrderNumber == ordernumid);
  232. if (order==null)
  233. {
  234. throw Oops.Oh("查询失败,没有找到订单");
  235. }
  236. var data = await _IWechatPay.PayOrderQuery(appId, order.OrderNumber, order.TransactionId);
  237. return new JsonResult(data);
  238. }
  239. /// <summary>
  240. /// 发送预定提醒
  241. /// </summary>
  242. /// <param name="input"></param>
  243. /// <returns></returns>
  244. [Route("wechat/reserve")]
  245. [HttpPost]
  246. public void SendReserveMessage(ReserveInput input)
  247. {
  248. _WechatService.SendReserveMessage(input);
  249. }
  250. /// <summary>
  251. /// 分账信息
  252. /// </summary>
  253. /// <param name="vm"></param>
  254. /// <returns></returns>
  255. [Route("wechat/profit/sharing")]
  256. [HttpPost]
  257. public Task<WeChatPayProfitSharingResponse> ProfitSharingRequest(ProfitSharingInput vm)
  258. {
  259. return _IProfitService.ProfitSharingRequest(vm);
  260. }
  261. /// <summary>
  262. /// 分账余额信息
  263. /// </summary>
  264. /// <param name="TransactionId">微信支付订单号</param>
  265. /// <returns></returns>
  266. [Route("wechat/profit/amountquery")]
  267. public Task<WeChatPayProfitSharingOrderAmountQueryResponse> WeChatPayProfitSharingOrderAmountQuery(string TransactionId,string appId)
  268. {
  269. return _IProfitService.WeChatPayProfitSharingOrderAmountQuery(TransactionId, appId);
  270. }
  271. }
  272. }