OrderService.cs 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System.Threading.Tasks;
  2. using GxPress.Common.WechatPay;
  3. using GxPress.Repository.Interface.Order;
  4. using GxPress.Service.Interface.Order;
  5. using GxPress.Repository.Interface.Media;
  6. using GxPress.Repository.Interface;
  7. using System;
  8. using System.Collections.Generic;
  9. namespace GxPress.Service.Implement.Order
  10. {
  11. public class OrderService : IOrderService
  12. {
  13. private readonly IOrderRepository orderRepository;
  14. private readonly IMediaRepository mediaRepository;
  15. private readonly IUserRepository userRepository;
  16. public OrderService(IOrderRepository orderRepository, IMediaRepository mediaRepository, IUserRepository userRepository)
  17. {
  18. this.orderRepository = orderRepository;
  19. this.mediaRepository = mediaRepository;
  20. this.userRepository = userRepository;
  21. }
  22. /// <summary>
  23. /// 创建微信订单
  24. /// </summary>
  25. /// <param name="data"></param>
  26. /// <returns></returns>
  27. public async Task<bool> InsertWxOrderAsync(WxPayData data)
  28. {
  29. var order = new Entity.Order.Order();
  30. var attach = data.GetValue("attach").ToString();
  31. if (attach.Split('_').Length > 0)
  32. {
  33. //获取medio属性
  34. var media = await mediaRepository.GetAsync(attach.Split('_')[0]);
  35. //获取用户数据
  36. var user = await userRepository.GetAsync(int.Parse(attach.Split('_')[1]));
  37. order.OutTradeNo = data.GetValue("out_trade_no").ToString();
  38. order.OrderType = 1;
  39. order.IsRefund = false;
  40. order.IsSuccess = true;
  41. order.IsVip = user.IsVip;
  42. order.MediaId = media.Id;
  43. order.Explain = media.Title;
  44. order.Name = user.Name;
  45. order.OrderNumber = DateTime.Now.Ticks.ToString();
  46. order.Price = Decimal.Parse(data.GetValue("total_fee").ToString()) / 100;
  47. order.TransactionId = data.GetValue("transaction_id").ToString();
  48. order.UserId = user.Id;
  49. order.PayWay = 1;
  50. return await orderRepository.InsertAsync(order) > 0;
  51. }
  52. return false;
  53. }
  54. /// <summary>
  55. /// 支付宝
  56. /// </summary>
  57. /// <param name="sArray"></param>
  58. /// <returns></returns>
  59. public async Task<bool> InsertAliPayOrderAsync(Dictionary<string, string> sArray)
  60. {
  61. var order = new Entity.Order.Order();
  62. var attach = sArray["out_trade_no"].ToString();
  63. if (attach.Split('_').Length > 0)
  64. {
  65. //获取medio属性
  66. var media = await mediaRepository.GetAsync(attach.Split('_')[0]);
  67. //获取用户数据
  68. var user = await userRepository.GetAsync(int.Parse(attach.Split('_')[1]));
  69. order.OutTradeNo = sArray["out_trade_no"].ToString();
  70. order.OrderType = 1;
  71. order.IsRefund = false;
  72. order.IsSuccess = true;
  73. order.IsVip = user.IsVip;
  74. order.MediaId = media.Id;
  75. order.Explain = media.Title;
  76. order.Name = user.Name;
  77. order.OrderNumber = DateTime.Now.Ticks.ToString();
  78. order.Price = decimal.Parse(sArray["total_amount"].ToString());
  79. order.TransactionId = sArray["trade_no"].ToString();
  80. order.UserId = user.Id;
  81. order.PayWay = 2;
  82. return await orderRepository.InsertAsync(order) > 0;
  83. }
  84. return false;
  85. }
  86. }
  87. }