/// <summary> /// Creates payment history entry. /// </summary> /// <param name="userId">User Id.</param> /// <param name="options">Payment options.</param> /// <param name="amount">Amount.</param> /// <param name="chargedTo">Account charged to.</param> /// <param name="transactionId">Transaction Id.</param> /// <param name="autoUpdateSubscription">Value indicating whether to automatically update user subscription.</param> public void CreatePaymentHistoryEntry( int userId, PaymentOptions options, int amount, string chargedTo, string transactionId, bool autoUpdateSubscription) { User u = null; if (autoUpdateSubscription) { using (var repo = Resolver.Resolve <IUserRepository>()) { u = repo.Select(userId); if (u != null) { if (u.Subscription == null) { u.Subscription = new UserSubscription(); } u.Subscription.Renewed = DateTime.UtcNow; u.Subscription.RenewedTo = options.Subscription; u.Subscription.RenewedToDuration = options.Duration; repo.Update(u); } } } var ret = new PaymentHistoryEntry() { UserId = userId, Date = DateTime.UtcNow, SubscriptionType = options.Subscription, Duration = options.Duration, Amount = amount / 100, // In dollars, not in cents ChargedTo = chargedTo, TransactionId = transactionId }; _payments.Update(ret); // Eventual consistency, you know... System.Threading.Thread.Sleep(500); }
/// <summary> /// Gets or sets value indicating whether the given user has valid payment for a given subscription. /// </summary> /// <param name="userId">User Id.</param> /// <param name="options">Payment options.</param> /// <returns>Value indicating whether the given user has valid payment for a given subscription.</returns> public bool HasValidPayment(int userId, PaymentOptions options) { bool ret = false; PaymentHistoryEntry firstEntry = null; var history = this.GetPaymentHistory(userId); if (history.Any()) { firstEntry = history.First(); if (options.Subscription == SubscriptionType.Pro) { ret = true; } else if (options.Subscription == SubscriptionType.Agency) { ret = firstEntry.SubscriptionType == SubscriptionType.Agency; } } return(ret); }
/// <summary> /// Returns the PDF stream of the given receipt. /// </summary> /// <param name="userId">User Id.</param> /// <param name="receiptId">Receipt Id.</param> /// <returns>Stream.</returns> public Stream GetReceiptStream(int userId, int receiptId) { User user = null; Stream ret = null; XMLWorker worker = null; IPipeline pipeline = null; XMLParser xmlParse = null; string html = string.Empty; ICSSResolver cssResolver = null; PaymentHistoryEntry receipt = null; HtmlPipelineContext htmlContext = null; Func <string, string> valueOrDefault = v => !string.IsNullOrWhiteSpace(v) ? v : "-"; if (userId > 0 && receiptId > 0) { receipt = _payments.Select(receiptId); if (receipt != null && receipt.UserId == userId) { user = Resolver.Resolve <IRepository <User> >().Select(receipt.UserId); if (user != null) { using (var stream = Assembly.GetExecutingAssembly() .GetManifestResourceStream("Ifly.Resources.PaymentReceiptTemplate.html")) { using (var reader = new StreamReader(stream)) html = reader.ReadToEnd(); } if (!string.IsNullOrEmpty(html)) { html = Utils.Input.FormatWith(html, new { Date = receipt.Date.ToString("R", _culture), UserName = string.Format("{0} ({1})", valueOrDefault(user.Name), valueOrDefault(user.Email)), CompanyName = valueOrDefault(user.CompanyName), CompanyAddress = valueOrDefault(user.CompanyAddress), SubscriptionType = Enum.GetName(typeof(SubscriptionType), receipt.SubscriptionType), Amount = receipt.Amount.ToString("F"), ChargedTo = valueOrDefault(receipt.ChargedTo), TransactionId = valueOrDefault(receipt.TransactionId) }); using (Document doc = new Document(PageSize.A4, 30, 30, 30, 30)) { using (var s = new MemoryStream()) { using (var writer = PdfWriter.GetInstance(doc, s)) { doc.Open(); htmlContext = new HtmlPipelineContext(null); htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory()); cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(false); pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(doc, writer))); worker = new XMLWorker(pipeline, true); xmlParse = new XMLParser(true, worker); xmlParse.Parse(new StringReader(html)); xmlParse.Flush(); doc.Close(); doc.Dispose(); ret = new MemoryStream(s.ToArray()); ret.Seek(0, SeekOrigin.Begin); } } } } } } } return(ret); }