@@ -523,232 +523,3 @@ def setup_intent_succeeded(self, setup_intent: stripe.SetupIntent) -> None:
523523 )
524524
525525 # --- Additional Functions for Robust Flows ---
526-
527- def payment_intent_payment_failed (self , payment_intent : stripe .PaymentIntent ) -> None :
528- log .error (
529- "Payment Intent Failed" ,
530- extra = dict (
531- stripe_customer_id = payment_intent .customer ,
532- payment_intent_id = payment_intent .id ,
533- ),
534- )
535- owners : QuerySet [Owner ] = Owner .objects .filter (
536- stripe_customer_id = payment_intent .customer
537- )
538- owners .update (delinquent = True )
539- self ._log_updated (list (owners ))
540- admins = get_all_admins_for_owners (owners )
541- task_service = TaskService ()
542- template_vars = {
543- "amount" : getattr (payment_intent , "amount_received" , 0 ) / 100 ,
544- "date" : datetime .now ().strftime ("%B %-d, %Y" ),
545- }
546- for admin in admins :
547- if admin .email :
548- task_service .send_email (
549- to_addr = admin .email ,
550- subject = "Payment Intent Failed" ,
551- template_name = "payment-intent-failed" ,
552- ** template_vars ,
553- )
554-
555- def charge_refunded (self , charge : stripe .Charge ) -> None :
556- log .info (
557- "Charge Refunded" ,
558- extra = dict (
559- stripe_charge_id = charge .id ,
560- amount_refunded = charge .amount_refunded ,
561- ),
562- )
563- owners : QuerySet [Owner ] = Owner .objects .filter (
564- stripe_customer_id = charge .customer
565- )
566- admins = get_all_admins_for_owners (owners )
567- task_service = TaskService ()
568- template_vars = {
569- "charge_id" : charge .id ,
570- "amount_refunded" : charge .amount_refunded / 100 ,
571- "date" : datetime .now ().strftime ("%B %-d, %Y" ),
572- }
573- for admin in admins :
574- if admin .email :
575- task_service .send_email (
576- to_addr = admin .email ,
577- subject = "Charge Refunded" ,
578- template_name = "charge-refunded" ,
579- ** template_vars ,
580- )
581-
582- def dispute_created (self , dispute : stripe .Dispute ) -> None :
583- log .warning (
584- "Dispute Created" ,
585- extra = dict (
586- dispute_id = dispute .id ,
587- amount = dispute .amount / 100 ,
588- status = dispute .status ,
589- ),
590- )
591- try :
592- charge = stripe .Charge .retrieve (dispute .charge )
593- except Exception as e :
594- log .error (
595- "Failed to retrieve charge for dispute" ,
596- extra = dict (dispute_id = dispute .id , error = str (e )),
597- )
598- return
599- owners : QuerySet [Owner ] = Owner .objects .filter (
600- stripe_customer_id = charge .customer
601- )
602- admins = get_all_admins_for_owners (owners )
603- task_service = TaskService ()
604- template_vars = {
605- "dispute_id" : dispute .id ,
606- "charge_id" : dispute .charge ,
607- "amount" : dispute .amount / 100 ,
608- "reason" : dispute .reason ,
609- "date" : datetime .now ().strftime ("%B %-d, %Y" ),
610- }
611- for admin in admins :
612- if admin .email :
613- task_service .send_email (
614- to_addr = admin .email ,
615- subject = "Dispute Created on a Charge" ,
616- template_name = "dispute-created" ,
617- ** template_vars ,
618- )
619-
620- def invoice_updated (self , invoice : stripe .Invoice ) -> None :
621- log .info (
622- "Invoice Updated" ,
623- extra = dict (stripe_invoice_id = invoice .id , status = invoice .status ),
624- )
625- if invoice .status == "open" :
626- owners = Owner .objects .filter (stripe_customer_id = invoice .customer )
627- admins = get_all_admins_for_owners (owners )
628- task_service = TaskService ()
629- template_vars = {
630- "amount" : invoice .total / 100 ,
631- "date" : datetime .now ().strftime ("%B %-d, %Y" ),
632- }
633- for admin in admins :
634- if admin .email :
635- task_service .send_email (
636- to_addr = admin .email ,
637- subject = "Invoice Updated" ,
638- template_name = "invoice-updated" ,
639- ** template_vars ,
640- )
641-
642- def account_updated (self , account : stripe .Account ) -> None :
643- log .info (
644- "Account Updated" ,
645- extra = dict (stripe_account_id = account .id , email = getattr (account , "email" , None )),
646- )
647-
648- def subscription_payment_retry (self , subscription : stripe .Subscription ) -> None :
649- log .info (
650- "Subscription Payment Retry" ,
651- extra = dict (
652- stripe_subscription_id = subscription .id ,
653- stripe_customer_id = subscription .customer ,
654- ),
655- )
656- owners : QuerySet [Owner ] = Owner .objects .filter (
657- stripe_subscription_id = subscription .id ,
658- stripe_customer_id = subscription .customer ,
659- )
660- owners .update (delinquent = True )
661- self ._log_updated (list (owners ))
662- admins = get_all_admins_for_owners (owners )
663- task_service = TaskService ()
664- template_vars = {
665- "date" : datetime .now ().strftime ("%B %-d, %Y" ),
666- "subscription_id" : subscription .id ,
667- }
668- for admin in admins :
669- if admin .email :
670- task_service .send_email (
671- to_addr = admin .email ,
672- subject = "Subscription Payment Retry" ,
673- template_name = "subscription-payment-retry" ,
674- ** template_vars ,
675- )
676-
677- def default_event_handler (self , event_object : Any ) -> None :
678- log .info (
679- "Default Event Handler: Unhandled event type" ,
680- extra = dict (event_object = event_object ),
681- )
682-
683- def simulate_event (self , event_type : str , event_data : dict ) -> None :
684- fake_event = type ("FakeEvent" , (object ,), {})()
685- fake_event .type = event_type
686- fake_event .data = type ("FakeEventData" , (object ,), {"object" : event_data })()
687- handler = getattr (self , event_type .replace ("." , "_" ), self .default_event_handler )
688- handler (fake_event .data .object )
689-
690- def update_owner_metadata (self , owner : Owner , metadata : dict ) -> None :
691- log .info (
692- "Updating owner metadata" ,
693- extra = dict (ownerid = owner .ownerid , metadata = metadata ),
694- )
695- for key , value in metadata .items ():
696- setattr (owner , key , value )
697- owner .save ()
698-
699- def revalidate_subscription (self , subscription : stripe .Subscription ) -> None :
700- log .info (
701- "Revalidating subscription" ,
702- extra = dict (subscription_id = subscription .id ),
703- )
704- owners : QuerySet [Owner ] = Owner .objects .filter (
705- stripe_subscription_id = subscription .id ,
706- stripe_customer_id = subscription .customer ,
707- )
708- if not owners .exists ():
709- log .error (
710- "Revalidate subscription: No owner found" ,
711- extra = dict (subscription_id = subscription .id ),
712- )
713- return
714- for owner in owners :
715- plan_service = PlanService (current_org = owner )
716- plan_service .update_plan (name = "Revalidated Plan" , user_count = subscription .quantity )
717- self ._log_updated (list (owners ))
718-
719- def simulate_delay (self , seconds : int ) -> None :
720- log .info ("Simulating delay" , extra = {"seconds" : seconds })
721- import time
722- time .sleep (seconds )
723-
724- def post (self , request : HttpRequest , * args : Any , ** kwargs : Any ) -> Response :
725- if settings .STRIPE_ENDPOINT_SECRET is None :
726- log .critical (
727- "Stripe endpoint secret improperly configured -- webhooks will not be processed."
728- )
729- try :
730- self .event = stripe .Webhook .construct_event (
731- self .request .body ,
732- self .request .META .get (StripeHTTPHeaders .SIGNATURE ),
733- settings .STRIPE_ENDPOINT_SECRET ,
734- )
735- except stripe .SignatureVerificationError as e :
736- log .warning (f"Stripe webhook event received with invalid signature -- { e } " )
737- return Response ("Invalid signature" , status = status .HTTP_400_BAD_REQUEST )
738- if self .event .type not in StripeWebhookEvents .subscribed_events :
739- log .warning (
740- "Unsupported Stripe webhook event received, exiting" ,
741- extra = dict (stripe_webhook_event = self .event .type ),
742- )
743- return Response ("Unsupported event type" , status = 204 )
744-
745- log .info (
746- "Stripe webhook event received" ,
747- extra = dict (stripe_webhook_event = self .event .type ),
748- )
749- handler = getattr (self , self .event .type .replace ("." , "_" ), self .default_event_handler )
750- try :
751- handler (self .event .data .object )
752- except Exception as e :
753- log .error ("Error handling event" , extra = {"error" : str (e )})
754- return Response (status = status .HTTP_204_NO_CONTENT )
0 commit comments