Ed *_*yko 10 python flask stripe-payments
我一直在尝试stripe.checkout.Session.create()像这样传递元数据:
stripe.api_key = STRIPE_SECRET_KEY
payments_blueprint = Blueprint('payments', __name__, url_prefix='/payments')
@payments_blueprint.route('/checkout', methods=['POST'])
def create_checkout_session():
try:
checkout_session = stripe.checkout.Session.create(
metadata=dict(key='val'),
payment_method_types=['card'],
line_items=request.form.get("lineItems", LINE_ITEMS),
success_url=f'{request.environ["HTTP_ORIGIN"]}/success',
cancel_url=f'{request.environ["HTTP_ORIGIN"]}/cancel',
mode='payment'
)
return redirect(checkout_session.url, code=HTTPStatus.SEE_OTHER)
except stripe.error.InvalidRequestError as err:
return redirect(f'{request.environ["HTTP_ORIGIN"]}/error', code=HTTPStatus.MOVED_PERMANENTLY)
Run Code Online (Sandbox Code Playgroud)
来自 stripe 的响应和通过我的 webhook 的事件都不包含任何元数据,即使 stripe 控制台中请求和响应的事件日志都包含:
"metadata": {
"key": "val"
},...
Run Code Online (Sandbox Code Playgroud)
我正在监听所有使用的事件,stripe listen --forward-to localhost:8000/hooks/ --print-json并且 /hooks 处的端点所做的就是将事件打印到标准输出。没有其他的。
我希望此元数据通过我的一系列预订验证网络钩子传递。参考这个:
https://stripe.com/docs/api/checkout/sessions/create#create_checkout_session-metadata
基本上我正在关注这些文档,通过调用发送元数据checkout.Session.create(),然后看不到此元数据。我尝试过使用dict()构造函数,使用字典语法代替({"key":"val"}),创建一个变量并将其设置为该字典,然后再将其传递给函数,以及我能想到的所有其他方式来传递此元数据字典,但我还没有尝试过从 stripe 中获取它。
这是我设置的用于转发这些事件的钩子:
class TestHook(Resource):
def post(self):
event = stripe.Event.construct_from(
json.loads(request.data),
stripe.api_key
).to_dict()
print(event['type'])
pprint(event['data']['object'])
Run Code Online (Sandbox Code Playgroud)
并将输出输出到标准输出:
payment_intent.created
{'amount': 20000,
'amount_capturable': 0,
'amount_received': 0,
'application': None,
'application_fee_amount': None,
'canceled_at': None,
'cancellation_reason': None,
'capture_method': 'automatic',
'charges': {},
'client_secret': 'pi_3JTYxxxx7t',
'confirmation_method': 'automatic',
'created': 1630184808,
'currency': 'usd',
'customer': None,
'description': None,
'id': 'pi_3JTYxxxxVm4',
'invoice': None,
'last_payment_error': None,
'livemode': False,
'metadata': <StripeObject at 0x105c061d0> JSON: {},
'next_action': None,
'object': 'payment_intent',
'on_behalf_of': None,
'payment_method': None,
'payment_method_options': {'card': {'installments': None,
'network': None,
'request_three_d_secure': 'automatic'}},
'payment_method_types': ['card'],
'receipt_email': None,
'review': None,
'setup_future_usage': None,
'shipping': None,
'source': None,
'statement_descriptor': None,
'statement_descriptor_suffix': None,
'status': 'requires_payment_method',
'transfer_data': None,
'transfer_group': None}
checkout.session.completed
{'allow_promotion_codes': None,
'amount_subtotal': 20000,
'amount_total': 20000,
'automatic_tax': {'enabled': False,
'status': None},
'billing_address_collection': None,
'cancel_url': 'http://localhost:9000/#/guides/cozumel-buzos-del-caribe/trips/7-day-dive?cancelpayment=true',
'client_reference_id': None,
'currency': 'usd',
'customer': 'cus_K7oxxxguu',
'customer_details': {'email': 'abc@gmail.com',
'tax_exempt': 'none',
'tax_ids': []},
'customer_email': None,
'id': 'cs_test_b1Yxxx9dM',
'livemode': False,
'locale': None,
'metadata': <StripeObject at 0x103d64a40> JSON: {},
'mode': 'payment',
'object': 'checkout.session',
'payment_intent': 'pi_3JTYxxxVm4',
'payment_method_options': <StripeObject at 0x103d648b0> JSON: {},
'payment_method_types': ['card'],
'payment_status': 'paid',
'setup_intent': None,
'shipping': None,
'shipping_address_collection': None,
'submit_type': None,
'subscription': None,
'success_url': 'http://localhost:9000/#/payment/success',
'total_details': {'amount_discount': 0,
'amount_shipping': 0,
'amount_tax': 0},
'url': None}
Run Code Online (Sandbox Code Playgroud)
在所有这些事件中,元数据是'metadata': <StripeObject at 0x103d64a40> JSON: {}
Dan*_*aws 18
这个问题已经得到解答,但由于在进一步实验之前我并没有立即清楚答案,所以我只是为有类似问题的人提供一些额外的信息。
在下面的会话创建片段中,请注意两个元数据声明:
stripe.checkout.Session.create(
success_url=success_url,
cancel_url=cancel_url,
payment_method_types=["card"],
mode="payment",
metadata={
"foo": "FOO",
},
payment_intent_data={
"metadata": {
"bar": "BAR",
}
},
line_items=[
{
"name": product_name,
"quantity": quantity,
"currency": currency,
"amount": unit_price,
},
]
)
Run Code Online (Sandbox Code Playgroud)
为了测试 Webhooks,我stripe listen --forward-to localhost:/webhook通过 Stripe CLI 使用了该命令,并从我的应用程序成功的支付交易中获得了以下结果。这是我观察到的:
我的“FOO”数据仅在从 webhook 回调接收到的有效负载中可用checkout.session.completed。
我的“BAR”数据仅在从 webhook 回调payment_intent.created接收到的有效负载中可用。payment_intent.succeededcharge.succeeded
customer.created'FOO' 和 'BAR' 在或中均不可用customer.updated。
这里的“会话响应”到底是什么意思?你能举个例子吗?
对于 Webhook,type您订阅了哪个具体事件?例如,如果您正在收听payment_intent.succeeded而不是checkout.session.completed,则预计会话 metadata不会出现。您可以选择metadata使用 提供底层支付意图payment_intent_data[metadata][key]=val,然后将其包含在支付意图事件正文中。