| 5 | namespace API.Services; |
| 6 | |
| 7 | public class DiscountService |
| 8 | { |
| 9 | public DiscountService(IConfiguration config) |
| 10 | { |
| 11 | StripeConfiguration.ApiKey = config["StripeSettings:SecretKey"]; |
| 12 | } |
| 13 | |
| 14 | public async Task<AppCoupon?> GetCouponFromPromoCode(string code) |
| 15 | { |
| 16 | var promotionService = new PromotionCodeService(); |
| 17 | |
| 18 | var options = new PromotionCodeListOptions |
| 19 | { |
| 20 | Code = code |
| 21 | }; |
| 22 | |
| 23 | var promotionCodes = await promotionService.ListAsync(options); |
| 24 | |
| 25 | var promotionCode = promotionCodes.FirstOrDefault(); |
| 26 | |
| 27 | if (promotionCode != null && promotionCode.Coupon != null) |
| 28 | { |
| 29 | return new AppCoupon |
| 30 | { |
| 31 | Name = promotionCode.Coupon.Name, |
| 32 | AmountOff = promotionCode.Coupon.AmountOff, |
| 33 | PercentOff = promotionCode.Coupon.PercentOff, |
| 34 | CouponId = promotionCode.Coupon.Id, |
| 35 | PromotionCode = promotionCode.Code |
| 36 | }; |
| 37 | } |
| 38 | |
| 39 | return null; |
| 40 | } |
| 41 | |
| 42 | public async Task<long> CalculateDiscountFromAmount(AppCoupon appCoupon, long amount, |
| 43 | bool removeDiscount = false) |
| 44 | { |
| 45 | var couponService = new CouponService(); |
| 46 | |
| 47 | var coupon = await couponService.GetAsync(appCoupon.CouponId); |
| 48 | |
| 49 | if (coupon.AmountOff.HasValue && !removeDiscount) |
| 50 | { |
| 51 | return (long)coupon.AmountOff; |
| 52 | } |
| 53 | else if (coupon.PercentOff.HasValue && !removeDiscount) |
| 54 | { |
| 55 | return (long)Math.Round(amount * (coupon.PercentOff.Value / 100), MidpointRounding.AwayFromZero); |
| 56 | } |
| 57 | |
| 58 | return 0; |
| 59 | } |
| 60 | } |
nothing calls this directly
no outgoing calls
no test coverage detected