Beispiel #1
0
        public async Task <IActionResult> Prepare([FromBody] DonationSubmissionModel model)
        {
            if (!ModelState.IsValid)
            {
                var errors = ModelState.ConcatErrors();
                return(BadRequest(errors));
            }
            var userId = User.GetId();

            model.Amount = Math.Round(model.Amount, 2);
            // Create the donation record.
            var donation = new Donation
            {
                Amount      = model.Amount,
                Message     = model.Message,
                Public      = model.Public,
                PaymentType = model.PaymentType,
                PaymentId   = "",
                PayerId     = "",
                UserId      = userId,
                State       = PaymentState.Initialized,
                Date        = DateTimeOffset.Now
            };

            DbContext.Donations.Add(donation);
            await DbContext.SaveChangesSafe();

            var payment = await _donationService.Prepare(model, donation.Id, userId, Request.GetUri().GetLeftPart(UriPartial.Authority));

            if (payment.HasError)
            {
                donation.State = PaymentState.Failed;
                await DbContext.SaveChangesSafe();

                return(BadRequest(payment.Error));
            }
            donation.PaymentId = payment.PaymentId;
            donation.TokenId   = payment.TokenId;
            donation.State     = PaymentState.Processing;
            await DbContext.SaveChangesSafe();

            return(Ok(payment));
        }