Пример #1
0
        public async Task <bool> CreateCouponAsync(string bookId, CouponForCreation coupon)
        {
            if (string.IsNullOrWhiteSpace(_couponTemplateJson))
            {
                throw new Exception($"Coupon JSON template file not loaded: {CouponJsonTemplateFileName}");
            }

            var url = $"https://leanpub.com/{bookId}/coupons.json?api_key={_apiKey}";

            var jsonString = _couponTemplateJson
                             .Replace("{{coupon_code}}", coupon.coupon_code)
                             .Replace("{{discounted_price}}", $"{coupon.discounted_price:0.0}")
                             .Replace("{{start_date}}", $"{coupon.start_date:yyyy-MM-dd}")
                             .Replace("{{end_date}}", $"{coupon.end_date:yyyy-MM-dd}")
                             .Replace("{{max_uses}}", $"{coupon.max_uses}");

            var jsonContent = new StringContent(jsonString, Encoding.UTF8, "application/json");
            var response    = await _httpClient.PostAsync(url, jsonContent);

            if (response.StatusCode == HttpStatusCode.OK || response.StatusCode == HttpStatusCode.Created)
            {
                return(true);
            }
            var msg = await response.Content.ReadAsStringAsync();

            throw new Exception($"Failed: {msg}");
        }
Пример #2
0
        public static async Task CreateCoupon(string bookId)
        {
            var coupon = new CouponForCreation
            {
                coupon_code      = "test1234",
                start_date       = DateTime.Now.Date,
                end_date         = DateTime.Now.AddYears(1).Date,
                max_uses         = 1,
                discounted_price = 0.0,
                suspended        = false,
                note             = string.Empty
            };

            if (await apiClient.CreateCouponAsync(bookId, coupon))
            {
                Console.WriteLine($"Coupon created: {coupon.coupon_code}");
            }
        }
Пример #3
0
        public static async Task BatchCreateCoupons(string bookId, string couponPrefix, int startNumber, int count)
        {
            const string Characters = "23456789abcdefghjkmnpqrstuvwxyz";

            var r = new Random((int)DateTime.Now.Ticks);

            for (int i = 0; i < count; i++)
            {
                int number = startNumber + i;
                var coupon = new CouponForCreation
                {
                    coupon_code      = $"{couponPrefix}{number:00}-{GetRandomCode()}",
                    start_date       = DateTime.Now.Date.AddDays(-1), // 確保立刻生效
                    end_date         = DateTime.Now.AddYears(1).Date, // 一年內有效
                    max_uses         = 1,                             // 只能使用一次
                    discounted_price = 0.0,                           // 零元結帳
                    suspended        = false,                         // 立刻生效
                    note             = string.Empty
                };

                if (await apiClient.CreateCouponAsync(bookId, coupon))
                {
                    Console.WriteLine($"Coupon created: {coupon.coupon_code}");
                }
            }

            string GetRandomCode()
            {
                var sb = new StringBuilder();

                for (int i = 0; i < 4; i++)
                {
                    int index = r.Next(0, Characters.Length - 1);
                    sb.Append(Characters[index]);
                }
                return(sb.ToString());
            }
        }