예제 #1
0
        public ActionResult Create(PriceCreateViewModel model, string redirectUrl)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            List <Prices> prices = Price.Get().Where(t => t.ServicesId == model.ServiceSelected).ToList().Where(p => p.FromDate <= model.FromDate && (p.ToDate > model.FromDate || p.ToDate == null)).ToList();

            if (prices.Count != 0)
            {
                prices[0].ToDate = model.FromDate;
                Price.Update(prices[0]);
            }

            var price = new Prices
            {
                ServicesId = model.ServiceSelected,
                ToDate     = null,
                Value      = model.Value,
                FromDate   = model.FromDate
            };

            if (prices.Count == 2)
            {
                price.ToDate = prices[1].FromDate;
            }

            Price.Create(price);

            return(RedirectToLocal(redirectUrl));
        }
        public static Product CreateProduct(StripeProductInfoModel model)
        {
            var stripeKey = StripeApiKey();

            var options = new ProductCreateOptions
            {
                Description = model.Description,
                Name        = model.Name,
                Active      = model.Active,
            };

            var service = new ProductService();
            var product = service.Create(options);

            var priceOption = new PriceCreateOptions
            {
                Product    = product.Id,
                UnitAmount = long.Parse(model.Price),
                Currency   = "usd",
            };
            var priceService = new PriceService();
            var price        = priceService.Create(priceOption);

            return(product);
        }
예제 #3
0
        private Stripe.Price CreateStripeProductPrice(string productId, long price)
        {
            StripeConfiguration.ApiKey = _appKeys.StripeApiKey;
            var options = new PriceCreateOptions
            {
                UnitAmount = price,
                Currency   = "usd",
                Product    = productId,
            };
            var service     = new PriceService();
            var stripePrice = service.Create(options);

            return(stripePrice);
        }
예제 #4
0
        public Price CreatePrice(long productPrice, Product product)
        {
            var options = new PriceCreateOptions
            {
                UnitAmount = productPrice * 100,
                Currency   = "dkk",
                Recurring  = new PriceRecurringOptions
                {
                    Interval = "month",
                },
                Product = product.Id,
            };
            var service = new PriceService();
            var price   = service.Create(options);

            return(price);
        }
예제 #5
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"));
        }
예제 #6
0
        protected Models.PaymentIntent UpdatePaymentIntent(string sessionId)
        {
            StripeConfiguration.ApiKey = ConfigurationManager.AppSettings["Cashier:Stripe:Secret"];
            var sessionService = new SessionService();
            var session        = sessionService.Get(sessionId);

            var setupIntentService = new SetupIntentService();
            var setupIntent        = setupIntentService.Get(session.SetupIntentId);

            var paymentIntent = PaymentService.GetPaymentIntentByTransactionRef(session.Metadata["TransactionRef"]);

            if (string.IsNullOrEmpty(setupIntent.PaymentMethodId))
            {
                paymentIntent.PaymentStatus = PaymentStatus.Failed;
                PaymentService.UpdatePaymentStatus(paymentIntent.TransactionReference, session.PaymentIntentId, PaymentStatus.Failed);
                return(paymentIntent);
            }

            var ddPriceName = $"Direct Debit - {paymentIntent.DirectDebitFrequencyInterval} {Enum.GetName(typeof(PaymentFrequencyUnit), paymentIntent.DirectDebitFrequencyUnit)}{(paymentIntent.DirectDebitFrequencyInterval > 1 ? "s" : "")} - {paymentIntent.Amount}";

            var productService = new ProductService();
            var product        = productService.List().FirstOrDefault(p => p.Description == "Direct Debit");

            if (product == null)
            {
                product = productService.Create(new ProductCreateOptions
                {
                    Name = ddPriceName,
                    Type = "service"
                });
            }

            var priceService = new PriceService();
            var price        = priceService.List().FirstOrDefault(p => p.Nickname == ddPriceName);

            if (price == null)
            {
                price = priceService.Create(new PriceCreateOptions
                {
                    Nickname   = ddPriceName,
                    Product    = product.Id,
                    UnitAmount = (long)paymentIntent.Amount * 100,
                    Currency   = paymentIntent.Currency,
                    Recurring  = new PriceRecurringOptions
                    {
                        Interval      = Enum.GetName(typeof(PaymentFrequencyUnit), paymentIntent.DirectDebitFrequencyUnit).ToLower(),
                        IntervalCount = paymentIntent.DirectDebitFrequencyInterval,
                        UsageType     = "licensed"
                    }
                });
            }

            var customerService = new CustomerService();
            var customer        = customerService.List().FirstOrDefault(c => c.Name == paymentIntent.CustomerUniqueReference);

            if (customer == null)
            {
                customer = customerService.Create(new CustomerCreateOptions
                {
                    Name          = paymentIntent.CustomerUniqueReference,
                    Description   = paymentIntent.CustomerUniqueReference,
                    PaymentMethod = setupIntent.PaymentMethodId,
                    Email         = paymentIntent.CustomerEmail,
                    Address       = new AddressOptions
                    {
                        Line1      = paymentIntent.CustomerAddressLines,
                        City       = paymentIntent.CustomerCity,
                        Country    = paymentIntent.CustomerCountry,
                        PostalCode = paymentIntent.CustomerPostcode
                    }
                });
            }
            else
            {
                var paymentMethodService = new PaymentMethodService();
                paymentMethodService.Attach(setupIntent.PaymentMethodId, new PaymentMethodAttachOptions
                {
                    Customer = customer.Id
                });
            }

            var subscriptionService = new SubscriptionService();
            var subscriptionCreate  = new SubscriptionCreateOptions
            {
                Customer             = customer.Id,
                DefaultPaymentMethod = setupIntent.PaymentMethodId,
                BillingCycleAnchor   = paymentIntent.DirectDebitStartDate,
                Items = new List <SubscriptionItemOptions>
                {
                    new SubscriptionItemOptions
                    {
                        Price = price.Id
                    }
                }
            };

            if (paymentIntent.DirectDebitTrialDateEnd.HasValue)
            {
                //let the trial date specify the days that should not be charged
                subscriptionCreate.TrialEnd = paymentIntent.DirectDebitTrialDateEnd;
            }
            else
            {
                //otherwise let it start on the anchor date and disable proration
                subscriptionCreate.ProrationBehavior = "none";
            }
            var subscription = subscriptionService.Create(subscriptionCreate);

            paymentIntent.PaymentStatus = PaymentStatus.Succeeded;
            PaymentService.UpdatePaymentStatus(paymentIntent.TransactionReference, subscription.Id, PaymentStatus.Succeeded);

            return(paymentIntent);
        }