|
| 1 | +using Bit.Billing.Constants; |
| 2 | +using Bit.Core.Settings; |
| 3 | +using Stripe; |
| 4 | + |
| 5 | +namespace Bit.Billing.Services.Implementations; |
| 6 | + |
| 7 | +public class StripeEventService : IStripeEventService |
| 8 | +{ |
| 9 | + private readonly GlobalSettings _globalSettings; |
| 10 | + private readonly IStripeFacade _stripeFacade; |
| 11 | + |
| 12 | + public StripeEventService( |
| 13 | + GlobalSettings globalSettings, |
| 14 | + IStripeFacade stripeFacade) |
| 15 | + { |
| 16 | + _globalSettings = globalSettings; |
| 17 | + _stripeFacade = stripeFacade; |
| 18 | + } |
| 19 | + |
| 20 | + public async Task<Charge> GetCharge(Event stripeEvent, bool fresh = false, List<string> expand = null) |
| 21 | + { |
| 22 | + var eventCharge = Extract<Charge>(stripeEvent); |
| 23 | + |
| 24 | + if (!fresh) |
| 25 | + { |
| 26 | + return eventCharge; |
| 27 | + } |
| 28 | + |
| 29 | + var charge = await _stripeFacade.GetCharge(eventCharge.Id, new ChargeGetOptions { Expand = expand }); |
| 30 | + |
| 31 | + if (charge == null) |
| 32 | + { |
| 33 | + throw new Exception( |
| 34 | + $"Received null Charge from Stripe for ID '{eventCharge.Id}' while processing Event with ID '{stripeEvent.Id}'"); |
| 35 | + } |
| 36 | + |
| 37 | + return charge; |
| 38 | + } |
| 39 | + |
| 40 | + public async Task<Customer> GetCustomer(Event stripeEvent, bool fresh = false, List<string> expand = null) |
| 41 | + { |
| 42 | + var eventCustomer = Extract<Customer>(stripeEvent); |
| 43 | + |
| 44 | + if (!fresh) |
| 45 | + { |
| 46 | + return eventCustomer; |
| 47 | + } |
| 48 | + |
| 49 | + var customer = await _stripeFacade.GetCustomer(eventCustomer.Id, new CustomerGetOptions { Expand = expand }); |
| 50 | + |
| 51 | + if (customer == null) |
| 52 | + { |
| 53 | + throw new Exception( |
| 54 | + $"Received null Customer from Stripe for ID '{eventCustomer.Id}' while processing Event with ID '{stripeEvent.Id}'"); |
| 55 | + } |
| 56 | + |
| 57 | + return customer; |
| 58 | + } |
| 59 | + |
| 60 | + public async Task<Invoice> GetInvoice(Event stripeEvent, bool fresh = false, List<string> expand = null) |
| 61 | + { |
| 62 | + var eventInvoice = Extract<Invoice>(stripeEvent); |
| 63 | + |
| 64 | + if (!fresh) |
| 65 | + { |
| 66 | + return eventInvoice; |
| 67 | + } |
| 68 | + |
| 69 | + var invoice = await _stripeFacade.GetInvoice(eventInvoice.Id, new InvoiceGetOptions { Expand = expand }); |
| 70 | + |
| 71 | + if (invoice == null) |
| 72 | + { |
| 73 | + throw new Exception( |
| 74 | + $"Received null Invoice from Stripe for ID '{eventInvoice.Id}' while processing Event with ID '{stripeEvent.Id}'"); |
| 75 | + } |
| 76 | + |
| 77 | + return invoice; |
| 78 | + } |
| 79 | + |
| 80 | + public async Task<PaymentMethod> GetPaymentMethod(Event stripeEvent, bool fresh = false, List<string> expand = null) |
| 81 | + { |
| 82 | + var eventPaymentMethod = Extract<PaymentMethod>(stripeEvent); |
| 83 | + |
| 84 | + if (!fresh) |
| 85 | + { |
| 86 | + return eventPaymentMethod; |
| 87 | + } |
| 88 | + |
| 89 | + var paymentMethod = await _stripeFacade.GetPaymentMethod(eventPaymentMethod.Id, new PaymentMethodGetOptions { Expand = expand }); |
| 90 | + |
| 91 | + if (paymentMethod == null) |
| 92 | + { |
| 93 | + throw new Exception( |
| 94 | + $"Received null Payment Method from Stripe for ID '{eventPaymentMethod.Id}' while processing Event with ID '{stripeEvent.Id}'"); |
| 95 | + } |
| 96 | + |
| 97 | + return paymentMethod; |
| 98 | + } |
| 99 | + |
| 100 | + public async Task<Subscription> GetSubscription(Event stripeEvent, bool fresh = false, List<string> expand = null) |
| 101 | + { |
| 102 | + var eventSubscription = Extract<Subscription>(stripeEvent); |
| 103 | + |
| 104 | + if (!fresh) |
| 105 | + { |
| 106 | + return eventSubscription; |
| 107 | + } |
| 108 | + |
| 109 | + var subscription = await _stripeFacade.GetSubscription(eventSubscription.Id, new SubscriptionGetOptions { Expand = expand }); |
| 110 | + |
| 111 | + if (subscription == null) |
| 112 | + { |
| 113 | + throw new Exception( |
| 114 | + $"Received null Subscription from Stripe for ID '{eventSubscription.Id}' while processing Event with ID '{stripeEvent.Id}'"); |
| 115 | + } |
| 116 | + |
| 117 | + return subscription; |
| 118 | + } |
| 119 | + |
| 120 | + public async Task<bool> ValidateCloudRegion(Event stripeEvent) |
| 121 | + { |
| 122 | + var serverRegion = _globalSettings.BaseServiceUri.CloudRegion; |
| 123 | + |
| 124 | + var customerExpansion = new List<string> { "customer" }; |
| 125 | + |
| 126 | + var customerMetadata = stripeEvent.Type switch |
| 127 | + { |
| 128 | + HandledStripeWebhook.SubscriptionDeleted or HandledStripeWebhook.SubscriptionUpdated => |
| 129 | + (await GetSubscription(stripeEvent, true, customerExpansion))?.Customer?.Metadata, |
| 130 | + |
| 131 | + HandledStripeWebhook.ChargeSucceeded or HandledStripeWebhook.ChargeRefunded => |
| 132 | + (await GetCharge(stripeEvent, true, customerExpansion))?.Customer?.Metadata, |
| 133 | + |
| 134 | + HandledStripeWebhook.UpcomingInvoice => |
| 135 | + (await GetInvoice(stripeEvent, true, customerExpansion))?.Customer?.Metadata, |
| 136 | + |
| 137 | + HandledStripeWebhook.PaymentSucceeded or HandledStripeWebhook.PaymentFailed or HandledStripeWebhook.InvoiceCreated => |
| 138 | + (await GetInvoice(stripeEvent, true, customerExpansion))?.Customer?.Metadata, |
| 139 | + |
| 140 | + HandledStripeWebhook.PaymentMethodAttached => |
| 141 | + (await GetPaymentMethod(stripeEvent, true, customerExpansion))?.Customer?.Metadata, |
| 142 | + |
| 143 | + HandledStripeWebhook.CustomerUpdated => |
| 144 | + (await GetCustomer(stripeEvent, true))?.Metadata, |
| 145 | + |
| 146 | + _ => null |
| 147 | + }; |
| 148 | + |
| 149 | + if (customerMetadata == null) |
| 150 | + { |
| 151 | + return false; |
| 152 | + } |
| 153 | + |
| 154 | + var customerRegion = GetCustomerRegion(customerMetadata); |
| 155 | + |
| 156 | + return customerRegion == serverRegion; |
| 157 | + } |
| 158 | + |
| 159 | + private static T Extract<T>(Event stripeEvent) |
| 160 | + { |
| 161 | + if (stripeEvent.Data.Object is not T type) |
| 162 | + { |
| 163 | + throw new Exception($"Stripe event with ID '{stripeEvent.Id}' does not have object matching type '{typeof(T).Name}'"); |
| 164 | + } |
| 165 | + |
| 166 | + return type; |
| 167 | + } |
| 168 | + |
| 169 | + private static string GetCustomerRegion(IDictionary<string, string> customerMetadata) |
| 170 | + { |
| 171 | + const string defaultRegion = "US"; |
| 172 | + |
| 173 | + if (customerMetadata is null) |
| 174 | + { |
| 175 | + return null; |
| 176 | + } |
| 177 | + |
| 178 | + if (customerMetadata.TryGetValue("region", out var value)) |
| 179 | + { |
| 180 | + return value; |
| 181 | + } |
| 182 | + |
| 183 | + var miscasedRegionKey = customerMetadata.Keys |
| 184 | + .FirstOrDefault(key => key.Equals("region", StringComparison.OrdinalIgnoreCase)); |
| 185 | + |
| 186 | + if (miscasedRegionKey is null) |
| 187 | + { |
| 188 | + return defaultRegion; |
| 189 | + } |
| 190 | + |
| 191 | + _ = customerMetadata.TryGetValue(miscasedRegionKey, out var regionValue); |
| 192 | + |
| 193 | + return !string.IsNullOrWhiteSpace(regionValue) |
| 194 | + ? regionValue |
| 195 | + : defaultRegion; |
| 196 | + } |
| 197 | +} |
0 commit comments