示例#1
0
        public IActionResult Post([FromBody] PaymentTypeDto paymentTypeDto)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                var paymentType = Mapper.Map <PaymentType>(paymentTypeDto);
                if (_paymentTypeRepository.PaymentTypeExists(paymentType))
                {
                    return(StatusCode(500, "PaymentType already exists."));
                }

                var userId             = User.FindFirstValue(ClaimTypes.NameIdentifier);
                var profile            = _accountRepository.GetUserProfile(userId);
                var createdPaymentType = _paymentTypeRepository.CreatePaymentType(paymentType, profile.UserProfileId);

                if (createdPaymentType == null)
                {
                    return(StatusCode(500, "A problem happened while handling your request."));
                }

                var createdPaymentTypeToReturn = Mapper.Map <PaymentTypeDto>(createdPaymentType);
                return(Created(createdPaymentTypeToReturn));
            }
            catch (Exception ex)
            {
                _logger.LogError($"Failed in Post /PaymentTypes: {ex}");
                return(StatusCode(500, "A problem happened while handling your request."));
            }
        }
示例#2
0
        public MpPaymentDetailReturn PostPayment(MpDonationAndDistributionRecord paymentRecord)
        {
            //check if invoice exists
            if (!_invoiceRepository.InvoiceExists(paymentRecord.InvoiceId))
            {
                throw new InvoiceNotFoundException(paymentRecord.InvoiceId);
            }

            //check if contact exists
            if (_contactRepository.GetContactById(paymentRecord.ContactId) == null)
            {
                throw new ContactNotFoundException(paymentRecord.ContactId);
            }

            if (paymentRecord.ProcessorId.Length > 50)
            {
                throw new Exception("Max length of 50 exceeded for transaction code");
            }

            var pymtId = PaymentType.GetPaymentType(paymentRecord.PymtType).id;
            var fee    = paymentRecord.FeeAmt.HasValue ? paymentRecord.FeeAmt / Constants.StripeDecimalConversionValue : null;

            //check if payment type exists
            if (!_paymentTypeRepository.PaymentTypeExists(pymtId))
            {
                throw new PaymentTypeNotFoundException(pymtId);
            }

            //create payment -- send model
            var payment = new MpPayment
            {
                InvoiceNumber      = paymentRecord.InvoiceId.ToString(),
                ContactId          = paymentRecord.ContactId,
                TransactionCode    = paymentRecord.ProcessorId,
                PaymentDate        = DateTime.Now,
                PaymentTotal       = paymentRecord.DonationAmt,
                PaymentTypeId      = pymtId,
                PaymentStatus      = _defaultPaymentStatus,
                ProcessorFeeAmount = fee
            };
            var paymentDetail = new MpPaymentDetail
            {
                Payment         = payment,
                PaymentAmount   = paymentRecord.DonationAmt,
                InvoiceDetailId = _invoiceRepository.GetInvoiceDetailForInvoice(paymentRecord.InvoiceId).InvoiceDetailId
            };

            var result = _paymentRepository.CreatePaymentAndDetail(paymentDetail);

            if (result.Status)
            {
                //update invoice payment status
                var invoice  = _invoiceRepository.GetInvoice(paymentRecord.InvoiceId);
                var payments = _paymentRepository.GetPaymentsForInvoice(paymentRecord.InvoiceId);
                payments = payments.Where(p => p.PaymentStatus != _declinedPaymentStatus).ToList();
                var paymentTotal = payments.Sum(p => p.PaymentTotal);

                _invoiceRepository.SetInvoiceStatus(paymentRecord.InvoiceId, paymentTotal >= invoice.InvoiceTotal ? _paidinfullStatus : _somepaidStatus);
                return(result.Value);
            }
            else
            {
                throw new Exception("Unable to save payment data");
            }
        }