#adambirds_code

1 messages ¡ Page 1 of 1 (latest)

fierce lintelBOT
#

👋 Welcome to your new thread!

⏲️ We'll be here soon! Typically we respond in a few minutes, but sometimes we might take a bit longer if the server is busy or if you have a particularly tricky question.

⏱️ We close idle threads, which makes them read-only. Once a thread is closed it won't be reopened, but you can always start a new thread if you have another question.

🔗 This thread will always be available, even after it's closed. You can find it again using Discord's search, or you can save this link: https://discord.com/channels/841573134531821608/1407372325505990666

📝 Have more to share? Add more details, code, screenshots, videos, etc. below.

undone mango
waxen crater
undone mango
waxen crater
#

Yeah so this is an example:

def handle_invoice_payment_succeeded(event: Dict[str, Any], webhook_event: WebhookEvent) -> None:
    invoice = event["data"]["object"]
    customer_id = invoice["customer"]
    subscription_id = invoice["subscription"]

    subscription, created = Subscription.objects.get_or_create(
        customer=Customer.objects.get(stripe_customer_id=customer_id),
    )

    stripe_subscription: stripe.Subscription = stripe.Subscription.retrieve(subscription_id)

    subscription.stripe_subscription_id = subscription_id
    subscription.status = stripe_subscription.status
    subscription.current_period_start = datetime.fromtimestamp(
        stripe_subscription.items.data[0].current_period_start
    )
    subscription.current_period_end = datetime.fromtimestamp(
        stripe_subscription.items.data[0].current_period_end
    )
    subscription.trial_end = (
        datetime.fromtimestamp(stripe_subscription.trial_end)
        if stripe_subscription.trial_end
        else None
    )
    subscription.save()

    ## Check if created at time is close to now to avoid sending renewal email at signupm so if created at time is say in last 30 mins dont send renewal email
    if not created and subscription.created_at < timezone.now() - timedelta(minutes=30):
        send_successful_subscription_renewal_email.delay(
            subscription.customer.user.email, subscription.customer.user.first_name
        )

    webhook_event.processed = True
    webhook_event.save()