我想在payment_intent.payment_failed事件中獲取用戶 ID,并且 client_refrence_id 不在來自條帶的 payment_failed 事件中的請求資料中。我試過metadata={'user_id': user_id}但這也沒有顯示在請求資料中。還嘗試了這個subscription_data.metadata = {"metadata": {"bar": "BAR", }},但它顯示語法錯誤“SyntaxError:運算式不能包含賦值,也許你的意思是“==”?即使我這樣做(==)它說SyntaxError: positional argument following keyword argument 這是我的代碼:
checkout_session = stripe.checkout.Session.create(
client_reference_id=user_id,
# metadata={'user_id': user_id},
subscription_data.metadata = {"metadata": {"bar": "BAR", }},
success_url="success_url",
cancel_url="cancel_url",
payment_method_types=["card"],
mode="subscription",
line_items=[
{
"price": price_id,
"quantity": 1,
}
],
)
這是我的 webhook 代碼
def webhook_received(self):
payload = request.data
endpoint_secret = 'my_secret_key'
sig_header = request.headers.get('stripe-signature')
try:
event = stripe.Webhook.construct_event(
payload, sig_header, endpoint_secret
)
data = event['data']
except:
pass
event_type = event['type']
if event_type == 'checkout.session.completed':
self.handle_checkout_session(data)
elif event_type == 'payment_intent.payment_failed':
self.saving_failed_subscription_data(data)
return "success"
uj5u.com熱心網友回復:
為了在 Python中創建結帳會話時將元資料傳遞給訂閱,您需要使用嵌套字典而不是點表示法。因此,在您的情況下,您將修改結帳會話的創建,如下所示:
checkout_session = stripe.checkout.Session.create(
client_reference_id=user_id,
# metadata={'user_id': user_id},
# subscription_data.metadata = {"metadata": {"bar": "BAR", }}, <-- won't work
# This should work to get data to invoice/subscription
subscription_data = {
"metadata": {"user_id": user_id}
},
# This will get the metadata on to the related payment_intent
payment_intent_data={
"metadata": {"user_id": user_id}
},
success_url="success_url",
cancel_url="cancel_url",
payment_method_types=["card"],
mode="subscription",
line_items=[
{
"price": price_id,
"quantity": 1,
}
],
)
Python 中需要這種使用字典的方法來發布 Stripe API 允許您操作的任何嵌套屬性的資料。
Create a Session檔案有很長的可配置引數串列,值得查看,現在您已經掌握了如何附加嵌套資料的語法。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/411183.html
標籤:
上一篇:如何從jinja2中洗掉空值?
