Exemplo n.º 1
0
        public async Task <BillViewModel> StoreBillAsync(BillViewModel model, string userId)
        {
            //驗證表單
            int planId       = model.PlanId;
            var selectedPlan = await _plansService.GetByIdAsync(planId);

            if (selectedPlan == null)
            {
                throw new EntityNotFoundException(new Plan {
                    Id = planId
                });
            }

            int payWayId       = model.PayWayId;
            var selectedPayWay = _paysService.GetPayWayById(payWayId);

            if (selectedPayWay == null || !selectedPayWay.Active)
            {
                throw new EntityNotFoundException(new PayWay {
                    Id = payWayId
                });
            }

            //取得訂閱紀錄
            var indexModel = await GetSubscribesIndexViewAsync(userId);

            var selectedPlanView = selectedPlan.MapViewModel(_mapper);
            //核對方案與金額
            var activePlanView = await CheckForExceptionsAsync(userId, indexModel, selectedPlanView);

            //進行到這裡表示通過驗證. 開始建立Bill
            var bill = new Bill
            {
                UserId      = userId,
                PlanId      = activePlanView.Id,
                Amount      = activePlanView.Price,
                HasDiscount = activePlanView.HasDiscount,
                PayWayId    = payWayId,
                DeadLine    = DateTime.Today.AddDays(_subscribesSettings.BillDaysToExpire).ToEndDate()
            };

            bill = await _billsService.CreateAsync(bill);

            return(bill.MapViewModel(_mapper));
        }
Exemplo n.º 2
0
        public async Task <ActionResult> Update(int id, [FromBody] BillViewModel model)
        {
            // BeginPay 支付帳單
            var existingBill = _billsService.GetById(id);

            if (existingBill == null)
            {
                return(NotFound());
            }

            int paywayId = model.PayWayId;
            var payway   = _paysService.GetPayWayById(paywayId);

            if (payway == null)
            {
                throw new EntityNotFoundException(new PayWay {
                    Id = paywayId
                });
            }


            if (existingBill.Payed)
            {
                ModelState.AddModelError("payed", "此訂單已經支付過了");
                return(BadRequest(ModelState));
            }

            if (existingBill.Expired)
            {
                ModelState.AddModelError("expired", "訂單已過繳款期限");
                return(BadRequest(ModelState));
            }

            var pay = Pay.Create(existingBill, payway, ThirdPartyPayment.EcPay);

            var amount          = Convert.ToInt32(existingBill.NeedPayMoney);
            var ecPayTradeModel = _thirdPartyPayService.CreateEcPayTrade(pay, amount);

            if (!ecPayTradeModel.HasToken)
            {
                ModelState.AddModelError("", "開啟支付程序失敗,請稍候再試.");
                return(BadRequest(ModelState));
            }

            await _paysService.CreateAsync(pay);

            if (existingBill.PayWayId != paywayId)
            {
                existingBill.PayWayId = paywayId;
                await _billsService.UpdateAsync(existingBill);
            }

            ecPayTradeModel.PaymentType = payway.Code;

            return(Ok(ecPayTradeModel));
        }