예제 #1
0
        public Uri GetShoppingUri(int tenant, int plan)
        {
            var key  = "billing/urls/" + tenant;
            var urls = cache.Get(key) as IDictionary <int, Uri>;

            if (urls == null)
            {
                urls = new Dictionary <int, Uri>();
            }
            if (!urls.ContainsKey(plan))
            {
                try
                {
                    using (var client = new BillingClient())
                    {
                        var q          = quotaService.GetTenantQuota(plan);
                        var avangateId = q.AvangateId;
                        if (!string.IsNullOrEmpty(avangateId))
                        {
                            try
                            {
                                var cached = GetBillingInfo(tenant);
                                if (cached == null || cached.Item1 == plan)
                                {
                                    urls[plan] = new Uri(client.GetPaymentUrl(tenant, avangateId).Value);
                                }
                                else
                                {
                                    urls[plan] = new Uri(client.GetPaymentUpdateUrl(tenant, avangateId).Value);
                                }
                            }
                            catch
                            {
                                // new tariff
                                urls[plan] = new Uri(client.GetPaymentUrl(tenant, avangateId).Value);
                            }
                        }
                        else
                        {
                            urls[plan] = null;
                        }
                    }
                }
                catch (Exception error)
                {
                    log.Error(error);
                    urls[plan] = null;
                }
                cache.Insert(key, urls, DateTime.UtcNow.Add(CacheExpiration));
            }
            return(urls[plan]);
        }
예제 #2
0
        public Tariff GetTariff(int tenantId)
        {
            var key    = "tariff/" + tenantId;
            var tariff = cache.Get(key) as Tariff;

            if (tariff == null)
            {
                tariff = Tariff.CreateDefault();

                try
                {
                    using (var client = new BillingClient())
                    {
                        var xelement = client.GetLastPayment(tenantId);

                        var productid = xelement.Element("product-id").Value;
                        var quota     = quotaService.GetTenantQuotas().SingleOrDefault(q => q.AvangateId == productid);
                        tariff.QuotaId = quota.Id;

                        var enddate = xelement.Element("end-date");
                        tariff.DueDate = DateTime.ParseExact(enddate.Value, "yyyy-MM-dd HH:mm:ss", null);

                        SaveBillingInfo(tenantId, Tuple.Create(tariff.QuotaId, tariff.DueDate));
                    }
                }
                catch (Exception error)
                {
                    log.Error(error);

                    var cached = GetBillingInfo(tenantId);
                    if (cached != null)
                    {
                        tariff.QuotaId = cached.Item1;
                        tariff.DueDate = cached.Item2;
                    }
                }

                CalculateState(tenantId, tariff);

                cache.Insert(key, tariff, DateTime.UtcNow.Add(CacheExpiration));
            }

            return(tariff);
        }
예제 #3
0
        public IEnumerable <PaymentInfo> GetPayments(int tenantId)
        {
            var key      = "billing/payments/" + tenantId;
            var payments = cache.Get(key) as List <PaymentInfo>;

            if (payments == null)
            {
                payments = new List <PaymentInfo>();
                try
                {
                    using (var client = new BillingClient())
                    {
                        var xelement = client.GetPayments(tenantId);
                        foreach (var x in xelement.Elements("payment"))
                        {
                            var pi = new PaymentInfo();

                            var name  = string.Empty;
                            var fname = x.Element("fname");
                            if (fname != null)
                            {
                                name += fname.Value;
                            }
                            var lname = x.Element("lname");
                            if (lname != null)
                            {
                                name += " " + lname.Value;
                            }
                            pi.Name = name.Trim();

                            var email = x.Element("email");
                            if (email != null)
                            {
                                pi.Email = email.Value;
                            }

                            var paymentdate = x.Element("payment-date");
                            if (paymentdate != null)
                            {
                                pi.Date = DateTime.ParseExact(paymentdate.Value, "yyyy-MM-dd HH:mm:ss", null);
                            }

                            var price = x.Element("price");
                            if (price != null)
                            {
                                var separator = CultureInfo.InvariantCulture.NumberFormat.CurrencyDecimalSeparator;
                                pi.Price = Decimal.Parse(price.Value.Replace(".", separator).Replace(",", separator), NumberStyles.Currency, CultureInfo.InvariantCulture);
                            }

                            var currency = x.Element("payment-currency");
                            if (currency != null)
                            {
                                pi.Currency = currency.Value;
                            }

                            var method = x.Element("payment-method");
                            if (method != null)
                            {
                                pi.Method = method.Value;
                            }

                            var cartid = x.Element("cart-id");
                            if (cartid != null)
                            {
                                pi.CartId = cartid.Value;
                            }

                            payments.Add(pi);
                        }
                    }
                }
                catch (Exception error)
                {
                    log.Error(error);
                }

                cache.Insert(key, payments, DateTime.UtcNow.Add(CacheExpiration));
            }

            return(payments);
        }