File size: 1,034 Bytes
5e1dfdc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import stripe
import os
from datetime import datetime

stripe.api_key = os.getenv("STRIPE_KEY")

class BillingEngine:
    def __init__(self):
        self.plans = {
            "basic": "price_1P...",
            "pro": "price_1P...",
            "enterprise": "price_1P..."
        }

    def create_subscription(self, email, tier, metadata={}):
        customer = stripe.Customer.create(email=email)
        sub = stripe.Subscription.create(
            customer=customer.id,
            items=[{"price": self.plans[tier]}],
            metadata={
                **metadata,
                "deployment_id": "agentic_v5",
                "start_date": datetime.now().isoformat()
            }
        )
        return sub.id

    def usage_webhook(self, event):
        if event["type"] == "agent.runtime_minutes":
            stripe.SubscriptionItem.create_usage_record(
                event["subscription_item"],
                quantity=event["minutes"],
                timestamp=int(datetime.now().timestamp())
            )