コード例 #1
0
ファイル: PayController.cs プロジェクト: fzh1111/SLCS_H5
        //创建支付请求
        public ActionResult Add(Enums.PayType payType, Enums.PaySourceType sourceType, string sourceIds)
        {
            var modelPay = new Pay
            {
                PayNo      = Common.CreateNo(),
                UserId     = currentUser.UserId,
                PayType    = payType,
                Status     = Enums.PayStatus.Wait,
                SourceType = sourceType,
                SourceIds  = sourceIds,
                AddTime    = DateTime.Now,
                LastTime   = DateTime.Now
            };
            var isWaitPay = false;

            switch (sourceType)
            {
            case Enums.PaySourceType.RechargeBalance:
                var modelRecharge = serviceRecharge.GetModel(sourceIds.To <long>());
                modelPay.Total  = modelRecharge.Total;
                modelPay.Remark = "充值";
                isWaitPay       = modelRecharge.Status == Enums.RechargeStatus.Wait;
                break;

            case Enums.PaySourceType.Order:
                var listOrder = serviceOrder.GetListByOrderIds(modelPay.SourceIds);
                modelPay.Total  = listOrder.Sum(s => s.Total);
                modelPay.Remark = "订单:" + string.Join(",", listOrder.Select(s => s.OrderNo));
                isWaitPay       = listOrder[0].Status == Enums.OrderStatus.WaitPay;
                break;
            }
            if (isWaitPay)
            {
                modelPay.PayId = servicePay.Add(modelPay);
                if (modelPay.Total > 0 && !Config.IsTest)
                {
                    //调用支付接口
                    switch (payType)
                    {
                    case Enums.PayType.WeChatMP:
                        var modelUser = serviceUser.GetModel(modelPay.UserId);

                        if (modelUser.WeChatMPOpenId == null || modelUser.WeChatMPOpenId.Length < 1)
                        {
                            result.mes = "请先关注公众号【三里茶社】!";
                        }
                        else
                        {
                            result = WeChatService.PayByMP(modelPay.PayNo, modelPay.Total, modelUser.WeChatMPOpenId, modelPay.Remark);
                        }
                        break;

                    case Enums.PayType.Balance:
                        if (serviceMoney.GetModel(currentUser.UserId).Balance >= modelPay.Total)
                        {
                            if (servicePay.PayComplete(modelPay))
                            {
                                result.data = Config.Pay_SuccessUrl;
                                result.code = -1;
                            }
                            else
                            {
                                result.mes = "余额不足";
                            }
                        }
                        else
                        {
                            result.mes = "余额不足";
                        }
                        break;
                    }
                }
                else
                {
                    if (servicePay.PayComplete(modelPay))
                    {
                        result.data = Config.Pay_SuccessUrl;
                        result.code = -1;
                    }
                    else
                    {
                        result.mes = "余额不足";
                    }
                }


                //支付完成后,更新订单使用的优惠券状态  201909
                if (sourceType == Enums.PaySourceType.Order && result.code == -1)
                {
                    var listOrder = serviceOrder.GetListByOrderIds(modelPay.SourceIds);

                    foreach (var roder in listOrder)
                    {
                        if (roder.OrderTicketId > 0)
                        {
                            userTicket.UpdateStatus(roder.OrderTicketId, 2, currentUser.UserId);
                        }
                    }
                }
            }
            else
            {
                result.mes = "订单已支付成功,请勿重复支付";
            }



            return(Json(result));
        }
コード例 #2
0
        //退款
        public JsonResult Refund(long id)
        {
            var modelOrder = serviceOrder.GetModel(id);

            if (modelOrder.UserId == currentUser.UserId)
            {
                modelOrder.Items = serviceDetail.GetListDtoByOrderId(id);
                var minTime = modelOrder.Items[0].ReserveTimes.Min();
                if (modelOrder.Status == Enums.OrderStatus.Unused && minTime > DateTime.Now.AddMinutes(30))
                {
                    //获取支付订单
                    var modelPay = servicePay.GetModelBySourceId(Enums.PaySourceType.Order, id);
                    //创建退款订单
                    var modelRefund = new Pay
                    {
                        PayNo      = Common.CreateNo(),
                        UserId     = currentUser.UserId,
                        PayType    = Enums.PayType.Refund,
                        Total      = modelOrder.Total * -1,
                        Status     = Enums.PayStatus.Wait,
                        SourceType = Enums.PaySourceType.Order,
                        SourceIds  = id.ToString(),
                        Remark     = "订单:" + modelOrder.OrderNo,
                        AddTime    = DateTime.Now,
                        LastTime   = DateTime.Now
                    };
                    try
                    {
                        db.BeginTransaction();
                        //修改订单状态
                        serviceOrder.UpdateStatus(id, Enums.OrderStatus.Refund, Enums.OrderStatus.Unused);
                        serviceDetail.UpdateStatusByOrderId(id, id * -1);
                        modelRefund.PayId = servicePay.Add(modelRefund);
                        if (modelPay.PayType == Enums.PayType.Balance)
                        {
                            //修改退款状态
                            servicePay.UpdateStatus(modelRefund.PayId, Enums.PayStatus.Success, Enums.PayStatus.Wait);
                            //创建余额进账流水
                            serviceFlow.Add(new Flow
                            {
                                Currency   = Enums.Currency.Balance,
                                Total      = modelOrder.Total,
                                SourceType = Enums.FlowSourceType.Consume,
                                SourceIds  = modelRefund.PayId.ToString(),
                                UserId     = modelRefund.UserId,
                                AddTime    = DateTime.Now,
                                Remark     = "退款编号:" + modelRefund.PayNo
                            });
                        }
                        db.CommitTransaction();
                        result.code = 1;
                    }
                    catch (Exception ex)
                    {
                        db.RollbackTransaction();
                        result.mes = ex.Message;
                    }
                    if (result.code == 1)
                    {
                        //调用退款接口
                        if (modelPay.PayType == Enums.PayType.WeChatMP)
                        {
                            WeChatService.Refund(modelPay.PayNo, modelRefund.PayNo, modelPay.Total, modelRefund.Total * -1);
                        }
                    }
                }
                else
                {
                    result.mes = "订单开始前的30之前才可退款!";
                }
            }
            return(Json(result));
        }