public CouponServiceTest(
            StripeMockFixture stripeMockFixture,
            MockHttpClientFixture mockHttpClientFixture)
            : base(stripeMockFixture, mockHttpClientFixture)
        {
            this.service = new CouponService(this.StripeClient);

            this.createOptions = new CouponCreateOptions
            {
                PercentOff = 25,
                Duration   = "forever",
            };

            this.updateOptions = new CouponUpdateOptions
            {
                Metadata = new Dictionary <string, string>
                {
                    { "key", "value" },
                },
            };

            this.listOptions = new CouponListOptions
            {
                Limit = 1,
            };
        }
Example #2
0
        public virtual async Task <Coupon> Create(CouponCreateOptions createOptions)
        {
            var url = this.ApplyAllParameters(createOptions, Urls.Coupons, false);

            var response = await Requestor.Post(url);

            return(Mapper <Coupon> .MapFromJson(response));
        }
Example #3
0
        private async Task <PostCreateCouponResponse> CreateCouponBodyAsync(PostCreateCouponRequest request)
        {
            using var transaction = await _unitOfWork.BeginTransactionAsync();

            var promotionCode = new PromotionCode();

            try
            {
                request.RedeemBy  = request.RedeemBy.Date;
                request.BeginDate = DateTime.Today;
                var coupon = await _couponService.CreateCouponAsync(request);

                await _unitOfWork.SaveChangesAsync();

                var couponOptions = new CouponCreateOptions()
                {
                    Id               = coupon.Id.ToString(),
                    Name             = coupon.Name,
                    AmountOff        = (long)(coupon.DiscountPrice * 100),
                    Currency         = "eur",
                    Duration         = "repeating",
                    DurationInMonths = request.DurationInMonths,
                    RedeemBy         = coupon.EndDate,
                };

                await _stripeCouponService.CreateAsync(couponOptions);

                // Hate stripe already
                var promoCodeOptions = new PromotionCodeCreateOptions()
                {
                    Coupon       = coupon.Id.ToString(),
                    Code         = coupon.Name,
                    Restrictions = new()
                    {
                        MinimumAmount         = (long)(coupon.OrderOverPrice * 100),
                        MinimumAmountCurrency = "eur"
                    }
                };

                promotionCode = await _promotionCodeService.CreateAsync(promoCodeOptions);

                promotionCode.Created = promotionCode.Created.Date;

                await transaction.CommitAsync();
            }
            catch (StripeException)
            {
                await transaction.RollbackAsync();

                throw;
            }

            var response = _mapper.Map <PostCreateCouponResponse>(promotionCode);

            return(response);
        }
Example #4
0
        public void Serialize()
        {
            var options = new CouponCreateOptions
            {
                PercentOff = 25,
                Duration   = "forever",
            };

            Assert.Equal("percent_off=25&duration=forever", FormEncoder.CreateQueryString(options));
        }
Example #5
0
        public void Serialize()
        {
            var options = new CouponCreateOptions()
            {
                PercentOff = 25,
                Duration   = "forever",
            };

            var url = this.service.ApplyAllParameters(options, string.Empty, false);

            Assert.Equal("?percent_off=25&duration=forever", url);
        }
        public CouponServiceTest()
        {
            this.service = new CouponService();

            this.createOptions = new CouponCreateOptions
            {
                PercentOff = 25,
                Duration   = "forever",
            };

            this.updateOptions = new CouponUpdateOptions
            {
                Metadata = new Dictionary <string, string>
                {
                    { "key", "value" },
                },
            };

            this.listOptions = new CouponListOptions
            {
                Limit = 1,
            };
        }
Example #7
0
        private static Coupon GetCoupon(Order order)
        {
            Coupon coupon = null;

            if (order.DiscountId.HasValue && order.Discount > 0)
            {
                var couponCode = order.DiscountCoupon;
                if (couponCode.IsNullEmptyOrWhiteSpace())
                {
                    couponCode = "COUPON_" + order.DiscountId;
                }
                //create a coupon
                var options = new CouponCreateOptions
                {
                    Duration  = "once",
                    Id        = couponCode,
                    AmountOff = (long)order.Discount
                };
                var service = new CouponService();
                coupon = service.Create(options);
            }

            return(coupon);
        }
        public async Task <PaymentGatewayResult <IPaymentCoupon> > CreateCouponAsync(CreateCouponViewModel model)
        {
            try
            {
                var options = new CouponCreateOptions();

                if (model.Duration != "repeating")
                {
                    model.DurationInMonths = null;
                }

                model.Currency       = "usd";
                model.MaxRedemptions = model.MaxRedemptions > 0 ? model.MaxRedemptions : null;
                model.PercentOff     = model.PercentOff > 0 ? model.PercentOff : null;
                model.AmountOff      = model.AmountOff > 0 ? model.AmountOff * 100 : null;

                options = _mapper.Map <CouponCreateOptions>(model);

                var coupon = await _couponService.CreateAsync(options);

                if (coupon.PercentOff != null)
                {
                    if (coupon.Duration == "once")
                    {
                        coupon.Object = coupon.PercentOff + "% off " + coupon.Duration;
                    }
                    else if (coupon.Duration == "forever")
                    {
                        coupon.Object = coupon.PercentOff + "% off " + coupon.Duration;
                    }

                    else if (coupon.Duration == "repeating")
                    {
                        if (coupon.DurationInMonths == 1)
                        {
                            coupon.Object = coupon.PercentOff + "% off every Year for 1 month";
                        }
                        else
                        {
                            coupon.Object = coupon.PercentOff + "% off every Year for " + coupon.DurationInMonths + " months";
                        }
                    }
                }

                if (coupon.AmountOff != null)
                {
                    if (coupon.Duration == "once")
                    {
                        coupon.Object = "$" + coupon.AmountOff + " off once";
                    }

                    else if (coupon.Duration == "forever")
                    {
                        coupon.Object = "$" + coupon.AmountOff + " off " + coupon.Duration;
                    }

                    else if (coupon.Duration == "repeating")
                    {
                        if (coupon.DurationInMonths == 1)
                        {
                            coupon.Object = coupon.Currency.ToUpper() + coupon.AmountOff + " off every Year for 1 month";
                        }
                        else
                        {
                            coupon.Object = "$" + coupon.AmountOff + " off every Year for " + coupon.DurationInMonths + " months";
                        }
                    }
                }
                return(PaymentGatewayResult <IPaymentCoupon> .Success(_mapper.Map <IPaymentCoupon>(coupon)));
            }
            catch (StripeException e)
            {
                return(PaymentGatewayResult <IPaymentCoupon> .Failed(e));
            }
        }