Skip to main content
 首页 » 编程设计

python之如何在 Python/Flask 中使用 stripe webhook 识别客户

2025年02月15日7haluo1

我创建了一些计划并将 Stripe subscription.id 和客户 ID 保存到我的数据库中。现在我正在创建 webhook 来接收 JSON 数据以更新到期时间以及其他一些字段。我现在只需要弄清楚如何根据 webhook 的用户来过滤用户,这样我就可以相应地更新模型。以下是我对 Flask 中 webhook 的看法。

@app.route('/webhooks', methods=['GET', 'POST']) 
def webhook(): 
    event_json = json.loads(request.data) 
    event = stripe.Event.retrieve(event_json['id']) 
    if event.type == 'invoice.payment_succeeded': 
    # Not sure how to query specific user what event is about 
    invoice = event.data.object 
    elif event.type == 'customer.subscription.updated': 
    #or 
    elif event.type == 'customer.subscription.active_until': 
 
    subscription = event.data.object 

我已经创建了 6 个计划并且都运行良好,比如我可以从本地服务器删除它,它也会自动从 stripe 中删除。我创建了一个计划,它也被更新为 strip 。我只需要用 webhook 数据更新更新包的“日期”。请帮忙。

请您参考如下方法:

https://stripe.com/docs/api#subscriptions

https://stripe.com/docs/api#event_object

您可以从 webhook 事件中收到的数据中找到 customer_id 和 subscription_id。你可以打印数据 print(event_json['data'])