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

How to identify customer with stripe webhook in Python/Flask

我制定了一些计划并将 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 中删除。我创建了一个计划,它也被更新为条带。我只需要更新 "date" 以获取包含 webhook 数据的更新包。请帮忙

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

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

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