李昊 4 years ago
parent
commit
b509ecc331

+ 13 - 2
gx_api/GxPress/Api/GxPress.Api/WebControllers/AlipayController.cs

@@ -2,6 +2,7 @@ using System.Collections.Generic;
 using System.Threading.Tasks;
 using Alipay.AopSdk.Core.Util;
 using GxPress.Common.AliPay;
+using GxPress.Common.WechatPay;
 using GxPress.Service.Interface.Order;
 using Microsoft.AspNetCore.Authorization;
 using Microsoft.AspNetCore.Http;
@@ -29,7 +30,7 @@ namespace GxPress.Api.WebControllers
         public string GetNativePayUrl()
         {
             var alipay = new PcPay();
-            return alipay.PayRequest("637263608658642540_7", "汪峰讲故事", "0.01", "汪峰讲故事");
+            return alipay.PayRequest("637263608658642543_7", "汪峰讲故事", "0.01", "汪峰讲故事");
         }
         /// <summary>
         /// 回调地址
@@ -50,7 +51,17 @@ namespace GxPress.Api.WebControllers
                         //Console.WriteLine($"同步验证通过,订单号:{sArray["out_trade_no"]}");
                         if (await orderService.InsertAliPayOrderAsync(sArray))
                         {
-                            _context.Response.StatusCode = 200;
+                            var res = new WxPayData();
+                            res.SetValue("out_trade_no", sArray["out_trade_no"]);
+                            res.SetValue("attach", sArray["out_trade_no"]);
+                            res.SetValue("total_fee", sArray["total_amount"]);
+                            res.SetValue("transaction_id", sArray["trade_no"]);
+                            //创建订单
+                            if (await orderService.InsertWxOrderAsync(res))
+                            {
+                                _context.Response.StatusCode = 200;
+
+                            }
                         }
                         else
                         {

+ 141 - 0
gx_api/GxPress/Api/GxPress.Api/WebControllers/PayController.cs

@@ -0,0 +1,141 @@
+using System;
+using System.Threading.Tasks;
+using GxPress.Auth;
+using GxPress.Common.AliPay;
+using GxPress.Common.Exceptions;
+using GxPress.Common.Tools;
+using GxPress.Common.WechatPay;
+using GxPress.EnumConst;
+using GxPress.Repository.Interface.Media;
+using GxPress.Repository.Interface.VipEquity;
+using GxPress.Request.Pay;
+using GxPress.Service.Interface.Order;
+using Microsoft.AspNetCore.Authorization;
+using Microsoft.AspNetCore.Http;
+using Microsoft.AspNetCore.Mvc;
+using GxPress.Repository.Interface.Order;
+
+namespace GxPress.Api.WebControllers
+{
+    /// <summary>
+    /// 支付接口
+    /// </summary>
+    [Route("api/web/pay")]
+    [ApiController]
+    [Authorize]
+    public class PayController : Controller
+    {
+        private readonly IOrderService orderService;
+        private IHttpContextAccessor _contextAccessor;
+        private readonly ILoginContext _loginContext;
+        private HttpContext _context { get { return _contextAccessor.HttpContext; } }
+        private readonly IMediaRepository mediaRepository;
+        private readonly IVipEquityRepository vipEquityRepository;
+        private IOrderRepository orderRepository;
+        public PayController(IOrderService orderService, IHttpContextAccessor contextAccessor, ILoginContext _loginContext, IMediaRepository mediaRepository, IVipEquityRepository vipEquityRepository, IOrderRepository orderRepository)
+        {
+            this.orderService = orderService;
+            _contextAccessor = contextAccessor;
+            this._loginContext = _loginContext;
+            this.mediaRepository = mediaRepository;
+            this.vipEquityRepository = vipEquityRepository;
+            this.orderRepository = orderRepository;
+        }
+        /// <summary>
+        /// 统一的返回支付地址
+        /// </summary>
+        /// <param name="request"></param>
+        /// <returns></returns>
+        [HttpPost()]
+        public async Task<string> GetNativePayUrl(PayRequest request)
+        {
+            if (request.PayWayType == 0)
+                throw new BusinessException("支付方式有误");
+            var userId = _loginContext.AccountId;
+            if (userId == 0)
+                _context.Response.StatusCode = LoginCodeConst.UnLogin.GetHashCode();
+            else
+            {
+                var attach = string.Empty;
+                decimal price = 0;
+                var title = string.Empty;
+                var antoNumber = DateTime.Now.Ticks.ToString();
+                if (request.MediaId > 0 || request.VipType > 0)
+                {
+                    //商品
+                    if (request.MediaId > 0)
+                    {
+                        var media = await mediaRepository.GetAsync(request.MediaId);
+                        if (media != null)
+                            attach = $"{antoNumber}";
+                        else
+                            throw new BusinessException("服务器异常");
+                        price = decimal.Parse("0.01");
+                        //创建订单
+                        var order = new Entity.Order.Order()
+                        {
+                            IsRefund = false,
+                            PayWay = request.PayWayType,
+                            OrderType = 1,
+                            IsVip = false,
+                            MediaId = media.Id,
+                            OrderNumber = antoNumber,
+                            UserId = userId,
+                            Name = _loginContext.Name,
+                            Explain = media.Title,
+                            Price = price,
+                            IsSuccess = false
+                        };
+                        var orderId = await orderRepository.InsertAsync(order);
+                        if (orderId <= 0)
+                            throw new BusinessException("服务器异常");
+
+                    }
+                    //权益
+                    if (request.VipType > 0)
+                    {
+                        var vipEquity = await vipEquityRepository.GetByVipTypeAsync(request.VipType);
+                        var vipTypeConst = ((VipCardTypeConst)vipEquity.VipType);
+                        if (vipTypeConst.GetHashCode() == 0)
+                            throw new BusinessException("服务器异常");
+                        // attach = $"{antoNumber}_{userId}_{PayUrlRequetTypeConst.Vip.GetHashCode()}_{vipEquity.Id}";
+                        price = decimal.Parse("0.01");
+                        var order = new Entity.Order.Order()
+                        {
+                            IsRefund = false,
+                            PayWay = request.PayWayType,
+                            OrderType = 2,
+                            IsVip = false,
+                            MediaId = vipEquity.Id,
+                            OrderNumber = antoNumber,
+                            UserId = userId,
+                            Name = _loginContext.Name,
+                            Explain = vipTypeConst.GetDescriptionOriginal(),
+                            Price = price,
+                            IsSuccess = false
+                        };
+                        var orderId = await orderRepository.InsertAsync(order);
+                        if (orderId <= 0)
+                            throw new BusinessException("服务器异常");
+                    }
+                    //支付宝
+                    if (request.PayWayType == PayWayTypeConst.AliyPay.GetHashCode())
+                    {
+                        var alipay = new PcPay();
+                        return alipay.PayRequest(attach, title, price.ToString(), title);
+                    }
+                    else
+                    {
+                        var nativePay = new NativePay();
+                        return nativePay.GetPayUrl(antoNumber, title, attach, Convert.ToInt32(price * 100));
+                    }
+                }
+                else
+                    throw new BusinessException("参数错误");
+            }
+            return string.Empty;
+
+
+        }
+    }
+}

+ 1 - 1
gx_api/GxPress/Api/GxPress.Api/WebControllers/WxpayController.cs

@@ -31,7 +31,7 @@ namespace GxPress.Api.WebControllers
         public string GetNativePayUrl()
         {
             var nativePay = new NativePay();
-            return nativePay.GetPayUrl("637263608658642540", "汪峰讲故事", 7, 1);
+            return nativePay.GetPayUrl("637263608658642540", "汪峰讲故事", "637263608658642540_7_2", 1);
         }
         /// <summary>
         /// 回调地址

+ 3 - 3
gx_api/GxPress/Infrastructure/GxPress.Common/WechatPay/NativePay.cs

@@ -9,14 +9,14 @@ namespace GxPress.Common.WechatPay
       * @param productId 商品ID
       * @return 模式二URL
       */
-        public string GetPayUrl(string productId,string productInfo,int userId,int total_fee)
+        public string GetPayUrl(string productId, string productInfo, string attach, int total_fee)
         {
             //Log.Info(this.GetType().ToString(), "Native pay mode 2 url is producing...");
             try
             {
                 WxPayData data = new WxPayData();
                 data.SetValue("body", productInfo);//商品描述
-                data.SetValue("attach", $"{productId}_{userId}");//附加数据
+                data.SetValue("attach", attach);//附加数据
                 data.SetValue("out_trade_no", WxPayApi.GenerateOutTradeNo());//随机字符串
                 data.SetValue("total_fee", total_fee);//总金额
                 data.SetValue("time_start", DateTime.Now.ToString("yyyyMMddHHmmss"));//交易起始时间
@@ -69,6 +69,6 @@ namespace GxPress.Common.WechatPay
             return result.ToPrintStr();
         }
 
-        
+
     }
 }

