예제 #1
0
        public async Task <ActionResult> ProcessPayment()
        {
            ViewBag.ShowSuccess = false;
            var     gateway = CreateBrainTreeGateway();
            Decimal amount;

            amount = Convert.ToDecimal(Request["amount"]);
            var nonce    = Request["payment_method_nonce"];
            var promoId  = Request["promoId"];
            var quantity = int.Parse(Request["quantity"]);

            ViewBag.Quantity = quantity;
            var promo = await _promoRepository.Get(promoId);

            ViewBag.Total = ViewBag.Quantity * promo.Price;

            var request = new TransactionRequest
            {
                Amount             = amount,
                PaymentMethodNonce = nonce,
                Options            = new TransactionOptionsRequest
                {
                    SubmitForSettlement = true
                }
            };
            Result <Transaction> result = gateway.Transaction.Sale(request);

            var transation = new PaymentTransaction
            {
                DateCreated     = DateTime.Now,
                PaymentId       = result.Target.Id,
                PromoId         = ObjectId.Parse(promoId),
                Quantity        = quantity,
                Status          = result.Target.Status.ToString(),
                TotalPrice      = amount,
                TransactionDate = DateTime.Now,
                User            = User.Identity.Name
            };
            await _paymentTransactionRepository.CreateSync(transation);

            if (result.IsSuccess())
            {
                for (var i = 1; i <= quantity; i++)
                {
                    await _couponRespository.CreateSync(new Coupon
                    {
                        DateCreated   = DateTime.Now,
                        PromoId       = ObjectId.Parse(promoId),
                        TransactionId = transation.Id,
                        User          = User.Identity.Name,
                        Status        = Enums.CouponStatus.Valid.ToString(),
                        DateExpires   = DateTime.Now.AddDays(promo.NumberOfDaysValid > 0 ? promo.NumberOfDaysValid : 30)
                    });
                }

                ViewBag.ShowSuccess = true;
            }
            else if (result.Transaction != null)
            {
            }
            else
            {
            }

            return(View(promo));
        }