public CreatePaymentResponse CreatePayment(CreatePaymentRequest req) { var res = new CreatePaymentResponse(); try { using (var idmClient = new IDMServiceClient()) using (var container = new TransactionModelContainer()) { ValidateCreatePaymentRequest(idmClient, req); var payment = new Payment( req.CreateBy, req.CustomerIDMPartyID, req.CustomerName, req.CustomerAddress, req.PaymentItems.CreatePaymentItems() ); var grandTotal = payment.GrandTotal(); var credits = container.CustomerCredits.Where(x => x.CustomerIdmPartyId == req.CustomerIDMPartyID && x.IsUsedOrRefund == false).ToList(); var credit = credits.GetCustomerCredit(grandTotal); foreach (var c in credit) payment.AddPaymentItem(PaymentItem.CreateCustomerCredit(c.Id, c.Amount)); CustomerCredit.UpdateCustomerCredit(credit); container.Payments.AddObject(payment); container.SaveChanges(); res.SetPaymentResponse(payment); res.Succeed(); } } catch (Exception x) { res.Fail(x); CreateLog(req, x); } return res; }