当您使用 Stripe API 时,您可以启动 Checkout Session。当您启动它时,它将返回该会话的对象。在该对象中,您可以访问结帐 ID
首先你像这样开始一个结帐会话
curl https://api.stripe.com/v1/checkout/sessions \
-u sk_test_4eC39HqLyjWDarjtT1zdp7dc: \
-d success_url="https://example.com/success" \
-d cancel_url="https://example.com/cancel" \
-d "payment_method_types[0]"=card \
-d "line_items[0][price]"=price_H5ggYwtDq4fbrJ \
-d "line_items[0][quantity]"=2 \
-d mode=payment
之后你会得到一个结果,类似于
{
"id": "cs_test_EHNhgisbBCZj4HfTaFiSyoLPLYr1qNPmHeVmZw0BRNDVRYWeAppSFrMt",
"object": "checkout.session",
"after_expiration": null,
"allow_promotion_codes": null,
"amount_subtotal": null,
"amount_total": null,
"automatic_tax": {
"enabled": false,
"status": null
},
"billing_address_collection": null,
"cancel_url": "https://example.com/cancel",
"client_reference_id": null,
"consent": null,
"consent_collection": null,
"currency": null,
"customer": null,
"customer_details": null,
"customer_email": null,
"expires_at": 1634799676,
"livemode": false,
"locale": null,
"metadata": {},
"mode": "payment",
"payment_intent": "pi_1Dpavq2eZvKYlo2Co0uGctWr",
"payment_method_options": {},
"payment_method_types": [
"card"
],
"payment_status": "unpaid",
"phone_number_collection": {
"enabled": false
},
"recovered_from": null,
"setup_intent": null,
"shipping": null,
"shipping_address_collection": null,
"submit_type": null,
"subscription": null,
"success_url": "https://example.com/success",
"total_details": null,
"url": "https://checkout.stripe.com/pay/..."
}
这个对象中最重要的两个属性是
ID 是会话的唯一标识符,您应该将用户重定向到要付款的 URL。
付款后,您可以查看 Checkout Session 并检查状态
curl https://api.stripe.com/v1/checkout/sessions/cs_test_EHNhgisbBCZj4HfTaFiSyoLPLYr1qNPmHeVmZw0BRNDVRYWeAppSFrMt \
-u sk_test_4eC39HqLyjWDarjtT1zdp7dc:
导致此示例响应
{
"id": "cs_test_EHNhgisbBCZj4HfTaFiSyoLPLYr1qNPmHeVmZw0BRNDVRYWeAppSFrMt",
"object": "checkout.session",
"after_expiration": null,
"allow_promotion_codes": null,
"amount_subtotal": null,
"amount_total": null,
"automatic_tax": {
"enabled": false,
"status": null
},
"billing_address_collection": null,
"cancel_url": "https://example.com/cancel",
"client_reference_id": null,
"consent": null,
"consent_collection": null,
"currency": null,
"customer": null,
"customer_details": null,
"customer_email": null,
"expires_at": 1634799676,
"livemode": false,
"locale": null,
"metadata": {},
"mode": "payment",
"payment_intent": "pi_1Dpavq2eZvKYlo2Co0uGctWr",
"payment_method_options": {},
"payment_method_types": [
"card"
],
"payment_status": "unpaid",
"phone_number_collection": {
"enabled": false
},
"recovered_from": null,
"setup_intent": null,
"shipping": null,
"shipping_address_collection": null,
"submit_type": null,
"subscription": null,
"success_url": "https://example.com/success",
"total_details": null,
"url": null
}
如您所见,您拥有payment_status 属性 - 使用此属性,您可以检查用户是否真的付费。
您也可以使用webhooks,但我更喜欢我的示例
祝你好运!