Exemple #1
0
        public ActionResult Pay(Payment item)
        {
            var me = (User)Session["me"];

            if (me == null)
            {
                return(RedirectToAction("Index"));
            }
            var stripeToken = Request.Form["stripeToken"];

            if (String.IsNullOrEmpty(stripeToken))
            {
                TempData["result_code"] = -1;
                TempData["message"]     = "Stripe set an error with your informations";
                TempData.Keep();
                return(RedirectToAction("Index"));
            }
            var Email               = Request.Form["stripeEmail"];
            var stripeTokenType     = Request.Form["stripeTokenType"];
            var productService      = new ProductService();
            var priceService        = new PriceService();
            var invoiceItemService  = new InvoiceItemService();
            var invoiceServices     = new InvoiceService();
            var customerService     = new CustomerService();
            var planService         = new PlanService();
            var subscriptionService = new SubscriptionService();

            var original_amount = 500;
            var amount          = Convert.ToInt32(me.reduction > 0 ? original_amount * me.reduction : original_amount);

            var product = productService.Create(new ProductCreateOptions
            {
                Name = "Name of Service",
            });

            var price = priceService.Create(new PriceCreateOptions
            {
                Product    = product.Id,
                UnitAmount = amount,
                Currency   = "usd",
                Recurring  = new PriceRecurringOptions
                {
                    Interval = "month",
                },
            });

            var customer = customerService.Create(new CustomerCreateOptions
            {
                Email  = Email,
                Source = stripeToken,
            });



            var plan = planService.Create(new PlanCreateOptions
            {
                Amount        = amount,
                Currency      = "usd",
                Interval      = "month",
                IntervalCount = 1,
                Product       = product.Id, // "prod_IinH4BV2oyao8L",
            });
            var subscription = subscriptionService.Create(new SubscriptionCreateOptions
            {
                Customer = customer.Id,
                Items    = new List <SubscriptionItemOptions>
                {
                    new SubscriptionItemOptions
                    {
                        Plan = plan.Id,
                    },
                },
            });

            if (subscription.Status == "active")
            {
                item.original_amount = 500;
                item.amount          = amount;
                item.code            = QRCodeModel.GenerateRandomString();
                item.payer_id        = me.id;
                db1.Payment.Add(item);
                db1.SaveChanges();
                TempData["result_code"] = 1;
                TempData["message"]     = "Subscription done successfully";
                TempData.Keep();
                return(RedirectToAction("Payments"));
            }
            TempData["result_code"] = -1;
            TempData["message"]     = "An error occured during the payment";
            TempData.Keep();
            return(RedirectToAction("Index"));
        }