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)); }
public async Task <EcPayTradeModel> BeginPayAsync(int billId, BillViewModel model, string userId) { // BeginPay 支付帳單 var existingBill = _billsService.GetById(billId); if (existingBill == null) { return(null); } int paywayId = model.PayWayId; var payway = _paysService.GetPayWayById(paywayId); if (payway == null) { throw new EntityNotFoundException(new PayWay { Id = paywayId }); } if (existingBill.Payed) { throw new BadRequestException(new RequestErrorViewModel { Key = "payed", Message = "此訂單已經支付過了" }); } if (existingBill.Expired) { throw new BadRequestException(new RequestErrorViewModel { Key = "expired", Message = "訂單已過繳款期限" }); } var pay = Pay.Create(existingBill, payway, ThirdPartyPayment.EcPay); var amount = Convert.ToInt32(existingBill.NeedPayMoney); var ecPayTradeModel = _thirdPartyPayService.CreateEcPayTrade(pay, amount); if (!ecPayTradeModel.HasToken) { throw new BadRequestException(new RequestErrorViewModel { Key = "", Message = "開啟支付程序失敗,請稍候再試." }); } await _paysService.CreateAsync(pay); if (existingBill.PayWayId != paywayId) { existingBill.PayWayId = paywayId; await _billsService.UpdateAsync(existingBill); } ecPayTradeModel.PaymentType = payway.Code; return(ecPayTradeModel); }