+ 13 - 0
gx_api/GxPress/Model/GxPress.EnumConst/LoginCodeConst.cs

@@ -0,0 +1,13 @@
+using System.ComponentModel;
+
+namespace GxPress.EnumConst
+{
+    /// <summary>
+    /// 登陆CODE
+    /// </summary>
+    public enum LoginCodeConst
+    {
+        [Description("未登录")]
+        UnLogin = 201,
+    }
+}

+ 11 - 1
gx_api/GxPress/Model/GxPress.EnumConst/VipTypeConst.cs

@@ -38,10 +38,20 @@ namespace GxPress.EnumConst
     /// </summary>
     public enum PayWayTypeConst
     {
-        WeChat = 1,
+        [Description("微信支付")]
+        WeChatPay = 1,
+        [Description("支付宝")]
         AliyPay = 2
     }
     /// <summary>
+    /// 支付类型
+    /// </summary>
+    public enum PayUrlRequetTypeConst
+    {
+        Media = 1,
+        Vip = 2,
+    }
+    /// <summary>
     /// 资源类型 文章100书籍20,课程30,音频40,期刊50,视频60,名栏90,公众号110,刊期120
     /// </summary>
     public enum ResourceTypeConst

+ 24 - 0
gx_api/GxPress/Model/GxPress.Request/Pay/PayRequest.cs

