无法在 Stripe webhook 中更新数据库
Unable to update database in Stripe webhook
如果用户通过 stripe 支付,我有一个 mongodb 数据库跟踪。用户付款后,我想将数据库从 False 更改为 True。然而,即使我的 webhook 似乎在 Stripe 仪表板上工作,数据库也没有更新。此外,如果我添加打印语句,它不会打印任何内容。
我在 stripe 上使用测试模式,并为我的端点使用 Heroku 域。我的条纹仪表板指示 invoice.payment_succeeded 工作正常并标记为 - 'succeeded'
这是我的 webhook 代码:
@main_bp.route("/hooks", methods=["POST"])
def stripe_webhook():
try:
payload = request.get_data(as_text=True)
sig_header = request.headers.get("Stripe-Signature")
try:
event = stripe.Webhook.construct_event(
payload, sig_header, endpoint_secret
)
data = event['data']
except ValueError as e:
# Invalid payload
return "Invalid payload", 400
except stripe.error.SignatureVerificationError as e:
# Invalid signature
return "Invalid signature", 400
# Handle the checkout.session.completed event
if event['type'] == 'invoice.paid':
# THIS PART IS NOT WORKING ↓
print("Payment was successful.")
mycol_users_consumer.find_one_and_update({"consumer_email": current_user.email},
{"$set": {"user_paid": "True"}})
elif event['type'] == 'invoice.payment_failed':
print("Payment not successful.")
print(data)
return "Success", 200
except Exception as e:
print(e)
Heroku 日志在 webbook 上显示 200:
2020-10-12T21:37:47.350062+00:00 heroku[router]: at=info method=POST path="/create-checkout-session" host=.herokuapp.com request_id=2d251ee6-3a4b-42f4-aad0-5afef2d5cf69 fwd="99.243.145.14" dyno=web.1 connect=1ms service=324ms status=200 bytes=240 protocol=http
2020-10-12T21:37:47.349260+00:00 app[web.1]: 10.155.219.74 - - [12/Oct/2020:21:37:47 +0000] "POST /create-checkout-session HTTP/1.1" 200 74 "http://.herokuapp.com/checkout" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36"
2020-10-12T21:38:12.840418+00:00 heroku[router]: at=info method=POST path="/hooks" host=.herokuapp.com request_id=6e2c93a1-1543-464a-960e-f455c48e0b88 fwd="54.187.205.235" dyno=web.1 connect=1ms service=3ms status=200 bytes=166 protocol=http
2020-10-12T21:38:12.842340+00:00 app[web.1]: 10.16.250.243 - - [12/Oct/2020:21:38:12 +0000] "POST /hooks HTTP/1.1" 200 7 "-" "Stripe/1.0 (+https://stripe.com/docs/webhooks)"
正如 Amanda Lee 所说,我在设置 webhook 时犯了一个错误。我用 invoice.paid 而不是 invoice.payment_succeeded.
您的问题是关于在仪表板中看到 invoice.payment_succeeded
事件,但您的代码处理的是 invoice.paid
事件类型。如果不匹配是无意的,您的代码需要更新以处理 invoice.payment_succeeded
事件类型。
如果用户通过 stripe 支付,我有一个 mongodb 数据库跟踪。用户付款后,我想将数据库从 False 更改为 True。然而,即使我的 webhook 似乎在 Stripe 仪表板上工作,数据库也没有更新。此外,如果我添加打印语句,它不会打印任何内容。
我在 stripe 上使用测试模式,并为我的端点使用 Heroku 域。我的条纹仪表板指示 invoice.payment_succeeded 工作正常并标记为 - 'succeeded'
这是我的 webhook 代码:
@main_bp.route("/hooks", methods=["POST"])
def stripe_webhook():
try:
payload = request.get_data(as_text=True)
sig_header = request.headers.get("Stripe-Signature")
try:
event = stripe.Webhook.construct_event(
payload, sig_header, endpoint_secret
)
data = event['data']
except ValueError as e:
# Invalid payload
return "Invalid payload", 400
except stripe.error.SignatureVerificationError as e:
# Invalid signature
return "Invalid signature", 400
# Handle the checkout.session.completed event
if event['type'] == 'invoice.paid':
# THIS PART IS NOT WORKING ↓
print("Payment was successful.")
mycol_users_consumer.find_one_and_update({"consumer_email": current_user.email},
{"$set": {"user_paid": "True"}})
elif event['type'] == 'invoice.payment_failed':
print("Payment not successful.")
print(data)
return "Success", 200
except Exception as e:
print(e)
Heroku 日志在 webbook 上显示 200:
2020-10-12T21:37:47.350062+00:00 heroku[router]: at=info method=POST path="/create-checkout-session" host=.herokuapp.com request_id=2d251ee6-3a4b-42f4-aad0-5afef2d5cf69 fwd="99.243.145.14" dyno=web.1 connect=1ms service=324ms status=200 bytes=240 protocol=http
2020-10-12T21:37:47.349260+00:00 app[web.1]: 10.155.219.74 - - [12/Oct/2020:21:37:47 +0000] "POST /create-checkout-session HTTP/1.1" 200 74 "http://.herokuapp.com/checkout" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36"
2020-10-12T21:38:12.840418+00:00 heroku[router]: at=info method=POST path="/hooks" host=.herokuapp.com request_id=6e2c93a1-1543-464a-960e-f455c48e0b88 fwd="54.187.205.235" dyno=web.1 connect=1ms service=3ms status=200 bytes=166 protocol=http
2020-10-12T21:38:12.842340+00:00 app[web.1]: 10.16.250.243 - - [12/Oct/2020:21:38:12 +0000] "POST /hooks HTTP/1.1" 200 7 "-" "Stripe/1.0 (+https://stripe.com/docs/webhooks)"
正如 Amanda Lee 所说,我在设置 webhook 时犯了一个错误。我用 invoice.paid 而不是 invoice.payment_succeeded.
您的问题是关于在仪表板中看到 invoice.payment_succeeded
事件,但您的代码处理的是 invoice.paid
事件类型。如果不匹配是无意的,您的代码需要更新以处理 invoice.payment_succeeded
事件类型。