Example #1
0
 protected virtual string Validate(DonationSubmissionModel model)
 {
     if (model.Amount < 1.0m)
     {
         return("Donation amount must be at least $1.00.");
     }
     return(null);
 }
Example #2
0
        public async Task <DonationResultModel> Prepare(DonationSubmissionModel model, int id, string userId, string urlRoot)
        {
            var result = new DonationResultModel
            {
                Id          = id,
                UserId      = userId,
                Error       = Validate(model),
                PaymentType = PaymentType
            };

            if (result.HasError)
            {
                return(result);
            }
            result = await DoPreparation(model, id, userId, urlRoot, result);

            return(result);
        }
Example #3
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));
        }
Example #4
0
 protected abstract Task <DonationResultModel> DoPreparation(DonationSubmissionModel model, int id, string userId, string urlRoot, DonationResultModel result);
Example #5
0
        protected override async Task <DonationResultModel> DoPreparation(DonationSubmissionModel model, int id, string userId, string urlRoot, DonationResultModel result)
        {
            var userInfo = await GetUserInfo(userId) ?? new UserPaymentInfo
            {
                UserName = "******",
                Email    = "",
                Id       = ""
            };
            var amt     = Math.Round(model.Amount, 2).ToString("0.00");
            var context = GetContext();
            var payment = new Payment
            {
                experience_profile_id = Config["Authentication:PayPal:ProfileID"],
                intent = "sale",
                payer  = new Payer
                {
                    payment_method = "paypal",
                    payer_info     = new PayerInfo
                    {
                        first_name = userInfo.UserName,
                        last_name  = userInfo.Id,
                        email      = userInfo.Email
                    }
                },
                transactions = new List <Transaction>
                {
                    new Transaction
                    {
                        description    = "Nakama Network Donation",
                        invoice_number = id.ToString(),
                        amount         = new Amount
                        {
                            currency = "USD",
                            total    = amt,
                            details  = new Details
                            {
                                subtotal = amt
                            }
                        },
                        item_list = new ItemList
                        {
                            items = new List <Item>
                            {
                                new Item
                                {
                                    name     = "Nakama Network Donation",
                                    price    = amt,
                                    quantity = "1",
                                    currency = "USD"
                                }
                            }
                        }
                    }
                },
                redirect_urls = new RedirectUrls
                {
                    return_url = urlRoot + "/donate/update",
                    cancel_url = urlRoot + "/donate/cancel"
                }
            };

            try
            {
                var serverPayment = Payment.Create(context, payment);
                result.RedirectUrl = serverPayment.links.First(x => x.rel == "approval_url").href;
                result.PaymentId   = serverPayment.id;
                result.TokenId     = serverPayment.token;
            }
            catch
            {
                result.Error = "An error has occurred trying to submit your payment. Please try again later. " +
                               "You have not been charged.";
            }
            result.State = GetState(payment.state);
            return(result);
        }