@@ -0,0 +1,24 @@
+namespace GxPress.Request.Pay
+{
+    /// <summary>
+    /// 支付
+    /// </summary>
+    public class PayRequest
+    {
+        /// <summary>
+        /// 1 月 2 季度 3 年 4连续月
+        /// </summary>
+        /// <value></value>
+        public int VipType { get; set; }
+        /// <summary>
+        /// 媒体ID
+        /// </summary>
+        /// <value></value>
+        public int MediaId { get; set; }
+        /// <summary>
+        /// 支付方式 1 微信 2支付宝
+        /// </summary>
+        /// <value></value>
+        public int PayWayType { get; set; }
+    }
+}

+ 5 - 0
gx_api/GxPress/Model/GxPress.Result/Media/MediaResult.cs

@@ -9,6 +9,11 @@ namespace GxPress.Result.Media
     /// </summary>
     public class MediaResult
     {
+         /// <summary>
+        /// 编号
+        /// </summary>
+        /// <value></value>
+        public string AutoNumber { get; set; }
         /// <summary>
         /// 
         /// </summary>

+ 5 - 0
gx_api/GxPress/Repository/GxPress.Repository.Implement/Order/OrderRepository.cs

@@ -123,5 +123,10 @@ namespace GxPress.Repository.Implement.Order
         {
             return await _repository.GetAsync(Q.Where(nameof(Entity.Order.Order.OrderNumber), orderNumber));
         }
+
+        public async Task<bool> UpdateAsync(Entity.Order.Order order)
+        {
+            return await _repository.UpdateAsync(order);
+        }
     }
 }

+ 10 - 1
gx_api/GxPress/Repository/GxPress.Repository.Implement/VipEquity/VipEquityRepository.cs

@@ -67,7 +67,7 @@ namespace GxPress.Repository.Implement.VipEquity
             return await _repository.DeleteAsync(id);
         }
         /// <summary>
-        /// /// 查询
+        ///  查询
         /// </summary>
         /// <param name="id"></param>
         /// <returns></returns>
@@ -75,5 +75,14 @@ namespace GxPress.Repository.Implement.VipEquity
         {
             return await _repository.GetAsync(id);
         }
+        /// <summary>
+        /// 查询1 月 2 季度 3 年 4连续月
+        /// </summary>
+        /// <param name="id"></param>
+        /// <returns></returns>
+        public async Task<Entity.tede2.VipEquity.VipEquity> GetByVipTypeAsync(int vipType)
+        {
+            return await _repository.GetAsync(Q.Where(nameof(Entity.tede2.VipEquity.VipEquity.VipType),vipType));
+        }
     }
 }

+ 1 - 0
gx_api/GxPress/Repository/GxPress.Repository.Interface/Order/IOrderRepository.cs

@@ -32,5 +32,6 @@ namespace GxPress.Repository.Interface.Order
         /// <param name="orderNumber"></param>
         /// <returns></returns>
         Task<Entity.Order.Order> GetOrderAsync(string orderNumber);
+        Task<bool> UpdateAsync(Entity.Order.Order order);
     }
 }

+ 7 - 1
gx_api/GxPress/Repository/GxPress.Repository.Interface/VipEquity/IVipEquityRepository.cs

@@ -18,6 +18,12 @@ namespace GxPress.Repository.Interface.VipEquity
         /// </summary>
         /// <param name="id"></param>
         /// <returns></returns>
