This repository was archived by the owner on Jun 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathbilling.py
More file actions
1095 lines (963 loc) · 42 KB
/
billing.py
File metadata and controls
1095 lines (963 loc) · 42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import logging
import re
from abc import ABC, abstractmethod
from datetime import datetime, timezone
import stripe
from dateutil.relativedelta import relativedelta
from django.conf import settings
from shared.plan.constants import PlanBillingRate, TierName
from shared.plan.service import PlanService
from billing.constants import REMOVED_INVOICE_STATUSES
from codecov_auth.models import Owner, Plan
log = logging.getLogger(__name__)
SCHEDULE_RELEASE_OFFSET = 10
if settings.STRIPE_API_KEY:
stripe.api_key = settings.STRIPE_API_KEY
stripe.api_version = "2024-12-18.acacia"
def _log_stripe_error(method):
def catch_and_raise(*args, **kwargs):
try:
return method(*args, **kwargs)
except stripe.StripeError as e:
log.warning(f"StripeError raised: {e.user_message}")
raise
return catch_and_raise
class AbstractPaymentService(ABC):
@abstractmethod
def get_invoice(self, owner, invoice_id):
pass
@abstractmethod
def list_filtered_invoices(self, owner, limit=10):
pass
@abstractmethod
def delete_subscription(self, owner):
pass
@abstractmethod
def modify_subscription(self, owner, plan):
pass
@abstractmethod
def create_checkout_session(self, owner, plan):
pass
@abstractmethod
def get_subscription(self, owner):
pass
@abstractmethod
def update_payment_method(self, owner, payment_method):
pass
@abstractmethod
def update_email_address(self, owner, email_address):
pass
@abstractmethod
def update_billing_address(self, owner, name, billing_address):
pass
@abstractmethod
def get_schedule(self, owner):
pass
@abstractmethod
def apply_cancellation_discount(self, owner: Owner):
pass
@abstractmethod
def create_setup_intent(self, owner):
pass
class StripeService(AbstractPaymentService):
def __init__(self, requesting_user):
if settings.STRIPE_API_KEY is None:
log.critical(
"Missing stripe API key configuration -- communication with stripe won't be possible."
)
if not isinstance(requesting_user, Owner):
raise Exception(
"StripeService requires requesting_user to be Owner instance"
)
self.requesting_user = requesting_user
def _get_checkout_session_and_subscription_metadata(self, owner: Owner):
return {
"service": owner.service,
"obo_organization": owner.ownerid,
"username": owner.username,
"obo_name": self.requesting_user.name,
"obo_email": self.requesting_user.email,
"obo": self.requesting_user.ownerid,
}
@_log_stripe_error
def get_invoice(self, owner, invoice_id):
log.info(
f"Fetching invoice {invoice_id} from Stripe for ownerid {owner.ownerid}"
)
try:
invoice = stripe.Invoice.retrieve(invoice_id)
except stripe.InvalidRequestError:
log.info(f"invoice {invoice_id} not found for owner {owner.ownerid}")
return None
if invoice["customer"] != owner.stripe_customer_id:
log.info(
f"customer id ({invoice['customer']}) on invoice does not match the owner customer id ({owner.stripe_customer_id})"
)
return None
return invoice
def filter_invoices_by_status(self, invoice):
if invoice["status"] and invoice["status"] not in REMOVED_INVOICE_STATUSES:
return invoice
def filter_invoices_by_total(self, invoice):
if invoice["total"] and invoice["total"] != 0:
return invoice
@_log_stripe_error
def list_filtered_invoices(self, owner: Owner, limit=10):
log.info(f"Fetching invoices from Stripe for ownerid {owner.ownerid}")
if owner.stripe_customer_id is None:
log.info("stripe_customer_id is None, not fetching invoices")
return []
invoices = stripe.Invoice.list(customer=owner.stripe_customer_id, limit=limit)[
"data"
]
invoices_filtered_by_status = filter(self.filter_invoices_by_status, invoices)
invoices_filtered_by_status_and_total = filter(
self.filter_invoices_by_total, invoices_filtered_by_status
)
return list(invoices_filtered_by_status_and_total)
def cancel_and_refund(
self,
owner,
current_subscription_datetime,
subscription_plan_interval,
autorefunds_remaining,
):
# cancels a Stripe customer subscription immediately and attempts to refund their payments for the current period
stripe.Subscription.cancel(owner.stripe_subscription_id)
start_of_last_period = current_subscription_datetime - relativedelta(months=1)
invoice_grace_period_start = current_subscription_datetime - relativedelta(
days=1
)
if subscription_plan_interval == "year":
start_of_last_period = current_subscription_datetime - relativedelta(
years=1
)
invoice_grace_period_start = current_subscription_datetime - relativedelta(
days=3
)
invoices_list = stripe.Invoice.list(
subscription=owner.stripe_subscription_id,
status="paid",
created={
"gte": int(start_of_last_period.timestamp()),
"lt": int(current_subscription_datetime.timestamp()),
},
)
# we only want to refund the invoices PAID recently for the latest, current period. "invoices_list" gives us any invoice
# created over the last month/year based on what period length they are on but the customer could have possibly
# switched from monthly to yearly recently.
recently_paid_invoices_list = [
invoice
for invoice in invoices_list["data"]
if invoice["status_transitions"]["paid_at"] is not None
and invoice["status_transitions"]["paid_at"]
>= int(invoice_grace_period_start.timestamp())
]
created_refund = False
# there could be multiple invoices that need to be refunded such as if the user increased seats within the grace period
for invoice in recently_paid_invoices_list:
# refund if the invoice has a charge and it has been fully paid
if invoice["charge"] is not None and invoice["amount_remaining"] == 0:
stripe.Refund.create(invoice["charge"])
created_refund = True
if created_refund:
# update the customer's balance back to 0 in accordance to
# https://support.stripe.com/questions/refunding-credit-balance-to-customer-after-subscription-downgrade-or-cancellation
stripe.Customer.modify(
owner.stripe_customer_id,
balance=0,
metadata={"autorefunds_remaining": str(autorefunds_remaining - 1)},
)
log.info(
"Grace period cancelled a subscription and autorefunded associated invoices",
extra=dict(
owner_id=owner.ownerid,
user_id=self.requesting_user.ownerid,
subscription_id=owner.stripe_subscription_id,
customer_id=owner.stripe_customer_id,
autorefunds_remaining=autorefunds_remaining - 1,
),
)
else:
log.info(
"Grace period cancelled a subscription but did not find any appropriate invoices to autorefund",
extra=dict(
owner_id=owner.ownerid,
user_id=self.requesting_user.ownerid,
subscription_id=owner.stripe_subscription_id,
customer_id=owner.stripe_customer_id,
autorefunds_remaining=autorefunds_remaining,
),
)
@_log_stripe_error
def delete_subscription(self, owner: Owner):
subscription = stripe.Subscription.retrieve(owner.stripe_subscription_id)
subscription_schedule_id = subscription.schedule
log.info(
f"Downgrade to basic plan from user plan for owner {owner.ownerid} by user #{self.requesting_user.ownerid}",
extra=dict(ownerid=owner.ownerid),
)
if subscription_schedule_id:
log.info(
f"Releasing subscription from schedule for owner {owner.ownerid} by user #{self.requesting_user.ownerid}",
extra=dict(ownerid=owner.ownerid),
)
stripe.SubscriptionSchedule.release(subscription_schedule_id)
# we give an auto-refund grace period of 24 hours for a monthly subscription or 72 hours for a yearly subscription
current_subscription_datetime = datetime.fromtimestamp(
subscription["current_period_start"], tz=timezone.utc
)
difference_from_now = datetime.now(timezone.utc) - current_subscription_datetime
subscription_plan_interval = getattr(
getattr(subscription, "plan", None), "interval", None
)
within_refund_grace_period = (
subscription_plan_interval == "month" and difference_from_now.days < 1
) or (subscription_plan_interval == "year" and difference_from_now.days < 3)
if within_refund_grace_period:
customer = stripe.Customer.retrieve(owner.stripe_customer_id)
# we are currently allowing customers 2 autorefund instances
autorefunds_remaining = int(
customer["metadata"].get("autorefunds_remaining", "2")
)
log.info(
"Deleting subscription with attempted immediate cancellation with autorefund within grace period",
extra=dict(
owner_id=owner.ownerid,
user_id=self.requesting_user.ownerid,
subscription_id=owner.stripe_subscription_id,
customer_id=owner.stripe_customer_id,
autorefunds_remaining=autorefunds_remaining,
),
)
if autorefunds_remaining > 0:
return self.cancel_and_refund(
owner,
current_subscription_datetime,
subscription_plan_interval,
autorefunds_remaining,
)
# schedule a cancellation at the end of the paid period with no refund
stripe.Subscription.modify(
owner.stripe_subscription_id,
cancel_at_period_end=True,
proration_behavior="none",
)
@_log_stripe_error
def get_subscription(self, owner: Owner):
if not owner.stripe_subscription_id:
return None
return stripe.Subscription.retrieve(
owner.stripe_subscription_id,
expand=[
"latest_invoice",
"customer",
"customer.invoice_settings.default_payment_method",
"customer.tax_ids",
],
)
@_log_stripe_error
def get_schedule(self, owner: Owner):
if not owner.stripe_subscription_id:
return None
subscription = self.get_subscription(owner)
subscription_schedule_id = subscription.schedule
if not subscription_schedule_id:
return None
return stripe.SubscriptionSchedule.retrieve(subscription_schedule_id)
@_log_stripe_error
def modify_subscription(self, owner: Owner, desired_plan: dict):
desired_plan_info = Plan.objects.filter(name=desired_plan["value"]).first()
if not desired_plan_info:
log.error(
f"Plan {desired_plan['value']} not found",
extra=dict(owner_id=owner.ownerid),
)
return
subscription = stripe.Subscription.retrieve(owner.stripe_subscription_id)
proration_behavior = self._get_proration_params(
owner,
desired_plan_info=desired_plan_info,
desired_quantity=desired_plan["quantity"],
)
subscription_schedule_id = subscription.schedule
# proration_behavior indicates whether we immediately invoice a user or not. We only immediately
# invoice a user if the user increases the number of seats or if the plan changes from monthly to yearly.
# An increase in seats and/or plan implies the user is upgrading, hence 'is_upgrading' is a consequence
# of proration_behavior providing an invoice, in this case, != "none"
# TODO: change this to "self._is_upgrading_seats(owner, desired_plan) or self._is_extending_term(owner, desired_plan)"
is_upgrading = (
True
if proration_behavior != "none" and desired_plan_info.stripe_id
else False
)
# Divide logic bw immediate updates and scheduled updates
# Immediate updates: when user upgrades seats or plan
# If the user is not in a schedule, update immediately
# If the user is in a schedule, update the existing schedule
# Scheduled updates: when the user decreases seats or plan
# If the user is not in a schedule, create a schedule
# If the user is in a schedule, update the existing schedule
if is_upgrading:
if subscription_schedule_id:
log.info(
f"Releasing Stripe schedule for owner {owner.ownerid} to {desired_plan['value']} with {desired_plan['quantity']} seats by user #{self.requesting_user.ownerid}"
)
stripe.SubscriptionSchedule.release(subscription_schedule_id)
log.info(
f"Updating Stripe subscription for owner {owner.ownerid} to {desired_plan['value']} by user #{self.requesting_user.ownerid}"
)
subscription = stripe.Subscription.modify(
owner.stripe_subscription_id,
cancel_at_period_end=False,
items=[
{
"id": subscription["items"]["data"][0]["id"],
"plan": desired_plan_info.stripe_id,
"quantity": desired_plan["quantity"],
}
],
metadata=self._get_checkout_session_and_subscription_metadata(owner),
proration_behavior=proration_behavior,
# TODO: we need to include this arg, but it means we need to remove some of the existing args
# on the .modify() call https://docs.stripe.com/billing/subscriptions/pending-updates-reference
# payment_behavior="pending_if_incomplete",
)
log.info(
f"Stripe subscription upgrade attempted for owner {owner.ownerid} by user #{self.requesting_user.ownerid}"
)
indication_of_payment_failure = getattr(
subscription, "pending_update", None
)
if indication_of_payment_failure:
# payment failed, raise this to user by setting as delinquent
owner.delinquent = True
owner.save()
log.info(
f"Stripe subscription upgrade failed for owner {owner.ownerid} by user #{self.requesting_user.ownerid}",
extra=dict(pending_update=indication_of_payment_failure),
)
else:
# payment successful
plan_service = PlanService(current_org=owner)
plan_service.update_plan(
name=desired_plan["value"], user_count=desired_plan["quantity"]
)
log.info(
f"Stripe subscription upgraded successfully for owner {owner.ownerid} by user #{self.requesting_user.ownerid}"
)
else:
if not subscription_schedule_id:
schedule = stripe.SubscriptionSchedule.create(
from_subscription=owner.stripe_subscription_id
)
subscription_schedule_id = schedule.id
self._modify_subscription_schedule(
owner, subscription, subscription_schedule_id, desired_plan
)
def _modify_subscription_schedule(
self,
owner: Owner,
subscription: stripe.Subscription,
subscription_schedule_id: str,
desired_plan: dict,
):
current_subscription_start_date = subscription["current_period_start"]
current_subscription_end_date = subscription["current_period_end"]
subscription_item = subscription["items"]["data"][0]
current_plan = subscription_item["plan"]["id"]
current_quantity = subscription_item["quantity"]
plan = Plan.objects.filter(name=desired_plan["value"]).first()
if not plan or not plan.stripe_id:
log.error(
f"Plan {desired_plan['value']} not found",
extra=dict(owner_id=owner.ownerid),
)
return
stripe.SubscriptionSchedule.modify(
subscription_schedule_id,
end_behavior="release",
phases=[
{
"start_date": current_subscription_start_date,
"end_date": current_subscription_end_date,
"items": [
{
"plan": current_plan,
"price": current_plan,
"quantity": current_quantity,
}
],
"proration_behavior": "none",
},
{
"start_date": current_subscription_end_date,
"end_date": current_subscription_end_date + SCHEDULE_RELEASE_OFFSET,
"items": [
{
"plan": plan.stripe_id,
"price": plan.stripe_id,
"quantity": desired_plan["quantity"],
}
],
"proration_behavior": "none",
},
],
metadata=self._get_checkout_session_and_subscription_metadata(owner),
)
def _is_upgrading_seats(self, owner: Owner, desired_quantity: int) -> bool:
"""
Returns `True` if purchasing more seats.
"""
return bool(owner.plan_user_count and owner.plan_user_count < desired_quantity)
def _is_extending_term(
self, current_plan_info: Plan, desired_plan_info: Plan
) -> bool:
"""
Returns `True` if switching from monthly to yearly plan.
"""
return bool(
current_plan_info
and current_plan_info.billing_rate == PlanBillingRate.MONTHLY.value
and desired_plan_info
and desired_plan_info.billing_rate == PlanBillingRate.YEARLY.value
)
def _is_similar_plan(
self,
owner: Owner,
current_plan_info: Plan,
desired_plan_info: Plan,
desired_quantity: int,
) -> bool:
"""
Returns `True` if switching to a plan with similar term and seats.
"""
is_same_term = (
current_plan_info
and desired_plan_info
and current_plan_info.billing_rate == desired_plan_info.billing_rate
)
is_same_seats = (
owner.plan_user_count and owner.plan_user_count == desired_quantity
)
# If from PRO to TEAM, then not a similar plan
if (
current_plan_info.tier.tier_name != TierName.TEAM.value
and desired_plan_info.tier.tier_name == TierName.TEAM.value
):
return False
# If from TEAM to PRO, then considered a similar plan but really is an upgrade
elif (
current_plan_info.tier.tier_name == TierName.TEAM.value
and desired_plan_info.tier.tier_name != TierName.TEAM.value
):
return True
return bool(is_same_term and is_same_seats)
def _get_proration_params(
self, owner: Owner, desired_plan_info: Plan, desired_quantity: int
) -> str:
current_plan_info = Plan.objects.select_related("tier").get(name=owner.plan)
if (
self._is_upgrading_seats(owner=owner, desired_quantity=desired_quantity)
or self._is_extending_term(
current_plan_info=current_plan_info, desired_plan_info=desired_plan_info
)
or self._is_similar_plan(
owner=owner,
current_plan_info=current_plan_info,
desired_plan_info=desired_plan_info,
desired_quantity=desired_quantity,
)
):
return "always_invoice"
else:
return "none"
def _get_success_and_cancel_url(self, owner: Owner):
short_services = {"github": "gh", "bitbucket": "bb", "gitlab": "gl"}
base_path = f"/plan/{short_services[owner.service]}/{owner.username}"
success_url = f"{settings.CODECOV_DASHBOARD_URL}{base_path}?success"
cancel_url = f"{settings.CODECOV_DASHBOARD_URL}{base_path}?cancel"
return success_url, cancel_url
@_log_stripe_error
def create_checkout_session(self, owner: Owner, desired_plan: dict):
success_url, cancel_url = self._get_success_and_cancel_url(owner)
log.info(
"Creating Stripe Checkout Session for owner",
extra=dict(owner_id=owner.ownerid),
)
plan = Plan.objects.filter(name=desired_plan["value"]).first()
if not plan or not plan.stripe_id:
log.error(
f"Plan {desired_plan['value']} not found",
extra=dict(owner_id=owner.ownerid),
)
return
session = stripe.checkout.Session.create(
payment_method_configuration=settings.STRIPE_PAYMENT_METHOD_CONFIGURATION_ID,
billing_address_collection="required",
payment_method_collection="if_required",
client_reference_id=str(owner.ownerid),
success_url=success_url,
cancel_url=cancel_url,
customer=owner.stripe_customer_id,
mode="subscription",
line_items=[
{
"price": plan.stripe_id,
"quantity": desired_plan["quantity"],
}
],
subscription_data={
"metadata": self._get_checkout_session_and_subscription_metadata(owner),
},
tax_id_collection={"enabled": True},
customer_update=(
{"name": "auto", "address": "auto"}
if owner.stripe_customer_id
else None
),
)
log.info(
f"Stripe Checkout Session created successfully for owner {owner.ownerid} by user #{self.requesting_user.ownerid}"
)
return session["id"]
def _is_unverified_payment_method(self, payment_method_id: str) -> bool:
payment_method = stripe.PaymentMethod.retrieve(payment_method_id)
is_us_bank_account = payment_method.type == "us_bank_account" and hasattr(
payment_method, "us_bank_account"
)
if is_us_bank_account:
setup_intents = stripe.SetupIntent.list(
payment_method=payment_method_id, limit=1
)
try:
latest_intent = setup_intents.data[0]
if (
latest_intent.status == "requires_action"
and latest_intent.next_action
and latest_intent.next_action.type == "verify_with_microdeposits"
):
return True
except Exception as e:
log.error(
"Error retrieving latest setup intent",
payment_method_id=payment_method_id,
extra=dict(error=e),
)
return False
return False
@_log_stripe_error
def update_payment_method(self, owner: Owner, payment_method: str) -> None:
log.info(
"Stripe update payment method for owner",
extra=dict(
owner_id=owner.ownerid,
user_id=self.requesting_user.ownerid,
subscription_id=owner.stripe_subscription_id,
customer_id=owner.stripe_customer_id,
),
)
if owner.stripe_subscription_id is None or owner.stripe_customer_id is None:
log.warn(
"Missing subscription or customer id, returning early",
extra=dict(
owner_id=owner.ownerid,
subscription_id=owner.stripe_subscription_id,
customer_id=owner.stripe_customer_id,
),
)
return None
# do not set as default if the new payment method is unverified (e.g., awaiting microdeposits)
should_set_as_default = not self._is_unverified_payment_method(payment_method)
if should_set_as_default:
stripe.PaymentMethod.attach(
payment_method, customer=owner.stripe_customer_id
)
stripe.Customer.modify(
owner.stripe_customer_id,
invoice_settings={"default_payment_method": payment_method},
)
stripe.Subscription.modify(
owner.stripe_subscription_id, default_payment_method=payment_method
)
log.info(
f"Successfully updated payment method for owner {owner.ownerid} by user #{self.requesting_user.ownerid}",
extra=dict(
owner_id=owner.ownerid,
user_id=self.requesting_user.ownerid,
subscription_id=owner.stripe_subscription_id,
customer_id=owner.stripe_customer_id,
),
)
@_log_stripe_error
def update_email_address(
self,
owner: Owner,
email_address: str,
apply_to_default_payment_method: bool = False,
):
if not re.fullmatch(r"[^@]+@[^@]+\.[^@]+", email_address):
return None
log.info(f"Stripe update email address for owner {owner.ownerid}")
if owner.stripe_subscription_id is None:
log.info(
f"stripe_subscription_id is None, not updating stripe email for owner {owner.ownerid}"
)
return None
stripe.Customer.modify(owner.stripe_customer_id, email=email_address)
log.info(
f"Stripe successfully updated email address for owner {owner.ownerid} by user #{self.requesting_user.ownerid}"
)
if apply_to_default_payment_method:
try:
default_payment_method = stripe.Customer.retrieve(
owner.stripe_customer_id
)["invoice_settings"]["default_payment_method"]
stripe.PaymentMethod.modify(
default_payment_method,
billing_details={"email": email_address},
)
log.info(
"Stripe successfully updated billing email for payment method",
extra=dict(
payment_method=default_payment_method,
stripe_customer_id=owner.stripe_customer_id,
ownerid=owner.ownerid,
),
)
except Exception as e:
log.error(
"Unable to update billing email for payment method",
extra=dict(
payment_method=default_payment_method,
stripe_customer_id=owner.stripe_customer_id,
error=str(e),
ownerid=owner.ownerid,
),
)
@_log_stripe_error
def update_billing_address(self, owner: Owner, name, billing_address):
log.info(f"Stripe update billing address for owner {owner.ownerid}")
if owner.stripe_customer_id is None:
log.info(
f"stripe_customer_id is None, cannot update default billing address for owner {owner.ownerid}"
)
return None
try:
customer = stripe.Customer.retrieve(owner.stripe_customer_id)
log.info(f"Retrieved customer: {customer}")
default_payment_method = customer.invoice_settings.default_payment_method
log.info(f"Retrieved default payment method: {default_payment_method}")
if default_payment_method is None:
log.warning(
f"Customer {owner.stripe_customer_id} has no default payment method, skipping payment method update"
)
# Still update the customer address even if there's no payment method
stripe.Customer.modify(owner.stripe_customer_id, address=billing_address)
log.info(
f"Stripe successfully updated customer address for owner {owner.ownerid} by user #{self.requesting_user.ownerid}"
)
return
log.info(
f"Modifying payment method billing details for customer {owner.stripe_customer_id}"
)
stripe.PaymentMethod.modify(
default_payment_method,
billing_details={"name": name, "address": billing_address},
)
log.info(f"Modifying customer address for customer {owner.stripe_customer_id}")
stripe.Customer.modify(owner.stripe_customer_id, address=billing_address)
log.info(
f"Stripe successfully updated billing address for owner {owner.ownerid} by user #{self.requesting_user.ownerid}"
)
except Exception as e:
log.error(
"Unable to update billing address for customer",
extra=dict(
customer_id=owner.stripe_customer_id,
subscription_id=owner.stripe_subscription_id,
error=str(e),
error_type=type(e).__name__,
),
)
@_log_stripe_error
def apply_cancellation_discount(self, owner: Owner):
if owner.stripe_subscription_id is None:
log.info(
f"stripe_subscription_id is None, not applying cancellation coupon for owner {owner.ownerid}"
)
return
plan_service = PlanService(current_org=owner)
billing_rate = plan_service.billing_rate
if billing_rate == PlanBillingRate.MONTHLY.value and not owner.stripe_coupon_id:
log.info(f"Creating Stripe cancellation coupon for owner {owner.ownerid}")
coupon = stripe.Coupon.create(
percent_off=30.0,
duration="repeating",
duration_in_months=6,
name="30% off for 6 months",
max_redemptions=1,
metadata={
"ownerid": owner.ownerid,
"username": owner.username,
"email": owner.email,
"name": owner.name,
},
)
owner.stripe_coupon_id = coupon.id
owner.save()
log.info(
f"Applying cancellation coupon to Stripe subscription for owner {owner.ownerid}"
)
stripe.Customer.modify(
owner.stripe_customer_id,
coupon=owner.stripe_coupon_id,
)
@_log_stripe_error
def create_setup_intent(self, owner: Owner) -> stripe.SetupIntent:
log.info(
"Stripe create setup intent for owner",
extra=dict(
owner_id=owner.ownerid,
requesting_user_id=self.requesting_user.ownerid,
subscription_id=owner.stripe_subscription_id,
customer_id=owner.stripe_customer_id,
),
)
return stripe.SetupIntent.create(
payment_method_configuration=settings.STRIPE_PAYMENT_METHOD_CONFIGURATION_ID,
customer=owner.stripe_customer_id,
)
@_log_stripe_error
def get_unverified_payment_methods(self, owner: Owner):
log.info(
"Getting unverified payment methods",
extra=dict(
owner_id=owner.ownerid, stripe_customer_id=owner.stripe_customer_id
),
)
if not owner.stripe_customer_id:
return []
unverified_payment_methods = []
# Check payment intents
has_more = True
starting_after = None
while has_more:
payment_intents = stripe.PaymentIntent.list(
customer=owner.stripe_customer_id,
limit=20,
starting_after=starting_after,
)
for intent in payment_intents.data or []:
if (
intent.get("next_action")
and intent.next_action
and intent.next_action.get("type") == "verify_with_microdeposits"
):
unverified_payment_methods.extend(
[
{
"payment_method_id": intent.payment_method,
"hosted_verification_url": intent.next_action.verify_with_microdeposits.hosted_verification_url,
}
]
)
has_more = payment_intents.has_more
if has_more and payment_intents.data:
starting_after = payment_intents.data[-1].id
# Check setup intents
has_more = True
starting_after = None
while has_more:
setup_intents = stripe.SetupIntent.list(
customer=owner.stripe_customer_id,
limit=20,
starting_after=starting_after,
)
for intent in setup_intents.data:
if (
intent.get("next_action")
and intent.next_action
and intent.next_action.get("type") == "verify_with_microdeposits"
):
unverified_payment_methods.extend(
[
{
"payment_method_id": intent.payment_method,
"hosted_verification_url": intent.next_action.verify_with_microdeposits.hosted_verification_url,
}
]
)
has_more = setup_intents.has_more
if has_more and setup_intents.data:
starting_after = setup_intents.data[-1].id
return unverified_payment_methods
class EnterprisePaymentService(AbstractPaymentService):
# enterprise has no payments setup so these are all noops
def get_invoice(self, owner, invoice_id):
pass
def list_filtered_invoices(self, owner, limit=10):
pass
def delete_subscription(self, owner):
pass
def modify_subscription(self, owner, plan):
pass
def create_checkout_session(self, owner, plan):
pass
def get_subscription(self, owner):
pass
def update_payment_method(self, owner, payment_method):
pass
def update_email_address(self, owner, email_address):
pass
def update_billing_address(self, owner, name, billing_address):
pass
def get_schedule(self, owner):
pass
def apply_cancellation_discount(self, owner: Owner):
pass
def create_setup_intent(self, owner):
pass
def get_unverified_payment_methods(self, owner: Owner):
pass
class BillingService:
payment_service = None
def __init__(self, payment_service=None, requesting_user=None):
if payment_service is None:
if settings.IS_ENTERPRISE:
self.payment_service = EnterprisePaymentService()
else:
self.payment_service = StripeService(requesting_user=requesting_user)
else:
self.payment_service = payment_service
if not issubclass(type(self.payment_service), AbstractPaymentService):
raise Exception(
"self.payment_service must subclass AbstractPaymentService!"
)
def get_subscription(self, owner):
return self.payment_service.get_subscription(owner)
def get_schedule(self, owner):
return self.payment_service.get_schedule(owner)
def get_invoice(self, owner, invoice_id):
return self.payment_service.get_invoice(owner, invoice_id)
def list_filtered_invoices(self, owner, limit=10):
return self.payment_service.list_filtered_invoices(owner, limit)
def get_unverified_payment_methods(self, owner: Owner):
return self.payment_service.get_unverified_payment_methods(owner)
def update_plan(self, owner, desired_plan):
"""
Takes an owner and desired plan, and updates the owner's plan. Depending
on current state, might create a stripe checkout session and return
the checkout session's ID, which is a string. Otherwise returns None.
"""
try:
plan = Plan.objects.get(name=desired_plan["value"])
except Plan.DoesNotExist:
log.warning(
f"Unable to find plan {desired_plan['value']} for owner {owner.ownerid}"
)
return None
if not plan.is_active:
log.warning(
f"Attempted to transition to non-existent or legacy plan: "
f"owner {owner.ownerid}, plan: {desired_plan}"
)
return None
if plan.paid_plan is False:
if owner.stripe_subscription_id is not None:
self.payment_service.delete_subscription(owner)
else:
plan_service = PlanService(current_org=owner)
plan_service.set_default_plan_data()
else:
if owner.stripe_subscription_id is not None:
# if the existing subscription is incomplete, clean it up and create a new checkout session
subscription = self.payment_service.get_subscription(owner)
if subscription and subscription.status == "incomplete":