Beispiel #1
0
        async Task <Subscribe> OnPayedAsync(Pay pay)  //當付款成功紀錄後執行
        {
            var bill = _billsService.GetById(pay.BillId);

            if (bill == null)
            {
                throw new BillNotFoundWhilePay($"bill id: {pay.BillId}");
            }

            if (!bill.Payed)
            {
                throw new NotPayedAfterPay(bill, pay);
            }

            bill.DeadLine = null;

            var payWay = _paysService.FindPayWayByCode(pay.PayWay);

            if (payWay == null)
            {
                _logger.LogException(new PayWayNotFound(pay.PayWay));
            }
            else
            {
                bill.PayWayId = payWay.Id;
            }

            await _billsService.UpdateAsync(bill);

            //建立 Subscribe
            var subscribe = _subscribesService.Find(bill);

            if (subscribe == null)
            {
                subscribe = await _subscribesService.CreateAsync(Subscribe.Create(bill));
            }

            //加入角色
            if (subscribe.Active)
            {
                await _usersService.AddSubscriberRoleAsync(subscribe.UserId);
            }

            return(subscribe);
        }