-         Task<Entity.tede2.VipEquity.VipEquity> GetAsync(int id);
+        Task<Entity.tede2.VipEquity.VipEquity> GetAsync(int id);
+        /// <summary>
+        /// 查询1 月 2 季度 3 年 4连续月
+        /// </summary>
+        /// <param name="id"></param>
+        /// <returns></returns>
+        Task<Entity.tede2.VipEquity.VipEquity> GetByVipTypeAsync(int vipType);
     }
 }

+ 45 - 21
gx_api/GxPress/Service/GxPress.Service.Implement/Order/OrderService.cs

@@ -6,6 +6,10 @@ using GxPress.Repository.Interface.Media;
 using GxPress.Repository.Interface;
 using System;
 using System.Collections.Generic;
+using GxPress.Repository.Interface.VipEquity;
+using GxPress.Common.Tools;
+using System.Transactions;
+using GxPress.EnumConst;
 
 namespace GxPress.Service.Implement.Order
 {
@@ -14,11 +18,13 @@ namespace GxPress.Service.Implement.Order
         private readonly IOrderRepository orderRepository;
         private readonly IMediaRepository mediaRepository;
         private readonly IUserRepository userRepository;
-        public OrderService(IOrderRepository orderRepository, IMediaRepository mediaRepository, IUserRepository userRepository)
+        private readonly IVipEquityRepository vipEquityRepository;
+        public OrderService(IOrderRepository orderRepository, IMediaRepository mediaRepository, IUserRepository userRepository, IVipEquityRepository vipEquityRepository)
         {
             this.orderRepository = orderRepository;
             this.mediaRepository = mediaRepository;
             this.userRepository = userRepository;
+            this.vipEquityRepository = vipEquityRepository;
         }
         /// <summary>
         /// 创建微信订单
@@ -27,30 +33,48 @@ namespace GxPress.Service.Implement.Order
         /// <returns></returns>
         public async Task<bool> InsertWxOrderAsync(WxPayData data)
         {
-            var order = new Entity.Order.Order();
+
             var attach = data.GetValue("attach").ToString();
             if (attach.Split('_').Length > 0)
             {
-                //获取medio属性
-                var media = await mediaRepository.GetAsync(attach.Split('_')[0]);
-                //获取用户数据
-                var user = await userRepository.GetAsync(int.Parse(attach.Split('_')[1]));
-                order.OutTradeNo = data.GetValue("out_trade_no").ToString();
-                order.OrderType = 1;
-                order.IsRefund = false;
-                order.IsSuccess = true;
-                order.IsVip = user.IsVip;
-                order.MediaId = media.Id;
-                order.Explain = media.Title;
-                order.Name = user.Name;
-                order.OrderNumber = DateTime.Now.Ticks.ToString();
-                order.Price = Decimal.Parse(data.GetValue("total_fee").ToString()) / 100;
-                order.TransactionId = data.GetValue("transaction_id").ToString();
-                order.UserId = user.Id;
-                order.PayWay = 1;
-                return await orderRepository.InsertAsync(order) > 0;
+                return true;
             }
-            return false;
+            try
+            {
+                using (var Transaction = new TransactionScope())
+                {
+                    var order = await orderRepository.GetOrderAsync(attach.ToString());
+                    var user = await userRepository.GetAsync(order.UserId);
+                    //获取用户数据
+                    order.Price = order.PayWay == PayWayTypeConst.WeChatPay.GetHashCode() ? Decimal.Parse(data.GetValue("total_fee").ToString()) / 100 : Decimal.Parse(data.GetValue("total_fee").ToString());
+                    order.TransactionId = data.GetValue("transaction_id").ToString();
+                    order.OutTradeNo = data.GetValue("out_trade_no").ToString();
+                    order.IsSuccess = true;
+                    //如果是用户增加
+                    if (order.OrderType == 2)
+                    {
+                        var vipEquity = await vipEquityRepository.GetAsync(order.MediaId);
+                        user.IsVip = true;
+                        if ((VipCardTypeConst)vipEquity.VipType == VipCardTypeConst.Month)
+                            user.EndTime = DateTime.Now.AddMonths(1);
+                        if ((VipCardTypeConst)vipEquity.VipType == VipCardTypeConst.Season)
+                            user.EndTime = DateTime.Now.AddMonths(3);
+                        if ((VipCardTypeConst)vipEquity.VipType == VipCardTypeConst.Year)
+                            user.EndTime = DateTime.Now.AddYears(1);
+                        user.AccumulativePrice = user.AccumulativePrice + order.Price;
+                        await userRepository.UpdateAsync(user);
+                    }
+                    if (user.IsVip)
+                        order.IsVip = true;
+                    Transaction.Complete();
+                    return await orderRepository.UpdateAsync(order);
+                }
+            }
+            catch
+            {
+                return false;
+            }
+
         }
         /// <summary>
         /// 支付宝