public async Task <IActionResult> Edit(int id, [Bind("Id,ClinicId,Amount,IssuedAt,PaidAt")] SubscriptionInvoices subscriptionInvoices) { if (id != subscriptionInvoices.Id) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(subscriptionInvoices); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!SubscriptionInvoicesExists(subscriptionInvoices.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } ViewData["ClinicId"] = new SelectList(_context.Clinics, "Id", "Name", subscriptionInvoices.ClinicId); return(View(subscriptionInvoices)); }
public async Task <IActionResult> PostSubscriptionInvoices([FromBody] SubscriptionInvoices subscriptionInvoices) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } _context.SubscriptionInvoices.Add(subscriptionInvoices); await _context.SaveChangesAsync(); return(CreatedAtAction("GetSubscriptionInvoices", new { id = subscriptionInvoices.Id }, subscriptionInvoices)); }
public async Task <IActionResult> Create([Bind("Id,ClinicId,Amount,IssuedAt,PaidAt")] SubscriptionInvoices subscriptionInvoices) { if (ModelState.IsValid) { _context.Add(subscriptionInvoices); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["ClinicId"] = new SelectList(_context.Clinics, "Id", "Name", subscriptionInvoices.ClinicId); return(View(subscriptionInvoices)); }
public async Task <IActionResult> OnGetAsync(int?id) { if (id == null) { return(NotFound()); } SubscriptionInvoices = await _context.SubscriptionInvoices .Include(s => s.Clinic).FirstOrDefaultAsync(m => m.Id == id); if (SubscriptionInvoices == null) { return(NotFound()); } return(Page()); }
public async Task <IActionResult> OnPostAsync(int?id) { if (id == null) { return(NotFound()); } SubscriptionInvoices = await _context.SubscriptionInvoices.FindAsync(id); if (SubscriptionInvoices != null) { _context.SubscriptionInvoices.Remove(SubscriptionInvoices); await _context.SaveChangesAsync(); } return(RedirectToPage("./Index")); }
public async Task <IActionResult> OnGetAsync(int?id) { if (id == null) { return(NotFound()); } SubscriptionInvoices = await _context.SubscriptionInvoices .Include(s => s.Clinic).FirstOrDefaultAsync(m => m.Id == id); if (SubscriptionInvoices == null) { return(NotFound()); } ViewData["ClinicId"] = new SelectList(_context.Clinics, "Id", "Address"); return(Page()); }
public async Task <IActionResult> PutSubscriptionInvoices([FromRoute] int id, [FromBody] SubscriptionInvoices subscriptionInvoices) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != subscriptionInvoices.Id) { return(BadRequest()); } _context.Entry(subscriptionInvoices).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!SubscriptionInvoicesExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public int Add(AddSubscriptionBookingParams param) { try { using (var transaction = new TransactionScope()) { var bookingCode = Helper.RandomString(Constant.BookingCodeLength); while (IsBookingCodeExists(bookingCode)) { bookingCode = Helper.RandomString(Constant.BookingCodeLength); } param.SubscriptionBookingsObject.BookingCode = bookingCode; // Insert New Bookings DayaxeDbContext.SubscriptionBookings.InsertOnSubmit(param.SubscriptionBookingsObject); Commit(); if (param.SubscriptionBookingDiscounts != null && param.SubscriptionBookingDiscounts.Id > 0) { var subscriptionDiscount = new SubscriptionBookingDiscounts { DiscountId = param.SubscriptionBookingDiscounts.Id, SubscriptionBookingId = param.SubscriptionBookingsObject.Id, SubscriptionId = param.SubscriptionBookingsObject.SubscriptionId }; DayaxeDbContext.SubscriptionBookingDiscounts.InsertOnSubmit(subscriptionDiscount); } // Insert to History for First Cycle var cycle = new SubscriptionCycles { SubscriptionBookingId = param.SubscriptionBookingsObject.Id, StartDate = param.SubscriptionBookingsObject.StartDate, EndDate = param.SubscriptionBookingsObject.EndDate, CancelDate = param.SubscriptionBookingsObject.CancelDate, LastUpdatedBy = param.SubscriptionBookingsObject.LastUpdatedBy, LastUpdatedDate = param.SubscriptionBookingsObject.LastUpdatedDate, Status = param.SubscriptionBookingsObject.Status, Price = param.ActualPrice, MerchantPrice = param.MerchantPrice, PayByCredit = param.PayByCredit, TotalPrice = param.TotalPrice, Quantity = param.SubscriptionBookingsObject.Quantity, StripeChargeId = string.Empty, StripeCouponId = param.SubscriptionBookingsObject.StripeCouponId, StripeInvoiceId = string.Empty, CycleNumber = 1 }; DayaxeDbContext.SubscriptionCycles.InsertOnSubmit(cycle); // Insert Discount for Current Customer active Subscription var discount = new Discounts { DiscountName = string.Format("{0} - {1} {2}", param.SubscriptionName, param.FirstName, param.LastName), Code = string.Format("SUP{0}", Helper.RandomString(8)), StartDate = param.SubscriptionBookingsObject.StartDate, EndDate = param.SubscriptionBookingsObject.EndDate, CodeRequired = true, PercentOff = 100, PromoType = (byte)Enums.PromoType.SubscriptionPromo, MinAmount = 0, IsAllProducts = true, MaxPurchases = (byte)param.MaxPurchases }; DayaxeDbContext.Discounts.InsertOnSubmit(discount); Commit(); // Add Invoices var subscriptionInvoice = new SubscriptionInvoices { SubscriptionCyclesId = cycle.Id, BookingStatus = cycle.Status, Quantity = cycle.Quantity, Price = cycle.Price, MerchantPrice = cycle.MerchantPrice, PayByCredit = cycle.PayByCredit, TotalPrice = cycle.TotalPrice, InvoiceStatus = (int)Enums.InvoiceStatus.Charge, StripeChargeId = cycle.StripeChargeId, ChargeAmount = cycle.Price, StripeRefundId = string.Empty, RefundAmount = 0, RefundCreditAmount = 0, StripeCouponId = cycle.StripeCouponId, CreatedDate = DateTime.UtcNow, CreatedBy = 1 }; DayaxeDbContext.SubscriptionInvoices.InsertOnSubmit(subscriptionInvoice); var discountUsed = new SubsciptionDiscountUseds { CustomerId = param.SubscriptionBookingsObject.CustomerId, DiscountId = discount.Id, SubscriptionBookingId = param.SubscriptionBookingsObject.Id }; DayaxeDbContext.SubsciptionDiscountUseds.InsertOnSubmit(discountUsed); var cusCredits = DayaxeDbContext.CustomerCredits .SingleOrDefault(cc => cc.CustomerId == param.CustomerCreditsObject.CustomerId); // Add Logs when refund by Upgrade to Subscription if (param.RefundCreditByUpgrade > 0) { var creditLogs = new CustomerCreditLogs { Amount = param.RefundCreditByUpgrade, ProductId = 0, BookingId = 0, SubscriptionId = param.SubscriptionBookingsObject.SubscriptionId, CreatedBy = param.CustomerCreditsObject.CustomerId, CreatedDate = DateTime.UtcNow, CreditType = (byte)Enums.CreditType.PartialPuchaseRefund, Description = param.RefundCreditDescription, CustomerId = param.CustomerCreditsObject.CustomerId, ReferralId = param.CustomerCreditsObject.ReferralCustomerId, SubscriptionBookingId = param.SubscriptionBookingsObject.Id, Status = true, GiftCardId = 0 }; DayaxeDbContext.CustomerCreditLogs.InsertOnSubmit(creditLogs); if (cusCredits != null) { cusCredits.Amount += param.RefundCreditByUpgrade; } } // Add Logs when pay by DayAxe Credit if (param.PayByCredit > 0) { var creditLogs = new CustomerCreditLogs { Amount = param.PayByCredit, ProductId = 0, BookingId = 0, SubscriptionId = param.SubscriptionBookingsObject.SubscriptionId, CreatedBy = param.CustomerCreditsObject.CustomerId, CreatedDate = DateTime.UtcNow, CreditType = (byte)Enums.CreditType.Charge, Description = param.Description, CustomerId = param.CustomerCreditsObject.CustomerId, ReferralId = param.CustomerCreditsObject.ReferralCustomerId, SubscriptionBookingId = param.SubscriptionBookingsObject.Id, Status = true, GiftCardId = 0 }; DayaxeDbContext.CustomerCreditLogs.InsertOnSubmit(creditLogs); if (cusCredits != null) { cusCredits.Amount -= param.PayByCredit; } } // First Buy of referral if (param.CustomerCreditsObject != null && param.CustomerCreditsObject.ReferralCustomerId > 0 && ( DayaxeDbContext.Bookings.Count(x => x.CustomerId == param.SubscriptionBookingsObject.CustomerId) == 1 || DayaxeDbContext.SubscriptionBookings.Count(x => x.CustomerId == param.SubscriptionBookingsObject.CustomerId) == 1)) { var logs = DayaxeDbContext.CustomerCreditLogs .Where(ccl => ccl.CustomerId == param.CustomerCreditsObject.ReferralCustomerId && ccl.ReferralId == param.SubscriptionBookingsObject.CustomerId && !ccl.Status) .ToList(); if (logs.Any()) { logs.ForEach(log => { var cus = DayaxeDbContext.CustomerCredits .FirstOrDefault(cc => cc.CustomerId == log.CustomerId); if (cus != null) { cus.Amount += log.Amount; } log.Status = true; }); Commit(); } } // Add to Customer Credit Log var logsReferred = DayaxeDbContext.CustomerCreditLogs .Where(ccl => ccl.ReferralId == param.SubscriptionBookingsObject.CustomerId && !ccl.Status) .ToList(); if (logsReferred.Any()) { logsReferred.ForEach(log => { var cus = DayaxeDbContext.CustomerCredits .FirstOrDefault(cc => cc.CustomerId == log.CustomerId); if (cus != null) { cus.Amount += log.Amount; } log.Status = true; }); } // Insert Schedule var schedules = new Schedules { ScheduleSendType = (int)Enums.ScheduleSendType.IsEmailConfirmSubscription, Name = "Send Email confirm Subscription", Status = (int)Enums.ScheduleType.NotRun, SubscriptionBookingId = param.SubscriptionBookingsObject.Id }; DayaxeDbContext.Schedules.InsertOnSubmit(schedules); // Maybe not use here because flow upgrade to subscription do not use this if (param.BookingId > 0) { var bookings = DayaxeDbContext.Bookings.FirstOrDefault(b => b.BookingId == param.BookingId); if (bookings != null) { var chargePrice = (int)((bookings.TotalPrice - bookings.HotelPrice) * 100); try { bookings.TotalPrice -= bookings.HotelPrice; bookings.PassStatus = (int)Enums.BookingStatus.Active; var discounts = new DiscountBookings { DiscountId = discount.Id, ProductId = bookings.ProductId, BookingId = bookings.BookingId }; DayaxeDbContext.DiscountBookings.InsertOnSubmit(discounts); bookings.IsActiveSubscription = true; string responseString; if (chargePrice > 0) { var chargeService = new StripeChargeService(); StripeCharge charge = chargeService.Capture(bookings.StripeChargeId, chargePrice); responseString = charge.StripeResponse.ResponseJson; } else { var refundOptions = new StripeRefundCreateOptions { Reason = StripeRefundReasons.RequestedByCustomer }; var refundService = new StripeRefundService(); StripeRefund refund = refundService.Create(bookings.StripeChargeId, refundOptions); responseString = refund.StripeResponse.ResponseJson; } var logs = new Logs { LogKey = "UpgradeSubscriptionCharge", UpdatedContent = string.Format("Params: {0} - Response: {1}", JsonConvert.SerializeObject(param, CustomSettings.SerializerSettings()), responseString), UpdatedBy = param.SubscriptionBookingsObject.CustomerId, UpdatedDate = DateTime.UtcNow }; AddLog(logs); } catch (Exception ex) { var logs = new Logs { LogKey = "UpgradeSubscriptionChargeError", UpdatedContent = string.Format("Params: {0} - {1} - {2} - {3}", JsonConvert.SerializeObject(param, CustomSettings.SerializerSettings()), ex.Message, ex.StackTrace, ex.Source), UpdatedBy = param.SubscriptionBookingsObject.CustomerId, UpdatedDate = DateTime.UtcNow }; AddLog(logs); } } } Commit(); transaction.Complete(); } } catch (Exception ex) { var logs = new Logs { LogKey = "AddBookingError", UpdatedContent = string.Format("{0} - {1} - {2} - {3}", param.SubscriptionBookingsObject.CustomerId, ex.Message, ex.StackTrace, ex.Source), UpdatedBy = param.SubscriptionBookingsObject.CustomerId, UpdatedDate = DateTime.UtcNow }; AddLog(logs); throw new Exception(ex.Message, ex); } return(param.SubscriptionBookingsObject.Id); }
static void Main(string[] args) { // Set Stripe Api Key StripeConfiguration.SetApiKey(AppConfiguration.StripeApiKey); using (var subscriptionBookingRepository = new SubscriptionBookingRepository()) { var subscriptionBookingList = subscriptionBookingRepository.SubscriptionBookingsList.ToList(); // Each Subscription Bookings subscriptionBookingList.ForEach(subscriptionBookings => { var customerInfos = subscriptionBookingRepository.CustomerInfoList .FirstOrDefault(ci => ci.CustomerId == subscriptionBookings.CustomerId); // Customer Infos if (customerInfos != null) { var subscriptionService = new StripeSubscriptionService(); StripeSubscription subscription = subscriptionService.Get(subscriptionBookings.StripeSubscriptionId); DateTime?canceledDate = subscription.CanceledAt; var invoiceService = new StripeInvoiceService(); // All Invoices of Customer var invoiceItems = invoiceService.List(new StripeInvoiceListOptions { CustomerId = customerInfos.StripeCustomerId } ).OrderBy(ic => ic.Date).ToList(); short cycleNumber = 0; invoiceItems.ForEach(invoice => { if (invoice.SubscriptionId.Equals(subscriptionBookings.StripeSubscriptionId, StringComparison.InvariantCulture)) { cycleNumber++; double totalCharge = (double)invoice.Total / 100; if (!totalCharge.Equals(subscriptionBookings.TotalPrice)) { subscriptionBookings.Price = totalCharge; } DateTime?periodStart = invoice.PeriodStart; DateTime?periodEnd = invoice.PeriodEnd; try { if (periodStart.Value.Date == periodEnd.Value.Date) { periodEnd = periodEnd.Value.AddDays(30); } periodStart = invoice.StripeInvoiceLineItems.Data[0].StripePeriod.Start; periodEnd = invoice.StripeInvoiceLineItems.Data[0].StripePeriod.End; } catch (Exception) { } var subscriptionCycle = new SubscriptionCycles { SubscriptionBookingId = subscriptionBookings.Id, StartDate = periodStart, EndDate = periodEnd, CancelDate = subscriptionBookings.CancelDate, Status = subscriptionBookings.Status, LastUpdatedDate = subscriptionBookings.LastUpdatedDate, LastUpdatedBy = subscriptionBookings.LastUpdatedBy, Price = totalCharge.Equals(0) && subscriptionBookings.PayByCredit > 0 ? subscriptionBookings.PayByCredit : totalCharge, MerchantPrice = subscriptionBookings.MerchantPrice, PayByCredit = subscriptionBookings.PayByCredit, TotalPrice = totalCharge, Quantity = subscriptionBookings.Quantity, StripeInvoiceId = invoice.Id, StripeChargeId = invoice.ChargeId, StripeCouponId = subscriptionBookings.StripeCouponId, CycleNumber = cycleNumber }; var param = new AddSubscriptionCycleParams { CanceledDate = canceledDate, SubscriptionCyclesObject = subscriptionCycle, SubscriptionInvoices = new List <SubscriptionInvoices>() }; SubscriptionInvoices subscriptionInvoice; // Paid Charge if (invoice.Paid) { if (!string.IsNullOrEmpty(invoice.ChargeId)) { var chargeService = new StripeChargeService(); StripeCharge charge = chargeService.Get(invoice.ChargeId); subscriptionInvoice = new SubscriptionInvoices { SubscriptionCyclesId = subscriptionCycle.Id, BookingStatus = subscriptionCycle.Status, Quantity = subscriptionBookings.Quantity, Price = (double)charge.Amount / 100, MerchantPrice = subscriptionBookings.MerchantPrice, PayByCredit = subscriptionBookings.PayByCredit, TotalPrice = (double)charge.Amount / 100, InvoiceStatus = (int)Enums.InvoiceStatus.Charge, StripeChargeId = charge.Id, ChargeAmount = (double)charge.Amount / 100, StripeRefundId = string.Empty, RefundAmount = 0, RefundCreditAmount = 0, StripeCouponId = subscriptionBookings.StripeCouponId, CreatedDate = DateTime.UtcNow, CreatedBy = 1 }; param.SubscriptionInvoices.Add(subscriptionInvoice); // Charge with Refunded if (charge.Refunded || charge.AmountRefunded > 0) { var refundList = charge.Refunds; for (int i = 0; i < refundList.Data.Count; i++) { var refundItem = charge.Refunds.Data[i]; var subscriptionInvoiceRefunded = new SubscriptionInvoices { SubscriptionCyclesId = subscriptionCycle.Id, BookingStatus = subscriptionCycle.Status, Quantity = subscriptionBookings.Quantity, Price = (double)charge.Amount / 100 - (double)refundItem.Amount / 100, MerchantPrice = subscriptionBookings.MerchantPrice, PayByCredit = subscriptionBookings.PayByCredit, TotalPrice = subscriptionBookings.MerchantPrice - (double)refundItem.Amount / 100 - subscriptionBookings.PayByCredit, InvoiceStatus = refundItem.Amount < charge.Amount ? (int)Enums.InvoiceStatus.PartialRefund : (int)Enums.InvoiceStatus.FullRefund, StripeChargeId = charge.Id, ChargeAmount = (double)charge.Amount / 100, StripeRefundId = refundItem.Id, RefundAmount = (double)refundItem.Amount / 100, RefundCreditAmount = 0, StripeCouponId = subscriptionBookings.StripeCouponId, CreatedDate = DateTime.UtcNow, CreatedBy = 1 }; param.SubscriptionInvoices.Add(subscriptionInvoiceRefunded); param.SubscriptionCyclesObject.Price = subscriptionInvoiceRefunded.Price; param.SubscriptionCyclesObject.TotalPrice = subscriptionInvoiceRefunded.TotalPrice; } } } else { // Charge but have Coupon and use DayAxe Credit so Amount = 0 subscriptionInvoice = new SubscriptionInvoices { SubscriptionCyclesId = subscriptionCycle.Id, BookingStatus = subscriptionCycle.Status, Quantity = subscriptionBookings.Quantity, Price = 0, MerchantPrice = subscriptionBookings.MerchantPrice, PayByCredit = subscriptionBookings.PayByCredit, TotalPrice = 0, InvoiceStatus = (int)Enums.InvoiceStatus.Charge, StripeChargeId = string.Empty, ChargeAmount = 0, StripeRefundId = string.Empty, RefundAmount = 0, RefundCreditAmount = 0, StripeCouponId = subscriptionBookings.StripeCouponId, CreatedDate = DateTime.UtcNow, CreatedBy = 1 }; param.SubscriptionInvoices.Add(subscriptionInvoice); } } else { // Closed Charge subscriptionInvoice = new SubscriptionInvoices { SubscriptionCyclesId = subscriptionCycle.Id, BookingStatus = subscriptionCycle.Status, Quantity = subscriptionBookings.Quantity, Price = totalCharge, MerchantPrice = subscriptionBookings.MerchantPrice, PayByCredit = subscriptionBookings.PayByCredit, TotalPrice = totalCharge, InvoiceStatus = (int)Enums.InvoiceStatus.Charge, StripeChargeId = invoice.ChargeId, ChargeAmount = totalCharge, StripeRefundId = string.Empty, RefundAmount = 0, RefundCreditAmount = 0, StripeCouponId = subscriptionBookings.StripeCouponId, CreatedDate = DateTime.UtcNow, CreatedBy = 1 }; param.SubscriptionInvoices.Add(subscriptionInvoice); } subscriptionBookingRepository.AddSubscriptionCycle(param); Console.WriteLine("Update - " + invoice.SubscriptionId); } }); } }); Console.WriteLine("Done!!!"); Console.ReadLine(); } }