public ActionResult <Subscription> CreateSubscription([FromBody] CreateSubscriptionRequest req) { // Attach payment method var options = new PaymentMethodAttachOptions { Customer = req.Customer, }; var service = new PaymentMethodService(); var paymentMethod = service.Attach(req.PaymentMethod, options); // Update customer's default invoice payment method var customerOptions = new CustomerUpdateOptions { InvoiceSettings = new CustomerInvoiceSettingsOptions { DefaultPaymentMethod = paymentMethod.Id, }, }; var customerService = new CustomerService(); customerService.Update(req.Customer, customerOptions); // Create subscription var subscriptionOptions = new SubscriptionCreateOptions { Customer = req.Customer, Items = new List <SubscriptionItemOptions> { new SubscriptionItemOptions { Price = Environment.GetEnvironmentVariable(req.Price), }, }, }; subscriptionOptions.AddExpand("latest_invoice.payment_intent"); subscriptionOptions.AddExpand("pending_setup_intent"); var subscriptionService = new SubscriptionService(); try { Subscription subscription = subscriptionService.Create(subscriptionOptions); return(subscription); } catch (StripeException e) { Console.WriteLine($"Failed to create subscription.{e}"); return(BadRequest()); } }
private IPaymentHandlerSubscriptionDTO CreateSubscription(string customerId, string priceId) { var subscriptionOptions = new SubscriptionCreateOptions { Customer = customerId, Items = new List <SubscriptionItemOptions> { new SubscriptionItemOptions { Price = priceId, }, }, }; subscriptionOptions.AddExpand("latest_invoice.payment_intent"); var subscription = _subscriptionService.Create(subscriptionOptions); var id = subscription.Id; var status = subscription.Status; var latestInvoicePaymentIntentStatus = subscription.LatestInvoice.PaymentIntent.Status; var latestInvoicePaymentIntentClientSecret = subscription.LatestInvoice.PaymentIntent.ClientSecret; var subscriptionDTO = new StripePaymentHandlerSubscriptionDTO(id, status, latestInvoicePaymentIntentStatus, latestInvoicePaymentIntentClientSecret); return(subscriptionDTO); }
public SubscriptionResult Create(User user, string email, string paymentToken, string planId) { var paymentMethodCreate = new PaymentMethodCreateOptions { Card = new PaymentMethodCardCreateOptions { Token = paymentToken }, Type = "card" }; var pmService = new PaymentMethodService(); var paymentMethod = pmService.Create(paymentMethodCreate); Console.WriteLine("Payment method: " + paymentMethod.Id); var custOptions = new CustomerCreateOptions { Email = email, PaymentMethod = paymentMethod.Id, InvoiceSettings = new CustomerInvoiceSettingsOptions { DefaultPaymentMethod = paymentMethod.Id, }, Metadata = new Dictionary <string, string> { { "userid", user.Id.ToString() } } }; var custService = new CustomerService(); var customer = custService.Create(custOptions); Console.WriteLine("Customer: " + customer.Id); var items = new List <SubscriptionItemOptions> { new SubscriptionItemOptions { Plan = planId, } }; var subscriptionOptions = new SubscriptionCreateOptions { Customer = customer.Id, Items = items, TrialPeriodDays = 7 }; subscriptionOptions.AddExpand("latest_invoice.payment_intent"); var subService = new SubscriptionService(); var subscription = subService.Create(subscriptionOptions); Console.WriteLine("Subscription: " + subscription.Id); return(new SubscriptionResult( customerId: customer.Id, subscriptionId: subscription.Id)); }
public string SignUserUpToSubscription(string paymentMethodId, User user, ClubSubscription clubSubscription) { var options = new PaymentMethodAttachOptions { Customer = user.StripeUserId, }; var service = new PaymentMethodService(); var paymentMethod = service.Attach(paymentMethodId, options); // Update customer's default invoice payment method var customerOptions = new CustomerUpdateOptions { InvoiceSettings = new CustomerInvoiceSettingsOptions { DefaultPaymentMethod = paymentMethod.Id, }, }; var customerService = new CustomerService(); customerService.Update(user.StripeUserId, customerOptions); // Create subscription var subscriptionOptions = new SubscriptionCreateOptions { Customer = user.StripeUserId, Items = new List <SubscriptionItemOptions>() { new SubscriptionItemOptions { Price = clubSubscription.StripePriceId, }, }, Metadata = new Dictionary <string, string>() { { "UserId", user.UserId.ToString() }, { "ClubSubscriptionId", clubSubscription.ClubSubscriptionId.ToString() }, } }; subscriptionOptions.AddExpand("latest_invoice.payment_intent"); var subscriptionService = new SubscriptionService(); try { Subscription subscription = subscriptionService.Create(subscriptionOptions); return("Went good"); } catch (StripeException e) { Console.WriteLine($"Failed to create subscription.{e}"); return("Went bad"); // return BadRequest(); } }
public async Task <IActionResult> CreateSubscription([FromBody] CreateSubscription sub) { var option = new PaymentMethodAttachOptions { Customer = sub.Customer, }; var service = new PaymentMethodService(); var paymentMethod = service.Attach(sub.PaymentMethod, option); // Update customer's default invoice payment method var customerOptions = new CustomerUpdateOptions { InvoiceSettings = new CustomerInvoiceSettingsOptions { DefaultPaymentMethod = paymentMethod.Id, }, }; var customerService = new CustomerService(); await customerService.UpdateAsync(sub.Customer, customerOptions); var subscriptionOptions = new SubscriptionCreateOptions { Customer = sub.Customer, Items = new List <SubscriptionItemOptions> { new SubscriptionItemOptions { Price = Environment.GetEnvironmentVariable(sub.Price), } }, }; subscriptionOptions.AddExpand("latest_invoice.payment_intent"); var subscriptionService = new SubscriptionService(); try { Subscription subscription = subscriptionService.Create(subscriptionOptions); return(Ok(new ResponseViewModel <Subscription> { Data = subscription, Message = StripeConstants.SubscriptionAdded })); } catch (StripeException e) { Console.WriteLine($"Failed to create subscription.{e}"); return(BadRequest()); } }
public ActionResult <SubscriptionResponse> CreateSubscription([FromBody] CreateSubscriptionRequest req) { var customerId = HttpContext.Request.Cookies["customer"]; // Attach payment method PaymentMethod paymentMethod; try { var options = new PaymentMethodAttachOptions { Customer = customerId, }; var service = new PaymentMethodService(); paymentMethod = service.Attach(req.PaymentMethod, options); } catch (StripeException e) { return(BadRequest(new { error = new { message = e.Message } })); } // Create subscription var subscriptionOptions = new SubscriptionCreateOptions { DefaultPaymentMethod = paymentMethod.Id, Customer = customerId, Items = new List <SubscriptionItemOptions> { new SubscriptionItemOptions { Price = Environment.GetEnvironmentVariable(req.Price.ToUpper()), }, }, }; subscriptionOptions.AddExpand("latest_invoice.payment_intent"); var subscriptionService = new SubscriptionService(); try { Subscription subscription = subscriptionService.Create(subscriptionOptions); return(new SubscriptionResponse { Subscription = subscription }); } catch (StripeException e) { Console.WriteLine($"Failed to create subscription.{e}"); return(BadRequest()); } }
public IActionResult Subscribe(string cardEmail, string plan, string stripeToken) { var customerOptions = new CustomerCreateOptions { Email = cardEmail, Source = stripeToken, }; var customerService = new CustomerService(); var customer = customerService.Create(customerOptions); Models.Customer custToDb = new Models.Customer(); custToDb.CustomerID = customer.Id; custToDb.CustomerEmail = cardEmail; _database.Customers.Add(custToDb); _database.SaveChanges(); var planId = _configuration["Stripe:Daily5"]; if (plan == "Daily10") { planId = _configuration["Stripe:Daily10"]; } var subscriptionOptions = new SubscriptionCreateOptions { Customer = customer.Id, Items = new List <SubscriptionItemOptions> { new SubscriptionItemOptions { Plan = planId }, }, }; subscriptionOptions.AddExpand("latest_invoice.payment_intent"); var subscriptionService = new SubscriptionService(); var subscription = subscriptionService.Create(subscriptionOptions); Models.Subscription subs = new Models.Subscription(); subs.SubID = subscription.Id; subs.CustomerID = customer.Id; subs.PlanID = planId; _database.Subscriptions.Add(subs); _database.SaveChanges(); ViewBag.stripeKey = _configuration["Stripe:PublishableKey"]; ViewBag.subscription = subscription.ToJson(); return(View("SubscribeResult")); }
public ActionResult <Subscription> CreateSubscription(SubscriptionCreateRequest req) { var myCustomer = req.CustomerId; var myPaymentMethod = req.PaymentMethodId; var myPrice = req.PriceId; // attach payment method var options = new PaymentMethodAttachOptions { Customer = myCustomer, }; _paymentMethodService.Attach(myPaymentMethod, options); // update customer's default invoice payment method var customerOptions = new CustomerUpdateOptions { InvoiceSettings = new CustomerInvoiceSettingsOptions { DefaultPaymentMethod = myPaymentMethod, }, }; _customerService.Update(myCustomer, customerOptions); //create subscription var subscriptionOptions = new SubscriptionCreateOptions { Customer = myCustomer, Items = new List <SubscriptionItemOptions> { new SubscriptionItemOptions { Price = myPrice, }, }, }; subscriptionOptions.AddExpand("latest_invoice.payment_intent"); try { Subscription subscription = _subscriptionService.Create(subscriptionOptions); return(subscription); } catch (StripeException e) { return(BadRequest(e)); } }
public IActionResult Subscribe(string email, string plan, string stripeToken) { StripeConfiguration.ApiKey = "sk_test_51Gzaa1HAh8lBnQxza9cOAzY7LbfgQ4FWX2sYqiuHsoVWJg4mNDppueQkAVd0XIPU4GhcrNBca8aemNgr24m4jDv200ooFw0Bhz"; var customerOptions = new CustomerCreateOptions { Email = email, Source = stripeToken, }; var customerService = new CustomerService(); var customer = customerService.Create(customerOptions); var planId = ""; if (plan == "Basic Plan") { planId = "price_1GzePPHAh8lBnQxzEOztVJZo"; } else if (plan == "Standard Plan") { planId = "price_1GzeOyHAh8lBnQxz1uv2YPwm"; } else if (plan == "Premium Plan") { planId = "price_1GzePbHAh8lBnQxzC9f5F9oh"; } var subscriptionOptions = new SubscriptionCreateOptions { Customer = customer.Id, Items = new List <SubscriptionItemOptions> { new SubscriptionItemOptions { Plan = planId }, }, }; subscriptionOptions.AddExpand("latest_invoice.payment_intent"); var subscriptionService = new SubscriptionService(); var subscription = subscriptionService.Create(subscriptionOptions); ViewBag.stripeKey = _configuration["Stripe:pk_test_51Gzaa1HAh8lBnQxzdxo2zIn3mzr0lkJiQcsQowOZSd1S85Sou79t2Ub8OwI1lkGcAcgHYxJeB3wIhmFimN9DdKnD00BesCjDg3"]; ViewBag.subscription = subscription.ToJson(); return(View("SubscribeResult")); }
//public Subscription CreateSubscription([FromBody] CreateSubscriptionRequest req) public Subscription CreateSubscription(Customer customer, string price) { //// Attach payment method //var options = new PaymentMethodAttachOptions //{ // Customer = req.Customer, //}; //var service = new PaymentMethodService(); //var paymentMethod = service.Attach(req.PaymentMethod, options); //// Update customer's default invoice payment method //var customerOptions = new CustomerUpdateOptions //{ // InvoiceSettings = new CustomerInvoiceSettingsOptions // { // DefaultPaymentMethod = paymentMethod.Id, // }, //}; //var customerService = new CustomerService(); //customerService.Update(req.Customer, customerOptions); // Create subscription var subscriptionOptions = new SubscriptionCreateOptions { Customer = customer.Id, Items = new List <SubscriptionItemOptions> { new SubscriptionItemOptions { Price = Environment.GetEnvironmentVariable(price), }, }, }; subscriptionOptions.AddExpand("latest_invoice.payment_intent"); var subscriptionService = new SubscriptionService(); try { Subscription subscription = subscriptionService.Create(subscriptionOptions); return(subscription); } catch (StripeException e) { Console.WriteLine($"Failed to create subscription.{e}"); return(null); } }
public static Subscription CreateSubscription(CreateSubscriptionRequest request) { List <SubscriptionItemOption> items = new List <SubscriptionItemOption> { new SubscriptionItemOption { Plan = request.PlanId } }; SubscriptionCreateOptions options = new SubscriptionCreateOptions { Customer = request.StripeCustomerId, Items = items }; options.AddExpand("latest_invoice.payment_intent"); SubscriptionService service = new SubscriptionService(); return(service.Create(options)); }
private Stripe.Subscription CreateStripeSubscription(StripeSubAddRequest request) { StripeConfiguration.ApiKey = _appKeys.StripeApiKey; var options = new SubscriptionCreateOptions { Customer = request.CustomerId, Items = new List <SubscriptionItemOptions> { new SubscriptionItemOptions { Price = request.PriceId, }, }, DefaultPaymentMethod = request.PaymentMethodId, }; options.AddExpand("latest_invoice.payment_intent"); var service = new SubscriptionService(); var subscription = service.Create(options); return(subscription); }
public ActionResult <SubscriptionCreateResponse> CreateSubscription([FromBody] CreateSubscriptionRequest req) { var customerId = HttpContext.Request.Cookies["customer"]; // Create subscription var subscriptionOptions = new SubscriptionCreateOptions { Customer = customerId, Items = new List <SubscriptionItemOptions> { new SubscriptionItemOptions { Price = req.PriceId, }, }, PaymentBehavior = "default_incomplete", }; subscriptionOptions.AddExpand("latest_invoice.payment_intent"); var subscriptionService = new SubscriptionService(); try { Subscription subscription = subscriptionService.Create(subscriptionOptions); return(new SubscriptionCreateResponse { SubscriptionId = subscription.Id, ClientSecret = subscription.LatestInvoice.PaymentIntent.ClientSecret, }); } catch (StripeException e) { Console.WriteLine($"Failed to create subscription.{e}"); return(BadRequest()); } }
public ActionResult <Subscription> CreateSubscription([FromBody] CreateSubscriptionRequest req) { if (!ModelState.IsValid) { return(this.FailWithMessage("invalid params")); } var newPrice = Environment.GetEnvironmentVariable(req.Price.ToUpper()); if (newPrice is null || newPrice == "") { return(this.FailWithMessage($"No price with the new price ID ({req.Price}) found in .env")); } // Attach payment method var options = new PaymentMethodAttachOptions { Customer = req.Customer, }; var service = new PaymentMethodService(); PaymentMethod paymentMethod; try { paymentMethod = service.Attach(req.PaymentMethod, options); } catch (Exception e) { return(this.FailWithMessage($"Failed to attach payment method {e}")); } // Update customer's default invoice payment method var customerOptions = new CustomerUpdateOptions { InvoiceSettings = new CustomerInvoiceSettingsOptions { DefaultPaymentMethod = paymentMethod.Id, }, }; var customerService = new CustomerService(); try { customerService.Update(req.Customer, customerOptions); } catch (StripeException e) { return(this.FailWithMessage($"Failed to attach payment method {e}")); } // Create subscription var subscriptionOptions = new SubscriptionCreateOptions { Customer = req.Customer, Items = new List <SubscriptionItemOptions> { new SubscriptionItemOptions { Price = Environment.GetEnvironmentVariable(req.Price), Quantity = req.Quantity, }, }, }; subscriptionOptions.AddExpand("latest_invoice.payment_intent"); var subscriptionService = new SubscriptionService(); try { return(subscriptionService.Create(subscriptionOptions)); } catch (StripeException e) { return(this.FailWithMessage($"Failed to attach payment method: {e}")); } }
public static TransactionResult CreateSubscription(TransactionRequest request, StripeSettings stripeSettings, ILogger logger) { var order = request.Order; InitStripe(stripeSettings); var parameters = request.Parameters; parameters.TryGetValue("cardNumber", out var cardNumber); parameters.TryGetValue("cardName", out var cardName); parameters.TryGetValue("expireMonth", out var expireMonthStr); parameters.TryGetValue("expireYear", out var expireYearStr); parameters.TryGetValue("cvv", out var cvv); var paymentMethodService = new PaymentMethodService(); var paymentMethod = paymentMethodService.Create(new PaymentMethodCreateOptions() { Card = new PaymentMethodCardCreateOptions() { Number = cardNumber.ToString(), ExpYear = long.Parse(expireYearStr.ToString()), ExpMonth = long.Parse(expireMonthStr.ToString()), Cvc = cvv.ToString() }, Type = "card" }); var address = DependencyResolver.Resolve <IDataSerializer>() .DeserializeAs <Address>(order.BillingAddressSerialized); InitStripe(stripeSettings, true); //do we have a saved stripe customer id? var customerId = GetCustomerId(order.User, paymentMethod, address); var subscriptionItems = new List <SubscriptionItemOptions>(); var productService = new ProductService(); var planService = new PlanService(); foreach (var orderItem in order.OrderItems) { var product = productService.Create(new ProductCreateOptions { Name = orderItem.Product.Name, Type = "service" }); var lineTotal = orderItem.Price * orderItem.Quantity + orderItem.Tax; GetFinalAmountDetails(lineTotal, order.CurrencyCode, address, out var currencyCode, out var finalAmount); var planOptions = new PlanCreateOptions() { Nickname = product.Name, Product = product.Id, Amount = (long)(finalAmount), Interval = GetInterval(orderItem.Product.SubscriptionCycle), IntervalCount = orderItem.Product.CycleCount == 0 ? 1 : orderItem.Product.CycleCount, Currency = currencyCode, UsageType = "licensed", TrialPeriodDays = orderItem.Product.TrialDays }; var plan = planService.Create(planOptions); subscriptionItems.Add(new SubscriptionItemOptions() { Plan = plan.Id, Quantity = orderItem.Quantity }); } //create a coupon if any var coupon = GetCoupon(order); var subscriptionOptions = new SubscriptionCreateOptions() { Customer = customerId, Items = subscriptionItems, Metadata = new Dictionary <string, string>() { { "orderGuid", order.Guid }, { "internalId", order.Id.ToString() }, { "isSubscription", bool.TrueString } }, Coupon = coupon?.Id, #if DEBUG TrialEnd = DateTime.UtcNow.AddMinutes(5) #endif }; subscriptionOptions.AddExpand("latest_invoice.payment_intent"); var subscriptionService = new SubscriptionService(); var subscription = subscriptionService.Create(subscriptionOptions); var processPaymentResult = new TransactionResult() { OrderGuid = order.Guid, }; if (subscription.Status == "active" || subscription.Status == "trialing") { processPaymentResult.NewStatus = PaymentStatus.Complete; processPaymentResult.TransactionCurrencyCode = order.CurrencyCode; processPaymentResult.IsSubscription = true; processPaymentResult.TransactionAmount = (subscription.Plan.AmountDecimal / 100) ?? order.OrderTotal; processPaymentResult.ResponseParameters = new Dictionary <string, object>() { { "subscriptionId", subscription.Id }, { "invoiceId", subscription.LatestInvoiceId }, { "feePercent", subscription.ApplicationFeePercent }, { "collectionMethod", subscription.CollectionMethod }, { "metaInfo", subscription.Metadata } }; processPaymentResult.Success = true; } else { processPaymentResult.Success = false; logger.Log <TransactionResult>(LogLevel.Warning, $"The subscription for Order#{order.Id} by stripe failed with status {subscription.Status}." + subscription.StripeResponse.Content); } return(processPaymentResult); }
public ActionResult Subscribe(string email, string plan, string stripeToken) { CustomerModel cm = new CustomerModel(); var customerOptions = new CustomerCreateOptions { Email = email, Source = stripeToken, }; try { var customerService = new CustomerService(); var customer = customerService.Create(customerOptions); customer_id = customer.Id; // Previous code in action var planId = ""; if (plan == "basicPlan") { planId = "price_1Htq5ZHQbClkzxezJlLhffk6"; global_plan_id = "price_1Htq5ZHQbClkzxezJlLhffk6"; } else { planId = "price_1I2w4MHQbClkzxezSyy1Cd1j"; global_plan_id = "price_1I2w4MHQbClkzxezSyy1Cd1j"; } var subscriptionOptions = new SubscriptionCreateOptions { Customer = customer.Id, Items = new List <SubscriptionItemOptions> { new SubscriptionItemOptions { Plan = planId }, }, }; subscriptionOptions.AddExpand("latest_invoice.payment_intent"); var subscriptionService = new SubscriptionService(); var subscription = subscriptionService.Create(subscriptionOptions); ViewBag.stripeKey = "{PUBLIC KEY}; ViewBag.subscription = subscription.ToJson(); cm.customer_id = subscription.CustomerId; cm.email = email.ToString(); cm.subscription_status = subscription.Status; if (planId == " price_1Htq5ZHQbClkzxezJlLhffk6 ") { cm.basic = " True "; cm.premium = " False "; } else { cm.basic = " False "; cm.premium = " True "; } cm.SaveDetails(); return View(" SubscribeResult "); } catch(Exception e) { cm.customer_id = " not - valid "; cm.email = email.ToString(); cm.subscription_status = " not - valid "; cm.basic = null; cm.premium = null; cm.SaveDetails(); return View(" SubscribeFailed "); } }